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