RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / au0828 / au0828-video.c
blob8a2241539546692c1494113885004edf7aa7eaf2
1 /*
2 * Auvitek AU0828 USB Bridge (Analog video support)
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * As published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
23 /* Developer Notes:
25 * VBI support is not yet working
26 * The hardware scaler supported is unimplemented
27 * AC97 audio support is unimplemented (only i2s audio mode)
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/suspend.h>
36 #include <linux/version.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/tuner.h>
41 #include "au0828.h"
42 #include "au0828-reg.h"
44 static DEFINE_MUTEX(au0828_sysfs_lock);
46 #define AU0828_VERSION_CODE KERNEL_VERSION(0, 0, 1)
48 /* ------------------------------------------------------------------
49 Videobuf operations
50 ------------------------------------------------------------------*/
52 static unsigned int isoc_debug;
53 module_param(isoc_debug, int, 0644);
54 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
56 #define au0828_isocdbg(fmt, arg...) \
57 do {\
58 if (isoc_debug) { \
59 printk(KERN_INFO "au0828 %s :"fmt, \
60 __func__ , ##arg); \
61 } \
62 } while (0)
64 static inline void print_err_status(struct au0828_dev *dev,
65 int packet, int status)
67 char *errmsg = "Unknown";
69 switch (status) {
70 case -ENOENT:
71 errmsg = "unlinked synchronuously";
72 break;
73 case -ECONNRESET:
74 errmsg = "unlinked asynchronuously";
75 break;
76 case -ENOSR:
77 errmsg = "Buffer error (overrun)";
78 break;
79 case -EPIPE:
80 errmsg = "Stalled (device not responding)";
81 break;
82 case -EOVERFLOW:
83 errmsg = "Babble (bad cable?)";
84 break;
85 case -EPROTO:
86 errmsg = "Bit-stuff error (bad cable?)";
87 break;
88 case -EILSEQ:
89 errmsg = "CRC/Timeout (could be anything)";
90 break;
91 case -ETIME:
92 errmsg = "Device does not respond";
93 break;
95 if (packet < 0) {
96 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
97 } else {
98 au0828_isocdbg("URB packet %d, status %d [%s].\n",
99 packet, status, errmsg);
103 static int check_dev(struct au0828_dev *dev)
105 if (dev->dev_state & DEV_DISCONNECTED) {
106 printk(KERN_INFO "v4l2 ioctl: device not present\n");
107 return -ENODEV;
110 if (dev->dev_state & DEV_MISCONFIGURED) {
111 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
112 "close and open it again\n");
113 return -EIO;
115 return 0;
119 * IRQ callback, called by URB callback
121 static void au0828_irq_callback(struct urb *urb)
123 struct au0828_dmaqueue *dma_q = urb->context;
124 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
125 int rc, i;
127 switch (urb->status) {
128 case 0: /* success */
129 case -ETIMEDOUT: /* NAK */
130 break;
131 case -ECONNRESET: /* kill */
132 case -ENOENT:
133 case -ESHUTDOWN:
134 au0828_isocdbg("au0828_irq_callback called: status kill\n");
135 return;
136 default: /* unknown error */
137 au0828_isocdbg("urb completition error %d.\n", urb->status);
138 break;
141 /* Copy data from URB */
142 spin_lock(&dev->slock);
143 rc = dev->isoc_ctl.isoc_copy(dev, urb);
144 spin_unlock(&dev->slock);
146 /* Reset urb buffers */
147 for (i = 0; i < urb->number_of_packets; i++) {
148 urb->iso_frame_desc[i].status = 0;
149 urb->iso_frame_desc[i].actual_length = 0;
151 urb->status = 0;
153 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
154 if (urb->status) {
155 au0828_isocdbg("urb resubmit failed (error=%i)\n",
156 urb->status);
161 * Stop and Deallocate URBs
163 void au0828_uninit_isoc(struct au0828_dev *dev)
165 struct urb *urb;
166 int i;
168 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
170 dev->isoc_ctl.nfields = -1;
171 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
172 urb = dev->isoc_ctl.urb[i];
173 if (urb) {
174 if (!irqs_disabled())
175 usb_kill_urb(urb);
176 else
177 usb_unlink_urb(urb);
179 if (dev->isoc_ctl.transfer_buffer[i]) {
180 usb_free_coherent(dev->usbdev,
181 urb->transfer_buffer_length,
182 dev->isoc_ctl.transfer_buffer[i],
183 urb->transfer_dma);
185 usb_free_urb(urb);
186 dev->isoc_ctl.urb[i] = NULL;
188 dev->isoc_ctl.transfer_buffer[i] = NULL;
191 kfree(dev->isoc_ctl.urb);
192 kfree(dev->isoc_ctl.transfer_buffer);
194 dev->isoc_ctl.urb = NULL;
195 dev->isoc_ctl.transfer_buffer = NULL;
196 dev->isoc_ctl.num_bufs = 0;
200 * Allocate URBs and start IRQ
202 int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
203 int num_bufs, int max_pkt_size,
204 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
206 struct au0828_dmaqueue *dma_q = &dev->vidq;
207 int i;
208 int sb_size, pipe;
209 struct urb *urb;
210 int j, k;
211 int rc;
213 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
215 /* De-allocates all pending stuff */
216 au0828_uninit_isoc(dev);
218 dev->isoc_ctl.isoc_copy = isoc_copy;
219 dev->isoc_ctl.num_bufs = num_bufs;
221 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
222 if (!dev->isoc_ctl.urb) {
223 au0828_isocdbg("cannot alloc memory for usb buffers\n");
224 return -ENOMEM;
227 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
228 GFP_KERNEL);
229 if (!dev->isoc_ctl.transfer_buffer) {
230 au0828_isocdbg("cannot allocate memory for usb transfer\n");
231 kfree(dev->isoc_ctl.urb);
232 return -ENOMEM;
235 dev->isoc_ctl.max_pkt_size = max_pkt_size;
236 dev->isoc_ctl.buf = NULL;
238 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
240 /* allocate urbs and transfer buffers */
241 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
242 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
243 if (!urb) {
244 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
245 au0828_uninit_isoc(dev);
246 return -ENOMEM;
248 dev->isoc_ctl.urb[i] = urb;
250 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
251 sb_size, GFP_KERNEL, &urb->transfer_dma);
252 if (!dev->isoc_ctl.transfer_buffer[i]) {
253 printk("unable to allocate %i bytes for transfer"
254 " buffer %i%s\n",
255 sb_size, i,
256 in_interrupt() ? " while in int" : "");
257 au0828_uninit_isoc(dev);
258 return -ENOMEM;
260 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
262 pipe = usb_rcvisocpipe(dev->usbdev,
263 dev->isoc_in_endpointaddr),
265 usb_fill_int_urb(urb, dev->usbdev, pipe,
266 dev->isoc_ctl.transfer_buffer[i], sb_size,
267 au0828_irq_callback, dma_q, 1);
269 urb->number_of_packets = max_packets;
270 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
272 k = 0;
273 for (j = 0; j < max_packets; j++) {
274 urb->iso_frame_desc[j].offset = k;
275 urb->iso_frame_desc[j].length =
276 dev->isoc_ctl.max_pkt_size;
277 k += dev->isoc_ctl.max_pkt_size;
281 init_waitqueue_head(&dma_q->wq);
283 /* submit urbs and enables IRQ */
284 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
285 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
286 if (rc) {
287 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
288 i, rc);
289 au0828_uninit_isoc(dev);
290 return rc;
294 return 0;
298 * Announces that a buffer were filled and request the next
300 static inline void buffer_filled(struct au0828_dev *dev,
301 struct au0828_dmaqueue *dma_q,
302 struct au0828_buffer *buf)
304 /* Advice that buffer was filled */
305 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
307 buf->vb.state = VIDEOBUF_DONE;
308 buf->vb.field_count++;
309 do_gettimeofday(&buf->vb.ts);
311 dev->isoc_ctl.buf = NULL;
313 list_del(&buf->vb.queue);
314 wake_up(&buf->vb.done);
317 static inline void vbi_buffer_filled(struct au0828_dev *dev,
318 struct au0828_dmaqueue *dma_q,
319 struct au0828_buffer *buf)
321 /* Advice that buffer was filled */
322 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
324 buf->vb.state = VIDEOBUF_DONE;
325 buf->vb.field_count++;
326 do_gettimeofday(&buf->vb.ts);
328 dev->isoc_ctl.vbi_buf = NULL;
330 list_del(&buf->vb.queue);
331 wake_up(&buf->vb.done);
335 * Identify the buffer header type and properly handles
337 static void au0828_copy_video(struct au0828_dev *dev,
338 struct au0828_dmaqueue *dma_q,
339 struct au0828_buffer *buf,
340 unsigned char *p,
341 unsigned char *outp, unsigned long len)
343 void *fieldstart, *startwrite, *startread;
344 int linesdone, currlinedone, offset, lencopy, remain;
345 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
347 if (len == 0)
348 return;
350 if (dma_q->pos + len > buf->vb.size)
351 len = buf->vb.size - dma_q->pos;
353 startread = p;
354 remain = len;
356 /* Interlaces frame */
357 if (buf->top_field)
358 fieldstart = outp;
359 else
360 fieldstart = outp + bytesperline;
362 linesdone = dma_q->pos / bytesperline;
363 currlinedone = dma_q->pos % bytesperline;
364 offset = linesdone * bytesperline * 2 + currlinedone;
365 startwrite = fieldstart + offset;
366 lencopy = bytesperline - currlinedone;
367 lencopy = lencopy > remain ? remain : lencopy;
369 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
370 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
371 ((char *)startwrite + lencopy) -
372 ((char *)outp + buf->vb.size));
373 remain = (char *)outp + buf->vb.size - (char *)startwrite;
374 lencopy = remain;
376 if (lencopy <= 0)
377 return;
378 memcpy(startwrite, startread, lencopy);
380 remain -= lencopy;
382 while (remain > 0) {
383 startwrite += lencopy + bytesperline;
384 startread += lencopy;
385 if (bytesperline > remain)
386 lencopy = remain;
387 else
388 lencopy = bytesperline;
390 if ((char *)startwrite + lencopy > (char *)outp +
391 buf->vb.size) {
392 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
393 ((char *)startwrite + lencopy) -
394 ((char *)outp + buf->vb.size));
395 lencopy = remain = (char *)outp + buf->vb.size -
396 (char *)startwrite;
398 if (lencopy <= 0)
399 break;
401 memcpy(startwrite, startread, lencopy);
403 remain -= lencopy;
406 if (offset > 1440) {
407 /* We have enough data to check for greenscreen */
408 if (outp[0] < 0x60 && outp[1440] < 0x60)
409 dev->greenscreen_detected = 1;
412 dma_q->pos += len;
416 * video-buf generic routine to get the next available buffer
418 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
419 struct au0828_buffer **buf)
421 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
423 if (list_empty(&dma_q->active)) {
424 au0828_isocdbg("No active queue to serve\n");
425 dev->isoc_ctl.buf = NULL;
426 *buf = NULL;
427 return;
430 /* Get the next buffer */
431 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
432 dev->isoc_ctl.buf = *buf;
434 return;
437 static void au0828_copy_vbi(struct au0828_dev *dev,
438 struct au0828_dmaqueue *dma_q,
439 struct au0828_buffer *buf,
440 unsigned char *p,
441 unsigned char *outp, unsigned long len)
443 unsigned char *startwrite, *startread;
444 int bytesperline;
445 int i, j = 0;
447 if (dev == NULL) {
448 au0828_isocdbg("dev is null\n");
449 return;
452 if (dma_q == NULL) {
453 au0828_isocdbg("dma_q is null\n");
454 return;
456 if (buf == NULL)
457 return;
458 if (p == NULL) {
459 au0828_isocdbg("p is null\n");
460 return;
462 if (outp == NULL) {
463 au0828_isocdbg("outp is null\n");
464 return;
467 bytesperline = dev->vbi_width;
469 if (dma_q->pos + len > buf->vb.size)
470 len = buf->vb.size - dma_q->pos;
472 startread = p;
473 startwrite = outp + (dma_q->pos / 2);
475 /* Make sure the bottom field populates the second half of the frame */
476 if (buf->top_field == 0)
477 startwrite += bytesperline * dev->vbi_height;
479 for (i = 0; i < len; i += 2)
480 startwrite[j++] = startread[i+1];
482 dma_q->pos += len;
487 * video-buf generic routine to get the next available VBI buffer
489 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
490 struct au0828_buffer **buf)
492 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
493 char *outp;
495 if (list_empty(&dma_q->active)) {
496 au0828_isocdbg("No active queue to serve\n");
497 dev->isoc_ctl.vbi_buf = NULL;
498 *buf = NULL;
499 return;
502 /* Get the next buffer */
503 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
504 /* Cleans up buffer - Usefull for testing for frame/URB loss */
505 outp = videobuf_to_vmalloc(&(*buf)->vb);
506 memset(outp, 0x00, (*buf)->vb.size);
508 dev->isoc_ctl.vbi_buf = *buf;
510 return;
514 * Controls the isoc copy of each urb packet
516 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
518 struct au0828_buffer *buf;
519 struct au0828_buffer *vbi_buf;
520 struct au0828_dmaqueue *dma_q = urb->context;
521 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
522 unsigned char *outp = NULL;
523 unsigned char *vbioutp = NULL;
524 int i, len = 0, rc = 1;
525 unsigned char *p;
526 unsigned char fbyte;
527 unsigned int vbi_field_size;
528 unsigned int remain, lencopy;
530 if (!dev)
531 return 0;
533 if ((dev->dev_state & DEV_DISCONNECTED) ||
534 (dev->dev_state & DEV_MISCONFIGURED))
535 return 0;
537 if (urb->status < 0) {
538 print_err_status(dev, -1, urb->status);
539 if (urb->status == -ENOENT)
540 return 0;
543 buf = dev->isoc_ctl.buf;
544 if (buf != NULL)
545 outp = videobuf_to_vmalloc(&buf->vb);
547 vbi_buf = dev->isoc_ctl.vbi_buf;
548 if (vbi_buf != NULL)
549 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
551 for (i = 0; i < urb->number_of_packets; i++) {
552 int status = urb->iso_frame_desc[i].status;
554 if (status < 0) {
555 print_err_status(dev, i, status);
556 if (urb->iso_frame_desc[i].status != -EPROTO)
557 continue;
560 if (urb->iso_frame_desc[i].actual_length <= 0)
561 continue;
563 if (urb->iso_frame_desc[i].actual_length >
564 dev->max_pkt_size) {
565 au0828_isocdbg("packet bigger than packet size");
566 continue;
569 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
570 fbyte = p[0];
571 len = urb->iso_frame_desc[i].actual_length - 4;
572 p += 4;
574 if (fbyte & 0x80) {
575 len -= 4;
576 p += 4;
577 au0828_isocdbg("Video frame %s\n",
578 (fbyte & 0x40) ? "odd" : "even");
579 if (!(fbyte & 0x40)) {
580 /* VBI */
581 if (vbi_buf != NULL)
582 vbi_buffer_filled(dev,
583 vbi_dma_q,
584 vbi_buf);
585 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
586 if (vbi_buf == NULL)
587 vbioutp = NULL;
588 else
589 vbioutp = videobuf_to_vmalloc(
590 &vbi_buf->vb);
592 /* Video */
593 if (buf != NULL)
594 buffer_filled(dev, dma_q, buf);
595 get_next_buf(dma_q, &buf);
596 if (buf == NULL)
597 outp = NULL;
598 else
599 outp = videobuf_to_vmalloc(&buf->vb);
602 if (buf != NULL) {
603 if (fbyte & 0x40)
604 buf->top_field = 1;
605 else
606 buf->top_field = 0;
609 if (vbi_buf != NULL) {
610 if (fbyte & 0x40)
611 vbi_buf->top_field = 1;
612 else
613 vbi_buf->top_field = 0;
616 dev->vbi_read = 0;
617 vbi_dma_q->pos = 0;
618 dma_q->pos = 0;
621 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
622 if (dev->vbi_read < vbi_field_size) {
623 remain = vbi_field_size - dev->vbi_read;
624 if (len < remain)
625 lencopy = len;
626 else
627 lencopy = remain;
629 if (vbi_buf != NULL)
630 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
631 vbioutp, len);
633 len -= lencopy;
634 p += lencopy;
635 dev->vbi_read += lencopy;
638 if (dev->vbi_read >= vbi_field_size && buf != NULL)
639 au0828_copy_video(dev, dma_q, buf, p, outp, len);
641 return rc;
644 static int
645 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
646 unsigned int *size)
648 struct au0828_fh *fh = vq->priv_data;
649 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
651 if (0 == *count)
652 *count = AU0828_DEF_BUF;
654 if (*count < AU0828_MIN_BUF)
655 *count = AU0828_MIN_BUF;
656 return 0;
659 /* This is called *without* dev->slock held; please keep it that way */
660 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
662 struct au0828_fh *fh = vq->priv_data;
663 struct au0828_dev *dev = fh->dev;
664 unsigned long flags = 0;
665 if (in_interrupt())
666 BUG();
668 /* We used to wait for the buffer to finish here, but this didn't work
669 because, as we were keeping the state as VIDEOBUF_QUEUED,
670 videobuf_queue_cancel marked it as finished for us.
671 (Also, it could wedge forever if the hardware was misconfigured.)
673 This should be safe; by the time we get here, the buffer isn't
674 queued anymore. If we ever start marking the buffers as
675 VIDEOBUF_ACTIVE, it won't be, though.
677 spin_lock_irqsave(&dev->slock, flags);
678 if (dev->isoc_ctl.buf == buf)
679 dev->isoc_ctl.buf = NULL;
680 spin_unlock_irqrestore(&dev->slock, flags);
682 videobuf_vmalloc_free(&buf->vb);
683 buf->vb.state = VIDEOBUF_NEEDS_INIT;
686 static int
687 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
688 enum v4l2_field field)
690 struct au0828_fh *fh = vq->priv_data;
691 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
692 struct au0828_dev *dev = fh->dev;
693 int rc = 0, urb_init = 0;
695 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
697 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
698 return -EINVAL;
700 buf->vb.width = dev->width;
701 buf->vb.height = dev->height;
702 buf->vb.field = field;
704 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
705 rc = videobuf_iolock(vq, &buf->vb, NULL);
706 if (rc < 0) {
707 printk(KERN_INFO "videobuf_iolock failed\n");
708 goto fail;
712 if (!dev->isoc_ctl.num_bufs)
713 urb_init = 1;
715 if (urb_init) {
716 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
717 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
718 au0828_isoc_copy);
719 if (rc < 0) {
720 printk(KERN_INFO "au0828_init_isoc failed\n");
721 goto fail;
725 buf->vb.state = VIDEOBUF_PREPARED;
726 return 0;
728 fail:
729 free_buffer(vq, buf);
730 return rc;
733 static void
734 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
736 struct au0828_buffer *buf = container_of(vb,
737 struct au0828_buffer,
738 vb);
739 struct au0828_fh *fh = vq->priv_data;
740 struct au0828_dev *dev = fh->dev;
741 struct au0828_dmaqueue *vidq = &dev->vidq;
743 buf->vb.state = VIDEOBUF_QUEUED;
744 list_add_tail(&buf->vb.queue, &vidq->active);
747 static void buffer_release(struct videobuf_queue *vq,
748 struct videobuf_buffer *vb)
750 struct au0828_buffer *buf = container_of(vb,
751 struct au0828_buffer,
752 vb);
754 free_buffer(vq, buf);
757 static struct videobuf_queue_ops au0828_video_qops = {
758 .buf_setup = buffer_setup,
759 .buf_prepare = buffer_prepare,
760 .buf_queue = buffer_queue,
761 .buf_release = buffer_release,
764 /* ------------------------------------------------------------------
765 V4L2 interface
766 ------------------------------------------------------------------*/
768 static int au0828_i2s_init(struct au0828_dev *dev)
770 /* Enable i2s mode */
771 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
772 return 0;
776 * Auvitek au0828 analog stream enable
777 * Please set interface0 to AS5 before enable the stream
779 int au0828_analog_stream_enable(struct au0828_dev *d)
781 dprintk(1, "au0828_analog_stream_enable called\n");
782 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
783 au0828_writereg(d, 0x106, 0x00);
784 /* set x position */
785 au0828_writereg(d, 0x110, 0x00);
786 au0828_writereg(d, 0x111, 0x00);
787 au0828_writereg(d, 0x114, 0xa0);
788 au0828_writereg(d, 0x115, 0x05);
789 /* set y position */
790 au0828_writereg(d, 0x112, 0x00);
791 au0828_writereg(d, 0x113, 0x00);
792 au0828_writereg(d, 0x116, 0xf2);
793 au0828_writereg(d, 0x117, 0x00);
794 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
796 return 0;
799 int au0828_analog_stream_disable(struct au0828_dev *d)
801 dprintk(1, "au0828_analog_stream_disable called\n");
802 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
803 return 0;
806 void au0828_analog_stream_reset(struct au0828_dev *dev)
808 dprintk(1, "au0828_analog_stream_reset called\n");
809 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
810 mdelay(30);
811 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
815 * Some operations needs to stop current streaming
817 static int au0828_stream_interrupt(struct au0828_dev *dev)
819 int ret = 0;
821 dev->stream_state = STREAM_INTERRUPT;
822 if (dev->dev_state == DEV_DISCONNECTED)
823 return -ENODEV;
824 else if (ret) {
825 dev->dev_state = DEV_MISCONFIGURED;
826 dprintk(1, "%s device is misconfigured!\n", __func__);
827 return ret;
829 return 0;
833 * au0828_release_resources
834 * unregister v4l2 devices
836 void au0828_analog_unregister(struct au0828_dev *dev)
838 dprintk(1, "au0828_release_resources called\n");
839 mutex_lock(&au0828_sysfs_lock);
841 if (dev->vdev)
842 video_unregister_device(dev->vdev);
843 if (dev->vbi_dev)
844 video_unregister_device(dev->vbi_dev);
846 mutex_unlock(&au0828_sysfs_lock);
850 /* Usage lock check functions */
851 static int res_get(struct au0828_fh *fh, unsigned int bit)
853 struct au0828_dev *dev = fh->dev;
855 if (fh->resources & bit)
856 /* have it already allocated */
857 return 1;
859 /* is it free? */
860 mutex_lock(&dev->lock);
861 if (dev->resources & bit) {
862 /* no, someone else uses it */
863 mutex_unlock(&dev->lock);
864 return 0;
866 /* it's free, grab it */
867 fh->resources |= bit;
868 dev->resources |= bit;
869 dprintk(1, "res: get %d\n", bit);
870 mutex_unlock(&dev->lock);
871 return 1;
874 static int res_check(struct au0828_fh *fh, unsigned int bit)
876 return fh->resources & bit;
879 static int res_locked(struct au0828_dev *dev, unsigned int bit)
881 return dev->resources & bit;
884 static void res_free(struct au0828_fh *fh, unsigned int bits)
886 struct au0828_dev *dev = fh->dev;
888 BUG_ON((fh->resources & bits) != bits);
890 mutex_lock(&dev->lock);
891 fh->resources &= ~bits;
892 dev->resources &= ~bits;
893 dprintk(1, "res: put %d\n", bits);
894 mutex_unlock(&dev->lock);
897 static int get_ressource(struct au0828_fh *fh)
899 switch (fh->type) {
900 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
901 return AU0828_RESOURCE_VIDEO;
902 case V4L2_BUF_TYPE_VBI_CAPTURE:
903 return AU0828_RESOURCE_VBI;
904 default:
905 BUG();
906 return 0;
910 static int au0828_v4l2_open(struct file *filp)
912 int ret = 0;
913 struct video_device *vdev = video_devdata(filp);
914 struct au0828_dev *dev = video_drvdata(filp);
915 struct au0828_fh *fh;
916 int type;
918 switch (vdev->vfl_type) {
919 case VFL_TYPE_GRABBER:
920 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
921 break;
922 case VFL_TYPE_VBI:
923 type = V4L2_BUF_TYPE_VBI_CAPTURE;
924 break;
925 default:
926 return -EINVAL;
929 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
930 if (NULL == fh) {
931 dprintk(1, "Failed allocate au0828_fh struct!\n");
932 return -ENOMEM;
935 fh->type = type;
936 fh->dev = dev;
937 filp->private_data = fh;
939 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
940 /* set au0828 interface0 to AS5 here again */
941 ret = usb_set_interface(dev->usbdev, 0, 5);
942 if (ret < 0) {
943 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
944 return -EBUSY;
946 dev->width = NTSC_STD_W;
947 dev->height = NTSC_STD_H;
948 dev->frame_size = dev->width * dev->height * 2;
949 dev->field_size = dev->width * dev->height;
950 dev->bytesperline = dev->width * 2;
952 au0828_analog_stream_enable(dev);
953 au0828_analog_stream_reset(dev);
955 /* If we were doing ac97 instead of i2s, it would go here...*/
956 au0828_i2s_init(dev);
958 dev->stream_state = STREAM_OFF;
959 dev->dev_state |= DEV_INITIALIZED;
962 dev->users++;
964 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
965 NULL, &dev->slock,
966 V4L2_BUF_TYPE_VIDEO_CAPTURE,
967 V4L2_FIELD_INTERLACED,
968 sizeof(struct au0828_buffer), fh);
970 /* VBI Setup */
971 dev->vbi_width = 720;
972 dev->vbi_height = 1;
973 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
974 NULL, &dev->slock,
975 V4L2_BUF_TYPE_VBI_CAPTURE,
976 V4L2_FIELD_SEQ_TB,
977 sizeof(struct au0828_buffer), fh);
980 return ret;
983 static int au0828_v4l2_close(struct file *filp)
985 int ret;
986 struct au0828_fh *fh = filp->private_data;
987 struct au0828_dev *dev = fh->dev;
989 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
990 videobuf_stop(&fh->vb_vidq);
991 res_free(fh, AU0828_RESOURCE_VIDEO);
994 if (res_check(fh, AU0828_RESOURCE_VBI)) {
995 videobuf_stop(&fh->vb_vbiq);
996 res_free(fh, AU0828_RESOURCE_VBI);
999 if (dev->users == 1) {
1000 if (dev->dev_state & DEV_DISCONNECTED) {
1001 au0828_analog_unregister(dev);
1002 kfree(dev);
1003 return 0;
1006 au0828_analog_stream_disable(dev);
1008 au0828_uninit_isoc(dev);
1010 /* Save some power by putting tuner to sleep */
1011 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1013 /* When close the device, set the usb intf0 into alt0 to free
1014 USB bandwidth */
1015 ret = usb_set_interface(dev->usbdev, 0, 0);
1016 if (ret < 0)
1017 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
1020 videobuf_mmap_free(&fh->vb_vidq);
1021 videobuf_mmap_free(&fh->vb_vbiq);
1022 kfree(fh);
1023 dev->users--;
1024 wake_up_interruptible_nr(&dev->open, 1);
1025 return 0;
1028 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1029 size_t count, loff_t *pos)
1031 struct au0828_fh *fh = filp->private_data;
1032 struct au0828_dev *dev = fh->dev;
1033 int rc;
1035 rc = check_dev(dev);
1036 if (rc < 0)
1037 return rc;
1039 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1040 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1041 return -EBUSY;
1043 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1044 filp->f_flags & O_NONBLOCK);
1047 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1048 if (!res_get(fh, AU0828_RESOURCE_VBI))
1049 return -EBUSY;
1051 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1052 filp->f_flags & O_NONBLOCK);
1055 return 0;
1058 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1060 struct au0828_fh *fh = filp->private_data;
1061 struct au0828_dev *dev = fh->dev;
1062 int rc;
1064 rc = check_dev(dev);
1065 if (rc < 0)
1066 return rc;
1068 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1069 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1070 return POLLERR;
1071 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1072 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1073 if (!res_get(fh, AU0828_RESOURCE_VBI))
1074 return POLLERR;
1075 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1076 } else {
1077 return POLLERR;
1081 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1083 struct au0828_fh *fh = filp->private_data;
1084 struct au0828_dev *dev = fh->dev;
1085 int rc;
1087 rc = check_dev(dev);
1088 if (rc < 0)
1089 return rc;
1091 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1092 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1093 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1094 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
1096 return rc;
1099 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1100 struct v4l2_format *format)
1102 int ret;
1103 int width = format->fmt.pix.width;
1104 int height = format->fmt.pix.height;
1105 unsigned int maxwidth, maxheight;
1107 maxwidth = 720;
1108 maxheight = 480;
1110 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1111 return -EINVAL;
1113 /* If they are demanding a format other than the one we support,
1114 bail out (tvtime asks for UYVY and then retries with YUYV) */
1115 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1116 return -EINVAL;
1118 /* format->fmt.pix.width only support 720 and height 480 */
1119 if (width != 720)
1120 width = 720;
1121 if (height != 480)
1122 height = 480;
1124 format->fmt.pix.width = width;
1125 format->fmt.pix.height = height;
1126 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1127 format->fmt.pix.bytesperline = width * 2;
1128 format->fmt.pix.sizeimage = width * height * 2;
1129 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1130 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1132 if (cmd == VIDIOC_TRY_FMT)
1133 return 0;
1135 /* maybe set new image format, driver current only support 720*480 */
1136 dev->width = width;
1137 dev->height = height;
1138 dev->frame_size = width * height * 2;
1139 dev->field_size = width * height;
1140 dev->bytesperline = width * 2;
1142 if (dev->stream_state == STREAM_ON) {
1143 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1144 ret = au0828_stream_interrupt(dev);
1145 if (ret != 0) {
1146 dprintk(1, "error interrupting video stream!\n");
1147 return ret;
1151 /* set au0828 interface0 to AS5 here again */
1152 ret = usb_set_interface(dev->usbdev, 0, 5);
1153 if (ret < 0) {
1154 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
1155 return -EBUSY;
1158 au0828_analog_stream_enable(dev);
1160 return 0;
1164 static int vidioc_queryctrl(struct file *file, void *priv,
1165 struct v4l2_queryctrl *qc)
1167 struct au0828_fh *fh = priv;
1168 struct au0828_dev *dev = fh->dev;
1169 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1170 if (qc->type)
1171 return 0;
1172 else
1173 return -EINVAL;
1176 static int vidioc_querycap(struct file *file, void *priv,
1177 struct v4l2_capability *cap)
1179 struct au0828_fh *fh = priv;
1180 struct au0828_dev *dev = fh->dev;
1182 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1183 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1184 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
1186 cap->version = AU0828_VERSION_CODE;
1188 /*set the device capabilities */
1189 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1190 V4L2_CAP_VBI_CAPTURE |
1191 V4L2_CAP_AUDIO |
1192 V4L2_CAP_READWRITE |
1193 V4L2_CAP_STREAMING |
1194 V4L2_CAP_TUNER;
1195 return 0;
1198 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1199 struct v4l2_fmtdesc *f)
1201 if (f->index)
1202 return -EINVAL;
1204 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1205 strcpy(f->description, "Packed YUV2");
1207 f->flags = 0;
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_fh *fh = priv;
1217 struct au0828_dev *dev = fh->dev;
1219 f->fmt.pix.width = dev->width;
1220 f->fmt.pix.height = dev->height;
1221 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1222 f->fmt.pix.bytesperline = dev->bytesperline;
1223 f->fmt.pix.sizeimage = dev->frame_size;
1224 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1225 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1226 return 0;
1229 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1230 struct v4l2_format *f)
1232 struct au0828_fh *fh = priv;
1233 struct au0828_dev *dev = fh->dev;
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_fh *fh = priv;
1242 struct au0828_dev *dev = fh->dev;
1243 int rc;
1245 rc = check_dev(dev);
1246 if (rc < 0)
1247 return rc;
1249 mutex_lock(&dev->lock);
1251 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1252 printk(KERN_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 mutex_unlock(&dev->lock);
1260 return rc;
1263 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1265 struct au0828_fh *fh = priv;
1266 struct au0828_dev *dev = fh->dev;
1269 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, *norm);
1270 return 0;
1273 static int vidioc_enum_input(struct file *file, void *priv,
1274 struct v4l2_input *input)
1276 struct au0828_fh *fh = priv;
1277 struct au0828_dev *dev = fh->dev;
1278 unsigned int tmp;
1280 static const char *inames[] = {
1281 [AU0828_VMUX_UNDEFINED] = "Undefined",
1282 [AU0828_VMUX_COMPOSITE] = "Composite",
1283 [AU0828_VMUX_SVIDEO] = "S-Video",
1284 [AU0828_VMUX_CABLE] = "Cable TV",
1285 [AU0828_VMUX_TELEVISION] = "Television",
1286 [AU0828_VMUX_DVB] = "DVB",
1287 [AU0828_VMUX_DEBUG] = "tv debug"
1290 tmp = input->index;
1292 if (tmp >= AU0828_MAX_INPUT)
1293 return -EINVAL;
1294 if (AUVI_INPUT(tmp).type == 0)
1295 return -EINVAL;
1297 input->index = tmp;
1298 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1299 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1300 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE))
1301 input->type |= V4L2_INPUT_TYPE_TUNER;
1302 else
1303 input->type |= V4L2_INPUT_TYPE_CAMERA;
1305 input->std = dev->vdev->tvnorms;
1307 return 0;
1310 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1312 struct au0828_fh *fh = priv;
1313 struct au0828_dev *dev = fh->dev;
1314 *i = dev->ctrl_input;
1315 return 0;
1318 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1320 struct au0828_fh *fh = priv;
1321 struct au0828_dev *dev = fh->dev;
1322 int i;
1324 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1325 index);
1326 if (index >= AU0828_MAX_INPUT)
1327 return -EINVAL;
1328 if (AUVI_INPUT(index).type == 0)
1329 return -EINVAL;
1330 dev->ctrl_input = index;
1332 switch (AUVI_INPUT(index).type) {
1333 case AU0828_VMUX_SVIDEO:
1334 dev->input_type = AU0828_VMUX_SVIDEO;
1335 break;
1336 case AU0828_VMUX_COMPOSITE:
1337 dev->input_type = AU0828_VMUX_COMPOSITE;
1338 break;
1339 case AU0828_VMUX_TELEVISION:
1340 dev->input_type = AU0828_VMUX_TELEVISION;
1341 break;
1342 default:
1343 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1344 AUVI_INPUT(index).type);
1345 break;
1348 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1349 AUVI_INPUT(index).vmux, 0, 0);
1351 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1352 int enable = 0;
1353 if (AUVI_INPUT(i).audio_setup == NULL)
1354 continue;
1356 if (i == index)
1357 enable = 1;
1358 else
1359 enable = 0;
1360 if (enable) {
1361 (AUVI_INPUT(i).audio_setup)(dev, enable);
1362 } else {
1363 /* Make sure we leave it turned on if some
1364 other input is routed to this callback */
1365 if ((AUVI_INPUT(i).audio_setup) !=
1366 ((AUVI_INPUT(index).audio_setup))) {
1367 (AUVI_INPUT(i).audio_setup)(dev, enable);
1372 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1373 AUVI_INPUT(index).amux, 0, 0);
1374 return 0;
1377 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1379 struct au0828_fh *fh = priv;
1380 struct au0828_dev *dev = fh->dev;
1381 unsigned int index = a->index;
1383 if (a->index > 1)
1384 return -EINVAL;
1386 index = dev->ctrl_ainput;
1387 if (index == 0)
1388 strcpy(a->name, "Television");
1389 else
1390 strcpy(a->name, "Line in");
1392 a->capability = V4L2_AUDCAP_STEREO;
1393 a->index = index;
1394 return 0;
1397 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1399 struct au0828_fh *fh = priv;
1400 struct au0828_dev *dev = fh->dev;
1401 if (a->index != dev->ctrl_ainput)
1402 return -EINVAL;
1403 return 0;
1406 static int vidioc_g_ctrl(struct file *file, void *priv,
1407 struct v4l2_control *ctrl)
1409 struct au0828_fh *fh = priv;
1410 struct au0828_dev *dev = fh->dev;
1412 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1413 return 0;
1417 static int vidioc_s_ctrl(struct file *file, void *priv,
1418 struct v4l2_control *ctrl)
1420 struct au0828_fh *fh = priv;
1421 struct au0828_dev *dev = fh->dev;
1422 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1423 return 0;
1426 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1428 struct au0828_fh *fh = priv;
1429 struct au0828_dev *dev = fh->dev;
1431 if (t->index != 0)
1432 return -EINVAL;
1434 strcpy(t->name, "Auvitek tuner");
1435 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1436 return 0;
1439 static int vidioc_s_tuner(struct file *file, void *priv,
1440 struct v4l2_tuner *t)
1442 struct au0828_fh *fh = priv;
1443 struct au0828_dev *dev = fh->dev;
1445 if (t->index != 0)
1446 return -EINVAL;
1448 t->type = V4L2_TUNER_ANALOG_TV;
1449 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1450 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1451 t->afc);
1452 return 0;
1456 static int vidioc_g_frequency(struct file *file, void *priv,
1457 struct v4l2_frequency *freq)
1459 struct au0828_fh *fh = priv;
1460 struct au0828_dev *dev = fh->dev;
1462 freq->type = V4L2_TUNER_ANALOG_TV;
1463 freq->frequency = dev->ctrl_freq;
1464 return 0;
1467 static int vidioc_s_frequency(struct file *file, void *priv,
1468 struct v4l2_frequency *freq)
1470 struct au0828_fh *fh = priv;
1471 struct au0828_dev *dev = fh->dev;
1473 if (freq->tuner != 0)
1474 return -EINVAL;
1475 if (freq->type != V4L2_TUNER_ANALOG_TV)
1476 return -EINVAL;
1478 dev->ctrl_freq = freq->frequency;
1480 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1482 au0828_analog_stream_reset(dev);
1484 return 0;
1488 /* RAW VBI ioctls */
1490 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1491 struct v4l2_format *format)
1493 struct au0828_fh *fh = priv;
1494 struct au0828_dev *dev = fh->dev;
1496 format->fmt.vbi.samples_per_line = dev->vbi_width;
1497 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1498 format->fmt.vbi.offset = 0;
1499 format->fmt.vbi.flags = 0;
1500 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1502 format->fmt.vbi.count[0] = dev->vbi_height;
1503 format->fmt.vbi.count[1] = dev->vbi_height;
1504 format->fmt.vbi.start[0] = 21;
1505 format->fmt.vbi.start[1] = 284;
1507 return 0;
1510 static int vidioc_g_chip_ident(struct file *file, void *priv,
1511 struct v4l2_dbg_chip_ident *chip)
1513 struct au0828_fh *fh = priv;
1514 struct au0828_dev *dev = fh->dev;
1515 chip->ident = V4L2_IDENT_NONE;
1516 chip->revision = 0;
1518 if (v4l2_chip_match_host(&chip->match)) {
1519 chip->ident = V4L2_IDENT_AU0828;
1520 return 0;
1523 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1524 if (chip->ident == V4L2_IDENT_NONE)
1525 return -EINVAL;
1527 return 0;
1530 static int vidioc_cropcap(struct file *file, void *priv,
1531 struct v4l2_cropcap *cc)
1533 struct au0828_fh *fh = priv;
1534 struct au0828_dev *dev = fh->dev;
1536 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1537 return -EINVAL;
1539 cc->bounds.left = 0;
1540 cc->bounds.top = 0;
1541 cc->bounds.width = dev->width;
1542 cc->bounds.height = dev->height;
1544 cc->defrect = cc->bounds;
1546 cc->pixelaspect.numerator = 54;
1547 cc->pixelaspect.denominator = 59;
1549 return 0;
1552 static int vidioc_streamon(struct file *file, void *priv,
1553 enum v4l2_buf_type type)
1555 struct au0828_fh *fh = priv;
1556 struct au0828_dev *dev = fh->dev;
1557 int rc = -EINVAL;
1559 rc = check_dev(dev);
1560 if (rc < 0)
1561 return rc;
1563 if (unlikely(type != fh->type))
1564 return -EINVAL;
1566 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1567 fh, type, fh->resources, dev->resources);
1569 if (unlikely(!res_get(fh, get_ressource(fh))))
1570 return -EBUSY;
1572 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1573 au0828_analog_stream_enable(dev);
1574 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1577 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1578 rc = videobuf_streamon(&fh->vb_vidq);
1579 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1580 rc = videobuf_streamon(&fh->vb_vbiq);
1582 return rc;
1585 static int vidioc_streamoff(struct file *file, void *priv,
1586 enum v4l2_buf_type type)
1588 struct au0828_fh *fh = priv;
1589 struct au0828_dev *dev = fh->dev;
1590 int rc;
1591 int i;
1593 rc = check_dev(dev);
1594 if (rc < 0)
1595 return rc;
1597 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1598 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1599 return -EINVAL;
1600 if (type != fh->type)
1601 return -EINVAL;
1603 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1604 fh, type, fh->resources, dev->resources);
1606 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1607 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1608 rc = au0828_stream_interrupt(dev);
1609 if (rc != 0)
1610 return rc;
1612 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1613 if (AUVI_INPUT(i).audio_setup == NULL)
1614 continue;
1615 (AUVI_INPUT(i).audio_setup)(dev, 0);
1618 videobuf_streamoff(&fh->vb_vidq);
1619 res_free(fh, AU0828_RESOURCE_VIDEO);
1620 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1621 videobuf_streamoff(&fh->vb_vbiq);
1622 res_free(fh, AU0828_RESOURCE_VBI);
1625 return 0;
1628 #ifdef CONFIG_VIDEO_ADV_DEBUG
1629 static int vidioc_g_register(struct file *file, void *priv,
1630 struct v4l2_dbg_register *reg)
1632 struct au0828_fh *fh = priv;
1633 struct au0828_dev *dev = fh->dev;
1635 switch (reg->match.type) {
1636 case V4L2_CHIP_MATCH_I2C_DRIVER:
1637 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1638 return 0;
1639 default:
1640 return -EINVAL;
1644 static int vidioc_s_register(struct file *file, void *priv,
1645 struct v4l2_dbg_register *reg)
1647 struct au0828_fh *fh = priv;
1648 struct au0828_dev *dev = fh->dev;
1650 switch (reg->match.type) {
1651 case V4L2_CHIP_MATCH_I2C_DRIVER:
1652 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1653 return 0;
1654 default:
1655 return -EINVAL;
1657 return 0;
1659 #endif
1661 static int vidioc_reqbufs(struct file *file, void *priv,
1662 struct v4l2_requestbuffers *rb)
1664 struct au0828_fh *fh = priv;
1665 struct au0828_dev *dev = fh->dev;
1666 int rc;
1668 rc = check_dev(dev);
1669 if (rc < 0)
1670 return rc;
1672 return videobuf_reqbufs(&fh->vb_vidq, rb);
1675 static int vidioc_querybuf(struct file *file, void *priv,
1676 struct v4l2_buffer *b)
1678 struct au0828_fh *fh = priv;
1679 struct au0828_dev *dev = fh->dev;
1680 int rc;
1682 rc = check_dev(dev);
1683 if (rc < 0)
1684 return rc;
1686 return videobuf_querybuf(&fh->vb_vidq, b);
1689 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1691 struct au0828_fh *fh = priv;
1692 struct au0828_dev *dev = fh->dev;
1693 int rc;
1695 rc = check_dev(dev);
1696 if (rc < 0)
1697 return rc;
1699 return videobuf_qbuf(&fh->vb_vidq, b);
1702 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1704 struct au0828_fh *fh = priv;
1705 struct au0828_dev *dev = fh->dev;
1706 int rc;
1708 rc = check_dev(dev);
1709 if (rc < 0)
1710 return rc;
1712 if (dev->greenscreen_detected == 1) {
1713 dprintk(1, "Detected green frame. Resetting stream...\n");
1714 au0828_analog_stream_reset(dev);
1715 dev->greenscreen_detected = 0;
1718 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1721 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1722 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1724 struct au0828_fh *fh = priv;
1726 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1728 #endif
1730 static struct v4l2_file_operations au0828_v4l_fops = {
1731 .owner = THIS_MODULE,
1732 .open = au0828_v4l2_open,
1733 .release = au0828_v4l2_close,
1734 .read = au0828_v4l2_read,
1735 .poll = au0828_v4l2_poll,
1736 .mmap = au0828_v4l2_mmap,
1737 .ioctl = video_ioctl2,
1740 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1741 .vidioc_querycap = vidioc_querycap,
1742 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1743 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1744 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1745 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1746 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1747 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1748 .vidioc_g_audio = vidioc_g_audio,
1749 .vidioc_s_audio = vidioc_s_audio,
1750 .vidioc_cropcap = vidioc_cropcap,
1751 .vidioc_reqbufs = vidioc_reqbufs,
1752 .vidioc_querybuf = vidioc_querybuf,
1753 .vidioc_qbuf = vidioc_qbuf,
1754 .vidioc_dqbuf = vidioc_dqbuf,
1755 .vidioc_s_std = vidioc_s_std,
1756 .vidioc_enum_input = vidioc_enum_input,
1757 .vidioc_g_input = vidioc_g_input,
1758 .vidioc_s_input = vidioc_s_input,
1759 .vidioc_queryctrl = vidioc_queryctrl,
1760 .vidioc_g_ctrl = vidioc_g_ctrl,
1761 .vidioc_s_ctrl = vidioc_s_ctrl,
1762 .vidioc_streamon = vidioc_streamon,
1763 .vidioc_streamoff = vidioc_streamoff,
1764 .vidioc_g_tuner = vidioc_g_tuner,
1765 .vidioc_s_tuner = vidioc_s_tuner,
1766 .vidioc_g_frequency = vidioc_g_frequency,
1767 .vidioc_s_frequency = vidioc_s_frequency,
1768 #ifdef CONFIG_VIDEO_ADV_DEBUG
1769 .vidioc_g_register = vidioc_g_register,
1770 .vidioc_s_register = vidioc_s_register,
1771 #endif
1772 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1773 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1774 .vidiocgmbuf = vidiocgmbuf,
1775 #endif
1778 static const struct video_device au0828_video_template = {
1779 .fops = &au0828_v4l_fops,
1780 .release = video_device_release,
1781 .ioctl_ops = &video_ioctl_ops,
1782 .tvnorms = V4L2_STD_NTSC_M,
1783 .current_norm = V4L2_STD_NTSC_M,
1786 /**************************************************************************/
1788 int au0828_analog_register(struct au0828_dev *dev,
1789 struct usb_interface *interface)
1791 int retval = -ENOMEM;
1792 struct usb_host_interface *iface_desc;
1793 struct usb_endpoint_descriptor *endpoint;
1794 int i;
1796 dprintk(1, "au0828_analog_register called!\n");
1798 /* set au0828 usb interface0 to as5 */
1799 retval = usb_set_interface(dev->usbdev,
1800 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1801 if (retval != 0) {
1802 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
1803 return retval;
1806 /* Figure out which endpoint has the isoc interface */
1807 iface_desc = interface->cur_altsetting;
1808 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1809 endpoint = &iface_desc->endpoint[i].desc;
1810 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1811 == USB_DIR_IN) &&
1812 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1813 == USB_ENDPOINT_XFER_ISOC)) {
1815 /* we find our isoc in endpoint */
1816 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1817 dev->max_pkt_size = (tmp & 0x07ff) *
1818 (((tmp & 0x1800) >> 11) + 1);
1819 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1822 if (!(dev->isoc_in_endpointaddr)) {
1823 printk(KERN_INFO "Could not locate isoc endpoint\n");
1824 kfree(dev);
1825 return -ENODEV;
1828 init_waitqueue_head(&dev->open);
1829 spin_lock_init(&dev->slock);
1830 mutex_init(&dev->lock);
1832 /* init video dma queues */
1833 INIT_LIST_HEAD(&dev->vidq.active);
1834 INIT_LIST_HEAD(&dev->vidq.queued);
1835 INIT_LIST_HEAD(&dev->vbiq.active);
1836 INIT_LIST_HEAD(&dev->vbiq.queued);
1838 dev->width = NTSC_STD_W;
1839 dev->height = NTSC_STD_H;
1840 dev->field_size = dev->width * dev->height;
1841 dev->frame_size = dev->field_size << 1;
1842 dev->bytesperline = dev->width << 1;
1843 dev->ctrl_ainput = 0;
1845 /* allocate and fill v4l2 video struct */
1846 dev->vdev = video_device_alloc();
1847 if (NULL == dev->vdev) {
1848 dprintk(1, "Can't allocate video_device.\n");
1849 return -ENOMEM;
1852 /* allocate the VBI struct */
1853 dev->vbi_dev = video_device_alloc();
1854 if (NULL == dev->vbi_dev) {
1855 dprintk(1, "Can't allocate vbi_device.\n");
1856 kfree(dev->vdev);
1857 return -ENOMEM;
1860 /* Fill the video capture device struct */
1861 *dev->vdev = au0828_video_template;
1862 dev->vdev->parent = &dev->usbdev->dev;
1863 strcpy(dev->vdev->name, "au0828a video");
1865 /* Setup the VBI device */
1866 *dev->vbi_dev = au0828_video_template;
1867 dev->vbi_dev->parent = &dev->usbdev->dev;
1868 strcpy(dev->vbi_dev->name, "au0828a vbi");
1870 /* Register the v4l2 device */
1871 video_set_drvdata(dev->vdev, dev);
1872 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1873 if (retval != 0) {
1874 dprintk(1, "unable to register video device (error = %d).\n",
1875 retval);
1876 video_device_release(dev->vdev);
1877 return -ENODEV;
1880 /* Register the vbi device */
1881 video_set_drvdata(dev->vbi_dev, dev);
1882 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
1883 if (retval != 0) {
1884 dprintk(1, "unable to register vbi device (error = %d).\n",
1885 retval);
1886 video_device_release(dev->vbi_dev);
1887 video_device_release(dev->vdev);
1888 return -ENODEV;
1891 dprintk(1, "%s completed!\n", __func__);
1893 return 0;