V4L/DVB (11066): au0828: add support for analog functionality in bridge
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / au0828 / au0828-video.c
blobe34464f81f3649a2d9845a63402ec30857ff7509
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/init.h>
33 #include <linux/device.h>
34 #include <linux/suspend.h>
35 #include <linux/version.h>
36 #include <linux/videodev.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 LIST_HEAD(au0828_devlist);
45 static DEFINE_MUTEX(au0828_sysfs_lock);
47 #define AU0828_VERSION_CODE KERNEL_VERSION(0, 0, 1)
49 /* Forward declarations */
50 void au0828_analog_stream_reset(struct au0828_dev *dev);
52 /* ------------------------------------------------------------------
53 Videobuf operations
54 ------------------------------------------------------------------*/
56 static unsigned int isoc_debug;
57 module_param(isoc_debug, int, 0644);
58 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
60 #define au0828_isocdbg(fmt, arg...) \
61 do {\
62 if (isoc_debug) { \
63 printk(KERN_INFO "au0828 %s :"fmt, \
64 __func__ , ##arg); \
65 } \
66 } while (0)
68 static inline void print_err_status(struct au0828_dev *dev,
69 int packet, int status)
71 char *errmsg = "Unknown";
73 switch (status) {
74 case -ENOENT:
75 errmsg = "unlinked synchronuously";
76 break;
77 case -ECONNRESET:
78 errmsg = "unlinked asynchronuously";
79 break;
80 case -ENOSR:
81 errmsg = "Buffer error (overrun)";
82 break;
83 case -EPIPE:
84 errmsg = "Stalled (device not responding)";
85 break;
86 case -EOVERFLOW:
87 errmsg = "Babble (bad cable?)";
88 break;
89 case -EPROTO:
90 errmsg = "Bit-stuff error (bad cable?)";
91 break;
92 case -EILSEQ:
93 errmsg = "CRC/Timeout (could be anything)";
94 break;
95 case -ETIME:
96 errmsg = "Device does not respond";
97 break;
99 if (packet < 0) {
100 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
101 } else {
102 au0828_isocdbg("URB packet %d, status %d [%s].\n",
103 packet, status, errmsg);
107 static int check_dev(struct au0828_dev *dev)
109 if (dev->dev_state & DEV_DISCONNECTED) {
110 printk("v4l2 ioctl: device not present\n");
111 return -ENODEV;
114 if (dev->dev_state & DEV_MISCONFIGURED) {
115 printk("v4l2 ioctl: device is misconfigured; "
116 "close and open it again\n");
117 return -EIO;
119 return 0;
123 * IRQ callback, called by URB callback
125 static void au0828_irq_callback(struct urb *urb)
127 struct au0828_dmaqueue *dma_q = urb->context;
128 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
129 int rc, i;
131 switch (urb->status) {
132 case 0: /* success */
133 case -ETIMEDOUT: /* NAK */
134 break;
135 case -ECONNRESET: /* kill */
136 case -ENOENT:
137 case -ESHUTDOWN:
138 au0828_isocdbg("au0828_irq_callback called: status kill\n");
139 return;
140 default: /* unknown error */
141 au0828_isocdbg("urb completition error %d.\n", urb->status);
142 break;
145 /* Copy data from URB */
146 spin_lock(&dev->slock);
147 rc = dev->isoc_ctl.isoc_copy(dev, urb);
148 spin_unlock(&dev->slock);
150 /* Reset urb buffers */
151 for (i = 0; i < urb->number_of_packets; i++) {
152 urb->iso_frame_desc[i].status = 0;
153 urb->iso_frame_desc[i].actual_length = 0;
155 urb->status = 0;
157 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
158 if (urb->status) {
159 au0828_isocdbg("urb resubmit failed (error=%i)\n",
160 urb->status);
165 * Stop and Deallocate URBs
167 void au0828_uninit_isoc(struct au0828_dev *dev)
169 struct urb *urb;
170 int i;
172 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
174 dev->isoc_ctl.nfields = -1;
175 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
176 urb = dev->isoc_ctl.urb[i];
177 if (urb) {
178 if (!irqs_disabled())
179 usb_kill_urb(urb);
180 else
181 usb_unlink_urb(urb);
183 if (dev->isoc_ctl.transfer_buffer[i]) {
184 usb_buffer_free(dev->usbdev,
185 urb->transfer_buffer_length,
186 dev->isoc_ctl.transfer_buffer[i],
187 urb->transfer_dma);
189 usb_free_urb(urb);
190 dev->isoc_ctl.urb[i] = NULL;
192 dev->isoc_ctl.transfer_buffer[i] = NULL;
195 kfree(dev->isoc_ctl.urb);
196 kfree(dev->isoc_ctl.transfer_buffer);
198 dev->isoc_ctl.urb = NULL;
199 dev->isoc_ctl.transfer_buffer = NULL;
200 dev->isoc_ctl.num_bufs = 0;
204 * Allocate URBs and start IRQ
206 int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
207 int num_bufs, int max_pkt_size,
208 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
210 struct au0828_dmaqueue *dma_q = &dev->vidq;
211 int i;
212 int sb_size, pipe;
213 struct urb *urb;
214 int j, k;
215 int rc;
217 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
219 /* De-allocates all pending stuff */
220 au0828_uninit_isoc(dev);
222 dev->isoc_ctl.isoc_copy = isoc_copy;
223 dev->isoc_ctl.num_bufs = num_bufs;
225 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
226 if (!dev->isoc_ctl.urb) {
227 au0828_isocdbg("cannot alloc memory for usb buffers\n");
228 return -ENOMEM;
231 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
232 GFP_KERNEL);
233 if (!dev->isoc_ctl.transfer_buffer) {
234 au0828_isocdbg("cannot allocate memory for usb transfer\n");
235 kfree(dev->isoc_ctl.urb);
236 return -ENOMEM;
239 dev->isoc_ctl.max_pkt_size = max_pkt_size;
240 dev->isoc_ctl.buf = NULL;
242 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
244 /* allocate urbs and transfer buffers */
245 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
246 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
247 if (!urb) {
248 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
249 au0828_uninit_isoc(dev);
250 return -ENOMEM;
252 dev->isoc_ctl.urb[i] = urb;
254 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->usbdev,
255 sb_size, GFP_KERNEL, &urb->transfer_dma);
256 if (!dev->isoc_ctl.transfer_buffer[i]) {
257 printk("unable to allocate %i bytes for transfer"
258 " buffer %i%s\n",
259 sb_size, i,
260 in_interrupt() ? " while in int" : "");
261 au0828_uninit_isoc(dev);
262 return -ENOMEM;
264 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
266 pipe = usb_rcvisocpipe(dev->usbdev,
267 dev->isoc_in_endpointaddr),
269 usb_fill_int_urb(urb, dev->usbdev, pipe,
270 dev->isoc_ctl.transfer_buffer[i], sb_size,
271 au0828_irq_callback, dma_q, 1);
273 urb->number_of_packets = max_packets;
274 urb->transfer_flags = URB_ISO_ASAP;
276 k = 0;
277 for (j = 0; j < max_packets; j++) {
278 urb->iso_frame_desc[j].offset = k;
279 urb->iso_frame_desc[j].length =
280 dev->isoc_ctl.max_pkt_size;
281 k += dev->isoc_ctl.max_pkt_size;
285 init_waitqueue_head(&dma_q->wq);
287 /* submit urbs and enables IRQ */
288 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
289 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
290 if (rc) {
291 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
292 i, rc);
293 au0828_uninit_isoc(dev);
294 return rc;
298 return 0;
302 * Announces that a buffer were filled and request the next
304 static inline void buffer_filled(struct au0828_dev *dev,
305 struct au0828_dmaqueue *dma_q,
306 struct au0828_buffer *buf)
308 /* Advice that buffer was filled */
309 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
311 buf->vb.state = VIDEOBUF_DONE;
312 buf->vb.field_count++;
313 do_gettimeofday(&buf->vb.ts);
315 dev->isoc_ctl.buf = NULL;
317 list_del(&buf->vb.queue);
318 wake_up(&buf->vb.done);
322 * Identify the buffer header type and properly handles
324 static void au0828_copy_video(struct au0828_dev *dev,
325 struct au0828_dmaqueue *dma_q,
326 struct au0828_buffer *buf,
327 unsigned char *p,
328 unsigned char *outp, unsigned long len)
330 void *fieldstart, *startwrite, *startread;
331 int linesdone, currlinedone, offset, lencopy, remain;
332 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
334 if (dma_q->pos + len > buf->vb.size)
335 len = buf->vb.size - dma_q->pos;
337 startread = p;
338 remain = len;
340 /* Interlaces frame */
341 if (buf->top_field)
342 fieldstart = outp;
343 else
344 fieldstart = outp + bytesperline;
346 linesdone = dma_q->pos / bytesperline;
347 currlinedone = dma_q->pos % bytesperline;
348 offset = linesdone * bytesperline * 2 + currlinedone;
349 startwrite = fieldstart + offset;
350 lencopy = bytesperline - currlinedone;
351 lencopy = lencopy > remain ? remain : lencopy;
353 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
354 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
355 ((char *)startwrite + lencopy) -
356 ((char *)outp + buf->vb.size));
357 remain = (char *)outp + buf->vb.size - (char *)startwrite;
358 lencopy = remain;
360 if (lencopy <= 0)
361 return;
362 memcpy(startwrite, startread, lencopy);
364 remain -= lencopy;
366 while (remain > 0) {
367 startwrite += lencopy + bytesperline;
368 startread += lencopy;
369 if (bytesperline > remain)
370 lencopy = remain;
371 else
372 lencopy = bytesperline;
374 if ((char *)startwrite + lencopy > (char *)outp +
375 buf->vb.size) {
376 au0828_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
377 ((char *)startwrite + lencopy) -
378 ((char *)outp + buf->vb.size));
379 lencopy = remain = (char *)outp + buf->vb.size -
380 (char *)startwrite;
382 if (lencopy <= 0)
383 break;
385 memcpy(startwrite, startread, lencopy);
387 remain -= lencopy;
390 if (offset > 1440) {
391 /* We have enough data to check for greenscreen */
392 if (outp[0] < 0x60 && outp[1440] < 0x60) {
393 dev->greenscreen_detected = 1;
397 dma_q->pos += len;
401 * video-buf generic routine to get the next available buffer
403 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
404 struct au0828_buffer **buf)
406 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
408 if (list_empty(&dma_q->active)) {
409 au0828_isocdbg("No active queue to serve\n");
410 dev->isoc_ctl.buf = NULL;
411 *buf = NULL;
412 return;
415 /* Get the next buffer */
416 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
417 dev->isoc_ctl.buf = *buf;
419 return;
423 * Controls the isoc copy of each urb packet
425 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
427 struct au0828_buffer *buf;
428 struct au0828_dmaqueue *dma_q = urb->context;
429 unsigned char *outp = NULL;
430 int i, len = 0, rc = 1;
431 unsigned char *p;
432 unsigned char fbyte;
434 if (!dev)
435 return 0;
437 if ((dev->dev_state & DEV_DISCONNECTED) ||
438 (dev->dev_state & DEV_MISCONFIGURED))
439 return 0;
441 if (urb->status < 0) {
442 print_err_status(dev, -1, urb->status);
443 if (urb->status == -ENOENT)
444 return 0;
447 buf = dev->isoc_ctl.buf;
448 if (buf != NULL)
449 outp = videobuf_to_vmalloc(&buf->vb);
451 for (i = 0; i < urb->number_of_packets; i++) {
452 int status = urb->iso_frame_desc[i].status;
454 if (status < 0) {
455 print_err_status(dev, i, status);
456 if (urb->iso_frame_desc[i].status != -EPROTO)
457 continue;
460 if (urb->iso_frame_desc[i].actual_length <= 0) {
461 continue;
463 if (urb->iso_frame_desc[i].actual_length >
464 dev->max_pkt_size) {
465 au0828_isocdbg("packet bigger than packet size");
466 continue;
469 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
470 fbyte = p[0];
471 len = urb->iso_frame_desc[i].actual_length - 4;
472 p += 4;
474 if (fbyte & 0x80) {
475 len -= 4;
476 p += 4;
477 au0828_isocdbg("Video frame %s\n",
478 (fbyte & 0x40) ? "odd" : "even");
479 if (!(fbyte & 0x40)) {
480 if (buf != NULL)
481 buffer_filled(dev, dma_q, buf);
482 get_next_buf(dma_q, &buf);
483 if (buf == NULL) {
484 outp = NULL;
485 } else
486 outp = videobuf_to_vmalloc(&buf->vb);
489 if (buf != NULL) {
490 if (fbyte & 0x40) {
491 buf->top_field = 1;
492 } else {
493 buf->top_field = 0;
497 dma_q->pos = 0;
499 if (buf != NULL) {
500 au0828_copy_video(dev, dma_q, buf, p, outp, len);
503 return rc;
506 static int
507 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
508 unsigned int *size)
510 struct au0828_fh *fh = vq->priv_data;
511 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
513 if (0 == *count)
514 *count = AU0828_DEF_BUF;
516 if (*count < AU0828_MIN_BUF)
517 *count = AU0828_MIN_BUF;
518 return 0;
521 /* This is called *without* dev->slock held; please keep it that way */
522 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
524 struct au0828_fh *fh = vq->priv_data;
525 struct au0828_dev *dev = fh->dev;
526 unsigned long flags = 0;
527 if (in_interrupt())
528 BUG();
530 /* We used to wait for the buffer to finish here, but this didn't work
531 because, as we were keeping the state as VIDEOBUF_QUEUED,
532 videobuf_queue_cancel marked it as finished for us.
533 (Also, it could wedge forever if the hardware was misconfigured.)
535 This should be safe; by the time we get here, the buffer isn't
536 queued anymore. If we ever start marking the buffers as
537 VIDEOBUF_ACTIVE, it won't be, though.
539 spin_lock_irqsave(&dev->slock, flags);
540 if (dev->isoc_ctl.buf == buf)
541 dev->isoc_ctl.buf = NULL;
542 spin_unlock_irqrestore(&dev->slock, flags);
544 videobuf_vmalloc_free(&buf->vb);
545 buf->vb.state = VIDEOBUF_NEEDS_INIT;
548 static int
549 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
550 enum v4l2_field field)
552 struct au0828_fh *fh = vq->priv_data;
553 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
554 struct au0828_dev *dev = fh->dev;
555 int rc = 0, urb_init = 0;
557 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
559 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
560 return -EINVAL;
562 buf->vb.width = dev->width;
563 buf->vb.height = dev->height;
564 buf->vb.field = field;
566 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
567 rc = videobuf_iolock(vq, &buf->vb, NULL);
568 if (rc < 0) {
569 printk("videobuf_iolock failed\n");
570 goto fail;
574 if (!dev->isoc_ctl.num_bufs)
575 urb_init = 1;
577 if (urb_init) {
578 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
579 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
580 au0828_isoc_copy);
581 if (rc < 0) {
582 printk("au0828_init_isoc failed\n");
583 goto fail;
587 buf->vb.state = VIDEOBUF_PREPARED;
588 return 0;
590 fail:
591 free_buffer(vq, buf);
592 return rc;
595 static void
596 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
598 struct au0828_buffer *buf = container_of(vb,
599 struct au0828_buffer,
600 vb);
601 struct au0828_fh *fh = vq->priv_data;
602 struct au0828_dev *dev = fh->dev;
603 struct au0828_dmaqueue *vidq = &dev->vidq;
605 buf->vb.state = VIDEOBUF_QUEUED;
606 list_add_tail(&buf->vb.queue, &vidq->active);
609 static void buffer_release(struct videobuf_queue *vq,
610 struct videobuf_buffer *vb)
612 struct au0828_buffer *buf = container_of(vb,
613 struct au0828_buffer,
614 vb);
616 free_buffer(vq, buf);
619 static struct videobuf_queue_ops au0828_video_qops = {
620 .buf_setup = buffer_setup,
621 .buf_prepare = buffer_prepare,
622 .buf_queue = buffer_queue,
623 .buf_release = buffer_release,
626 /* ------------------------------------------------------------------
627 V4L2 interface
628 ------------------------------------------------------------------*/
630 static int au0828_i2s_init(struct au0828_dev *dev)
632 /* Enable i2s mode */
633 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
634 return 0;
638 * Auvitek au0828 analog stream enable
639 * Please set interface0 to AS5 before enable the stream
641 int au0828_analog_stream_enable(struct au0828_dev *d)
643 dprintk(1, "au0828_analog_stream_enable called\n");
644 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
645 au0828_writereg(d, 0x106, 0x00);
646 /* set x position */
647 au0828_writereg(d, 0x110, 0x00);
648 au0828_writereg(d, 0x111, 0x00);
649 au0828_writereg(d, 0x114, 0xa0);
650 au0828_writereg(d, 0x115, 0x05);
651 /* set y position */
652 au0828_writereg(d, 0x112, 0x02);
653 au0828_writereg(d, 0x113, 0x00);
654 au0828_writereg(d, 0x116, 0xf2);
655 au0828_writereg(d, 0x117, 0x00);
656 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
658 return 0;
661 int au0828_analog_stream_disable(struct au0828_dev *d)
663 dprintk(1, "au0828_analog_stream_disable called\n");
664 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
665 return 0;
668 void au0828_analog_stream_reset(struct au0828_dev *dev)
670 dprintk(1, "au0828_analog_stream_reset called\n");
671 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
672 mdelay(30);
673 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
677 * Some operations needs to stop current streaming
679 static int au0828_stream_interrupt(struct au0828_dev *dev)
681 int ret = 0;
683 dev->stream_state = STREAM_INTERRUPT;
684 if(dev->dev_state == DEV_DISCONNECTED)
685 return -ENODEV;
686 else if(ret) {
687 dev->dev_state = DEV_MISCONFIGURED;
688 dprintk(1, "%s device is misconfigured!\n", __FUNCTION__);
689 return ret;
691 return 0;
695 * au0828_release_resources
696 * unregister v4l2 devices
698 void au0828_analog_unregister(struct au0828_dev *dev)
700 dprintk(1, "au0828_release_resources called\n");
701 mutex_lock(&au0828_sysfs_lock);
703 list_del(&dev->au0828list);
704 video_unregister_device(dev->vdev);
705 video_unregister_device(dev->vbi_dev);
707 mutex_unlock(&au0828_sysfs_lock);
711 /* Usage lock check functions */
712 static int res_get(struct au0828_fh *fh)
714 struct au0828_dev *dev = fh->dev;
715 int rc = 0;
717 /* This instance already has stream_on */
718 if (fh->stream_on)
719 return rc;
721 if (dev->stream_on)
722 return -EBUSY;
724 dev->stream_on = 1;
725 fh->stream_on = 1;
726 return rc;
729 static int res_check(struct au0828_fh *fh)
731 return fh->stream_on;
734 static void res_free(struct au0828_fh *fh)
736 struct au0828_dev *dev = fh->dev;
738 fh->stream_on = 0;
739 dev->stream_on = 0;
742 static int au0828_v4l2_open(struct file *filp)
744 int minor = video_devdata(filp)->minor;
745 int ret = 0;
746 struct au0828_dev *h, *dev = NULL;
747 struct au0828_fh *fh;
748 int type = 0;
749 struct list_head *list;
751 list_for_each(list, &au0828_devlist) {
752 h = list_entry(list, struct au0828_dev, au0828list);
753 if(h->vdev->minor == minor) {
754 dev = h;
755 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
757 if(h->vbi_dev->minor == minor) {
758 dev = h;
759 type = V4L2_BUF_TYPE_VBI_CAPTURE;
763 if(NULL == dev)
764 return -ENODEV;
766 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
767 if(NULL == fh) {
768 dprintk(1, "Failed allocate au0828_fh struct!\n");
769 return -ENOMEM;
772 fh->type = type;
773 fh->dev = dev;
774 filp->private_data = fh;
776 if(fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
777 /* set au0828 interface0 to AS5 here again */
778 ret = usb_set_interface(dev->usbdev, 0, 5);
779 if(ret < 0) {
780 printk("Au0828 can't set alt setting to 5!\n");
781 return -EBUSY;
783 dev->width = NTSC_STD_W;
784 dev->height = NTSC_STD_H;
785 dev->frame_size = dev->width * dev->height * 2;
786 dev->field_size = dev->width * dev->height;
787 dev->bytesperline = dev->width * 2;
789 au0828_analog_stream_enable(dev);
790 au0828_analog_stream_reset(dev);
792 /* If we were doing ac97 instead of i2s, it would go here...*/
793 au0828_i2s_init(dev);
795 dev->stream_state = STREAM_OFF;
796 dev->dev_state |= DEV_INITIALIZED;
799 dev->users++;
801 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
802 NULL, &dev->slock, fh->type,
803 V4L2_FIELD_INTERLACED,
804 sizeof(struct au0828_buffer), fh);
806 return ret;
809 static int au0828_v4l2_close(struct file *filp)
811 int ret;
812 struct au0828_fh *fh = filp->private_data;
813 struct au0828_dev *dev = fh->dev;
815 mutex_lock(&dev->lock);
816 if (res_check(fh))
817 res_free(fh);
819 if(dev->users == 1) {
820 videobuf_stop(&fh->vb_vidq);
821 videobuf_mmap_free(&fh->vb_vidq);
823 if(dev->dev_state & DEV_DISCONNECTED) {
824 au0828_analog_unregister(dev);
825 mutex_unlock(&dev->lock);
826 kfree(dev);
827 return 0;
830 au0828_analog_stream_disable(dev);
832 au0828_uninit_isoc(dev);
834 /* When close the device, set the usb intf0 into alt0 to free
835 USB bandwidth */
836 ret = usb_set_interface(dev->usbdev, 0, 0);
837 if(ret < 0)
838 printk("Au0828 can't set alt setting to 0!\n");
841 kfree(fh);
842 dev->users--;
843 wake_up_interruptible_nr(&dev->open, 1);
844 mutex_unlock(&dev->lock);
845 return 0;
848 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
849 size_t count, loff_t *pos)
851 struct au0828_fh *fh = filp->private_data;
852 struct au0828_dev *dev = fh->dev;
853 int rc;
855 rc = check_dev(dev);
856 if (rc < 0)
857 return rc;
859 if(fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
860 mutex_lock(&dev->lock);
861 rc = res_get(fh);
862 mutex_unlock(&dev->lock);
864 if (unlikely(rc < 0))
865 return rc;
867 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
868 filp->f_flags & O_NONBLOCK);
870 return 0;
873 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
875 struct au0828_fh *fh = filp->private_data;
876 struct au0828_dev *dev = fh->dev;
877 int rc;
879 rc = check_dev(dev);
880 if (rc < 0)
881 return rc;
883 mutex_lock(&dev->lock);
884 rc = res_get(fh);
885 mutex_unlock(&dev->lock);
887 if (unlikely(rc < 0))
888 return POLLERR;
890 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
891 return POLLERR;
893 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
896 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
898 struct au0828_fh *fh = filp->private_data;
899 struct au0828_dev *dev = fh->dev;
900 int rc;
902 rc = check_dev(dev);
903 if (rc < 0)
904 return rc;
906 mutex_lock(&dev->lock);
907 rc = res_get(fh);
908 mutex_unlock(&dev->lock);
910 if (unlikely(rc < 0))
911 return rc;
913 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
915 dprintk(2, "vma start=0x%08lx, size=%ld, ret=%d\n",
916 (unsigned long)vma->vm_start,
917 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
918 rc);
920 return rc;
923 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
924 struct v4l2_format *format)
926 int ret;
927 int width = format->fmt.pix.width;
928 int height = format->fmt.pix.height;
929 unsigned int maxwidth, maxheight;
931 maxwidth = 720;
932 maxheight = 480;
934 if(format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
935 dprintk(1, "VBI format set: to be supported!\n");
936 return 0;
938 if(format->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
939 return 0;
941 if(format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
942 return -EINVAL;
945 /* If they are demanding a format other than the one we support,
946 bail out (tvtime asks for UYVY and then retries with YUYV) */
947 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY) {
948 return -EINVAL;
951 /* format->fmt.pix.width only support 720 and height 480 */
952 if(width != 720)
953 width = 720;
954 if(height != 480)
955 height = 480;
957 format->fmt.pix.width = width;
958 format->fmt.pix.height = height;
959 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
960 format->fmt.pix.bytesperline = width * 2;
961 format->fmt.pix.sizeimage = width * height * 2;
962 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
963 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
965 if(cmd == VIDIOC_TRY_FMT)
966 return 0;
968 /* maybe set new image format, driver current only support 720*480 */
969 dev->width = width;
970 dev->height = height;
971 dev->frame_size = width * height * 2;
972 dev->field_size = width * height;
973 dev->bytesperline = width * 2;
975 if(dev->stream_state == STREAM_ON) {
976 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
977 if((ret = au0828_stream_interrupt(dev))) {
978 dprintk(1, "error interrupting video stream!\n");
979 return ret;
983 /* set au0828 interface0 to AS5 here again */
984 ret = usb_set_interface(dev->usbdev, 0, 5);
985 if(ret < 0) {
986 printk("Au0828 can't set alt setting to 5!\n");
987 return -EBUSY;
990 au0828_analog_stream_enable(dev);
992 return 0;
996 static int vidioc_queryctrl(struct file *file, void *priv,
997 struct v4l2_queryctrl *qc)
999 struct au0828_fh *fh = priv;
1000 struct au0828_dev *dev = fh->dev;
1001 au0828_call_i2c_clients(dev, VIDIOC_QUERYCTRL, qc);
1002 if (qc->type)
1003 return 0;
1004 else
1005 return -EINVAL;
1008 static int vidioc_querycap(struct file *file, void *priv,
1009 struct v4l2_capability *cap)
1011 struct au0828_fh *fh = priv;
1012 struct au0828_dev *dev = fh->dev;
1014 memset(cap, 0, sizeof(*cap));
1015 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1016 strlcpy(cap->card, au0828_boards[dev->board].name, sizeof(cap->card));
1017 strlcpy(cap->bus_info, dev->usbdev->dev.bus_id, sizeof(cap->bus_info));
1019 cap->version = AU0828_VERSION_CODE;
1021 /*set the device capabilities */
1022 cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1023 V4L2_CAP_VIDEO_CAPTURE |
1024 V4L2_CAP_AUDIO |
1025 V4L2_CAP_READWRITE |
1026 V4L2_CAP_STREAMING |
1027 V4L2_CAP_TUNER;
1028 return 0;
1031 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1032 struct v4l2_fmtdesc *f)
1034 if(f->index)
1035 return -EINVAL;
1037 memset(f, 0, sizeof(*f));
1038 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1039 strcpy(f->description, "Packed YUV2");
1041 f->flags = 0;
1042 f->pixelformat = V4L2_PIX_FMT_UYVY;
1044 memset(f->reserved, 0, sizeof(f->reserved));
1045 return 0;
1048 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1049 struct v4l2_format *f)
1051 struct au0828_fh *fh = priv;
1052 struct au0828_dev *dev = fh->dev;
1054 f->fmt.pix.width = dev->width;
1055 f->fmt.pix.height = dev->height;
1056 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1057 f->fmt.pix.bytesperline = dev->bytesperline;
1058 f->fmt.pix.sizeimage = dev->frame_size;
1059 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1060 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1061 return 0;
1064 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1065 struct v4l2_format *f)
1067 struct au0828_fh *fh = priv;
1068 struct au0828_dev *dev = fh->dev;
1070 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1073 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1074 struct v4l2_format *f)
1076 struct au0828_fh *fh = priv;
1077 struct au0828_dev *dev = fh->dev;
1078 int rc;
1080 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1081 printk("%s queue busy\n", __func__);
1082 rc = -EBUSY;
1083 goto out;
1086 if (dev->stream_on && !fh->stream_on) {
1087 printk("%s device in use by another fh\n", __func__);
1088 rc = -EBUSY;
1089 goto out;
1092 return au0828_set_format(dev, VIDIOC_S_FMT, f);
1093 out:
1094 return rc;
1097 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1099 struct au0828_fh *fh = priv;
1100 struct au0828_dev *dev = fh->dev;
1102 /* FIXME: when we support something other than NTSC, we are going to
1103 have to make the au0828 bridge adjust the size of its capture
1104 buffer, which is currently hardcoded at 720x480 */
1106 au0828_call_i2c_clients(dev, VIDIOC_S_STD, norm);
1107 return 0;
1110 static int vidioc_enum_input(struct file *file, void *priv,
1111 struct v4l2_input *input)
1113 struct au0828_fh *fh = priv;
1114 struct au0828_dev *dev = fh->dev;
1115 unsigned int tmp;
1117 static const char *inames[] = {
1118 [AU0828_VMUX_COMPOSITE] = "Composite",
1119 [AU0828_VMUX_SVIDEO] = "S-Video",
1120 [AU0828_VMUX_CABLE] = "Cable TV",
1121 [AU0828_VMUX_TELEVISION] = "Television",
1122 [AU0828_VMUX_DVB] = "DVB",
1123 [AU0828_VMUX_DEBUG] = "tv debug"
1126 tmp = input->index;
1128 if(tmp > AU0828_MAX_INPUT)
1129 return -EINVAL;
1130 if(AUVI_INPUT(tmp)->type == 0)
1131 return -EINVAL;
1133 memset(input, 0, sizeof(*input));
1134 input->index = tmp;
1135 strcpy(input->name, inames[AUVI_INPUT(tmp)->type]);
1136 if((AUVI_INPUT(tmp)->type == AU0828_VMUX_TELEVISION) ||
1137 (AUVI_INPUT(tmp)->type == AU0828_VMUX_CABLE))
1138 input->type |= V4L2_INPUT_TYPE_TUNER;
1139 else
1140 input->type |= V4L2_INPUT_TYPE_CAMERA;
1142 input->std = dev->vdev->tvnorms;
1144 return 0;
1147 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1149 struct au0828_fh *fh = priv;
1150 struct au0828_dev *dev = fh->dev;
1151 *i = dev->ctrl_input;
1152 return 0;
1155 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1157 struct au0828_fh *fh = priv;
1158 struct au0828_dev *dev = fh->dev;
1159 int i;
1160 struct v4l2_routing route;
1162 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __FUNCTION__,
1163 index);
1164 if(index >= AU0828_MAX_INPUT)
1165 return -EINVAL;
1166 if(AUVI_INPUT(index)->type == 0)
1167 return -EINVAL;
1168 dev->ctrl_input = index;
1170 switch(AUVI_INPUT(index)->type) {
1171 case AU0828_VMUX_SVIDEO:
1173 dev->input_type = AU0828_VMUX_SVIDEO;
1174 break;
1176 case AU0828_VMUX_COMPOSITE:
1178 dev->input_type = AU0828_VMUX_COMPOSITE;
1179 break;
1181 case AU0828_VMUX_TELEVISION:
1183 dev->input_type = AU0828_VMUX_TELEVISION;
1184 break;
1186 default:
1190 route.input = AUVI_INPUT(index)->vmux;
1191 route.output = 0;
1192 au0828_call_i2c_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1194 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1195 int enable = 0;
1196 if (AUVI_INPUT(i)->audio_setup == NULL) {
1197 continue;
1200 if (i == index)
1201 enable = 1;
1202 else
1203 enable = 0;
1204 if (enable) {
1205 (AUVI_INPUT(i)->audio_setup)(dev, enable);
1206 } else {
1207 /* Make sure we leave it turned on if some
1208 other input is routed to this callback */
1209 if ((AUVI_INPUT(i)->audio_setup) !=
1210 ((AUVI_INPUT(index)->audio_setup))) {
1211 (AUVI_INPUT(i)->audio_setup)(dev, enable);
1216 route.input = AUVI_INPUT(index)->amux;
1217 au0828_call_i2c_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
1218 &route);
1219 return 0;
1222 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1224 struct au0828_fh *fh = priv;
1225 struct au0828_dev *dev = fh->dev;
1226 unsigned int index = a->index;
1228 if(a->index > 1)
1229 return -EINVAL;
1231 memset(a, 0, sizeof(*a));
1232 index = dev->ctrl_ainput;
1233 if(index == 0)
1234 strcpy(a->name, "Television");
1235 else
1236 strcpy(a->name, "Line in");
1238 a->capability = V4L2_AUDCAP_STEREO;
1239 a->index = index;
1240 return 0;
1243 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1245 struct au0828_fh *fh = priv;
1246 struct au0828_dev *dev = fh->dev;
1247 if(a->index != dev->ctrl_ainput)
1248 return -EINVAL;
1249 return 0;
1252 static int vidioc_g_ctrl(struct file *file, void *priv,
1253 struct v4l2_control *ctrl)
1255 struct au0828_fh *fh = priv;
1256 struct au0828_dev *dev = fh->dev;
1258 au0828_call_i2c_clients(dev, VIDIOC_G_CTRL, ctrl);
1259 return 0;
1263 static int vidioc_s_ctrl(struct file *file, void *priv,
1264 struct v4l2_control *ctrl)
1266 struct au0828_fh *fh = priv;
1267 struct au0828_dev *dev = fh->dev;
1268 au0828_call_i2c_clients(dev, VIDIOC_S_CTRL, ctrl);
1269 return 0;
1272 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1274 struct au0828_fh *fh = priv;
1275 struct au0828_dev *dev = fh->dev;
1277 if(t->index != 0)
1278 return -EINVAL;
1280 memset(t, 0, sizeof(*t));
1281 strcpy(t->name, "Auvitek tuner");
1283 au0828_call_i2c_clients(dev, VIDIOC_G_TUNER, t);
1284 return 0;
1287 static int vidioc_s_tuner(struct file *file, void *priv,
1288 struct v4l2_tuner *t)
1290 struct au0828_fh *fh = priv;
1291 struct au0828_dev *dev = fh->dev;
1293 if(t->index != 0)
1294 return -EINVAL;
1296 t->type = V4L2_TUNER_ANALOG_TV;
1297 au0828_call_i2c_clients(dev, VIDIOC_S_TUNER, t);
1298 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1299 t->afc);
1300 return 0;
1304 static int vidioc_g_frequency(struct file *file, void *priv,
1305 struct v4l2_frequency *freq)
1307 struct au0828_fh *fh = priv;
1308 struct au0828_dev *dev = fh->dev;
1309 memset(freq, 0, sizeof(*freq));
1310 freq->type = V4L2_TUNER_ANALOG_TV;
1311 freq->frequency = dev->ctrl_freq;
1312 return 0;
1315 static int vidioc_s_frequency(struct file *file, void *priv,
1316 struct v4l2_frequency *freq)
1318 struct au0828_fh *fh = priv;
1319 struct au0828_dev *dev = fh->dev;
1321 if(freq->tuner != 0)
1322 return -EINVAL;
1323 if(freq->type != V4L2_TUNER_ANALOG_TV)
1324 return -EINVAL;
1326 dev->ctrl_freq = freq->frequency;
1328 au0828_call_i2c_clients(dev, VIDIOC_S_FREQUENCY, freq);
1330 au0828_analog_stream_reset(dev);
1332 return 0;
1335 static int vidioc_g_chip_ident(struct file *file, void *priv,
1336 struct v4l2_dbg_chip_ident *chip)
1338 struct au0828_fh *fh = priv;
1339 struct au0828_dev *dev = fh->dev;
1340 chip->ident = V4L2_IDENT_NONE;
1341 chip->revision = 0;
1343 au0828_call_i2c_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
1344 return 0;
1347 static int vidioc_cropcap(struct file *file, void *priv,
1348 struct v4l2_cropcap *cc)
1350 struct au0828_fh *fh = priv;
1351 struct au0828_dev *dev = fh->dev;
1353 if(cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1354 return -EINVAL;
1356 cc->bounds.left = 0;
1357 cc->bounds.top = 0;
1358 cc->bounds.width = dev->width;
1359 cc->bounds.height = dev->height;
1361 cc->defrect = cc->bounds;
1363 cc->pixelaspect.numerator = 54;
1364 cc->pixelaspect.denominator = 59;
1366 return 0;
1369 static int vidioc_streamon(struct file *file, void *priv,
1370 enum v4l2_buf_type type)
1372 struct au0828_fh *fh = priv;
1373 struct au0828_dev *dev = fh->dev;
1374 int b = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1375 int rc;
1377 rc = check_dev(dev);
1378 if (rc < 0)
1379 return rc;
1381 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1382 au0828_analog_stream_enable(dev);
1383 au0828_call_i2c_clients(dev, VIDIOC_STREAMON, &b);
1386 mutex_lock(&dev->lock);
1387 rc = res_get(fh);
1389 if (likely(rc >= 0))
1390 rc = videobuf_streamon(&fh->vb_vidq);
1391 mutex_unlock(&dev->lock);
1393 return rc;
1396 static int vidioc_streamoff(struct file *file, void *priv,
1397 enum v4l2_buf_type type)
1399 struct au0828_fh *fh = priv;
1400 struct au0828_dev *dev = fh->dev;
1401 int b = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1402 int i;
1403 int ret;
1404 int rc;
1406 rc = check_dev(dev);
1407 if (rc < 0)
1408 return rc;
1410 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1411 return -EINVAL;
1412 if (type != fh->type)
1413 return -EINVAL;
1415 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1416 au0828_call_i2c_clients(dev, VIDIOC_STREAMOFF, &b);
1417 if((ret = au0828_stream_interrupt(dev)) != 0)
1418 return ret;
1421 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1422 if (AUVI_INPUT(i)->audio_setup == NULL) {
1423 continue;
1425 (AUVI_INPUT(i)->audio_setup)(dev, 0);
1428 mutex_lock(&dev->lock);
1429 videobuf_streamoff(&fh->vb_vidq);
1430 res_free(fh);
1431 mutex_unlock(&dev->lock);
1433 return 0;
1436 static int vidioc_g_register(struct file *file, void *priv,
1437 struct v4l2_dbg_register *reg)
1439 struct au0828_fh *fh = priv;
1440 struct au0828_dev *dev = fh->dev;
1442 switch (reg->match.type) {
1443 case V4L2_CHIP_MATCH_I2C_DRIVER:
1444 au0828_call_i2c_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
1445 return 0;
1446 default:
1447 return -EINVAL;
1451 static int vidioc_s_register(struct file *file, void *priv,
1452 struct v4l2_dbg_register *reg)
1454 struct au0828_fh *fh = priv;
1455 struct au0828_dev *dev = fh->dev;
1457 switch (reg->match.type) {
1458 case V4L2_CHIP_MATCH_I2C_DRIVER:
1459 au0828_call_i2c_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
1460 return 0;
1461 default:
1462 return -EINVAL;
1464 return 0;
1467 static int vidioc_reqbufs(struct file *file, void *priv,
1468 struct v4l2_requestbuffers *rb)
1470 struct au0828_fh *fh = priv;
1471 struct au0828_dev *dev = fh->dev;
1472 int rc;
1474 rc = check_dev(dev);
1475 if (rc < 0)
1476 return rc;
1478 return videobuf_reqbufs(&fh->vb_vidq, rb);
1481 static int vidioc_querybuf(struct file *file, void *priv,
1482 struct v4l2_buffer *b)
1484 struct au0828_fh *fh = priv;
1485 struct au0828_dev *dev = fh->dev;
1486 int rc;
1488 rc = check_dev(dev);
1489 if (rc < 0)
1490 return rc;
1492 return videobuf_querybuf(&fh->vb_vidq, b);
1495 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1497 struct au0828_fh *fh = priv;
1498 struct au0828_dev *dev = fh->dev;
1499 int rc;
1501 rc = check_dev(dev);
1502 if (rc < 0)
1503 return rc;
1505 return videobuf_qbuf(&fh->vb_vidq, b);
1508 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1510 struct au0828_fh *fh = priv;
1511 struct au0828_dev *dev = fh->dev;
1512 int rc;
1514 rc = check_dev(dev);
1515 if (rc < 0)
1516 return rc;
1518 /* Workaround for a bug in the au0828 hardware design that sometimes
1519 results in the colorspace being inverted */
1520 if (dev->greenscreen_detected == 1) {
1521 dprintk(1, "Detected green frame. Resetting stream...\n");
1522 au0828_analog_stream_reset(dev);
1523 dev->greenscreen_detected = 0;
1526 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1529 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1530 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1532 struct au0828_fh *fh = priv;
1534 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1536 #endif
1538 static struct v4l2_file_operations au0828_v4l_fops = {
1539 .owner = THIS_MODULE,
1540 .open = au0828_v4l2_open,
1541 .release = au0828_v4l2_close,
1542 .read = au0828_v4l2_read,
1543 .poll = au0828_v4l2_poll,
1544 .mmap = au0828_v4l2_mmap,
1545 .ioctl = video_ioctl2,
1548 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1549 .vidioc_querycap = vidioc_querycap,
1550 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1551 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1552 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1553 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1554 .vidioc_g_audio = vidioc_g_audio,
1555 .vidioc_s_audio = vidioc_s_audio,
1556 .vidioc_cropcap = vidioc_cropcap,
1557 #ifdef AAA
1558 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1559 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1560 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1561 #endif
1562 .vidioc_reqbufs = vidioc_reqbufs,
1563 .vidioc_querybuf = vidioc_querybuf,
1564 .vidioc_qbuf = vidioc_qbuf,
1565 .vidioc_dqbuf = vidioc_dqbuf,
1566 .vidioc_s_std = vidioc_s_std,
1567 .vidioc_enum_input = vidioc_enum_input,
1568 .vidioc_g_input = vidioc_g_input,
1569 .vidioc_s_input = vidioc_s_input,
1570 .vidioc_queryctrl = vidioc_queryctrl,
1571 .vidioc_g_ctrl = vidioc_g_ctrl,
1572 .vidioc_s_ctrl = vidioc_s_ctrl,
1573 .vidioc_streamon = vidioc_streamon,
1574 .vidioc_streamoff = vidioc_streamoff,
1575 .vidioc_g_tuner = vidioc_g_tuner,
1576 .vidioc_s_tuner = vidioc_s_tuner,
1577 .vidioc_g_frequency = vidioc_g_frequency,
1578 .vidioc_s_frequency = vidioc_s_frequency,
1579 #ifdef CONFIG_VIDEO_ADV_DEBUG
1580 .vidioc_g_register = vidioc_g_register,
1581 .vidioc_s_register = vidioc_s_register,
1582 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1583 #endif
1584 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1585 .vidiocgmbuf = vidiocgmbuf,
1586 #endif
1589 static const struct video_device au0828_video_template = {
1590 .fops = &au0828_v4l_fops,
1591 .release = video_device_release,
1592 .ioctl_ops = &video_ioctl_ops,
1593 .minor = -1,
1594 .tvnorms = V4L2_STD_NTSC,
1595 .current_norm = V4L2_STD_NTSC,
1598 /**************************************************************************/
1600 int au0828_analog_register(struct au0828_dev *dev)
1602 int retval = -ENOMEM;
1604 dprintk(1, "au0828_analog_register called!\n");
1606 /* Load the analog demodulator driver (note this would need to be
1607 abstracted out if we ever need to support a different demod) */
1608 request_module("au8522");
1610 /* Load the tuner module, which results in i2c enumeration and
1611 attachment of whatever tuner is on the bus */
1612 request_module("tuner");
1614 init_waitqueue_head(&dev->open);
1615 spin_lock_init(&dev->slock);
1616 mutex_init(&dev->lock);
1618 INIT_LIST_HEAD(&dev->vidq.active);
1619 INIT_LIST_HEAD(&dev->vidq.queued);
1621 dev->width = NTSC_STD_W;
1622 dev->height = NTSC_STD_H;
1623 dev->field_size = dev->width * dev->height;
1624 dev->frame_size = dev->field_size << 1;
1625 dev->bytesperline = dev->width << 1;
1626 dev->ctrl_ainput = 0;
1628 /* allocate and fill v4l2 video struct */
1629 dev->vdev = video_device_alloc();
1630 if(NULL == dev->vdev) {
1631 dprintk(1, "Can't allocate video_device.\n");
1632 return -ENOMEM;
1635 dev->vbi_dev = video_device_alloc();
1636 if(NULL == dev->vbi_dev) {
1637 dprintk(1, "Can't allocate vbi_device.\n");
1638 kfree(dev->vdev);
1639 return -ENOMEM;
1642 /* Fill the video capture device struct */
1643 *dev->vdev = au0828_video_template;
1644 dev->vdev->vfl_type = VID_TYPE_CAPTURE | VID_TYPE_TUNER;
1645 dev->vdev->parent = &dev->usbdev->dev;
1646 strcpy(dev->vdev->name, "au0828a video");
1648 /* Setup the VBI device */
1649 *dev->vbi_dev = au0828_video_template;
1650 dev->vbi_dev->vfl_type = VFL_TYPE_VBI;
1651 dev->vbi_dev->parent = &dev->usbdev->dev;
1652 strcpy(dev->vbi_dev->name, "au0828a vbi");
1654 list_add_tail(&dev->au0828list, &au0828_devlist);
1656 /* Register the v4l2 device */
1657 if((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1)) != 0) {
1658 dprintk(1, "unable to register video device (error = %d).\n", retval);
1659 list_del(&dev->au0828list);
1660 video_device_release(dev->vdev);
1661 return -ENODEV;
1664 /* Register the vbi device */
1665 if((retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1)) != 0) {
1666 dprintk(1, "unable to register vbi device (error = %d).\n", retval);
1667 list_del(&dev->au0828list);
1668 video_device_release(dev->vbi_dev);
1669 video_device_release(dev->vdev);
1670 return -ENODEV;
1673 dprintk(1, "%s completed!\n", __FUNCTION__);
1675 return 0;