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