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