[media] au0828: add try_fmt_vbi support, zero vbi.reserved, pix.priv
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / usb / au0828 / au0828-video.c
CommitLineData
8b2f0795
DH
1/*
2 * Auvitek AU0828 USB Bridge (Analog video support)
3 *
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23/* Developer Notes:
24 *
25 * VBI support is not yet working
26 * The hardware scaler supported is unimplemented
27 * AC97 audio support is unimplemented (only i2s audio mode)
28 *
29 */
30
31#include <linux/module.h>
5a0e3ad6 32#include <linux/slab.h>
8b2f0795
DH
33#include <linux/init.h>
34#include <linux/device.h>
35#include <linux/suspend.h>
8b2f0795
DH
36#include <media/v4l2-common.h>
37#include <media/v4l2-ioctl.h>
83b09422 38#include <media/v4l2-event.h>
8b2f0795
DH
39#include <media/v4l2-chip-ident.h>
40#include <media/tuner.h>
41#include "au0828.h"
42#include "au0828-reg.h"
43
8b2f0795
DH
44static DEFINE_MUTEX(au0828_sysfs_lock);
45
8b2f0795
DH
46/* ------------------------------------------------------------------
47 Videobuf operations
48 ------------------------------------------------------------------*/
49
50static unsigned int isoc_debug;
51module_param(isoc_debug, int, 0644);
52MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
53
54#define au0828_isocdbg(fmt, arg...) \
55do {\
56 if (isoc_debug) { \
57 printk(KERN_INFO "au0828 %s :"fmt, \
58 __func__ , ##arg); \
59 } \
60 } while (0)
61
62static inline void print_err_status(struct au0828_dev *dev,
63 int packet, int status)
64{
65 char *errmsg = "Unknown";
66
67 switch (status) {
68 case -ENOENT:
69 errmsg = "unlinked synchronuously";
70 break;
71 case -ECONNRESET:
72 errmsg = "unlinked asynchronuously";
73 break;
74 case -ENOSR:
75 errmsg = "Buffer error (overrun)";
76 break;
77 case -EPIPE:
78 errmsg = "Stalled (device not responding)";
79 break;
80 case -EOVERFLOW:
81 errmsg = "Babble (bad cable?)";
82 break;
83 case -EPROTO:
84 errmsg = "Bit-stuff error (bad cable?)";
85 break;
86 case -EILSEQ:
87 errmsg = "CRC/Timeout (could be anything)";
88 break;
89 case -ETIME:
90 errmsg = "Device does not respond";
91 break;
92 }
93 if (packet < 0) {
94 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
95 } else {
96 au0828_isocdbg("URB packet %d, status %d [%s].\n",
97 packet, status, errmsg);
98 }
99}
100
101static int check_dev(struct au0828_dev *dev)
102{
103 if (dev->dev_state & DEV_DISCONNECTED) {
62899a28 104 printk(KERN_INFO "v4l2 ioctl: device not present\n");
8b2f0795
DH
105 return -ENODEV;
106 }
107
108 if (dev->dev_state & DEV_MISCONFIGURED) {
62899a28 109 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
8b2f0795
DH
110 "close and open it again\n");
111 return -EIO;
112 }
113 return 0;
114}
115
116/*
117 * IRQ callback, called by URB callback
118 */
119static void au0828_irq_callback(struct urb *urb)
120{
121 struct au0828_dmaqueue *dma_q = urb->context;
122 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
78ca5005 123 unsigned long flags = 0;
e92ba283 124 int i;
8b2f0795
DH
125
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;
138 }
139
140 /* Copy data from URB */
78ca5005 141 spin_lock_irqsave(&dev->slock, flags);
e92ba283 142 dev->isoc_ctl.isoc_copy(dev, urb);
78ca5005 143 spin_unlock_irqrestore(&dev->slock, flags);
8b2f0795
DH
144
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;
149 }
150 urb->status = 0;
151
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);
156 }
157}
158
159/*
160 * Stop and Deallocate URBs
161 */
a094ca46 162static void au0828_uninit_isoc(struct au0828_dev *dev)
8b2f0795
DH
163{
164 struct urb *urb;
165 int i;
166
167 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
168
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);
177
178 if (dev->isoc_ctl.transfer_buffer[i]) {
997ea58e 179 usb_free_coherent(dev->usbdev,
8b2f0795
DH
180 urb->transfer_buffer_length,
181 dev->isoc_ctl.transfer_buffer[i],
182 urb->transfer_dma);
183 }
184 usb_free_urb(urb);
185 dev->isoc_ctl.urb[i] = NULL;
186 }
187 dev->isoc_ctl.transfer_buffer[i] = NULL;
188 }
189
190 kfree(dev->isoc_ctl.urb);
191 kfree(dev->isoc_ctl.transfer_buffer);
192
193 dev->isoc_ctl.urb = NULL;
194 dev->isoc_ctl.transfer_buffer = NULL;
195 dev->isoc_ctl.num_bufs = 0;
196}
197
198/*
199 * Allocate URBs and start IRQ
200 */
a094ca46
MCC
201static 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))
8b2f0795
DH
204{
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;
211
212 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
213
214 /* De-allocates all pending stuff */
215 au0828_uninit_isoc(dev);
216
217 dev->isoc_ctl.isoc_copy = isoc_copy;
218 dev->isoc_ctl.num_bufs = num_bufs;
219
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;
224 }
225
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;
232 }
233
234 dev->isoc_ctl.max_pkt_size = max_pkt_size;
235 dev->isoc_ctl.buf = NULL;
236
237 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
238
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;
246 }
247 dev->isoc_ctl.urb[i] = urb;
248
997ea58e 249 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
8b2f0795
DH
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;
258 }
259 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
260
261 pipe = usb_rcvisocpipe(dev->usbdev,
262 dev->isoc_in_endpointaddr),
263
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);
267
268 urb->number_of_packets = max_packets;
fadadb7d 269 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
8b2f0795
DH
270
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;
277 }
278 }
279
280 init_waitqueue_head(&dma_q->wq);
281
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;
290 }
291 }
292
293 return 0;
294}
295
296/*
297 * Announces that a buffer were filled and request the next
298 */
299static inline void buffer_filled(struct au0828_dev *dev,
300 struct au0828_dmaqueue *dma_q,
301 struct au0828_buffer *buf)
302{
303 /* Advice that buffer was filled */
304 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
305
306 buf->vb.state = VIDEOBUF_DONE;
307 buf->vb.field_count++;
8e6057b5 308 v4l2_get_timestamp(&buf->vb.ts);
8b2f0795
DH
309
310 dev->isoc_ctl.buf = NULL;
311
312 list_del(&buf->vb.queue);
313 wake_up(&buf->vb.done);
314}
315
7f8eacd2
DH
316static inline void vbi_buffer_filled(struct au0828_dev *dev,
317 struct au0828_dmaqueue *dma_q,
318 struct au0828_buffer *buf)
319{
320 /* Advice that buffer was filled */
321 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
322
323 buf->vb.state = VIDEOBUF_DONE;
324 buf->vb.field_count++;
8e6057b5 325 v4l2_get_timestamp(&buf->vb.ts);
7f8eacd2
DH
326
327 dev->isoc_ctl.vbi_buf = NULL;
328
329 list_del(&buf->vb.queue);
330 wake_up(&buf->vb.done);
331}
332
8b2f0795
DH
333/*
334 * Identify the buffer header type and properly handles
335 */
336static void au0828_copy_video(struct au0828_dev *dev,
337 struct au0828_dmaqueue *dma_q,
338 struct au0828_buffer *buf,
339 unsigned char *p,
340 unsigned char *outp, unsigned long len)
341{
342 void *fieldstart, *startwrite, *startread;
343 int linesdone, currlinedone, offset, lencopy, remain;
344 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
345
7f8eacd2
DH
346 if (len == 0)
347 return;
348
8b2f0795
DH
349 if (dma_q->pos + len > buf->vb.size)
350 len = buf->vb.size - dma_q->pos;
351
352 startread = p;
353 remain = len;
354
355 /* Interlaces frame */
356 if (buf->top_field)
357 fieldstart = outp;
358 else
359 fieldstart = outp + bytesperline;
360
361 linesdone = dma_q->pos / bytesperline;
362 currlinedone = dma_q->pos % bytesperline;
363 offset = linesdone * bytesperline * 2 + currlinedone;
364 startwrite = fieldstart + offset;
365 lencopy = bytesperline - currlinedone;
366 lencopy = lencopy > remain ? remain : lencopy;
367
368 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
369 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
370 ((char *)startwrite + lencopy) -
371 ((char *)outp + buf->vb.size));
372 remain = (char *)outp + buf->vb.size - (char *)startwrite;
373 lencopy = remain;
374 }
375 if (lencopy <= 0)
376 return;
377 memcpy(startwrite, startread, lencopy);
378
379 remain -= lencopy;
380
381 while (remain > 0) {
382 startwrite += lencopy + bytesperline;
383 startread += lencopy;
384 if (bytesperline > remain)
385 lencopy = remain;
386 else
387 lencopy = bytesperline;
388
389 if ((char *)startwrite + lencopy > (char *)outp +
390 buf->vb.size) {
62899a28 391 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
8b2f0795
DH
392 ((char *)startwrite + lencopy) -
393 ((char *)outp + buf->vb.size));
394 lencopy = remain = (char *)outp + buf->vb.size -
395 (char *)startwrite;
396 }
397 if (lencopy <= 0)
398 break;
399
400 memcpy(startwrite, startread, lencopy);
401
402 remain -= lencopy;
403 }
404
405 if (offset > 1440) {
406 /* We have enough data to check for greenscreen */
62899a28 407 if (outp[0] < 0x60 && outp[1440] < 0x60)
8b2f0795 408 dev->greenscreen_detected = 1;
8b2f0795
DH
409 }
410
411 dma_q->pos += len;
412}
413
414/*
415 * video-buf generic routine to get the next available buffer
416 */
417static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
418 struct au0828_buffer **buf)
419{
420 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
421
422 if (list_empty(&dma_q->active)) {
423 au0828_isocdbg("No active queue to serve\n");
424 dev->isoc_ctl.buf = NULL;
425 *buf = NULL;
426 return;
427 }
428
429 /* Get the next buffer */
430 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
431 dev->isoc_ctl.buf = *buf;
432
433 return;
434}
435
7f8eacd2
DH
436static void au0828_copy_vbi(struct au0828_dev *dev,
437 struct au0828_dmaqueue *dma_q,
438 struct au0828_buffer *buf,
439 unsigned char *p,
440 unsigned char *outp, unsigned long len)
441{
442 unsigned char *startwrite, *startread;
b5f5933a 443 int bytesperline;
7f8eacd2
DH
444 int i, j = 0;
445
446 if (dev == NULL) {
447 au0828_isocdbg("dev is null\n");
448 return;
449 }
450
451 if (dma_q == NULL) {
452 au0828_isocdbg("dma_q is null\n");
453 return;
454 }
455 if (buf == NULL)
456 return;
457 if (p == NULL) {
458 au0828_isocdbg("p is null\n");
459 return;
460 }
461 if (outp == NULL) {
462 au0828_isocdbg("outp is null\n");
463 return;
464 }
465
b5f5933a
DC
466 bytesperline = dev->vbi_width;
467
7f8eacd2
DH
468 if (dma_q->pos + len > buf->vb.size)
469 len = buf->vb.size - dma_q->pos;
470
471 startread = p;
472 startwrite = outp + (dma_q->pos / 2);
473
474 /* Make sure the bottom field populates the second half of the frame */
475 if (buf->top_field == 0)
476 startwrite += bytesperline * dev->vbi_height;
477
478 for (i = 0; i < len; i += 2)
479 startwrite[j++] = startread[i+1];
480
481 dma_q->pos += len;
482}
483
484
485/*
486 * video-buf generic routine to get the next available VBI buffer
487 */
488static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
489 struct au0828_buffer **buf)
490{
491 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
492 char *outp;
493
494 if (list_empty(&dma_q->active)) {
495 au0828_isocdbg("No active queue to serve\n");
496 dev->isoc_ctl.vbi_buf = NULL;
497 *buf = NULL;
498 return;
499 }
500
501 /* Get the next buffer */
502 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
25985edc 503 /* Cleans up buffer - Useful for testing for frame/URB loss */
7f8eacd2
DH
504 outp = videobuf_to_vmalloc(&(*buf)->vb);
505 memset(outp, 0x00, (*buf)->vb.size);
506
507 dev->isoc_ctl.vbi_buf = *buf;
508
509 return;
510}
511
8b2f0795
DH
512/*
513 * Controls the isoc copy of each urb packet
514 */
515static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
516{
517 struct au0828_buffer *buf;
7f8eacd2 518 struct au0828_buffer *vbi_buf;
8b2f0795 519 struct au0828_dmaqueue *dma_q = urb->context;
7f8eacd2 520 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
8b2f0795 521 unsigned char *outp = NULL;
7f8eacd2 522 unsigned char *vbioutp = NULL;
8b2f0795
DH
523 int i, len = 0, rc = 1;
524 unsigned char *p;
525 unsigned char fbyte;
7f8eacd2
DH
526 unsigned int vbi_field_size;
527 unsigned int remain, lencopy;
8b2f0795
DH
528
529 if (!dev)
530 return 0;
531
532 if ((dev->dev_state & DEV_DISCONNECTED) ||
533 (dev->dev_state & DEV_MISCONFIGURED))
534 return 0;
535
536 if (urb->status < 0) {
537 print_err_status(dev, -1, urb->status);
538 if (urb->status == -ENOENT)
539 return 0;
540 }
541
542 buf = dev->isoc_ctl.buf;
543 if (buf != NULL)
544 outp = videobuf_to_vmalloc(&buf->vb);
545
7f8eacd2
DH
546 vbi_buf = dev->isoc_ctl.vbi_buf;
547 if (vbi_buf != NULL)
548 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
549
8b2f0795
DH
550 for (i = 0; i < urb->number_of_packets; i++) {
551 int status = urb->iso_frame_desc[i].status;
552
553 if (status < 0) {
554 print_err_status(dev, i, status);
555 if (urb->iso_frame_desc[i].status != -EPROTO)
556 continue;
557 }
558
62899a28 559 if (urb->iso_frame_desc[i].actual_length <= 0)
8b2f0795 560 continue;
62899a28 561
8b2f0795
DH
562 if (urb->iso_frame_desc[i].actual_length >
563 dev->max_pkt_size) {
564 au0828_isocdbg("packet bigger than packet size");
565 continue;
566 }
567
568 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
569 fbyte = p[0];
570 len = urb->iso_frame_desc[i].actual_length - 4;
571 p += 4;
572
573 if (fbyte & 0x80) {
574 len -= 4;
575 p += 4;
576 au0828_isocdbg("Video frame %s\n",
577 (fbyte & 0x40) ? "odd" : "even");
bde3bb9a 578 if (fbyte & 0x40) {
7f8eacd2
DH
579 /* VBI */
580 if (vbi_buf != NULL)
581 vbi_buffer_filled(dev,
582 vbi_dma_q,
583 vbi_buf);
584 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
585 if (vbi_buf == NULL)
586 vbioutp = NULL;
587 else
588 vbioutp = videobuf_to_vmalloc(
589 &vbi_buf->vb);
590
591 /* Video */
8b2f0795
DH
592 if (buf != NULL)
593 buffer_filled(dev, dma_q, buf);
594 get_next_buf(dma_q, &buf);
62899a28 595 if (buf == NULL)
8b2f0795 596 outp = NULL;
62899a28 597 else
8b2f0795 598 outp = videobuf_to_vmalloc(&buf->vb);
78ca5005
DH
599
600 /* As long as isoc traffic is arriving, keep
601 resetting the timer */
602 if (dev->vid_timeout_running)
603 mod_timer(&dev->vid_timeout,
604 jiffies + (HZ / 10));
605 if (dev->vbi_timeout_running)
606 mod_timer(&dev->vbi_timeout,
607 jiffies + (HZ / 10));
8b2f0795
DH
608 }
609
610 if (buf != NULL) {
62899a28 611 if (fbyte & 0x40)
8b2f0795 612 buf->top_field = 1;
62899a28 613 else
8b2f0795 614 buf->top_field = 0;
8b2f0795
DH
615 }
616
7f8eacd2
DH
617 if (vbi_buf != NULL) {
618 if (fbyte & 0x40)
619 vbi_buf->top_field = 1;
620 else
621 vbi_buf->top_field = 0;
622 }
623
624 dev->vbi_read = 0;
625 vbi_dma_q->pos = 0;
8b2f0795
DH
626 dma_q->pos = 0;
627 }
7f8eacd2
DH
628
629 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
630 if (dev->vbi_read < vbi_field_size) {
631 remain = vbi_field_size - dev->vbi_read;
632 if (len < remain)
633 lencopy = len;
634 else
635 lencopy = remain;
636
637 if (vbi_buf != NULL)
638 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
639 vbioutp, len);
640
641 len -= lencopy;
642 p += lencopy;
643 dev->vbi_read += lencopy;
644 }
645
646 if (dev->vbi_read >= vbi_field_size && buf != NULL)
8b2f0795 647 au0828_copy_video(dev, dma_q, buf, p, outp, len);
8b2f0795
DH
648 }
649 return rc;
650}
651
652static int
653buffer_setup(struct videobuf_queue *vq, unsigned int *count,
654 unsigned int *size)
655{
656 struct au0828_fh *fh = vq->priv_data;
657 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
658
659 if (0 == *count)
660 *count = AU0828_DEF_BUF;
661
662 if (*count < AU0828_MIN_BUF)
663 *count = AU0828_MIN_BUF;
664 return 0;
665}
666
667/* This is called *without* dev->slock held; please keep it that way */
668static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
669{
670 struct au0828_fh *fh = vq->priv_data;
671 struct au0828_dev *dev = fh->dev;
672 unsigned long flags = 0;
673 if (in_interrupt())
674 BUG();
675
676 /* We used to wait for the buffer to finish here, but this didn't work
677 because, as we were keeping the state as VIDEOBUF_QUEUED,
678 videobuf_queue_cancel marked it as finished for us.
679 (Also, it could wedge forever if the hardware was misconfigured.)
680
681 This should be safe; by the time we get here, the buffer isn't
682 queued anymore. If we ever start marking the buffers as
683 VIDEOBUF_ACTIVE, it won't be, though.
684 */
685 spin_lock_irqsave(&dev->slock, flags);
686 if (dev->isoc_ctl.buf == buf)
687 dev->isoc_ctl.buf = NULL;
688 spin_unlock_irqrestore(&dev->slock, flags);
689
690 videobuf_vmalloc_free(&buf->vb);
691 buf->vb.state = VIDEOBUF_NEEDS_INIT;
692}
693
694static int
695buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
696 enum v4l2_field field)
697{
698 struct au0828_fh *fh = vq->priv_data;
699 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
700 struct au0828_dev *dev = fh->dev;
701 int rc = 0, urb_init = 0;
702
703 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
704
705 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
706 return -EINVAL;
707
708 buf->vb.width = dev->width;
709 buf->vb.height = dev->height;
710 buf->vb.field = field;
711
712 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
713 rc = videobuf_iolock(vq, &buf->vb, NULL);
714 if (rc < 0) {
62899a28 715 printk(KERN_INFO "videobuf_iolock failed\n");
8b2f0795
DH
716 goto fail;
717 }
718 }
719
720 if (!dev->isoc_ctl.num_bufs)
721 urb_init = 1;
722
723 if (urb_init) {
724 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
725 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
726 au0828_isoc_copy);
727 if (rc < 0) {
62899a28 728 printk(KERN_INFO "au0828_init_isoc failed\n");
8b2f0795
DH
729 goto fail;
730 }
731 }
732
733 buf->vb.state = VIDEOBUF_PREPARED;
734 return 0;
735
736fail:
737 free_buffer(vq, buf);
738 return rc;
739}
740
741static void
742buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
743{
744 struct au0828_buffer *buf = container_of(vb,
745 struct au0828_buffer,
746 vb);
747 struct au0828_fh *fh = vq->priv_data;
748 struct au0828_dev *dev = fh->dev;
749 struct au0828_dmaqueue *vidq = &dev->vidq;
750
751 buf->vb.state = VIDEOBUF_QUEUED;
752 list_add_tail(&buf->vb.queue, &vidq->active);
753}
754
755static void buffer_release(struct videobuf_queue *vq,
756 struct videobuf_buffer *vb)
757{
758 struct au0828_buffer *buf = container_of(vb,
759 struct au0828_buffer,
760 vb);
761
762 free_buffer(vq, buf);
763}
764
765static struct videobuf_queue_ops au0828_video_qops = {
766 .buf_setup = buffer_setup,
767 .buf_prepare = buffer_prepare,
768 .buf_queue = buffer_queue,
769 .buf_release = buffer_release,
770};
771
772/* ------------------------------------------------------------------
773 V4L2 interface
774 ------------------------------------------------------------------*/
775
776static int au0828_i2s_init(struct au0828_dev *dev)
777{
778 /* Enable i2s mode */
779 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
780 return 0;
781}
782
783/*
784 * Auvitek au0828 analog stream enable
785 * Please set interface0 to AS5 before enable the stream
786 */
a094ca46 787static int au0828_analog_stream_enable(struct au0828_dev *d)
8b2f0795
DH
788{
789 dprintk(1, "au0828_analog_stream_enable called\n");
790 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
791 au0828_writereg(d, 0x106, 0x00);
792 /* set x position */
793 au0828_writereg(d, 0x110, 0x00);
794 au0828_writereg(d, 0x111, 0x00);
795 au0828_writereg(d, 0x114, 0xa0);
796 au0828_writereg(d, 0x115, 0x05);
797 /* set y position */
7f8eacd2 798 au0828_writereg(d, 0x112, 0x00);
8b2f0795
DH
799 au0828_writereg(d, 0x113, 0x00);
800 au0828_writereg(d, 0x116, 0xf2);
801 au0828_writereg(d, 0x117, 0x00);
802 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
803
804 return 0;
805}
806
807int au0828_analog_stream_disable(struct au0828_dev *d)
808{
809 dprintk(1, "au0828_analog_stream_disable called\n");
810 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
811 return 0;
812}
813
a094ca46 814static void au0828_analog_stream_reset(struct au0828_dev *dev)
8b2f0795
DH
815{
816 dprintk(1, "au0828_analog_stream_reset called\n");
817 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
818 mdelay(30);
819 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
820}
821
822/*
823 * Some operations needs to stop current streaming
824 */
825static int au0828_stream_interrupt(struct au0828_dev *dev)
826{
827 int ret = 0;
828
829 dev->stream_state = STREAM_INTERRUPT;
62899a28 830 if (dev->dev_state == DEV_DISCONNECTED)
8b2f0795 831 return -ENODEV;
62899a28 832 else if (ret) {
8b2f0795 833 dev->dev_state = DEV_MISCONFIGURED;
62899a28 834 dprintk(1, "%s device is misconfigured!\n", __func__);
8b2f0795
DH
835 return ret;
836 }
837 return 0;
838}
839
840/*
841 * au0828_release_resources
842 * unregister v4l2 devices
843 */
844void au0828_analog_unregister(struct au0828_dev *dev)
845{
846 dprintk(1, "au0828_release_resources called\n");
847 mutex_lock(&au0828_sysfs_lock);
848
63b0d5ad 849 if (dev->vdev)
5a5a4e16
DH
850 video_unregister_device(dev->vdev);
851 if (dev->vbi_dev)
852 video_unregister_device(dev->vbi_dev);
8b2f0795
DH
853
854 mutex_unlock(&au0828_sysfs_lock);
855}
856
857
858/* Usage lock check functions */
7f8eacd2 859static int res_get(struct au0828_fh *fh, unsigned int bit)
8b2f0795 860{
7f8eacd2 861 struct au0828_dev *dev = fh->dev;
8b2f0795 862
7f8eacd2
DH
863 if (fh->resources & bit)
864 /* have it already allocated */
865 return 1;
8b2f0795 866
7f8eacd2 867 /* is it free? */
7f8eacd2
DH
868 if (dev->resources & bit) {
869 /* no, someone else uses it */
7f8eacd2
DH
870 return 0;
871 }
872 /* it's free, grab it */
873 fh->resources |= bit;
874 dev->resources |= bit;
875 dprintk(1, "res: get %d\n", bit);
549ee4df 876
7f8eacd2
DH
877 return 1;
878}
8b2f0795 879
7f8eacd2
DH
880static int res_check(struct au0828_fh *fh, unsigned int bit)
881{
882 return fh->resources & bit;
8b2f0795
DH
883}
884
7f8eacd2 885static int res_locked(struct au0828_dev *dev, unsigned int bit)
8b2f0795 886{
7f8eacd2 887 return dev->resources & bit;
8b2f0795
DH
888}
889
7f8eacd2 890static void res_free(struct au0828_fh *fh, unsigned int bits)
8b2f0795 891{
7f8eacd2
DH
892 struct au0828_dev *dev = fh->dev;
893
894 BUG_ON((fh->resources & bits) != bits);
8b2f0795 895
7f8eacd2
DH
896 fh->resources &= ~bits;
897 dev->resources &= ~bits;
898 dprintk(1, "res: put %d\n", bits);
7f8eacd2
DH
899}
900
901static int get_ressource(struct au0828_fh *fh)
902{
903 switch (fh->type) {
904 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
905 return AU0828_RESOURCE_VIDEO;
906 case V4L2_BUF_TYPE_VBI_CAPTURE:
907 return AU0828_RESOURCE_VBI;
908 default:
909 BUG();
910 return 0;
911 }
8b2f0795
DH
912}
913
6e04b7b9
DH
914/* This function ensures that video frames continue to be delivered even if
915 the ITU-656 input isn't receiving any data (thereby preventing applications
916 such as tvtime from hanging) */
a094ca46 917static void au0828_vid_buffer_timeout(unsigned long data)
6e04b7b9
DH
918{
919 struct au0828_dev *dev = (struct au0828_dev *) data;
920 struct au0828_dmaqueue *dma_q = &dev->vidq;
921 struct au0828_buffer *buf;
922 unsigned char *vid_data;
78ca5005 923 unsigned long flags = 0;
6e04b7b9 924
78ca5005 925 spin_lock_irqsave(&dev->slock, flags);
6e04b7b9
DH
926
927 buf = dev->isoc_ctl.buf;
928 if (buf != NULL) {
929 vid_data = videobuf_to_vmalloc(&buf->vb);
930 memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
931 buffer_filled(dev, dma_q, buf);
6e04b7b9 932 }
78ca5005
DH
933 get_next_buf(dma_q, &buf);
934
935 if (dev->vid_timeout_running == 1)
936 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
6e04b7b9 937
78ca5005 938 spin_unlock_irqrestore(&dev->slock, flags);
6e04b7b9
DH
939}
940
a094ca46 941static void au0828_vbi_buffer_timeout(unsigned long data)
6e04b7b9
DH
942{
943 struct au0828_dev *dev = (struct au0828_dev *) data;
944 struct au0828_dmaqueue *dma_q = &dev->vbiq;
945 struct au0828_buffer *buf;
946 unsigned char *vbi_data;
78ca5005 947 unsigned long flags = 0;
6e04b7b9 948
78ca5005 949 spin_lock_irqsave(&dev->slock, flags);
6e04b7b9
DH
950
951 buf = dev->isoc_ctl.vbi_buf;
952 if (buf != NULL) {
953 vbi_data = videobuf_to_vmalloc(&buf->vb);
954 memset(vbi_data, 0x00, buf->vb.size);
955 vbi_buffer_filled(dev, dma_q, buf);
6e04b7b9 956 }
78ca5005 957 vbi_get_next_buf(dma_q, &buf);
6e04b7b9 958
78ca5005
DH
959 if (dev->vbi_timeout_running == 1)
960 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
961 spin_unlock_irqrestore(&dev->slock, flags);
6e04b7b9
DH
962}
963
964
8b2f0795
DH
965static int au0828_v4l2_open(struct file *filp)
966{
8b2f0795 967 int ret = 0;
7f8eacd2 968 struct video_device *vdev = video_devdata(filp);
63b0d5ad 969 struct au0828_dev *dev = video_drvdata(filp);
8b2f0795 970 struct au0828_fh *fh;
7f8eacd2 971 int type;
63b0d5ad 972
7f8eacd2
DH
973 switch (vdev->vfl_type) {
974 case VFL_TYPE_GRABBER:
975 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
976 break;
977 case VFL_TYPE_VBI:
63b0d5ad 978 type = V4L2_BUF_TYPE_VBI_CAPTURE;
7f8eacd2
DH
979 break;
980 default:
981 return -EINVAL;
982 }
8b2f0795
DH
983
984 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
62899a28 985 if (NULL == fh) {
8b2f0795
DH
986 dprintk(1, "Failed allocate au0828_fh struct!\n");
987 return -ENOMEM;
988 }
989
990 fh->type = type;
991 fh->dev = dev;
83b09422 992 v4l2_fh_init(&fh->fh, vdev);
8b2f0795
DH
993 filp->private_data = fh;
994
62899a28 995 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
8b2f0795
DH
996 /* set au0828 interface0 to AS5 here again */
997 ret = usb_set_interface(dev->usbdev, 0, 5);
62899a28
DH
998 if (ret < 0) {
999 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
8b2f0795
DH
1000 return -EBUSY;
1001 }
1002 dev->width = NTSC_STD_W;
1003 dev->height = NTSC_STD_H;
1004 dev->frame_size = dev->width * dev->height * 2;
1005 dev->field_size = dev->width * dev->height;
1006 dev->bytesperline = dev->width * 2;
1007
1008 au0828_analog_stream_enable(dev);
1009 au0828_analog_stream_reset(dev);
1010
1011 /* If we were doing ac97 instead of i2s, it would go here...*/
1012 au0828_i2s_init(dev);
1013
1014 dev->stream_state = STREAM_OFF;
1015 dev->dev_state |= DEV_INITIALIZED;
1016 }
1017
1018 dev->users++;
1019
1020 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
7f8eacd2
DH
1021 NULL, &dev->slock,
1022 V4L2_BUF_TYPE_VIDEO_CAPTURE,
8b2f0795 1023 V4L2_FIELD_INTERLACED,
549ee4df
DH
1024 sizeof(struct au0828_buffer), fh,
1025 &dev->lock);
8b2f0795 1026
7f8eacd2
DH
1027 /* VBI Setup */
1028 dev->vbi_width = 720;
1029 dev->vbi_height = 1;
1030 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1031 NULL, &dev->slock,
1032 V4L2_BUF_TYPE_VBI_CAPTURE,
1033 V4L2_FIELD_SEQ_TB,
549ee4df
DH
1034 sizeof(struct au0828_buffer), fh,
1035 &dev->lock);
83b09422 1036 v4l2_fh_add(&fh->fh);
8b2f0795
DH
1037 return ret;
1038}
1039
1040static int au0828_v4l2_close(struct file *filp)
1041{
1042 int ret;
1043 struct au0828_fh *fh = filp->private_data;
1044 struct au0828_dev *dev = fh->dev;
1045
83b09422
HV
1046 v4l2_fh_del(&fh->fh);
1047 v4l2_fh_exit(&fh->fh);
7f8eacd2 1048 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
78ca5005
DH
1049 /* Cancel timeout thread in case they didn't call streamoff */
1050 dev->vid_timeout_running = 0;
1051 del_timer_sync(&dev->vid_timeout);
1052
8b2f0795 1053 videobuf_stop(&fh->vb_vidq);
7f8eacd2
DH
1054 res_free(fh, AU0828_RESOURCE_VIDEO);
1055 }
8b2f0795 1056
7f8eacd2 1057 if (res_check(fh, AU0828_RESOURCE_VBI)) {
78ca5005
DH
1058 /* Cancel timeout thread in case they didn't call streamoff */
1059 dev->vbi_timeout_running = 0;
1060 del_timer_sync(&dev->vbi_timeout);
1061
7f8eacd2
DH
1062 videobuf_stop(&fh->vb_vbiq);
1063 res_free(fh, AU0828_RESOURCE_VBI);
1064 }
1065
1066 if (dev->users == 1) {
62899a28 1067 if (dev->dev_state & DEV_DISCONNECTED) {
8b2f0795 1068 au0828_analog_unregister(dev);
83b09422 1069 kfree(fh);
8b2f0795
DH
1070 kfree(dev);
1071 return 0;
1072 }
1073
1074 au0828_analog_stream_disable(dev);
1075
1076 au0828_uninit_isoc(dev);
1077
e4b8bc52 1078 /* Save some power by putting tuner to sleep */
622b828a 1079 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
e4b8bc52 1080
8b2f0795
DH
1081 /* When close the device, set the usb intf0 into alt0 to free
1082 USB bandwidth */
1083 ret = usb_set_interface(dev->usbdev, 0, 0);
62899a28
DH
1084 if (ret < 0)
1085 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
8b2f0795
DH
1086 }
1087
7f8eacd2
DH
1088 videobuf_mmap_free(&fh->vb_vidq);
1089 videobuf_mmap_free(&fh->vb_vbiq);
8b2f0795
DH
1090 kfree(fh);
1091 dev->users--;
1092 wake_up_interruptible_nr(&dev->open, 1);
8b2f0795
DH
1093 return 0;
1094}
1095
1096static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1097 size_t count, loff_t *pos)
1098{
1099 struct au0828_fh *fh = filp->private_data;
1100 struct au0828_dev *dev = fh->dev;
1101 int rc;
1102
1103 rc = check_dev(dev);
1104 if (rc < 0)
1105 return rc;
1106
62899a28 1107 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
7f8eacd2
DH
1108 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1109 return -EBUSY;
8b2f0795
DH
1110
1111 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1112 filp->f_flags & O_NONBLOCK);
1113 }
7f8eacd2
DH
1114
1115 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1116 if (!res_get(fh, AU0828_RESOURCE_VBI))
1117 return -EBUSY;
1118
bf797165
DH
1119 if (dev->vbi_timeout_running == 0) {
1120 /* Handle case where caller tries to read without
1121 calling streamon first */
1122 dev->vbi_timeout_running = 1;
1123 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1124 }
1125
7f8eacd2
DH
1126 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1127 filp->f_flags & O_NONBLOCK);
1128 }
1129
8b2f0795
DH
1130 return 0;
1131}
1132
1133static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1134{
1135 struct au0828_fh *fh = filp->private_data;
1136 struct au0828_dev *dev = fh->dev;
83b09422
HV
1137 unsigned long req_events = poll_requested_events(wait);
1138 unsigned int res;
8b2f0795 1139
83b09422
HV
1140 if (check_dev(dev) < 0)
1141 return POLLERR;
1142
1143 res = v4l2_ctrl_poll(filp, wait);
1144 if (!(req_events & (POLLIN | POLLRDNORM)))
1145 return res;
8b2f0795 1146
7f8eacd2
DH
1147 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1148 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1149 return POLLERR;
83b09422
HV
1150 return res | videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1151 }
1152 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
7f8eacd2
DH
1153 if (!res_get(fh, AU0828_RESOURCE_VBI))
1154 return POLLERR;
83b09422 1155 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
7f8eacd2 1156 }
83b09422 1157 return POLLERR;
8b2f0795
DH
1158}
1159
1160static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1161{
1162 struct au0828_fh *fh = filp->private_data;
1163 struct au0828_dev *dev = fh->dev;
1164 int rc;
1165
1166 rc = check_dev(dev);
1167 if (rc < 0)
1168 return rc;
1169
7f8eacd2
DH
1170 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1171 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1172 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1173 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
8b2f0795 1174
8b2f0795
DH
1175 return rc;
1176}
1177
1178static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1179 struct v4l2_format *format)
1180{
1181 int ret;
1182 int width = format->fmt.pix.width;
1183 int height = format->fmt.pix.height;
8b2f0795 1184
8b2f0795
DH
1185 /* If they are demanding a format other than the one we support,
1186 bail out (tvtime asks for UYVY and then retries with YUYV) */
62899a28 1187 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
8b2f0795 1188 return -EINVAL;
8b2f0795
DH
1189
1190 /* format->fmt.pix.width only support 720 and height 480 */
62899a28 1191 if (width != 720)
8b2f0795 1192 width = 720;
62899a28 1193 if (height != 480)
8b2f0795
DH
1194 height = 480;
1195
1196 format->fmt.pix.width = width;
1197 format->fmt.pix.height = height;
1198 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1199 format->fmt.pix.bytesperline = width * 2;
1200 format->fmt.pix.sizeimage = width * height * 2;
1201 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1202 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
8d86e4e8 1203 format->fmt.pix.priv = 0;
8b2f0795 1204
62899a28 1205 if (cmd == VIDIOC_TRY_FMT)
8b2f0795
DH
1206 return 0;
1207
1208 /* maybe set new image format, driver current only support 720*480 */
1209 dev->width = width;
1210 dev->height = height;
1211 dev->frame_size = width * height * 2;
1212 dev->field_size = width * height;
1213 dev->bytesperline = width * 2;
1214
62899a28 1215 if (dev->stream_state == STREAM_ON) {
8b2f0795 1216 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
62899a28
DH
1217 ret = au0828_stream_interrupt(dev);
1218 if (ret != 0) {
8b2f0795
DH
1219 dprintk(1, "error interrupting video stream!\n");
1220 return ret;
1221 }
1222 }
1223
1224 /* set au0828 interface0 to AS5 here again */
1225 ret = usb_set_interface(dev->usbdev, 0, 5);
62899a28
DH
1226 if (ret < 0) {
1227 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
8b2f0795
DH
1228 return -EBUSY;
1229 }
1230
1231 au0828_analog_stream_enable(dev);
1232
1233 return 0;
1234}
1235
1236
8b2f0795
DH
1237static int vidioc_querycap(struct file *file, void *priv,
1238 struct v4l2_capability *cap)
1239{
59e05484
HV
1240 struct video_device *vdev = video_devdata(file);
1241 struct au0828_fh *fh = priv;
8b2f0795
DH
1242 struct au0828_dev *dev = fh->dev;
1243
8b2f0795 1244 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
f1add5b5 1245 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
59e05484 1246 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
8b2f0795 1247
59e05484
HV
1248 /* set the device capabilities */
1249 cap->device_caps = V4L2_CAP_AUDIO |
8b2f0795
DH
1250 V4L2_CAP_READWRITE |
1251 V4L2_CAP_STREAMING |
1252 V4L2_CAP_TUNER;
59e05484
HV
1253 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1254 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1255 else
1256 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1257 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1258 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
8b2f0795
DH
1259 return 0;
1260}
1261
1262static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1263 struct v4l2_fmtdesc *f)
1264{
62899a28 1265 if (f->index)
8b2f0795
DH
1266 return -EINVAL;
1267
8b2f0795
DH
1268 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1269 strcpy(f->description, "Packed YUV2");
1270
1271 f->flags = 0;
1272 f->pixelformat = V4L2_PIX_FMT_UYVY;
1273
8b2f0795
DH
1274 return 0;
1275}
1276
1277static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1278 struct v4l2_format *f)
1279{
1280 struct au0828_fh *fh = priv;
1281 struct au0828_dev *dev = fh->dev;
1282
1283 f->fmt.pix.width = dev->width;
1284 f->fmt.pix.height = dev->height;
1285 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1286 f->fmt.pix.bytesperline = dev->bytesperline;
1287 f->fmt.pix.sizeimage = dev->frame_size;
1288 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1289 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
8d86e4e8 1290 f->fmt.pix.priv = 0;
8b2f0795
DH
1291 return 0;
1292}
1293
1294static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1295 struct v4l2_format *f)
1296{
1297 struct au0828_fh *fh = priv;
1298 struct au0828_dev *dev = fh->dev;
1299
1300 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1301}
1302
1303static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1304 struct v4l2_format *f)
1305{
1306 struct au0828_fh *fh = priv;
1307 struct au0828_dev *dev = fh->dev;
1308 int rc;
1309
7f8eacd2
DH
1310 rc = check_dev(dev);
1311 if (rc < 0)
1312 return rc;
1313
8b2f0795 1314 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
62899a28 1315 printk(KERN_INFO "%s queue busy\n", __func__);
8b2f0795
DH
1316 rc = -EBUSY;
1317 goto out;
1318 }
1319
7f8eacd2 1320 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
8b2f0795
DH
1321out:
1322 return rc;
1323}
1324
314527ac 1325static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
8b2f0795
DH
1326{
1327 struct au0828_fh *fh = priv;
1328 struct au0828_dev *dev = fh->dev;
1329
e58071f0
DH
1330 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1331 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);
1332
8b2f0795
DH
1333 /* FIXME: when we support something other than NTSC, we are going to
1334 have to make the au0828 bridge adjust the size of its capture
1335 buffer, which is currently hardcoded at 720x480 */
1336
314527ac 1337 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, norm);
c98bc032 1338 dev->std_set_in_tuner_core = 1;
e58071f0
DH
1339
1340 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1341 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);
1342
8b2f0795
DH
1343 return 0;
1344}
1345
1346static int vidioc_enum_input(struct file *file, void *priv,
1347 struct v4l2_input *input)
1348{
1349 struct au0828_fh *fh = priv;
1350 struct au0828_dev *dev = fh->dev;
1351 unsigned int tmp;
1352
1353 static const char *inames[] = {
3d62287e 1354 [AU0828_VMUX_UNDEFINED] = "Undefined",
8b2f0795
DH
1355 [AU0828_VMUX_COMPOSITE] = "Composite",
1356 [AU0828_VMUX_SVIDEO] = "S-Video",
1357 [AU0828_VMUX_CABLE] = "Cable TV",
1358 [AU0828_VMUX_TELEVISION] = "Television",
1359 [AU0828_VMUX_DVB] = "DVB",
1360 [AU0828_VMUX_DEBUG] = "tv debug"
1361 };
1362
1363 tmp = input->index;
1364
f5e20c34 1365 if (tmp >= AU0828_MAX_INPUT)
8b2f0795 1366 return -EINVAL;
62899a28 1367 if (AUVI_INPUT(tmp).type == 0)
8b2f0795
DH
1368 return -EINVAL;
1369
8b2f0795 1370 input->index = tmp;
f1add5b5 1371 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
62899a28 1372 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
a2cf96f9 1373 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
8b2f0795 1374 input->type |= V4L2_INPUT_TYPE_TUNER;
a2cf96f9
HV
1375 input->audioset = 1;
1376 } else {
8b2f0795 1377 input->type |= V4L2_INPUT_TYPE_CAMERA;
a2cf96f9
HV
1378 input->audioset = 2;
1379 }
8b2f0795
DH
1380
1381 input->std = dev->vdev->tvnorms;
1382
1383 return 0;
1384}
1385
1386static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1387{
1388 struct au0828_fh *fh = priv;
1389 struct au0828_dev *dev = fh->dev;
1390 *i = dev->ctrl_input;
1391 return 0;
1392}
1393
1394static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1395{
1396 struct au0828_fh *fh = priv;
1397 struct au0828_dev *dev = fh->dev;
1398 int i;
8b2f0795 1399
62899a28 1400 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
8b2f0795 1401 index);
62899a28 1402 if (index >= AU0828_MAX_INPUT)
8b2f0795 1403 return -EINVAL;
62899a28 1404 if (AUVI_INPUT(index).type == 0)
8b2f0795
DH
1405 return -EINVAL;
1406 dev->ctrl_input = index;
1407
62899a28 1408 switch (AUVI_INPUT(index).type) {
8b2f0795 1409 case AU0828_VMUX_SVIDEO:
8b2f0795 1410 dev->input_type = AU0828_VMUX_SVIDEO;
a2cf96f9 1411 dev->ctrl_ainput = 1;
8b2f0795 1412 break;
8b2f0795 1413 case AU0828_VMUX_COMPOSITE:
8b2f0795 1414 dev->input_type = AU0828_VMUX_COMPOSITE;
a2cf96f9 1415 dev->ctrl_ainput = 1;
8b2f0795 1416 break;
8b2f0795 1417 case AU0828_VMUX_TELEVISION:
8b2f0795 1418 dev->input_type = AU0828_VMUX_TELEVISION;
a2cf96f9 1419 dev->ctrl_ainput = 0;
8b2f0795 1420 break;
8b2f0795 1421 default:
a1094c4c
DH
1422 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1423 AUVI_INPUT(index).type);
1424 break;
8b2f0795
DH
1425 }
1426
5325b427
HV
1427 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1428 AUVI_INPUT(index).vmux, 0, 0);
8b2f0795
DH
1429
1430 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1431 int enable = 0;
62899a28 1432 if (AUVI_INPUT(i).audio_setup == NULL)
8b2f0795 1433 continue;
8b2f0795
DH
1434
1435 if (i == index)
1436 enable = 1;
1437 else
1438 enable = 0;
1439 if (enable) {
f1add5b5 1440 (AUVI_INPUT(i).audio_setup)(dev, enable);
8b2f0795
DH
1441 } else {
1442 /* Make sure we leave it turned on if some
1443 other input is routed to this callback */
f1add5b5
DH
1444 if ((AUVI_INPUT(i).audio_setup) !=
1445 ((AUVI_INPUT(index).audio_setup))) {
1446 (AUVI_INPUT(i).audio_setup)(dev, enable);
8b2f0795
DH
1447 }
1448 }
1449 }
1450
5325b427
HV
1451 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1452 AUVI_INPUT(index).amux, 0, 0);
8b2f0795
DH
1453 return 0;
1454}
1455
a2cf96f9
HV
1456static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1457{
1458 if (a->index > 1)
1459 return -EINVAL;
1460
1461 if (a->index == 0)
1462 strcpy(a->name, "Television");
1463 else
1464 strcpy(a->name, "Line in");
1465
1466 a->capability = V4L2_AUDCAP_STEREO;
1467 return 0;
1468}
1469
8b2f0795
DH
1470static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1471{
1472 struct au0828_fh *fh = priv;
1473 struct au0828_dev *dev = fh->dev;
8b2f0795 1474
a2cf96f9
HV
1475 a->index = dev->ctrl_ainput;
1476 if (a->index == 0)
8b2f0795
DH
1477 strcpy(a->name, "Television");
1478 else
1479 strcpy(a->name, "Line in");
1480
1481 a->capability = V4L2_AUDCAP_STEREO;
8b2f0795
DH
1482 return 0;
1483}
1484
0e8025b9 1485static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
8b2f0795
DH
1486{
1487 struct au0828_fh *fh = priv;
1488 struct au0828_dev *dev = fh->dev;
a2cf96f9 1489
62899a28 1490 if (a->index != dev->ctrl_ainput)
8b2f0795
DH
1491 return -EINVAL;
1492 return 0;
1493}
1494
8b2f0795
DH
1495static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1496{
1497 struct au0828_fh *fh = priv;
1498 struct au0828_dev *dev = fh->dev;
1499
62899a28 1500 if (t->index != 0)
8b2f0795
DH
1501 return -EINVAL;
1502
8b2f0795 1503 strcpy(t->name, "Auvitek tuner");
2689d3dc 1504 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
8b2f0795
DH
1505 return 0;
1506}
1507
1508static int vidioc_s_tuner(struct file *file, void *priv,
2f73c7c5 1509 const struct v4l2_tuner *t)
8b2f0795
DH
1510{
1511 struct au0828_fh *fh = priv;
1512 struct au0828_dev *dev = fh->dev;
1513
62899a28 1514 if (t->index != 0)
8b2f0795
DH
1515 return -EINVAL;
1516
e58071f0
DH
1517 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1518 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);
1519
2689d3dc 1520 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
e58071f0
DH
1521
1522 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1523 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);
1524
8b2f0795
DH
1525 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1526 t->afc);
e58071f0 1527
8b2f0795
DH
1528 return 0;
1529
1530}
1531
1532static int vidioc_g_frequency(struct file *file, void *priv,
1533 struct v4l2_frequency *freq)
1534{
1535 struct au0828_fh *fh = priv;
1536 struct au0828_dev *dev = fh->dev;
c888923d 1537
e1cf6587
HV
1538 if (freq->tuner != 0)
1539 return -EINVAL;
8b2f0795
DH
1540 freq->frequency = dev->ctrl_freq;
1541 return 0;
1542}
1543
1544static int vidioc_s_frequency(struct file *file, void *priv,
b530a447 1545 const struct v4l2_frequency *freq)
8b2f0795
DH
1546{
1547 struct au0828_fh *fh = priv;
1548 struct au0828_dev *dev = fh->dev;
e1cf6587 1549 struct v4l2_frequency new_freq = *freq;
8b2f0795 1550
62899a28 1551 if (freq->tuner != 0)
8b2f0795 1552 return -EINVAL;
8b2f0795 1553
4a03dafc
DH
1554 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1555 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);
1556
c98bc032 1557 if (dev->std_set_in_tuner_core == 0) {
38823b29
HV
1558 /* If we've never sent the standard in tuner core, do so now.
1559 We don't do this at device probe because we don't want to
1560 incur the cost of a firmware load */
1561 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
1562 dev->vdev->tvnorms);
1563 dev->std_set_in_tuner_core = 1;
c98bc032
DH
1564 }
1565
2689d3dc 1566 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
e1cf6587
HV
1567 /* Get the actual set (and possibly clamped) frequency */
1568 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1569 dev->ctrl_freq = new_freq.frequency;
8b2f0795 1570
4a03dafc
DH
1571 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1572 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);
1573
8b2f0795
DH
1574 au0828_analog_stream_reset(dev);
1575
1576 return 0;
1577}
1578
7f8eacd2
DH
1579
1580/* RAW VBI ioctls */
1581
1582static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1583 struct v4l2_format *format)
1584{
1585 struct au0828_fh *fh = priv;
1586 struct au0828_dev *dev = fh->dev;
1587
1588 format->fmt.vbi.samples_per_line = dev->vbi_width;
1589 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1590 format->fmt.vbi.offset = 0;
1591 format->fmt.vbi.flags = 0;
1592 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1593
1594 format->fmt.vbi.count[0] = dev->vbi_height;
1595 format->fmt.vbi.count[1] = dev->vbi_height;
1596 format->fmt.vbi.start[0] = 21;
1597 format->fmt.vbi.start[1] = 284;
8d86e4e8 1598 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
7f8eacd2
DH
1599
1600 return 0;
1601}
1602
8b2f0795
DH
1603static int vidioc_g_chip_ident(struct file *file, void *priv,
1604 struct v4l2_dbg_chip_ident *chip)
1605{
1606 struct au0828_fh *fh = priv;
1607 struct au0828_dev *dev = fh->dev;
1608 chip->ident = V4L2_IDENT_NONE;
1609 chip->revision = 0;
1610
d9109bef
DH
1611 if (v4l2_chip_match_host(&chip->match)) {
1612 chip->ident = V4L2_IDENT_AU0828;
1613 return 0;
1614 }
1615
2689d3dc 1616 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
d9109bef
DH
1617 if (chip->ident == V4L2_IDENT_NONE)
1618 return -EINVAL;
1619
8b2f0795
DH
1620 return 0;
1621}
1622
1623static int vidioc_cropcap(struct file *file, void *priv,
1624 struct v4l2_cropcap *cc)
1625{
1626 struct au0828_fh *fh = priv;
1627 struct au0828_dev *dev = fh->dev;
1628
62899a28 1629 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
8b2f0795
DH
1630 return -EINVAL;
1631
1632 cc->bounds.left = 0;
1633 cc->bounds.top = 0;
1634 cc->bounds.width = dev->width;
1635 cc->bounds.height = dev->height;
1636
1637 cc->defrect = cc->bounds;
1638
1639 cc->pixelaspect.numerator = 54;
1640 cc->pixelaspect.denominator = 59;
1641
1642 return 0;
1643}
1644
1645static int vidioc_streamon(struct file *file, void *priv,
1646 enum v4l2_buf_type type)
1647{
7f8eacd2
DH
1648 struct au0828_fh *fh = priv;
1649 struct au0828_dev *dev = fh->dev;
1650 int rc = -EINVAL;
8b2f0795
DH
1651
1652 rc = check_dev(dev);
1653 if (rc < 0)
1654 return rc;
1655
7f8eacd2
DH
1656 if (unlikely(type != fh->type))
1657 return -EINVAL;
1658
1659 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1660 fh, type, fh->resources, dev->resources);
1661
1662 if (unlikely(!res_get(fh, get_ressource(fh))))
1663 return -EBUSY;
1664
8b2f0795
DH
1665 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1666 au0828_analog_stream_enable(dev);
2689d3dc 1667 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
8b2f0795
DH
1668 }
1669
78ca5005 1670 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
8b2f0795 1671 rc = videobuf_streamon(&fh->vb_vidq);
78ca5005
DH
1672 dev->vid_timeout_running = 1;
1673 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1674 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
7f8eacd2 1675 rc = videobuf_streamon(&fh->vb_vbiq);
78ca5005
DH
1676 dev->vbi_timeout_running = 1;
1677 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1678 }
8b2f0795
DH
1679
1680 return rc;
1681}
1682
1683static int vidioc_streamoff(struct file *file, void *priv,
1684 enum v4l2_buf_type type)
1685{
7f8eacd2
DH
1686 struct au0828_fh *fh = priv;
1687 struct au0828_dev *dev = fh->dev;
1688 int rc;
1689 int i;
8b2f0795
DH
1690
1691 rc = check_dev(dev);
1692 if (rc < 0)
1693 return rc;
1694
7f8eacd2
DH
1695 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1696 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
8b2f0795
DH
1697 return -EINVAL;
1698 if (type != fh->type)
1699 return -EINVAL;
1700
7f8eacd2
DH
1701 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1702 fh, type, fh->resources, dev->resources);
1703
1704 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
78ca5005
DH
1705 dev->vid_timeout_running = 0;
1706 del_timer_sync(&dev->vid_timeout);
1707
2689d3dc 1708 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
7f8eacd2
DH
1709 rc = au0828_stream_interrupt(dev);
1710 if (rc != 0)
1711 return rc;
8b2f0795 1712
7f8eacd2
DH
1713 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1714 if (AUVI_INPUT(i).audio_setup == NULL)
1715 continue;
1716 (AUVI_INPUT(i).audio_setup)(dev, 0);
1717 }
8b2f0795 1718
a595c1ce
DH
1719 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1720 videobuf_streamoff(&fh->vb_vidq);
1721 res_free(fh, AU0828_RESOURCE_VIDEO);
1722 }
7f8eacd2 1723 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
78ca5005
DH
1724 dev->vbi_timeout_running = 0;
1725 del_timer_sync(&dev->vbi_timeout);
1726
a595c1ce
DH
1727 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1728 videobuf_streamoff(&fh->vb_vbiq);
1729 res_free(fh, AU0828_RESOURCE_VBI);
1730 }
7f8eacd2 1731 }
8b2f0795
DH
1732
1733 return 0;
1734}
1735
80c6e358 1736#ifdef CONFIG_VIDEO_ADV_DEBUG
8b2f0795
DH
1737static int vidioc_g_register(struct file *file, void *priv,
1738 struct v4l2_dbg_register *reg)
1739{
1740 struct au0828_fh *fh = priv;
1741 struct au0828_dev *dev = fh->dev;
1742
1743 switch (reg->match.type) {
1744 case V4L2_CHIP_MATCH_I2C_DRIVER:
2689d3dc 1745 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
8b2f0795
DH
1746 return 0;
1747 default:
364d2db2
DH
1748 if (!v4l2_chip_match_host(&reg->match))
1749 return -EINVAL;
8b2f0795 1750 }
364d2db2
DH
1751
1752 reg->val = au0828_read(dev, reg->reg);
1753 return 0;
8b2f0795
DH
1754}
1755
1756static int vidioc_s_register(struct file *file, void *priv,
977ba3b1 1757 const struct v4l2_dbg_register *reg)
8b2f0795
DH
1758{
1759 struct au0828_fh *fh = priv;
1760 struct au0828_dev *dev = fh->dev;
1761
1762 switch (reg->match.type) {
1763 case V4L2_CHIP_MATCH_I2C_DRIVER:
2689d3dc 1764 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
8b2f0795
DH
1765 return 0;
1766 default:
364d2db2
DH
1767 if (!v4l2_chip_match_host(&reg->match))
1768 return -EINVAL;
8b2f0795 1769 }
364d2db2 1770 return au0828_writereg(dev, reg->reg, reg->val);
8b2f0795 1771}
80c6e358 1772#endif
8b2f0795 1773
83b09422
HV
1774static int vidioc_log_status(struct file *file, void *fh)
1775{
1776 struct video_device *vdev = video_devdata(file);
1777
1778 v4l2_ctrl_log_status(file, fh);
1779 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1780 return 0;
1781}
1782
8b2f0795
DH
1783static int vidioc_reqbufs(struct file *file, void *priv,
1784 struct v4l2_requestbuffers *rb)
1785{
1786 struct au0828_fh *fh = priv;
1787 struct au0828_dev *dev = fh->dev;
1788 int rc;
1789
1790 rc = check_dev(dev);
1791 if (rc < 0)
1792 return rc;
1793
54ebb8b8
DH
1794 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1795 rc = videobuf_reqbufs(&fh->vb_vidq, rb);
1796 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1797 rc = videobuf_reqbufs(&fh->vb_vbiq, rb);
1798
1799 return rc;
8b2f0795
DH
1800}
1801
1802static int vidioc_querybuf(struct file *file, void *priv,
1803 struct v4l2_buffer *b)
1804{
1805 struct au0828_fh *fh = priv;
1806 struct au0828_dev *dev = fh->dev;
1807 int rc;
1808
1809 rc = check_dev(dev);
1810 if (rc < 0)
1811 return rc;
1812
54ebb8b8
DH
1813 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1814 rc = videobuf_querybuf(&fh->vb_vidq, b);
1815 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1816 rc = videobuf_querybuf(&fh->vb_vbiq, b);
1817
1818 return rc;
8b2f0795
DH
1819}
1820
1821static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1822{
1823 struct au0828_fh *fh = priv;
1824 struct au0828_dev *dev = fh->dev;
1825 int rc;
1826
1827 rc = check_dev(dev);
1828 if (rc < 0)
1829 return rc;
1830
54ebb8b8
DH
1831 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1832 rc = videobuf_qbuf(&fh->vb_vidq, b);
1833 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1834 rc = videobuf_qbuf(&fh->vb_vbiq, b);
1835
1836 return rc;
8b2f0795
DH
1837}
1838
1839static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1840{
1841 struct au0828_fh *fh = priv;
1842 struct au0828_dev *dev = fh->dev;
1843 int rc;
1844
1845 rc = check_dev(dev);
1846 if (rc < 0)
1847 return rc;
1848
1849 /* Workaround for a bug in the au0828 hardware design that sometimes
1850 results in the colorspace being inverted */
1851 if (dev->greenscreen_detected == 1) {
1852 dprintk(1, "Detected green frame. Resetting stream...\n");
1853 au0828_analog_stream_reset(dev);
1854 dev->greenscreen_detected = 0;
1855 }
1856
54ebb8b8
DH
1857 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1858 rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1859 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1860 rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK);
1861
1862 return rc;
8b2f0795
DH
1863}
1864
8b2f0795
DH
1865static struct v4l2_file_operations au0828_v4l_fops = {
1866 .owner = THIS_MODULE,
1867 .open = au0828_v4l2_open,
1868 .release = au0828_v4l2_close,
1869 .read = au0828_v4l2_read,
1870 .poll = au0828_v4l2_poll,
1871 .mmap = au0828_v4l2_mmap,
549ee4df 1872 .unlocked_ioctl = video_ioctl2,
8b2f0795
DH
1873};
1874
1875static const struct v4l2_ioctl_ops video_ioctl_ops = {
1876 .vidioc_querycap = vidioc_querycap,
1877 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1878 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1879 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1880 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
5a5a4e16 1881 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
8d86e4e8 1882 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
7f8eacd2 1883 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
a2cf96f9 1884 .vidioc_enumaudio = vidioc_enumaudio,
8b2f0795
DH
1885 .vidioc_g_audio = vidioc_g_audio,
1886 .vidioc_s_audio = vidioc_s_audio,
1887 .vidioc_cropcap = vidioc_cropcap,
8b2f0795
DH
1888 .vidioc_reqbufs = vidioc_reqbufs,
1889 .vidioc_querybuf = vidioc_querybuf,
1890 .vidioc_qbuf = vidioc_qbuf,
1891 .vidioc_dqbuf = vidioc_dqbuf,
1892 .vidioc_s_std = vidioc_s_std,
1893 .vidioc_enum_input = vidioc_enum_input,
1894 .vidioc_g_input = vidioc_g_input,
1895 .vidioc_s_input = vidioc_s_input,
8b2f0795
DH
1896 .vidioc_streamon = vidioc_streamon,
1897 .vidioc_streamoff = vidioc_streamoff,
1898 .vidioc_g_tuner = vidioc_g_tuner,
1899 .vidioc_s_tuner = vidioc_s_tuner,
1900 .vidioc_g_frequency = vidioc_g_frequency,
1901 .vidioc_s_frequency = vidioc_s_frequency,
1902#ifdef CONFIG_VIDEO_ADV_DEBUG
1903 .vidioc_g_register = vidioc_g_register,
1904 .vidioc_s_register = vidioc_s_register,
8b2f0795 1905#endif
80c6e358 1906 .vidioc_g_chip_ident = vidioc_g_chip_ident,
83b09422
HV
1907 .vidioc_log_status = vidioc_log_status,
1908 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1909 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
8b2f0795
DH
1910};
1911
1912static const struct video_device au0828_video_template = {
1913 .fops = &au0828_v4l_fops,
1914 .release = video_device_release,
1915 .ioctl_ops = &video_ioctl_ops,
0ef21071
DH
1916 .tvnorms = V4L2_STD_NTSC_M,
1917 .current_norm = V4L2_STD_NTSC_M,
8b2f0795
DH
1918};
1919
1920/**************************************************************************/
1921
fc4ce6cd
DH
1922int au0828_analog_register(struct au0828_dev *dev,
1923 struct usb_interface *interface)
8b2f0795
DH
1924{
1925 int retval = -ENOMEM;
fc4ce6cd
DH
1926 struct usb_host_interface *iface_desc;
1927 struct usb_endpoint_descriptor *endpoint;
d63b21bf 1928 int i, ret;
8b2f0795
DH
1929
1930 dprintk(1, "au0828_analog_register called!\n");
1931
fc4ce6cd
DH
1932 /* set au0828 usb interface0 to as5 */
1933 retval = usb_set_interface(dev->usbdev,
62899a28 1934 interface->cur_altsetting->desc.bInterfaceNumber, 5);
fc4ce6cd 1935 if (retval != 0) {
62899a28 1936 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
fc4ce6cd
DH
1937 return retval;
1938 }
1939
1940 /* Figure out which endpoint has the isoc interface */
1941 iface_desc = interface->cur_altsetting;
62899a28 1942 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
fc4ce6cd 1943 endpoint = &iface_desc->endpoint[i].desc;
62899a28
DH
1944 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1945 == USB_DIR_IN) &&
1946 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1947 == USB_ENDPOINT_XFER_ISOC)) {
fc4ce6cd
DH
1948
1949 /* we find our isoc in endpoint */
1950 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
62899a28
DH
1951 dev->max_pkt_size = (tmp & 0x07ff) *
1952 (((tmp & 0x1800) >> 11) + 1);
fc4ce6cd
DH
1953 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1954 }
1955 }
62899a28
DH
1956 if (!(dev->isoc_in_endpointaddr)) {
1957 printk(KERN_INFO "Could not locate isoc endpoint\n");
fc4ce6cd
DH
1958 kfree(dev);
1959 return -ENODEV;
1960 }
1961
8b2f0795
DH
1962 init_waitqueue_head(&dev->open);
1963 spin_lock_init(&dev->slock);
8b2f0795 1964
7f8eacd2 1965 /* init video dma queues */
8b2f0795
DH
1966 INIT_LIST_HEAD(&dev->vidq.active);
1967 INIT_LIST_HEAD(&dev->vidq.queued);
7f8eacd2
DH
1968 INIT_LIST_HEAD(&dev->vbiq.active);
1969 INIT_LIST_HEAD(&dev->vbiq.queued);
8b2f0795 1970
78ca5005
DH
1971 dev->vid_timeout.function = au0828_vid_buffer_timeout;
1972 dev->vid_timeout.data = (unsigned long) dev;
1973 init_timer(&dev->vid_timeout);
1974
1975 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
1976 dev->vbi_timeout.data = (unsigned long) dev;
1977 init_timer(&dev->vbi_timeout);
1978
8b2f0795
DH
1979 dev->width = NTSC_STD_W;
1980 dev->height = NTSC_STD_H;
1981 dev->field_size = dev->width * dev->height;
1982 dev->frame_size = dev->field_size << 1;
1983 dev->bytesperline = dev->width << 1;
1984 dev->ctrl_ainput = 0;
e1cf6587 1985 dev->ctrl_freq = 960;
8b2f0795
DH
1986
1987 /* allocate and fill v4l2 video struct */
1988 dev->vdev = video_device_alloc();
62899a28 1989 if (NULL == dev->vdev) {
8b2f0795
DH
1990 dprintk(1, "Can't allocate video_device.\n");
1991 return -ENOMEM;
1992 }
1993
7f8eacd2 1994 /* allocate the VBI struct */
8b2f0795 1995 dev->vbi_dev = video_device_alloc();
62899a28 1996 if (NULL == dev->vbi_dev) {
8b2f0795 1997 dprintk(1, "Can't allocate vbi_device.\n");
d63b21bf
JL
1998 ret = -ENOMEM;
1999 goto err_vdev;
8b2f0795
DH
2000 }
2001
2002 /* Fill the video capture device struct */
2003 *dev->vdev = au0828_video_template;
e8c26f45 2004 dev->vdev->v4l2_dev = &dev->v4l2_dev;
549ee4df 2005 dev->vdev->lock = &dev->lock;
83b09422 2006 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev->flags);
8b2f0795
DH
2007 strcpy(dev->vdev->name, "au0828a video");
2008
2009 /* Setup the VBI device */
2010 *dev->vbi_dev = au0828_video_template;
e8c26f45 2011 dev->vbi_dev->v4l2_dev = &dev->v4l2_dev;
549ee4df 2012 dev->vbi_dev->lock = &dev->lock;
83b09422 2013 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vbi_dev->flags);
8b2f0795
DH
2014 strcpy(dev->vbi_dev->name, "au0828a vbi");
2015
8b2f0795 2016 /* Register the v4l2 device */
63b0d5ad 2017 video_set_drvdata(dev->vdev, dev);
62899a28
DH
2018 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
2019 if (retval != 0) {
2020 dprintk(1, "unable to register video device (error = %d).\n",
2021 retval);
d63b21bf
JL
2022 ret = -ENODEV;
2023 goto err_vbi_dev;
8b2f0795
DH
2024 }
2025
2026 /* Register the vbi device */
63b0d5ad 2027 video_set_drvdata(dev->vbi_dev, dev);
62899a28
DH
2028 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
2029 if (retval != 0) {
2030 dprintk(1, "unable to register vbi device (error = %d).\n",
2031 retval);
d63b21bf
JL
2032 ret = -ENODEV;
2033 goto err_vbi_dev;
8b2f0795
DH
2034 }
2035
62899a28 2036 dprintk(1, "%s completed!\n", __func__);
8b2f0795
DH
2037
2038 return 0;
d63b21bf
JL
2039
2040err_vbi_dev:
2041 video_device_release(dev->vbi_dev);
2042err_vdev:
2043 video_device_release(dev->vdev);
2044 return ret;
8b2f0795
DH
2045}
2046