Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / au0828 / au0828-video.c
blobdc67bc40f36fb60e4d21973f966a2fab39b64522
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 <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/tuner.h>
40 #include "au0828.h"
41 #include "au0828-reg.h"
43 static DEFINE_MUTEX(au0828_sysfs_lock);
45 #define AU0828_VERSION_CODE KERNEL_VERSION(0, 0, 1)
47 /* ------------------------------------------------------------------
48 Videobuf operations
49 ------------------------------------------------------------------*/
51 static unsigned int isoc_debug;
52 module_param(isoc_debug, int, 0644);
53 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
55 #define au0828_isocdbg(fmt, arg...) \
56 do {\
57 if (isoc_debug) { \
58 printk(KERN_INFO "au0828 %s :"fmt, \
59 __func__ , ##arg); \
60 } \
61 } while (0)
63 static inline void print_err_status(struct au0828_dev *dev,
64 int packet, int status)
66 char *errmsg = "Unknown";
68 switch (status) {
69 case -ENOENT:
70 errmsg = "unlinked synchronuously";
71 break;
72 case -ECONNRESET:
73 errmsg = "unlinked asynchronuously";
74 break;
75 case -ENOSR:
76 errmsg = "Buffer error (overrun)";
77 break;
78 case -EPIPE:
79 errmsg = "Stalled (device not responding)";
80 break;
81 case -EOVERFLOW:
82 errmsg = "Babble (bad cable?)";
83 break;
84 case -EPROTO:
85 errmsg = "Bit-stuff error (bad cable?)";
86 break;
87 case -EILSEQ:
88 errmsg = "CRC/Timeout (could be anything)";
89 break;
90 case -ETIME:
91 errmsg = "Device does not respond";
92 break;
94 if (packet < 0) {
95 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
96 } else {
97 au0828_isocdbg("URB packet %d, status %d [%s].\n",
98 packet, status, errmsg);
102 static int check_dev(struct au0828_dev *dev)
104 if (dev->dev_state & DEV_DISCONNECTED) {
105 printk(KERN_INFO "v4l2 ioctl: device not present\n");
106 return -ENODEV;
109 if (dev->dev_state & DEV_MISCONFIGURED) {
110 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
111 "close and open it again\n");
112 return -EIO;
114 return 0;
118 * IRQ callback, called by URB callback
120 static void au0828_irq_callback(struct urb *urb)
122 struct au0828_dmaqueue *dma_q = urb->context;
123 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
124 int rc, i;
126 switch (urb->status) {
127 case 0: /* success */
128 case -ETIMEDOUT: /* NAK */
129 break;
130 case -ECONNRESET: /* kill */
131 case -ENOENT:
132 case -ESHUTDOWN:
133 au0828_isocdbg("au0828_irq_callback called: status kill\n");
134 return;
135 default: /* unknown error */
136 au0828_isocdbg("urb completition error %d.\n", urb->status);
137 break;
140 /* Copy data from URB */
141 spin_lock(&dev->slock);
142 rc = dev->isoc_ctl.isoc_copy(dev, urb);
143 spin_unlock(&dev->slock);
145 /* Reset urb buffers */
146 for (i = 0; i < urb->number_of_packets; i++) {
147 urb->iso_frame_desc[i].status = 0;
148 urb->iso_frame_desc[i].actual_length = 0;
150 urb->status = 0;
152 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
153 if (urb->status) {
154 au0828_isocdbg("urb resubmit failed (error=%i)\n",
155 urb->status);
160 * Stop and Deallocate URBs
162 void au0828_uninit_isoc(struct au0828_dev *dev)
164 struct urb *urb;
165 int i;
167 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
169 dev->isoc_ctl.nfields = -1;
170 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
171 urb = dev->isoc_ctl.urb[i];
172 if (urb) {
173 if (!irqs_disabled())
174 usb_kill_urb(urb);
175 else
176 usb_unlink_urb(urb);
178 if (dev->isoc_ctl.transfer_buffer[i]) {
179 usb_buffer_free(dev->usbdev,
180 urb->transfer_buffer_length,
181 dev->isoc_ctl.transfer_buffer[i],
182 urb->transfer_dma);
184 usb_free_urb(urb);
185 dev->isoc_ctl.urb[i] = NULL;
187 dev->isoc_ctl.transfer_buffer[i] = NULL;
190 kfree(dev->isoc_ctl.urb);
191 kfree(dev->isoc_ctl.transfer_buffer);
193 dev->isoc_ctl.urb = NULL;
194 dev->isoc_ctl.transfer_buffer = NULL;
195 dev->isoc_ctl.num_bufs = 0;
199 * Allocate URBs and start IRQ
201 int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
202 int num_bufs, int max_pkt_size,
203 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
205 struct au0828_dmaqueue *dma_q = &dev->vidq;
206 int i;
207 int sb_size, pipe;
208 struct urb *urb;
209 int j, k;
210 int rc;
212 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
214 /* De-allocates all pending stuff */
215 au0828_uninit_isoc(dev);
217 dev->isoc_ctl.isoc_copy = isoc_copy;
218 dev->isoc_ctl.num_bufs = num_bufs;
220 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
221 if (!dev->isoc_ctl.urb) {
222 au0828_isocdbg("cannot alloc memory for usb buffers\n");
223 return -ENOMEM;
226 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
227 GFP_KERNEL);
228 if (!dev->isoc_ctl.transfer_buffer) {
229 au0828_isocdbg("cannot allocate memory for usb transfer\n");
230 kfree(dev->isoc_ctl.urb);
231 return -ENOMEM;
234 dev->isoc_ctl.max_pkt_size = max_pkt_size;
235 dev->isoc_ctl.buf = NULL;
237 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
239 /* allocate urbs and transfer buffers */
240 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
241 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
242 if (!urb) {
243 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
244 au0828_uninit_isoc(dev);
245 return -ENOMEM;
247 dev->isoc_ctl.urb[i] = urb;
249 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->usbdev,
250 sb_size, GFP_KERNEL, &urb->transfer_dma);
251 if (!dev->isoc_ctl.transfer_buffer[i]) {
252 printk("unable to allocate %i bytes for transfer"
253 " buffer %i%s\n",
254 sb_size, i,
255 in_interrupt() ? " while in int" : "");
256 au0828_uninit_isoc(dev);
257 return -ENOMEM;
259 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
261 pipe = usb_rcvisocpipe(dev->usbdev,
262 dev->isoc_in_endpointaddr),
264 usb_fill_int_urb(urb, dev->usbdev, pipe,
265 dev->isoc_ctl.transfer_buffer[i], sb_size,
266 au0828_irq_callback, dma_q, 1);
268 urb->number_of_packets = max_packets;
269 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
271 k = 0;
272 for (j = 0; j < max_packets; j++) {
273 urb->iso_frame_desc[j].offset = k;
274 urb->iso_frame_desc[j].length =
275 dev->isoc_ctl.max_pkt_size;
276 k += dev->isoc_ctl.max_pkt_size;
280 init_waitqueue_head(&dma_q->wq);
282 /* submit urbs and enables IRQ */
283 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
284 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
285 if (rc) {
286 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
287 i, rc);
288 au0828_uninit_isoc(dev);
289 return rc;
293 return 0;
297 * Announces that a buffer were filled and request the next
299 static inline void buffer_filled(struct au0828_dev *dev,
300 struct au0828_dmaqueue *dma_q,
301 struct au0828_buffer *buf)
303 /* Advice that buffer was filled */
304 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
306 buf->vb.state = VIDEOBUF_DONE;
307 buf->vb.field_count++;
308 do_gettimeofday(&buf->vb.ts);
310 dev->isoc_ctl.buf = NULL;
312 list_del(&buf->vb.queue);
313 wake_up(&buf->vb.done);
317 * Identify the buffer header type and properly handles
319 static void au0828_copy_video(struct au0828_dev *dev,
320 struct au0828_dmaqueue *dma_q,
321 struct au0828_buffer *buf,
322 unsigned char *p,
323 unsigned char *outp, unsigned long len)
325 void *fieldstart, *startwrite, *startread;
326 int linesdone, currlinedone, offset, lencopy, remain;
327 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
329 if (dma_q->pos + len > buf->vb.size)
330 len = buf->vb.size - dma_q->pos;
332 startread = p;
333 remain = len;
335 /* Interlaces frame */
336 if (buf->top_field)
337 fieldstart = outp;
338 else
339 fieldstart = outp + bytesperline;
341 linesdone = dma_q->pos / bytesperline;
342 currlinedone = dma_q->pos % bytesperline;
343 offset = linesdone * bytesperline * 2 + currlinedone;
344 startwrite = fieldstart + offset;
345 lencopy = bytesperline - currlinedone;
346 lencopy = lencopy > remain ? remain : lencopy;
348 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
349 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
350 ((char *)startwrite + lencopy) -
351 ((char *)outp + buf->vb.size));
352 remain = (char *)outp + buf->vb.size - (char *)startwrite;
353 lencopy = remain;
355 if (lencopy <= 0)
356 return;
357 memcpy(startwrite, startread, lencopy);
359 remain -= lencopy;
361 while (remain > 0) {
362 startwrite += lencopy + bytesperline;
363 startread += lencopy;
364 if (bytesperline > remain)
365 lencopy = remain;
366 else
367 lencopy = bytesperline;
369 if ((char *)startwrite + lencopy > (char *)outp +
370 buf->vb.size) {
371 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
372 ((char *)startwrite + lencopy) -
373 ((char *)outp + buf->vb.size));
374 lencopy = remain = (char *)outp + buf->vb.size -
375 (char *)startwrite;
377 if (lencopy <= 0)
378 break;
380 memcpy(startwrite, startread, lencopy);
382 remain -= lencopy;
385 if (offset > 1440) {
386 /* We have enough data to check for greenscreen */
387 if (outp[0] < 0x60 && outp[1440] < 0x60)
388 dev->greenscreen_detected = 1;
391 dma_q->pos += len;
395 * video-buf generic routine to get the next available buffer
397 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
398 struct au0828_buffer **buf)
400 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
402 if (list_empty(&dma_q->active)) {
403 au0828_isocdbg("No active queue to serve\n");
404 dev->isoc_ctl.buf = NULL;
405 *buf = NULL;
406 return;
409 /* Get the next buffer */
410 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
411 dev->isoc_ctl.buf = *buf;
413 return;
417 * Controls the isoc copy of each urb packet
419 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
421 struct au0828_buffer *buf;
422 struct au0828_dmaqueue *dma_q = urb->context;
423 unsigned char *outp = NULL;
424 int i, len = 0, rc = 1;
425 unsigned char *p;
426 unsigned char fbyte;
428 if (!dev)
429 return 0;
431 if ((dev->dev_state & DEV_DISCONNECTED) ||
432 (dev->dev_state & DEV_MISCONFIGURED))
433 return 0;
435 if (urb->status < 0) {
436 print_err_status(dev, -1, urb->status);
437 if (urb->status == -ENOENT)
438 return 0;
441 buf = dev->isoc_ctl.buf;
442 if (buf != NULL)
443 outp = videobuf_to_vmalloc(&buf->vb);
445 for (i = 0; i < urb->number_of_packets; i++) {
446 int status = urb->iso_frame_desc[i].status;
448 if (status < 0) {
449 print_err_status(dev, i, status);
450 if (urb->iso_frame_desc[i].status != -EPROTO)
451 continue;
454 if (urb->iso_frame_desc[i].actual_length <= 0)
455 continue;
457 if (urb->iso_frame_desc[i].actual_length >
458 dev->max_pkt_size) {
459 au0828_isocdbg("packet bigger than packet size");
460 continue;
463 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
464 fbyte = p[0];
465 len = urb->iso_frame_desc[i].actual_length - 4;
466 p += 4;
468 if (fbyte & 0x80) {
469 len -= 4;
470 p += 4;
471 au0828_isocdbg("Video frame %s\n",
472 (fbyte & 0x40) ? "odd" : "even");
473 if (!(fbyte & 0x40)) {
474 if (buf != NULL)
475 buffer_filled(dev, dma_q, buf);
476 get_next_buf(dma_q, &buf);
477 if (buf == NULL)
478 outp = NULL;
479 else
480 outp = videobuf_to_vmalloc(&buf->vb);
483 if (buf != NULL) {
484 if (fbyte & 0x40)
485 buf->top_field = 1;
486 else
487 buf->top_field = 0;
490 dma_q->pos = 0;
492 if (buf != NULL)
493 au0828_copy_video(dev, dma_q, buf, p, outp, len);
495 return rc;
498 static int
499 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
500 unsigned int *size)
502 struct au0828_fh *fh = vq->priv_data;
503 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
505 if (0 == *count)
506 *count = AU0828_DEF_BUF;
508 if (*count < AU0828_MIN_BUF)
509 *count = AU0828_MIN_BUF;
510 return 0;
513 /* This is called *without* dev->slock held; please keep it that way */
514 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
516 struct au0828_fh *fh = vq->priv_data;
517 struct au0828_dev *dev = fh->dev;
518 unsigned long flags = 0;
519 if (in_interrupt())
520 BUG();
522 /* We used to wait for the buffer to finish here, but this didn't work
523 because, as we were keeping the state as VIDEOBUF_QUEUED,
524 videobuf_queue_cancel marked it as finished for us.
525 (Also, it could wedge forever if the hardware was misconfigured.)
527 This should be safe; by the time we get here, the buffer isn't
528 queued anymore. If we ever start marking the buffers as
529 VIDEOBUF_ACTIVE, it won't be, though.
531 spin_lock_irqsave(&dev->slock, flags);
532 if (dev->isoc_ctl.buf == buf)
533 dev->isoc_ctl.buf = NULL;
534 spin_unlock_irqrestore(&dev->slock, flags);
536 videobuf_vmalloc_free(&buf->vb);
537 buf->vb.state = VIDEOBUF_NEEDS_INIT;
540 static int
541 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
542 enum v4l2_field field)
544 struct au0828_fh *fh = vq->priv_data;
545 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
546 struct au0828_dev *dev = fh->dev;
547 int rc = 0, urb_init = 0;
549 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
551 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
552 return -EINVAL;
554 buf->vb.width = dev->width;
555 buf->vb.height = dev->height;
556 buf->vb.field = field;
558 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
559 rc = videobuf_iolock(vq, &buf->vb, NULL);
560 if (rc < 0) {
561 printk(KERN_INFO "videobuf_iolock failed\n");
562 goto fail;
566 if (!dev->isoc_ctl.num_bufs)
567 urb_init = 1;
569 if (urb_init) {
570 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
571 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
572 au0828_isoc_copy);
573 if (rc < 0) {
574 printk(KERN_INFO "au0828_init_isoc failed\n");
575 goto fail;
579 buf->vb.state = VIDEOBUF_PREPARED;
580 return 0;
582 fail:
583 free_buffer(vq, buf);
584 return rc;
587 static void
588 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
590 struct au0828_buffer *buf = container_of(vb,
591 struct au0828_buffer,
592 vb);
593 struct au0828_fh *fh = vq->priv_data;
594 struct au0828_dev *dev = fh->dev;
595 struct au0828_dmaqueue *vidq = &dev->vidq;
597 buf->vb.state = VIDEOBUF_QUEUED;
598 list_add_tail(&buf->vb.queue, &vidq->active);
601 static void buffer_release(struct videobuf_queue *vq,
602 struct videobuf_buffer *vb)
604 struct au0828_buffer *buf = container_of(vb,
605 struct au0828_buffer,
606 vb);
608 free_buffer(vq, buf);
611 static struct videobuf_queue_ops au0828_video_qops = {
612 .buf_setup = buffer_setup,
613 .buf_prepare = buffer_prepare,
614 .buf_queue = buffer_queue,
615 .buf_release = buffer_release,
618 /* ------------------------------------------------------------------
619 V4L2 interface
620 ------------------------------------------------------------------*/
622 static int au0828_i2s_init(struct au0828_dev *dev)
624 /* Enable i2s mode */
625 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
626 return 0;
630 * Auvitek au0828 analog stream enable
631 * Please set interface0 to AS5 before enable the stream
633 int au0828_analog_stream_enable(struct au0828_dev *d)
635 dprintk(1, "au0828_analog_stream_enable called\n");
636 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
637 au0828_writereg(d, 0x106, 0x00);
638 /* set x position */
639 au0828_writereg(d, 0x110, 0x00);
640 au0828_writereg(d, 0x111, 0x00);
641 au0828_writereg(d, 0x114, 0xa0);
642 au0828_writereg(d, 0x115, 0x05);
643 /* set y position */
644 au0828_writereg(d, 0x112, 0x02);
645 au0828_writereg(d, 0x113, 0x00);
646 au0828_writereg(d, 0x116, 0xf2);
647 au0828_writereg(d, 0x117, 0x00);
648 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
650 return 0;
653 int au0828_analog_stream_disable(struct au0828_dev *d)
655 dprintk(1, "au0828_analog_stream_disable called\n");
656 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
657 return 0;
660 void au0828_analog_stream_reset(struct au0828_dev *dev)
662 dprintk(1, "au0828_analog_stream_reset called\n");
663 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
664 mdelay(30);
665 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
669 * Some operations needs to stop current streaming
671 static int au0828_stream_interrupt(struct au0828_dev *dev)
673 int ret = 0;
675 dev->stream_state = STREAM_INTERRUPT;
676 if (dev->dev_state == DEV_DISCONNECTED)
677 return -ENODEV;
678 else if (ret) {
679 dev->dev_state = DEV_MISCONFIGURED;
680 dprintk(1, "%s device is misconfigured!\n", __func__);
681 return ret;
683 return 0;
687 * au0828_release_resources
688 * unregister v4l2 devices
690 void au0828_analog_unregister(struct au0828_dev *dev)
692 dprintk(1, "au0828_release_resources called\n");
693 mutex_lock(&au0828_sysfs_lock);
695 if (dev->vdev)
696 video_unregister_device(dev->vdev);
697 if (dev->vbi_dev)
698 video_unregister_device(dev->vbi_dev);
700 mutex_unlock(&au0828_sysfs_lock);
704 /* Usage lock check functions */
705 static int res_get(struct au0828_fh *fh)
707 struct au0828_dev *dev = fh->dev;
708 int rc = 0;
710 /* This instance already has stream_on */
711 if (fh->stream_on)
712 return rc;
714 if (dev->stream_on)
715 return -EBUSY;
717 dev->stream_on = 1;
718 fh->stream_on = 1;
719 return rc;
722 static int res_check(struct au0828_fh *fh)
724 return fh->stream_on;
727 static void res_free(struct au0828_fh *fh)
729 struct au0828_dev *dev = fh->dev;
731 fh->stream_on = 0;
732 dev->stream_on = 0;
735 static int au0828_v4l2_open(struct file *filp)
737 int ret = 0;
738 struct au0828_dev *dev = video_drvdata(filp);
739 struct au0828_fh *fh;
740 int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
742 #ifdef VBI_IS_WORKING
743 if (video_devdata(filp)->vfl_type == VFL_TYPE_GRABBER)
744 type = V4L2_BUF_TYPE_VBI_CAPTURE;
745 #endif
747 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
748 if (NULL == fh) {
749 dprintk(1, "Failed allocate au0828_fh struct!\n");
750 return -ENOMEM;
753 fh->type = type;
754 fh->dev = dev;
755 filp->private_data = fh;
757 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
758 /* set au0828 interface0 to AS5 here again */
759 ret = usb_set_interface(dev->usbdev, 0, 5);
760 if (ret < 0) {
761 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
762 return -EBUSY;
764 dev->width = NTSC_STD_W;
765 dev->height = NTSC_STD_H;
766 dev->frame_size = dev->width * dev->height * 2;
767 dev->field_size = dev->width * dev->height;
768 dev->bytesperline = dev->width * 2;
770 au0828_analog_stream_enable(dev);
771 au0828_analog_stream_reset(dev);
773 /* If we were doing ac97 instead of i2s, it would go here...*/
774 au0828_i2s_init(dev);
776 dev->stream_state = STREAM_OFF;
777 dev->dev_state |= DEV_INITIALIZED;
780 dev->users++;
782 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
783 NULL, &dev->slock, fh->type,
784 V4L2_FIELD_INTERLACED,
785 sizeof(struct au0828_buffer), fh);
787 return ret;
790 static int au0828_v4l2_close(struct file *filp)
792 int ret;
793 struct au0828_fh *fh = filp->private_data;
794 struct au0828_dev *dev = fh->dev;
796 mutex_lock(&dev->lock);
797 if (res_check(fh))
798 res_free(fh);
800 if (dev->users == 1) {
801 videobuf_stop(&fh->vb_vidq);
802 videobuf_mmap_free(&fh->vb_vidq);
804 if (dev->dev_state & DEV_DISCONNECTED) {
805 au0828_analog_unregister(dev);
806 mutex_unlock(&dev->lock);
807 kfree(dev);
808 return 0;
811 au0828_analog_stream_disable(dev);
813 au0828_uninit_isoc(dev);
815 /* Save some power by putting tuner to sleep */
816 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
818 /* When close the device, set the usb intf0 into alt0 to free
819 USB bandwidth */
820 ret = usb_set_interface(dev->usbdev, 0, 0);
821 if (ret < 0)
822 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
825 kfree(fh);
826 dev->users--;
827 wake_up_interruptible_nr(&dev->open, 1);
828 mutex_unlock(&dev->lock);
829 return 0;
832 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
833 size_t count, loff_t *pos)
835 struct au0828_fh *fh = filp->private_data;
836 struct au0828_dev *dev = fh->dev;
837 int rc;
839 rc = check_dev(dev);
840 if (rc < 0)
841 return rc;
843 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
844 mutex_lock(&dev->lock);
845 rc = res_get(fh);
846 mutex_unlock(&dev->lock);
848 if (unlikely(rc < 0))
849 return rc;
851 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
852 filp->f_flags & O_NONBLOCK);
854 return 0;
857 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
859 struct au0828_fh *fh = filp->private_data;
860 struct au0828_dev *dev = fh->dev;
861 int rc;
863 rc = check_dev(dev);
864 if (rc < 0)
865 return rc;
867 mutex_lock(&dev->lock);
868 rc = res_get(fh);
869 mutex_unlock(&dev->lock);
871 if (unlikely(rc < 0))
872 return POLLERR;
874 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
875 return POLLERR;
877 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
880 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
882 struct au0828_fh *fh = filp->private_data;
883 struct au0828_dev *dev = fh->dev;
884 int rc;
886 rc = check_dev(dev);
887 if (rc < 0)
888 return rc;
890 mutex_lock(&dev->lock);
891 rc = res_get(fh);
892 mutex_unlock(&dev->lock);
894 if (unlikely(rc < 0))
895 return rc;
897 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
899 return rc;
902 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
903 struct v4l2_format *format)
905 int ret;
906 int width = format->fmt.pix.width;
907 int height = format->fmt.pix.height;
908 unsigned int maxwidth, maxheight;
910 maxwidth = 720;
911 maxheight = 480;
913 #ifdef VBI_IS_WORKING
914 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
915 dprintk(1, "VBI format set: to be supported!\n");
916 return 0;
918 if (format->type == V4L2_BUF_TYPE_VBI_CAPTURE)
919 return 0;
920 #endif
921 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
922 return -EINVAL;
924 /* If they are demanding a format other than the one we support,
925 bail out (tvtime asks for UYVY and then retries with YUYV) */
926 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
927 return -EINVAL;
929 /* format->fmt.pix.width only support 720 and height 480 */
930 if (width != 720)
931 width = 720;
932 if (height != 480)
933 height = 480;
935 format->fmt.pix.width = width;
936 format->fmt.pix.height = height;
937 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
938 format->fmt.pix.bytesperline = width * 2;
939 format->fmt.pix.sizeimage = width * height * 2;
940 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
941 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
943 if (cmd == VIDIOC_TRY_FMT)
944 return 0;
946 /* maybe set new image format, driver current only support 720*480 */
947 dev->width = width;
948 dev->height = height;
949 dev->frame_size = width * height * 2;
950 dev->field_size = width * height;
951 dev->bytesperline = width * 2;
953 if (dev->stream_state == STREAM_ON) {
954 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
955 ret = au0828_stream_interrupt(dev);
956 if (ret != 0) {
957 dprintk(1, "error interrupting video stream!\n");
958 return ret;
962 /* set au0828 interface0 to AS5 here again */
963 ret = usb_set_interface(dev->usbdev, 0, 5);
964 if (ret < 0) {
965 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
966 return -EBUSY;
969 au0828_analog_stream_enable(dev);
971 return 0;
975 static int vidioc_queryctrl(struct file *file, void *priv,
976 struct v4l2_queryctrl *qc)
978 struct au0828_fh *fh = priv;
979 struct au0828_dev *dev = fh->dev;
980 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
981 if (qc->type)
982 return 0;
983 else
984 return -EINVAL;
987 static int vidioc_querycap(struct file *file, void *priv,
988 struct v4l2_capability *cap)
990 struct au0828_fh *fh = priv;
991 struct au0828_dev *dev = fh->dev;
993 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
994 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
995 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
997 cap->version = AU0828_VERSION_CODE;
999 /*set the device capabilities */
1000 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1001 #ifdef VBI_IS_WORKING
1002 V4L2_CAP_VBI_CAPTURE |
1003 #endif
1004 V4L2_CAP_AUDIO |
1005 V4L2_CAP_READWRITE |
1006 V4L2_CAP_STREAMING |
1007 V4L2_CAP_TUNER;
1008 return 0;
1011 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1012 struct v4l2_fmtdesc *f)
1014 if (f->index)
1015 return -EINVAL;
1017 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1018 strcpy(f->description, "Packed YUV2");
1020 f->flags = 0;
1021 f->pixelformat = V4L2_PIX_FMT_UYVY;
1023 return 0;
1026 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1027 struct v4l2_format *f)
1029 struct au0828_fh *fh = priv;
1030 struct au0828_dev *dev = fh->dev;
1032 f->fmt.pix.width = dev->width;
1033 f->fmt.pix.height = dev->height;
1034 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1035 f->fmt.pix.bytesperline = dev->bytesperline;
1036 f->fmt.pix.sizeimage = dev->frame_size;
1037 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1038 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1039 return 0;
1042 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1043 struct v4l2_format *f)
1045 struct au0828_fh *fh = priv;
1046 struct au0828_dev *dev = fh->dev;
1048 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1051 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1052 struct v4l2_format *f)
1054 struct au0828_fh *fh = priv;
1055 struct au0828_dev *dev = fh->dev;
1056 int rc;
1058 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1059 printk(KERN_INFO "%s queue busy\n", __func__);
1060 rc = -EBUSY;
1061 goto out;
1064 if (dev->stream_on && !fh->stream_on) {
1065 printk(KERN_INFO "%s device in use by another fh\n", __func__);
1066 rc = -EBUSY;
1067 goto out;
1070 return au0828_set_format(dev, VIDIOC_S_FMT, f);
1071 out:
1072 return rc;
1075 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1077 struct au0828_fh *fh = priv;
1078 struct au0828_dev *dev = fh->dev;
1080 /* FIXME: when we support something other than NTSC, we are going to
1081 have to make the au0828 bridge adjust the size of its capture
1082 buffer, which is currently hardcoded at 720x480 */
1084 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, *norm);
1085 return 0;
1088 static int vidioc_enum_input(struct file *file, void *priv,
1089 struct v4l2_input *input)
1091 struct au0828_fh *fh = priv;
1092 struct au0828_dev *dev = fh->dev;
1093 unsigned int tmp;
1095 static const char *inames[] = {
1096 [AU0828_VMUX_UNDEFINED] = "Undefined",
1097 [AU0828_VMUX_COMPOSITE] = "Composite",
1098 [AU0828_VMUX_SVIDEO] = "S-Video",
1099 [AU0828_VMUX_CABLE] = "Cable TV",
1100 [AU0828_VMUX_TELEVISION] = "Television",
1101 [AU0828_VMUX_DVB] = "DVB",
1102 [AU0828_VMUX_DEBUG] = "tv debug"
1105 tmp = input->index;
1107 if (tmp > AU0828_MAX_INPUT)
1108 return -EINVAL;
1109 if (AUVI_INPUT(tmp).type == 0)
1110 return -EINVAL;
1112 input->index = tmp;
1113 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1114 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1115 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE))
1116 input->type |= V4L2_INPUT_TYPE_TUNER;
1117 else
1118 input->type |= V4L2_INPUT_TYPE_CAMERA;
1120 input->std = dev->vdev->tvnorms;
1122 return 0;
1125 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1127 struct au0828_fh *fh = priv;
1128 struct au0828_dev *dev = fh->dev;
1129 *i = dev->ctrl_input;
1130 return 0;
1133 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1135 struct au0828_fh *fh = priv;
1136 struct au0828_dev *dev = fh->dev;
1137 int i;
1139 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1140 index);
1141 if (index >= AU0828_MAX_INPUT)
1142 return -EINVAL;
1143 if (AUVI_INPUT(index).type == 0)
1144 return -EINVAL;
1145 dev->ctrl_input = index;
1147 switch (AUVI_INPUT(index).type) {
1148 case AU0828_VMUX_SVIDEO:
1149 dev->input_type = AU0828_VMUX_SVIDEO;
1150 break;
1151 case AU0828_VMUX_COMPOSITE:
1152 dev->input_type = AU0828_VMUX_COMPOSITE;
1153 break;
1154 case AU0828_VMUX_TELEVISION:
1155 dev->input_type = AU0828_VMUX_TELEVISION;
1156 break;
1157 default:
1158 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1159 AUVI_INPUT(index).type);
1160 break;
1163 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1164 AUVI_INPUT(index).vmux, 0, 0);
1166 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1167 int enable = 0;
1168 if (AUVI_INPUT(i).audio_setup == NULL)
1169 continue;
1171 if (i == index)
1172 enable = 1;
1173 else
1174 enable = 0;
1175 if (enable) {
1176 (AUVI_INPUT(i).audio_setup)(dev, enable);
1177 } else {
1178 /* Make sure we leave it turned on if some
1179 other input is routed to this callback */
1180 if ((AUVI_INPUT(i).audio_setup) !=
1181 ((AUVI_INPUT(index).audio_setup))) {
1182 (AUVI_INPUT(i).audio_setup)(dev, enable);
1187 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1188 AUVI_INPUT(index).amux, 0, 0);
1189 return 0;
1192 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1194 struct au0828_fh *fh = priv;
1195 struct au0828_dev *dev = fh->dev;
1196 unsigned int index = a->index;
1198 if (a->index > 1)
1199 return -EINVAL;
1201 index = dev->ctrl_ainput;
1202 if (index == 0)
1203 strcpy(a->name, "Television");
1204 else
1205 strcpy(a->name, "Line in");
1207 a->capability = V4L2_AUDCAP_STEREO;
1208 a->index = index;
1209 return 0;
1212 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1214 struct au0828_fh *fh = priv;
1215 struct au0828_dev *dev = fh->dev;
1216 if (a->index != dev->ctrl_ainput)
1217 return -EINVAL;
1218 return 0;
1221 static int vidioc_g_ctrl(struct file *file, void *priv,
1222 struct v4l2_control *ctrl)
1224 struct au0828_fh *fh = priv;
1225 struct au0828_dev *dev = fh->dev;
1227 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1228 return 0;
1232 static int vidioc_s_ctrl(struct file *file, void *priv,
1233 struct v4l2_control *ctrl)
1235 struct au0828_fh *fh = priv;
1236 struct au0828_dev *dev = fh->dev;
1237 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1238 return 0;
1241 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1243 struct au0828_fh *fh = priv;
1244 struct au0828_dev *dev = fh->dev;
1246 if (t->index != 0)
1247 return -EINVAL;
1249 strcpy(t->name, "Auvitek tuner");
1250 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1251 return 0;
1254 static int vidioc_s_tuner(struct file *file, void *priv,
1255 struct v4l2_tuner *t)
1257 struct au0828_fh *fh = priv;
1258 struct au0828_dev *dev = fh->dev;
1260 if (t->index != 0)
1261 return -EINVAL;
1263 t->type = V4L2_TUNER_ANALOG_TV;
1264 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1265 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1266 t->afc);
1267 return 0;
1271 static int vidioc_g_frequency(struct file *file, void *priv,
1272 struct v4l2_frequency *freq)
1274 struct au0828_fh *fh = priv;
1275 struct au0828_dev *dev = fh->dev;
1277 freq->type = V4L2_TUNER_ANALOG_TV;
1278 freq->frequency = dev->ctrl_freq;
1279 return 0;
1282 static int vidioc_s_frequency(struct file *file, void *priv,
1283 struct v4l2_frequency *freq)
1285 struct au0828_fh *fh = priv;
1286 struct au0828_dev *dev = fh->dev;
1288 if (freq->tuner != 0)
1289 return -EINVAL;
1290 if (freq->type != V4L2_TUNER_ANALOG_TV)
1291 return -EINVAL;
1293 dev->ctrl_freq = freq->frequency;
1295 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1297 au0828_analog_stream_reset(dev);
1299 return 0;
1302 static int vidioc_g_chip_ident(struct file *file, void *priv,
1303 struct v4l2_dbg_chip_ident *chip)
1305 struct au0828_fh *fh = priv;
1306 struct au0828_dev *dev = fh->dev;
1307 chip->ident = V4L2_IDENT_NONE;
1308 chip->revision = 0;
1310 if (v4l2_chip_match_host(&chip->match)) {
1311 chip->ident = V4L2_IDENT_AU0828;
1312 return 0;
1315 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1316 if (chip->ident == V4L2_IDENT_NONE)
1317 return -EINVAL;
1319 return 0;
1322 static int vidioc_cropcap(struct file *file, void *priv,
1323 struct v4l2_cropcap *cc)
1325 struct au0828_fh *fh = priv;
1326 struct au0828_dev *dev = fh->dev;
1328 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1329 return -EINVAL;
1331 cc->bounds.left = 0;
1332 cc->bounds.top = 0;
1333 cc->bounds.width = dev->width;
1334 cc->bounds.height = dev->height;
1336 cc->defrect = cc->bounds;
1338 cc->pixelaspect.numerator = 54;
1339 cc->pixelaspect.denominator = 59;
1341 return 0;
1344 static int vidioc_streamon(struct file *file, void *priv,
1345 enum v4l2_buf_type type)
1347 struct au0828_fh *fh = priv;
1348 struct au0828_dev *dev = fh->dev;
1349 int rc;
1351 rc = check_dev(dev);
1352 if (rc < 0)
1353 return rc;
1355 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1356 au0828_analog_stream_enable(dev);
1357 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1360 mutex_lock(&dev->lock);
1361 rc = res_get(fh);
1363 if (likely(rc >= 0))
1364 rc = videobuf_streamon(&fh->vb_vidq);
1365 mutex_unlock(&dev->lock);
1367 return rc;
1370 static int vidioc_streamoff(struct file *file, void *priv,
1371 enum v4l2_buf_type type)
1373 struct au0828_fh *fh = priv;
1374 struct au0828_dev *dev = fh->dev;
1375 int i;
1376 int ret;
1377 int rc;
1379 rc = check_dev(dev);
1380 if (rc < 0)
1381 return rc;
1383 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1384 return -EINVAL;
1385 if (type != fh->type)
1386 return -EINVAL;
1388 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1389 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1390 ret = au0828_stream_interrupt(dev);
1391 if (ret != 0)
1392 return ret;
1395 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1396 if (AUVI_INPUT(i).audio_setup == NULL)
1397 continue;
1398 (AUVI_INPUT(i).audio_setup)(dev, 0);
1401 mutex_lock(&dev->lock);
1402 videobuf_streamoff(&fh->vb_vidq);
1403 res_free(fh);
1404 mutex_unlock(&dev->lock);
1406 return 0;
1409 #ifdef CONFIG_VIDEO_ADV_DEBUG
1410 static int vidioc_g_register(struct file *file, void *priv,
1411 struct v4l2_dbg_register *reg)
1413 struct au0828_fh *fh = priv;
1414 struct au0828_dev *dev = fh->dev;
1416 switch (reg->match.type) {
1417 case V4L2_CHIP_MATCH_I2C_DRIVER:
1418 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1419 return 0;
1420 default:
1421 return -EINVAL;
1425 static int vidioc_s_register(struct file *file, void *priv,
1426 struct v4l2_dbg_register *reg)
1428 struct au0828_fh *fh = priv;
1429 struct au0828_dev *dev = fh->dev;
1431 switch (reg->match.type) {
1432 case V4L2_CHIP_MATCH_I2C_DRIVER:
1433 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1434 return 0;
1435 default:
1436 return -EINVAL;
1438 return 0;
1440 #endif
1442 static int vidioc_reqbufs(struct file *file, void *priv,
1443 struct v4l2_requestbuffers *rb)
1445 struct au0828_fh *fh = priv;
1446 struct au0828_dev *dev = fh->dev;
1447 int rc;
1449 rc = check_dev(dev);
1450 if (rc < 0)
1451 return rc;
1453 return videobuf_reqbufs(&fh->vb_vidq, rb);
1456 static int vidioc_querybuf(struct file *file, void *priv,
1457 struct v4l2_buffer *b)
1459 struct au0828_fh *fh = priv;
1460 struct au0828_dev *dev = fh->dev;
1461 int rc;
1463 rc = check_dev(dev);
1464 if (rc < 0)
1465 return rc;
1467 return videobuf_querybuf(&fh->vb_vidq, b);
1470 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1472 struct au0828_fh *fh = priv;
1473 struct au0828_dev *dev = fh->dev;
1474 int rc;
1476 rc = check_dev(dev);
1477 if (rc < 0)
1478 return rc;
1480 return videobuf_qbuf(&fh->vb_vidq, b);
1483 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1485 struct au0828_fh *fh = priv;
1486 struct au0828_dev *dev = fh->dev;
1487 int rc;
1489 rc = check_dev(dev);
1490 if (rc < 0)
1491 return rc;
1493 /* Workaround for a bug in the au0828 hardware design that sometimes
1494 results in the colorspace being inverted */
1495 if (dev->greenscreen_detected == 1) {
1496 dprintk(1, "Detected green frame. Resetting stream...\n");
1497 au0828_analog_stream_reset(dev);
1498 dev->greenscreen_detected = 0;
1501 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1504 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1505 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1507 struct au0828_fh *fh = priv;
1509 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1511 #endif
1513 static struct v4l2_file_operations au0828_v4l_fops = {
1514 .owner = THIS_MODULE,
1515 .open = au0828_v4l2_open,
1516 .release = au0828_v4l2_close,
1517 .read = au0828_v4l2_read,
1518 .poll = au0828_v4l2_poll,
1519 .mmap = au0828_v4l2_mmap,
1520 .ioctl = video_ioctl2,
1523 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1524 .vidioc_querycap = vidioc_querycap,
1525 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1526 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1527 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1528 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1529 #ifdef VBI_IS_WORKING
1530 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1531 .vidioc_try_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
1532 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
1533 #endif
1534 .vidioc_g_audio = vidioc_g_audio,
1535 .vidioc_s_audio = vidioc_s_audio,
1536 .vidioc_cropcap = vidioc_cropcap,
1537 #ifdef VBI_IS_WORKING
1538 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1539 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1540 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1541 #endif
1542 .vidioc_reqbufs = vidioc_reqbufs,
1543 .vidioc_querybuf = vidioc_querybuf,
1544 .vidioc_qbuf = vidioc_qbuf,
1545 .vidioc_dqbuf = vidioc_dqbuf,
1546 .vidioc_s_std = vidioc_s_std,
1547 .vidioc_enum_input = vidioc_enum_input,
1548 .vidioc_g_input = vidioc_g_input,
1549 .vidioc_s_input = vidioc_s_input,
1550 .vidioc_queryctrl = vidioc_queryctrl,
1551 .vidioc_g_ctrl = vidioc_g_ctrl,
1552 .vidioc_s_ctrl = vidioc_s_ctrl,
1553 .vidioc_streamon = vidioc_streamon,
1554 .vidioc_streamoff = vidioc_streamoff,
1555 .vidioc_g_tuner = vidioc_g_tuner,
1556 .vidioc_s_tuner = vidioc_s_tuner,
1557 .vidioc_g_frequency = vidioc_g_frequency,
1558 .vidioc_s_frequency = vidioc_s_frequency,
1559 #ifdef CONFIG_VIDEO_ADV_DEBUG
1560 .vidioc_g_register = vidioc_g_register,
1561 .vidioc_s_register = vidioc_s_register,
1562 #endif
1563 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1564 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1565 .vidiocgmbuf = vidiocgmbuf,
1566 #endif
1569 static const struct video_device au0828_video_template = {
1570 .fops = &au0828_v4l_fops,
1571 .release = video_device_release,
1572 .ioctl_ops = &video_ioctl_ops,
1573 .tvnorms = V4L2_STD_NTSC_M,
1574 .current_norm = V4L2_STD_NTSC_M,
1577 /**************************************************************************/
1579 int au0828_analog_register(struct au0828_dev *dev,
1580 struct usb_interface *interface)
1582 int retval = -ENOMEM;
1583 struct usb_host_interface *iface_desc;
1584 struct usb_endpoint_descriptor *endpoint;
1585 int i;
1587 dprintk(1, "au0828_analog_register called!\n");
1589 /* set au0828 usb interface0 to as5 */
1590 retval = usb_set_interface(dev->usbdev,
1591 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1592 if (retval != 0) {
1593 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
1594 return retval;
1597 /* Figure out which endpoint has the isoc interface */
1598 iface_desc = interface->cur_altsetting;
1599 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1600 endpoint = &iface_desc->endpoint[i].desc;
1601 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1602 == USB_DIR_IN) &&
1603 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1604 == USB_ENDPOINT_XFER_ISOC)) {
1606 /* we find our isoc in endpoint */
1607 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1608 dev->max_pkt_size = (tmp & 0x07ff) *
1609 (((tmp & 0x1800) >> 11) + 1);
1610 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1613 if (!(dev->isoc_in_endpointaddr)) {
1614 printk(KERN_INFO "Could not locate isoc endpoint\n");
1615 kfree(dev);
1616 return -ENODEV;
1619 init_waitqueue_head(&dev->open);
1620 spin_lock_init(&dev->slock);
1621 mutex_init(&dev->lock);
1623 INIT_LIST_HEAD(&dev->vidq.active);
1624 INIT_LIST_HEAD(&dev->vidq.queued);
1626 dev->width = NTSC_STD_W;
1627 dev->height = NTSC_STD_H;
1628 dev->field_size = dev->width * dev->height;
1629 dev->frame_size = dev->field_size << 1;
1630 dev->bytesperline = dev->width << 1;
1631 dev->ctrl_ainput = 0;
1633 /* allocate and fill v4l2 video struct */
1634 dev->vdev = video_device_alloc();
1635 if (NULL == dev->vdev) {
1636 dprintk(1, "Can't allocate video_device.\n");
1637 return -ENOMEM;
1640 #ifdef VBI_IS_WORKING
1641 dev->vbi_dev = video_device_alloc();
1642 if (NULL == dev->vbi_dev) {
1643 dprintk(1, "Can't allocate vbi_device.\n");
1644 kfree(dev->vdev);
1645 return -ENOMEM;
1647 #endif
1649 /* Fill the video capture device struct */
1650 *dev->vdev = au0828_video_template;
1651 dev->vdev->parent = &dev->usbdev->dev;
1652 strcpy(dev->vdev->name, "au0828a video");
1654 #ifdef VBI_IS_WORKING
1655 /* Setup the VBI device */
1656 *dev->vbi_dev = au0828_video_template;
1657 dev->vbi_dev->parent = &dev->usbdev->dev;
1658 strcpy(dev->vbi_dev->name, "au0828a vbi");
1659 #endif
1661 /* Register the v4l2 device */
1662 video_set_drvdata(dev->vdev, dev);
1663 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1664 if (retval != 0) {
1665 dprintk(1, "unable to register video device (error = %d).\n",
1666 retval);
1667 video_device_release(dev->vdev);
1668 return -ENODEV;
1671 #ifdef VBI_IS_WORKING
1672 /* Register the vbi device */
1673 video_set_drvdata(dev->vbi_dev, dev);
1674 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
1675 if (retval != 0) {
1676 dprintk(1, "unable to register vbi device (error = %d).\n",
1677 retval);
1678 video_device_release(dev->vbi_dev);
1679 video_device_release(dev->vdev);
1680 return -ENODEV;
1682 #endif
1684 dprintk(1, "%s completed!\n", __func__);
1686 return 0;