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