drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / media / usb / au0828 / au0828-video.c
blobe9cd2a335f7faf27b029a3a79112d48d6a13188c
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 * 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 * 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 lencopy = umin(len, remain);
607 if (vbi_buf != NULL)
608 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
609 vbioutp, len);
611 len -= lencopy;
612 p += lencopy;
613 dev->vbi_read += lencopy;
616 if (dev->vbi_read >= vbi_field_size && buf != NULL)
617 au0828_copy_video(dev, dma_q, buf, p, outp, len);
619 return rc;
622 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
624 #ifdef CONFIG_MEDIA_CONTROLLER
625 int i;
627 for (i = 0; i < AU0828_MAX_INPUT; i++) {
628 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
629 return;
630 media_device_unregister_entity(&dev->input_ent[i]);
632 #endif
635 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
637 struct au0828_dev *dev =
638 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
640 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
641 v4l2_device_unregister(&dev->v4l2_dev);
642 au0828_usb_v4l2_media_release(dev);
643 au0828_usb_release(dev);
646 int au0828_v4l2_device_register(struct usb_interface *interface,
647 struct au0828_dev *dev)
649 int retval;
651 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
652 return 0;
654 /* Create the v4l2_device */
655 #ifdef CONFIG_MEDIA_CONTROLLER
656 dev->v4l2_dev.mdev = dev->media_dev;
657 #endif
658 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
659 if (retval) {
660 pr_err("%s() v4l2_device_register failed\n",
661 __func__);
662 return retval;
665 dev->v4l2_dev.release = au0828_usb_v4l2_release;
667 /* This control handler will inherit the controls from au8522 */
668 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
669 if (retval) {
670 pr_err("%s() v4l2_ctrl_handler_init failed\n",
671 __func__);
672 return retval;
674 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
676 return 0;
679 static int queue_setup(struct vb2_queue *vq,
680 unsigned int *nbuffers, unsigned int *nplanes,
681 unsigned int sizes[], struct device *alloc_devs[])
683 struct au0828_dev *dev = vb2_get_drv_priv(vq);
684 unsigned long size = dev->height * dev->bytesperline;
686 if (*nplanes)
687 return sizes[0] < size ? -EINVAL : 0;
688 *nplanes = 1;
689 sizes[0] = size;
690 return 0;
693 static int
694 buffer_prepare(struct vb2_buffer *vb)
696 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
697 struct au0828_buffer *buf = container_of(vbuf,
698 struct au0828_buffer, vb);
699 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
701 buf->length = dev->height * dev->bytesperline;
703 if (vb2_plane_size(vb, 0) < buf->length) {
704 pr_err("%s data will not fit into plane (%lu < %lu)\n",
705 __func__, vb2_plane_size(vb, 0), buf->length);
706 return -EINVAL;
708 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
709 return 0;
712 static void
713 buffer_queue(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,
718 vb);
719 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
720 struct au0828_dmaqueue *vidq = &dev->vidq;
721 unsigned long flags = 0;
723 buf->mem = vb2_plane_vaddr(vb, 0);
724 buf->length = vb2_plane_size(vb, 0);
726 spin_lock_irqsave(&dev->slock, flags);
727 list_add_tail(&buf->list, &vidq->active);
728 spin_unlock_irqrestore(&dev->slock, flags);
731 static int au0828_i2s_init(struct au0828_dev *dev)
733 /* Enable i2s mode */
734 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
735 return 0;
739 * Auvitek au0828 analog stream enable
741 static int au0828_analog_stream_enable(struct au0828_dev *d)
743 struct usb_interface *iface;
744 int ret, h, w;
746 dprintk(1, "au0828_analog_stream_enable called\n");
748 if (test_bit(DEV_DISCONNECTED, &d->dev_state))
749 return -ENODEV;
751 iface = usb_ifnum_to_if(d->usbdev, 0);
752 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
753 dprintk(1, "Changing intf#0 to alt 5\n");
754 /* set au0828 interface0 to AS5 here again */
755 ret = usb_set_interface(d->usbdev, 0, 5);
756 if (ret < 0) {
757 pr_info("Au0828 can't set alt setting to 5!\n");
758 return -EBUSY;
762 h = d->height / 2 + 2;
763 w = d->width * 2;
765 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
766 au0828_writereg(d, 0x106, 0x00);
767 /* set x position */
768 au0828_writereg(d, 0x110, 0x00);
769 au0828_writereg(d, 0x111, 0x00);
770 au0828_writereg(d, 0x114, w & 0xff);
771 au0828_writereg(d, 0x115, w >> 8);
772 /* set y position */
773 au0828_writereg(d, 0x112, 0x00);
774 au0828_writereg(d, 0x113, 0x00);
775 au0828_writereg(d, 0x116, h & 0xff);
776 au0828_writereg(d, 0x117, h >> 8);
777 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
779 return 0;
782 static int au0828_analog_stream_disable(struct au0828_dev *d)
784 dprintk(1, "au0828_analog_stream_disable called\n");
785 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
786 return 0;
789 static void au0828_analog_stream_reset(struct au0828_dev *dev)
791 dprintk(1, "au0828_analog_stream_reset called\n");
792 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
793 mdelay(30);
794 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
798 * Some operations needs to stop current streaming
800 static int au0828_stream_interrupt(struct au0828_dev *dev)
802 dev->stream_state = STREAM_INTERRUPT;
803 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
804 return -ENODEV;
805 return 0;
808 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
810 struct au0828_dev *dev = vb2_get_drv_priv(vq);
811 int rc = 0;
813 dprintk(1, "au0828_start_analog_streaming called %d\n",
814 dev->streaming_users);
816 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
817 dev->frame_count = 0;
818 else
819 dev->vbi_frame_count = 0;
821 if (dev->streaming_users == 0) {
822 /* If we were doing ac97 instead of i2s, it would go here...*/
823 au0828_i2s_init(dev);
824 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
825 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
826 au0828_isoc_copy);
827 if (rc < 0) {
828 pr_info("au0828_init_isoc failed\n");
829 return rc;
832 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
834 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
835 dev->vid_timeout_running = 1;
836 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
837 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
838 dev->vbi_timeout_running = 1;
839 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
842 dev->streaming_users++;
843 return rc;
846 static void au0828_stop_streaming(struct vb2_queue *vq)
848 struct au0828_dev *dev = vb2_get_drv_priv(vq);
849 struct au0828_dmaqueue *vidq = &dev->vidq;
850 unsigned long flags = 0;
852 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
854 if (dev->streaming_users-- == 1) {
855 au0828_uninit_isoc(dev);
856 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
859 dev->vid_timeout_running = 0;
860 del_timer_sync(&dev->vid_timeout);
862 spin_lock_irqsave(&dev->slock, flags);
863 if (dev->isoc_ctl.buf != NULL) {
864 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
865 VB2_BUF_STATE_ERROR);
866 dev->isoc_ctl.buf = NULL;
868 while (!list_empty(&vidq->active)) {
869 struct au0828_buffer *buf;
871 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
872 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
873 list_del(&buf->list);
875 spin_unlock_irqrestore(&dev->slock, flags);
878 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
880 struct au0828_dev *dev = vb2_get_drv_priv(vq);
881 struct au0828_dmaqueue *vbiq = &dev->vbiq;
882 unsigned long flags = 0;
884 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
885 dev->streaming_users);
887 if (dev->streaming_users-- == 1) {
888 au0828_uninit_isoc(dev);
889 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
892 spin_lock_irqsave(&dev->slock, flags);
893 if (dev->isoc_ctl.vbi_buf != NULL) {
894 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
895 VB2_BUF_STATE_ERROR);
896 dev->isoc_ctl.vbi_buf = NULL;
898 while (!list_empty(&vbiq->active)) {
899 struct au0828_buffer *buf;
901 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
902 list_del(&buf->list);
903 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
905 spin_unlock_irqrestore(&dev->slock, flags);
907 dev->vbi_timeout_running = 0;
908 del_timer_sync(&dev->vbi_timeout);
911 static const struct vb2_ops au0828_video_qops = {
912 .queue_setup = queue_setup,
913 .buf_prepare = buffer_prepare,
914 .buf_queue = buffer_queue,
915 .prepare_streaming = v4l_vb2q_enable_media_source,
916 .start_streaming = au0828_start_analog_streaming,
917 .stop_streaming = au0828_stop_streaming,
920 /* ------------------------------------------------------------------
921 V4L2 interface
922 ------------------------------------------------------------------*/
924 * au0828_analog_unregister
925 * unregister v4l2 devices
927 int au0828_analog_unregister(struct au0828_dev *dev)
929 dprintk(1, "au0828_analog_unregister called\n");
931 /* No analog TV */
932 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
933 return 0;
935 mutex_lock(&au0828_sysfs_lock);
936 vb2_video_unregister_device(&dev->vdev);
937 vb2_video_unregister_device(&dev->vbi_dev);
938 mutex_unlock(&au0828_sysfs_lock);
940 v4l2_device_disconnect(&dev->v4l2_dev);
941 v4l2_device_put(&dev->v4l2_dev);
943 return 1;
946 /* This function ensures that video frames continue to be delivered even if
947 the ITU-656 input isn't receiving any data (thereby preventing applications
948 such as tvtime from hanging) */
949 static void au0828_vid_buffer_timeout(struct timer_list *t)
951 struct au0828_dev *dev = from_timer(dev, t, vid_timeout);
952 struct au0828_dmaqueue *dma_q = &dev->vidq;
953 struct au0828_buffer *buf;
954 unsigned char *vid_data;
955 unsigned long flags = 0;
957 spin_lock_irqsave(&dev->slock, flags);
959 buf = dev->isoc_ctl.buf;
960 if (buf != NULL) {
961 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
962 memset(vid_data, 0x00, buf->length); /* Blank green frame */
963 buffer_filled(dev, dma_q, buf);
965 get_next_buf(dma_q, &buf);
967 if (dev->vid_timeout_running == 1)
968 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
970 spin_unlock_irqrestore(&dev->slock, flags);
973 static void au0828_vbi_buffer_timeout(struct timer_list *t)
975 struct au0828_dev *dev = from_timer(dev, t, vbi_timeout);
976 struct au0828_dmaqueue *dma_q = &dev->vbiq;
977 struct au0828_buffer *buf;
978 unsigned char *vbi_data;
979 unsigned long flags = 0;
981 spin_lock_irqsave(&dev->slock, flags);
983 buf = dev->isoc_ctl.vbi_buf;
984 if (buf != NULL) {
985 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
986 memset(vbi_data, 0x00, buf->length);
987 buffer_filled(dev, dma_q, buf);
989 vbi_get_next_buf(dma_q, &buf);
991 if (dev->vbi_timeout_running == 1)
992 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
993 spin_unlock_irqrestore(&dev->slock, flags);
996 static int au0828_v4l2_open(struct file *filp)
998 struct au0828_dev *dev = video_drvdata(filp);
999 int ret;
1001 dprintk(1,
1002 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1003 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1004 dev->streaming_users, dev->users);
1006 if (mutex_lock_interruptible(&dev->lock))
1007 return -ERESTARTSYS;
1009 ret = v4l2_fh_open(filp);
1010 if (ret) {
1011 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1012 __func__, ret);
1013 mutex_unlock(&dev->lock);
1014 return ret;
1017 if (dev->users == 0) {
1018 au0828_analog_stream_enable(dev);
1019 au0828_analog_stream_reset(dev);
1020 dev->stream_state = STREAM_OFF;
1021 set_bit(DEV_INITIALIZED, &dev->dev_state);
1023 dev->users++;
1024 mutex_unlock(&dev->lock);
1025 return ret;
1028 static int au0828_v4l2_close(struct file *filp)
1030 int ret;
1031 struct au0828_dev *dev = video_drvdata(filp);
1032 struct video_device *vdev = video_devdata(filp);
1034 dprintk(1,
1035 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1036 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1037 dev->streaming_users, dev->users);
1039 mutex_lock(&dev->lock);
1040 if (vdev->vfl_type == VFL_TYPE_VIDEO && dev->vid_timeout_running) {
1041 /* Cancel timeout thread in case they didn't call streamoff */
1042 dev->vid_timeout_running = 0;
1043 del_timer_sync(&dev->vid_timeout);
1044 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1045 dev->vbi_timeout_running) {
1046 /* Cancel timeout thread in case they didn't call streamoff */
1047 dev->vbi_timeout_running = 0;
1048 del_timer_sync(&dev->vbi_timeout);
1051 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1052 goto end;
1054 if (dev->users == 1) {
1056 * Avoid putting tuner in sleep if DVB or ALSA are
1057 * streaming.
1059 * On most USB devices like au0828 the tuner can
1060 * be safely put in sleep state here if ALSA isn't
1061 * streaming. Exceptions are some very old USB tuner
1062 * models such as em28xx-based WinTV USB2 which have
1063 * a separate audio output jack. The devices that have
1064 * a separate audio output jack have analog tuners,
1065 * like Philips FM1236. Those devices are always on,
1066 * so the s_power callback are silently ignored.
1067 * So, the current logic here does the following:
1068 * Disable (put tuner to sleep) when
1069 * - ALSA and DVB aren't streaming.
1070 * - the last V4L2 file handler is closed.
1072 * FIXME:
1074 * Additionally, this logic could be improved to
1075 * disable the media source if the above conditions
1076 * are met and if the device:
1077 * - doesn't have a separate audio out plug (or
1078 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1080 * Once this additional logic is in place, a callback
1081 * is needed to enable the media source and power on
1082 * the tuner, for radio to work.
1084 ret = v4l_enable_media_source(vdev);
1085 if (ret == 0)
1086 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
1087 standby);
1088 dev->std_set_in_tuner_core = 0;
1090 /* When close the device, set the usb intf0 into alt0 to free
1091 USB bandwidth */
1092 ret = usb_set_interface(dev->usbdev, 0, 0);
1093 if (ret < 0)
1094 pr_info("Au0828 can't set alternate to 0!\n");
1096 end:
1097 _vb2_fop_release(filp, NULL);
1098 dev->users--;
1099 mutex_unlock(&dev->lock);
1100 return 0;
1103 /* Must be called with dev->lock held */
1104 static void au0828_init_tuner(struct au0828_dev *dev)
1106 struct v4l2_frequency f = {
1107 .frequency = dev->ctrl_freq,
1108 .type = V4L2_TUNER_ANALOG_TV,
1111 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1112 dev->std_set_in_tuner_core, dev->dev_state);
1114 if (dev->std_set_in_tuner_core)
1115 return;
1116 dev->std_set_in_tuner_core = 1;
1117 i2c_gate_ctrl(dev, 1);
1118 /* If we've never sent the standard in tuner core, do so now.
1119 We don't do this at device probe because we don't want to
1120 incur the cost of a firmware load */
1121 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1122 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1123 i2c_gate_ctrl(dev, 0);
1126 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1127 struct v4l2_format *format)
1129 int ret;
1130 int width = format->fmt.pix.width;
1131 int height = format->fmt.pix.height;
1133 /* If they are demanding a format other than the one we support,
1134 bail out (tvtime asks for UYVY and then retries with YUYV) */
1135 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1136 return -EINVAL;
1138 /* format->fmt.pix.width only support 720 and height 480 */
1139 if (width != 720)
1140 width = 720;
1141 if (height != 480)
1142 height = 480;
1144 format->fmt.pix.width = width;
1145 format->fmt.pix.height = height;
1146 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1147 format->fmt.pix.bytesperline = width * 2;
1148 format->fmt.pix.sizeimage = width * height * 2;
1149 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1150 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1152 if (cmd == VIDIOC_TRY_FMT)
1153 return 0;
1155 /* maybe set new image format, driver current only support 720*480 */
1156 dev->width = width;
1157 dev->height = height;
1158 dev->frame_size = width * height * 2;
1159 dev->field_size = width * height;
1160 dev->bytesperline = width * 2;
1162 if (dev->stream_state == STREAM_ON) {
1163 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1164 ret = au0828_stream_interrupt(dev);
1165 if (ret != 0) {
1166 dprintk(1, "error interrupting video stream!\n");
1167 return ret;
1171 au0828_analog_stream_enable(dev);
1173 return 0;
1176 static int vidioc_querycap(struct file *file, void *priv,
1177 struct v4l2_capability *cap)
1179 struct au0828_dev *dev = video_drvdata(file);
1181 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1182 dev->std_set_in_tuner_core, dev->dev_state);
1184 strscpy(cap->driver, "au0828", sizeof(cap->driver));
1185 strscpy(cap->card, dev->board.name, sizeof(cap->card));
1186 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1188 /* set the device capabilities */
1189 cap->capabilities =
1190 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1191 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1192 V4L2_CAP_DEVICE_CAPS;
1193 return 0;
1196 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1197 struct v4l2_fmtdesc *f)
1199 if (f->index)
1200 return -EINVAL;
1202 dprintk(1, "%s called\n", __func__);
1204 f->pixelformat = V4L2_PIX_FMT_UYVY;
1206 return 0;
1209 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1210 struct v4l2_format *f)
1212 struct au0828_dev *dev = video_drvdata(file);
1214 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1215 dev->std_set_in_tuner_core, dev->dev_state);
1217 f->fmt.pix.width = dev->width;
1218 f->fmt.pix.height = dev->height;
1219 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1220 f->fmt.pix.bytesperline = dev->bytesperline;
1221 f->fmt.pix.sizeimage = dev->frame_size;
1222 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1223 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1224 return 0;
1227 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1228 struct v4l2_format *f)
1230 struct au0828_dev *dev = video_drvdata(file);
1232 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1233 dev->std_set_in_tuner_core, dev->dev_state);
1235 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1238 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1239 struct v4l2_format *f)
1241 struct au0828_dev *dev = video_drvdata(file);
1242 int rc;
1244 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1245 dev->std_set_in_tuner_core, dev->dev_state);
1247 rc = check_dev(dev);
1248 if (rc < 0)
1249 return rc;
1251 if (vb2_is_busy(&dev->vb_vidq)) {
1252 pr_info("%s queue busy\n", __func__);
1253 rc = -EBUSY;
1254 goto out;
1257 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1258 out:
1259 return rc;
1262 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
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 if (norm == dev->std)
1270 return 0;
1272 if (dev->streaming_users > 0)
1273 return -EBUSY;
1275 dev->std = norm;
1277 au0828_init_tuner(dev);
1279 i2c_gate_ctrl(dev, 1);
1282 * FIXME: when we support something other than 60Hz standards,
1283 * we are going to have to make the au0828 bridge adjust the size
1284 * of its capture buffer, which is currently hardcoded at 720x480
1287 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1289 i2c_gate_ctrl(dev, 0);
1291 return 0;
1294 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1296 struct au0828_dev *dev = video_drvdata(file);
1298 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1299 dev->std_set_in_tuner_core, dev->dev_state);
1301 *norm = dev->std;
1302 return 0;
1305 static int vidioc_enum_input(struct file *file, void *priv,
1306 struct v4l2_input *input)
1308 struct au0828_dev *dev = video_drvdata(file);
1309 unsigned int tmp;
1311 static const char *inames[] = {
1312 [AU0828_VMUX_UNDEFINED] = "Undefined",
1313 [AU0828_VMUX_COMPOSITE] = "Composite",
1314 [AU0828_VMUX_SVIDEO] = "S-Video",
1315 [AU0828_VMUX_CABLE] = "Cable TV",
1316 [AU0828_VMUX_TELEVISION] = "Television",
1317 [AU0828_VMUX_DVB] = "DVB",
1320 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1321 dev->std_set_in_tuner_core, dev->dev_state);
1323 tmp = input->index;
1325 if (tmp >= AU0828_MAX_INPUT)
1326 return -EINVAL;
1327 if (AUVI_INPUT(tmp).type == 0)
1328 return -EINVAL;
1330 input->index = tmp;
1331 strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name));
1332 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1333 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1334 input->type |= V4L2_INPUT_TYPE_TUNER;
1335 input->audioset = 1;
1336 } else {
1337 input->type |= V4L2_INPUT_TYPE_CAMERA;
1338 input->audioset = 2;
1341 input->std = dev->vdev.tvnorms;
1343 return 0;
1346 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1348 struct au0828_dev *dev = video_drvdata(file);
1350 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1351 dev->std_set_in_tuner_core, dev->dev_state);
1353 *i = dev->ctrl_input;
1354 return 0;
1357 static void au0828_s_input(struct au0828_dev *dev, int index)
1359 int i;
1361 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1362 dev->std_set_in_tuner_core, dev->dev_state);
1364 switch (AUVI_INPUT(index).type) {
1365 case AU0828_VMUX_SVIDEO:
1366 dev->input_type = AU0828_VMUX_SVIDEO;
1367 dev->ctrl_ainput = 1;
1368 break;
1369 case AU0828_VMUX_COMPOSITE:
1370 dev->input_type = AU0828_VMUX_COMPOSITE;
1371 dev->ctrl_ainput = 1;
1372 break;
1373 case AU0828_VMUX_TELEVISION:
1374 dev->input_type = AU0828_VMUX_TELEVISION;
1375 dev->ctrl_ainput = 0;
1376 break;
1377 default:
1378 dprintk(1, "unknown input type set [%d]\n",
1379 AUVI_INPUT(index).type);
1380 return;
1383 dev->ctrl_input = index;
1385 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1386 AUVI_INPUT(index).vmux, 0, 0);
1388 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1389 int enable = 0;
1390 if (AUVI_INPUT(i).audio_setup == NULL)
1391 continue;
1393 if (i == index)
1394 enable = 1;
1395 else
1396 enable = 0;
1397 if (enable) {
1398 (AUVI_INPUT(i).audio_setup)(dev, enable);
1399 } else {
1400 /* Make sure we leave it turned on if some
1401 other input is routed to this callback */
1402 if ((AUVI_INPUT(i).audio_setup) !=
1403 ((AUVI_INPUT(index).audio_setup))) {
1404 (AUVI_INPUT(i).audio_setup)(dev, enable);
1409 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1410 AUVI_INPUT(index).amux, 0, 0);
1413 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1415 struct au0828_dev *dev = video_drvdata(file);
1416 struct video_device *vfd = video_devdata(file);
1418 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1419 index);
1420 if (index >= AU0828_MAX_INPUT)
1421 return -EINVAL;
1422 if (AUVI_INPUT(index).type == 0)
1423 return -EINVAL;
1425 if (dev->ctrl_input == index)
1426 return 0;
1428 au0828_s_input(dev, index);
1431 * Input has been changed. Disable the media source
1432 * associated with the old input and enable source
1433 * for the newly set input
1435 v4l_disable_media_source(vfd);
1436 return v4l_enable_media_source(vfd);
1439 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1441 if (a->index > 1)
1442 return -EINVAL;
1444 dprintk(1, "%s called\n", __func__);
1446 if (a->index == 0)
1447 strscpy(a->name, "Television", sizeof(a->name));
1448 else
1449 strscpy(a->name, "Line in", sizeof(a->name));
1451 a->capability = V4L2_AUDCAP_STEREO;
1452 return 0;
1455 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1457 struct au0828_dev *dev = video_drvdata(file);
1459 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1460 dev->std_set_in_tuner_core, dev->dev_state);
1462 a->index = dev->ctrl_ainput;
1463 if (a->index == 0)
1464 strscpy(a->name, "Television", sizeof(a->name));
1465 else
1466 strscpy(a->name, "Line in", sizeof(a->name));
1468 a->capability = V4L2_AUDCAP_STEREO;
1469 return 0;
1472 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1474 struct au0828_dev *dev = video_drvdata(file);
1476 if (a->index != dev->ctrl_ainput)
1477 return -EINVAL;
1479 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1480 dev->std_set_in_tuner_core, dev->dev_state);
1481 return 0;
1484 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1486 struct au0828_dev *dev = video_drvdata(file);
1487 struct video_device *vfd = video_devdata(file);
1488 int ret;
1490 if (t->index != 0)
1491 return -EINVAL;
1493 ret = v4l_enable_media_source(vfd);
1494 if (ret)
1495 return ret;
1497 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1498 dev->std_set_in_tuner_core, dev->dev_state);
1500 strscpy(t->name, "Auvitek tuner", sizeof(t->name));
1502 au0828_init_tuner(dev);
1503 i2c_gate_ctrl(dev, 1);
1504 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1505 i2c_gate_ctrl(dev, 0);
1506 return 0;
1509 static int vidioc_s_tuner(struct file *file, void *priv,
1510 const struct v4l2_tuner *t)
1512 struct au0828_dev *dev = video_drvdata(file);
1514 if (t->index != 0)
1515 return -EINVAL;
1517 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1518 dev->std_set_in_tuner_core, dev->dev_state);
1520 au0828_init_tuner(dev);
1521 i2c_gate_ctrl(dev, 1);
1522 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1523 i2c_gate_ctrl(dev, 0);
1525 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1526 t->afc);
1528 return 0;
1532 static int vidioc_g_frequency(struct file *file, void *priv,
1533 struct v4l2_frequency *freq)
1535 struct au0828_dev *dev = video_drvdata(file);
1537 if (freq->tuner != 0)
1538 return -EINVAL;
1539 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1540 dev->std_set_in_tuner_core, dev->dev_state);
1541 freq->frequency = dev->ctrl_freq;
1542 return 0;
1545 static int vidioc_s_frequency(struct file *file, void *priv,
1546 const struct v4l2_frequency *freq)
1548 struct au0828_dev *dev = video_drvdata(file);
1549 struct v4l2_frequency new_freq = *freq;
1551 if (freq->tuner != 0)
1552 return -EINVAL;
1554 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1555 dev->std_set_in_tuner_core, dev->dev_state);
1557 au0828_init_tuner(dev);
1558 i2c_gate_ctrl(dev, 1);
1560 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1561 /* Get the actual set (and possibly clamped) frequency */
1562 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1563 dev->ctrl_freq = new_freq.frequency;
1565 i2c_gate_ctrl(dev, 0);
1567 au0828_analog_stream_reset(dev);
1569 return 0;
1573 /* RAW VBI ioctls */
1575 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1576 struct v4l2_format *format)
1578 struct au0828_dev *dev = video_drvdata(file);
1580 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1581 dev->std_set_in_tuner_core, dev->dev_state);
1583 format->fmt.vbi.samples_per_line = dev->vbi_width;
1584 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1585 format->fmt.vbi.offset = 0;
1586 format->fmt.vbi.flags = 0;
1587 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1589 format->fmt.vbi.count[0] = dev->vbi_height;
1590 format->fmt.vbi.count[1] = dev->vbi_height;
1591 format->fmt.vbi.start[0] = 21;
1592 format->fmt.vbi.start[1] = 284;
1593 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1595 return 0;
1598 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1599 int type, struct v4l2_fract *f)
1601 struct au0828_dev *dev = video_drvdata(file);
1603 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1604 return -EINVAL;
1606 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1607 dev->std_set_in_tuner_core, dev->dev_state);
1609 f->numerator = 54;
1610 f->denominator = 59;
1612 return 0;
1615 static int vidioc_g_selection(struct file *file, void *priv,
1616 struct v4l2_selection *s)
1618 struct au0828_dev *dev = video_drvdata(file);
1620 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1621 return -EINVAL;
1623 switch (s->target) {
1624 case V4L2_SEL_TGT_CROP_BOUNDS:
1625 case V4L2_SEL_TGT_CROP_DEFAULT:
1626 s->r.left = 0;
1627 s->r.top = 0;
1628 s->r.width = dev->width;
1629 s->r.height = dev->height;
1630 break;
1631 default:
1632 return -EINVAL;
1634 return 0;
1637 #ifdef CONFIG_VIDEO_ADV_DEBUG
1638 static int vidioc_g_register(struct file *file, void *priv,
1639 struct v4l2_dbg_register *reg)
1641 struct au0828_dev *dev = video_drvdata(file);
1643 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1644 dev->std_set_in_tuner_core, dev->dev_state);
1646 reg->val = au0828_read(dev, reg->reg);
1647 reg->size = 1;
1648 return 0;
1651 static int vidioc_s_register(struct file *file, void *priv,
1652 const struct v4l2_dbg_register *reg)
1654 struct au0828_dev *dev = video_drvdata(file);
1656 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1657 dev->std_set_in_tuner_core, dev->dev_state);
1659 return au0828_writereg(dev, reg->reg, reg->val);
1661 #endif
1663 static int vidioc_log_status(struct file *file, void *fh)
1665 struct video_device *vdev = video_devdata(file);
1667 dprintk(1, "%s called\n", __func__);
1669 v4l2_ctrl_log_status(file, fh);
1670 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1671 return 0;
1674 void au0828_v4l2_suspend(struct au0828_dev *dev)
1676 struct urb *urb;
1677 int i;
1679 pr_info("stopping V4L2\n");
1681 if (dev->stream_state == STREAM_ON) {
1682 pr_info("stopping V4L2 active URBs\n");
1683 au0828_analog_stream_disable(dev);
1684 /* stop urbs */
1685 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1686 urb = dev->isoc_ctl.urb[i];
1687 if (urb) {
1688 if (!irqs_disabled())
1689 usb_kill_urb(urb);
1690 else
1691 usb_unlink_urb(urb);
1696 if (dev->vid_timeout_running)
1697 del_timer_sync(&dev->vid_timeout);
1698 if (dev->vbi_timeout_running)
1699 del_timer_sync(&dev->vbi_timeout);
1702 void au0828_v4l2_resume(struct au0828_dev *dev)
1704 int i, rc;
1706 pr_info("restarting V4L2\n");
1708 if (dev->stream_state == STREAM_ON) {
1709 au0828_stream_interrupt(dev);
1710 au0828_init_tuner(dev);
1713 if (dev->vid_timeout_running)
1714 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1715 if (dev->vbi_timeout_running)
1716 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1718 /* If we were doing ac97 instead of i2s, it would go here...*/
1719 au0828_i2s_init(dev);
1721 au0828_analog_stream_enable(dev);
1723 if (!(dev->stream_state == STREAM_ON)) {
1724 au0828_analog_stream_reset(dev);
1725 /* submit urbs */
1726 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1727 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1728 if (rc) {
1729 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1730 i, rc);
1731 au0828_uninit_isoc(dev);
1737 static const struct v4l2_file_operations au0828_v4l_fops = {
1738 .owner = THIS_MODULE,
1739 .open = au0828_v4l2_open,
1740 .release = au0828_v4l2_close,
1741 .read = vb2_fop_read,
1742 .poll = vb2_fop_poll,
1743 .mmap = vb2_fop_mmap,
1744 .unlocked_ioctl = video_ioctl2,
1747 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1748 .vidioc_querycap = vidioc_querycap,
1749 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1750 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1751 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1752 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1753 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1754 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1755 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1756 .vidioc_enumaudio = vidioc_enumaudio,
1757 .vidioc_g_audio = vidioc_g_audio,
1758 .vidioc_s_audio = vidioc_s_audio,
1759 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1760 .vidioc_g_selection = vidioc_g_selection,
1762 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1763 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1764 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1765 .vidioc_querybuf = vb2_ioctl_querybuf,
1766 .vidioc_qbuf = vb2_ioctl_qbuf,
1767 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1768 .vidioc_expbuf = vb2_ioctl_expbuf,
1770 .vidioc_s_std = vidioc_s_std,
1771 .vidioc_g_std = vidioc_g_std,
1772 .vidioc_enum_input = vidioc_enum_input,
1773 .vidioc_g_input = vidioc_g_input,
1774 .vidioc_s_input = vidioc_s_input,
1776 .vidioc_streamon = vb2_ioctl_streamon,
1777 .vidioc_streamoff = vb2_ioctl_streamoff,
1779 .vidioc_g_tuner = vidioc_g_tuner,
1780 .vidioc_s_tuner = vidioc_s_tuner,
1781 .vidioc_g_frequency = vidioc_g_frequency,
1782 .vidioc_s_frequency = vidioc_s_frequency,
1783 #ifdef CONFIG_VIDEO_ADV_DEBUG
1784 .vidioc_g_register = vidioc_g_register,
1785 .vidioc_s_register = vidioc_s_register,
1786 #endif
1787 .vidioc_log_status = vidioc_log_status,
1788 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1789 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1792 static const struct video_device au0828_video_template = {
1793 .fops = &au0828_v4l_fops,
1794 .release = video_device_release_empty,
1795 .ioctl_ops = &video_ioctl_ops,
1796 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1799 static int au0828_vb2_setup(struct au0828_dev *dev)
1801 int rc;
1802 struct vb2_queue *q;
1804 /* Setup Videobuf2 for Video capture */
1805 q = &dev->vb_vidq;
1806 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1807 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1808 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1809 q->drv_priv = dev;
1810 q->buf_struct_size = sizeof(struct au0828_buffer);
1811 q->ops = &au0828_video_qops;
1812 q->mem_ops = &vb2_vmalloc_memops;
1814 rc = vb2_queue_init(q);
1815 if (rc < 0)
1816 return rc;
1818 /* Setup Videobuf2 for VBI capture */
1819 q = &dev->vb_vbiq;
1820 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1821 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1822 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1823 q->drv_priv = dev;
1824 q->buf_struct_size = sizeof(struct au0828_buffer);
1825 q->ops = &au0828_vbi_qops;
1826 q->mem_ops = &vb2_vmalloc_memops;
1828 rc = vb2_queue_init(q);
1829 if (rc < 0)
1830 return rc;
1832 return 0;
1835 static void au0828_analog_create_entities(struct au0828_dev *dev)
1837 #if defined(CONFIG_MEDIA_CONTROLLER)
1838 static const char * const inames[] = {
1839 [AU0828_VMUX_COMPOSITE] = "Composite",
1840 [AU0828_VMUX_SVIDEO] = "S-Video",
1841 [AU0828_VMUX_CABLE] = "Cable TV",
1842 [AU0828_VMUX_TELEVISION] = "Television",
1843 [AU0828_VMUX_DVB] = "DVB",
1845 int ret, i;
1847 /* Initialize Video and VBI pads */
1848 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1849 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1850 if (ret < 0)
1851 pr_err("failed to initialize video media entity!\n");
1853 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1854 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1855 if (ret < 0)
1856 pr_err("failed to initialize vbi media entity!\n");
1858 /* Create entities for each input connector */
1859 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1860 struct media_entity *ent = &dev->input_ent[i];
1862 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1863 break;
1865 ent->name = inames[AUVI_INPUT(i).type];
1866 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1867 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1869 switch (AUVI_INPUT(i).type) {
1870 case AU0828_VMUX_COMPOSITE:
1871 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1872 break;
1873 case AU0828_VMUX_SVIDEO:
1874 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1875 break;
1876 case AU0828_VMUX_CABLE:
1877 case AU0828_VMUX_TELEVISION:
1878 case AU0828_VMUX_DVB:
1879 default: /* Just to shut up a warning */
1880 ent->function = MEDIA_ENT_F_CONN_RF;
1881 break;
1884 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1885 if (ret < 0)
1886 pr_err("failed to initialize input pad[%d]!\n", i);
1888 ret = media_device_register_entity(dev->media_dev, ent);
1889 if (ret < 0)
1890 pr_err("failed to register input entity %d!\n", i);
1892 #endif
1895 /**************************************************************************/
1897 int au0828_analog_register(struct au0828_dev *dev,
1898 struct usb_interface *interface)
1900 int retval = -ENOMEM;
1901 struct usb_host_interface *iface_desc;
1902 struct usb_endpoint_descriptor *endpoint;
1903 int i, ret;
1905 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1906 interface->cur_altsetting->desc.bInterfaceNumber);
1908 /* No analog TV */
1909 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1910 return 0;
1912 /* set au0828 usb interface0 to as5 */
1913 retval = usb_set_interface(dev->usbdev,
1914 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1915 if (retval != 0) {
1916 pr_info("Failure setting usb interface0 to as5\n");
1917 return retval;
1920 /* Figure out which endpoint has the isoc interface */
1921 iface_desc = interface->cur_altsetting;
1922 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1923 endpoint = &iface_desc->endpoint[i].desc;
1924 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1925 == USB_DIR_IN) &&
1926 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1927 == USB_ENDPOINT_XFER_ISOC)) {
1929 /* we find our isoc in endpoint */
1930 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1931 dev->max_pkt_size = (tmp & 0x07ff) *
1932 (((tmp & 0x1800) >> 11) + 1);
1933 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1934 dprintk(1,
1935 "Found isoc endpoint 0x%02x, max size = %d\n",
1936 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1939 if (!(dev->isoc_in_endpointaddr)) {
1940 pr_info("Could not locate isoc endpoint\n");
1941 return -ENODEV;
1944 init_waitqueue_head(&dev->open);
1945 spin_lock_init(&dev->slock);
1947 /* init video dma queues */
1948 INIT_LIST_HEAD(&dev->vidq.active);
1949 INIT_LIST_HEAD(&dev->vbiq.active);
1951 timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
1952 timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
1954 dev->width = NTSC_STD_W;
1955 dev->height = NTSC_STD_H;
1956 dev->field_size = dev->width * dev->height;
1957 dev->frame_size = dev->field_size << 1;
1958 dev->bytesperline = dev->width << 1;
1959 dev->vbi_width = 720;
1960 dev->vbi_height = 1;
1961 dev->ctrl_ainput = 0;
1962 dev->ctrl_freq = 960;
1963 dev->std = V4L2_STD_NTSC_M;
1964 /* Default input is TV Tuner */
1965 au0828_s_input(dev, 0);
1967 mutex_init(&dev->vb_queue_lock);
1968 mutex_init(&dev->vb_vbi_queue_lock);
1970 /* Fill the video capture device struct */
1971 dev->vdev = au0828_video_template;
1972 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1973 dev->vdev.lock = &dev->lock;
1974 dev->vdev.queue = &dev->vb_vidq;
1975 dev->vdev.queue->lock = &dev->vb_queue_lock;
1976 dev->vdev.device_caps =
1977 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1978 V4L2_CAP_TUNER | V4L2_CAP_VIDEO_CAPTURE;
1979 strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name));
1981 /* Setup the VBI device */
1982 dev->vbi_dev = au0828_video_template;
1983 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1984 dev->vbi_dev.lock = &dev->lock;
1985 dev->vbi_dev.queue = &dev->vb_vbiq;
1986 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1987 dev->vbi_dev.device_caps =
1988 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1989 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE;
1990 strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name));
1992 /* Init entities at the Media Controller */
1993 au0828_analog_create_entities(dev);
1995 /* initialize videobuf2 stuff */
1996 retval = au0828_vb2_setup(dev);
1997 if (retval != 0) {
1998 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
1999 retval);
2000 return -ENODEV;
2003 /* Register the v4l2 device */
2004 video_set_drvdata(&dev->vdev, dev);
2005 retval = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
2006 if (retval != 0) {
2007 dprintk(1, "unable to register video device (error = %d).\n",
2008 retval);
2009 return -ENODEV;
2012 /* Register the vbi device */
2013 video_set_drvdata(&dev->vbi_dev, dev);
2014 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2015 if (retval != 0) {
2016 dprintk(1, "unable to register vbi device (error = %d).\n",
2017 retval);
2018 ret = -ENODEV;
2019 goto err_reg_vbi_dev;
2022 #ifdef CONFIG_MEDIA_CONTROLLER
2023 retval = v4l2_mc_create_media_graph(dev->media_dev);
2024 if (retval) {
2025 pr_err("%s() au0282_dev_register failed to create graph\n",
2026 __func__);
2027 ret = -ENODEV;
2028 goto err_reg_vbi_dev;
2030 #endif
2032 dprintk(1, "%s completed!\n", __func__);
2034 return 0;
2036 err_reg_vbi_dev:
2037 vb2_video_unregister_device(&dev->vdev);
2038 return ret;