[media] uapi/media.h: Rename entities types to functions
[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
8b2f0795
DH
221 dev->isoc_ctl.isoc_copy = isoc_copy;
222 dev->isoc_ctl.num_bufs = num_bufs;
223
224 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
225 if (!dev->isoc_ctl.urb) {
226 au0828_isocdbg("cannot alloc memory for usb buffers\n");
227 return -ENOMEM;
228 }
229
230 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
231 GFP_KERNEL);
232 if (!dev->isoc_ctl.transfer_buffer) {
233 au0828_isocdbg("cannot allocate memory for usb transfer\n");
234 kfree(dev->isoc_ctl.urb);
235 return -ENOMEM;
236 }
237
238 dev->isoc_ctl.max_pkt_size = max_pkt_size;
239 dev->isoc_ctl.buf = NULL;
240
241 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
242
243 /* allocate urbs and transfer buffers */
244 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
245 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
246 if (!urb) {
247 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
248 au0828_uninit_isoc(dev);
249 return -ENOMEM;
250 }
251 dev->isoc_ctl.urb[i] = urb;
252
997ea58e 253 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
8b2f0795
DH
254 sb_size, GFP_KERNEL, &urb->transfer_dma);
255 if (!dev->isoc_ctl.transfer_buffer[i]) {
256 printk("unable to allocate %i bytes for transfer"
257 " buffer %i%s\n",
258 sb_size, i,
259 in_interrupt() ? " while in int" : "");
260 au0828_uninit_isoc(dev);
261 return -ENOMEM;
262 }
263 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
264
265 pipe = usb_rcvisocpipe(dev->usbdev,
266 dev->isoc_in_endpointaddr),
267
268 usb_fill_int_urb(urb, dev->usbdev, pipe,
269 dev->isoc_ctl.transfer_buffer[i], sb_size,
270 au0828_irq_callback, dma_q, 1);
271
272 urb->number_of_packets = max_packets;
fadadb7d 273 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
8b2f0795
DH
274
275 k = 0;
276 for (j = 0; j < max_packets; j++) {
277 urb->iso_frame_desc[j].offset = k;
278 urb->iso_frame_desc[j].length =
279 dev->isoc_ctl.max_pkt_size;
280 k += dev->isoc_ctl.max_pkt_size;
281 }
282 }
283
8b2f0795
DH
284 /* submit urbs and enables IRQ */
285 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
286 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
287 if (rc) {
288 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
289 i, rc);
290 au0828_uninit_isoc(dev);
291 return rc;
292 }
293 }
294
295 return 0;
296}
297
298/*
299 * Announces that a buffer were filled and request the next
300 */
301static inline void buffer_filled(struct au0828_dev *dev,
c5036d61
LP
302 struct au0828_dmaqueue *dma_q,
303 struct au0828_buffer *buf)
8b2f0795 304{
2d700715
JS
305 struct vb2_v4l2_buffer *vb = &buf->vb;
306 struct vb2_queue *q = vb->vb2_buf.vb2_queue;
8b2f0795 307
7f8eacd2 308 /* Advice that buffer was filled */
05439b1a 309 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
7f8eacd2 310
c5036d61 311 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2d700715 312 vb->sequence = dev->frame_count++;
c5036d61 313 else
2d700715 314 vb->sequence = dev->vbi_frame_count++;
c5036d61 315
2d700715 316 vb->field = V4L2_FIELD_INTERLACED;
d6dd645e 317 vb->vb2_buf.timestamp = ktime_get_ns();
2d700715 318 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
7f8eacd2
DH
319}
320
8b2f0795
DH
321/*
322 * Identify the buffer header type and properly handles
323 */
324static void au0828_copy_video(struct au0828_dev *dev,
325 struct au0828_dmaqueue *dma_q,
326 struct au0828_buffer *buf,
327 unsigned char *p,
328 unsigned char *outp, unsigned long len)
329{
330 void *fieldstart, *startwrite, *startread;
331 int linesdone, currlinedone, offset, lencopy, remain;
332 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
333
7f8eacd2
DH
334 if (len == 0)
335 return;
336
05439b1a
SK
337 if (dma_q->pos + len > buf->length)
338 len = buf->length - dma_q->pos;
8b2f0795
DH
339
340 startread = p;
341 remain = len;
342
343 /* Interlaces frame */
344 if (buf->top_field)
345 fieldstart = outp;
346 else
347 fieldstart = outp + bytesperline;
348
349 linesdone = dma_q->pos / bytesperline;
350 currlinedone = dma_q->pos % bytesperline;
351 offset = linesdone * bytesperline * 2 + currlinedone;
352 startwrite = fieldstart + offset;
353 lencopy = bytesperline - currlinedone;
354 lencopy = lencopy > remain ? remain : lencopy;
355
05439b1a 356 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
8b2f0795
DH
357 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
358 ((char *)startwrite + lencopy) -
05439b1a
SK
359 ((char *)outp + buf->length));
360 remain = (char *)outp + buf->length - (char *)startwrite;
8b2f0795
DH
361 lencopy = remain;
362 }
363 if (lencopy <= 0)
364 return;
365 memcpy(startwrite, startread, lencopy);
366
367 remain -= lencopy;
368
369 while (remain > 0) {
370 startwrite += lencopy + bytesperline;
371 startread += lencopy;
372 if (bytesperline > remain)
373 lencopy = remain;
374 else
375 lencopy = bytesperline;
376
377 if ((char *)startwrite + lencopy > (char *)outp +
05439b1a 378 buf->length) {
62899a28 379 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
8b2f0795 380 ((char *)startwrite + lencopy) -
05439b1a
SK
381 ((char *)outp + buf->length));
382 lencopy = remain = (char *)outp + buf->length -
8b2f0795
DH
383 (char *)startwrite;
384 }
385 if (lencopy <= 0)
386 break;
387
388 memcpy(startwrite, startread, lencopy);
389
390 remain -= lencopy;
391 }
392
393 if (offset > 1440) {
394 /* We have enough data to check for greenscreen */
62899a28 395 if (outp[0] < 0x60 && outp[1440] < 0x60)
8b2f0795 396 dev->greenscreen_detected = 1;
8b2f0795
DH
397 }
398
399 dma_q->pos += len;
400}
401
402/*
403 * video-buf generic routine to get the next available buffer
404 */
405static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
406 struct au0828_buffer **buf)
407{
408 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
409
410 if (list_empty(&dma_q->active)) {
411 au0828_isocdbg("No active queue to serve\n");
412 dev->isoc_ctl.buf = NULL;
413 *buf = NULL;
414 return;
415 }
416
417 /* Get the next buffer */
05439b1a
SK
418 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
419 /* Cleans up buffer - Useful for testing for frame/URB loss */
420 list_del(&(*buf)->list);
421 dma_q->pos = 0;
422 (*buf)->vb_buf = (*buf)->mem;
8b2f0795
DH
423 dev->isoc_ctl.buf = *buf;
424
425 return;
426}
427
7f8eacd2
DH
428static void au0828_copy_vbi(struct au0828_dev *dev,
429 struct au0828_dmaqueue *dma_q,
430 struct au0828_buffer *buf,
431 unsigned char *p,
432 unsigned char *outp, unsigned long len)
433{
434 unsigned char *startwrite, *startread;
b5f5933a 435 int bytesperline;
7f8eacd2
DH
436 int i, j = 0;
437
438 if (dev == NULL) {
439 au0828_isocdbg("dev is null\n");
440 return;
441 }
442
443 if (dma_q == NULL) {
444 au0828_isocdbg("dma_q is null\n");
445 return;
446 }
447 if (buf == NULL)
448 return;
449 if (p == NULL) {
450 au0828_isocdbg("p is null\n");
451 return;
452 }
453 if (outp == NULL) {
454 au0828_isocdbg("outp is null\n");
455 return;
456 }
457
b5f5933a
DC
458 bytesperline = dev->vbi_width;
459
05439b1a
SK
460 if (dma_q->pos + len > buf->length)
461 len = buf->length - dma_q->pos;
7f8eacd2
DH
462
463 startread = p;
464 startwrite = outp + (dma_q->pos / 2);
465
466 /* Make sure the bottom field populates the second half of the frame */
467 if (buf->top_field == 0)
468 startwrite += bytesperline * dev->vbi_height;
469
470 for (i = 0; i < len; i += 2)
471 startwrite[j++] = startread[i+1];
472
473 dma_q->pos += len;
474}
475
476
477/*
478 * video-buf generic routine to get the next available VBI buffer
479 */
480static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
481 struct au0828_buffer **buf)
482{
483 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
7f8eacd2
DH
484
485 if (list_empty(&dma_q->active)) {
486 au0828_isocdbg("No active queue to serve\n");
487 dev->isoc_ctl.vbi_buf = NULL;
488 *buf = NULL;
489 return;
490 }
491
492 /* Get the next buffer */
05439b1a 493 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
25985edc 494 /* Cleans up buffer - Useful for testing for frame/URB loss */
05439b1a
SK
495 list_del(&(*buf)->list);
496 dma_q->pos = 0;
497 (*buf)->vb_buf = (*buf)->mem;
7f8eacd2 498 dev->isoc_ctl.vbi_buf = *buf;
7f8eacd2
DH
499 return;
500}
501
8b2f0795
DH
502/*
503 * Controls the isoc copy of each urb packet
504 */
505static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
506{
507 struct au0828_buffer *buf;
7f8eacd2 508 struct au0828_buffer *vbi_buf;
8b2f0795 509 struct au0828_dmaqueue *dma_q = urb->context;
7f8eacd2 510 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
8b2f0795 511 unsigned char *outp = NULL;
7f8eacd2 512 unsigned char *vbioutp = NULL;
8b2f0795
DH
513 int i, len = 0, rc = 1;
514 unsigned char *p;
515 unsigned char fbyte;
7f8eacd2
DH
516 unsigned int vbi_field_size;
517 unsigned int remain, lencopy;
8b2f0795
DH
518
519 if (!dev)
520 return 0;
521
522 if ((dev->dev_state & DEV_DISCONNECTED) ||
523 (dev->dev_state & DEV_MISCONFIGURED))
524 return 0;
525
526 if (urb->status < 0) {
527 print_err_status(dev, -1, urb->status);
528 if (urb->status == -ENOENT)
529 return 0;
530 }
531
532 buf = dev->isoc_ctl.buf;
533 if (buf != NULL)
2d700715 534 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
8b2f0795 535
7f8eacd2
DH
536 vbi_buf = dev->isoc_ctl.vbi_buf;
537 if (vbi_buf != NULL)
2d700715 538 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
7f8eacd2 539
8b2f0795
DH
540 for (i = 0; i < urb->number_of_packets; i++) {
541 int status = urb->iso_frame_desc[i].status;
542
543 if (status < 0) {
544 print_err_status(dev, i, status);
545 if (urb->iso_frame_desc[i].status != -EPROTO)
546 continue;
547 }
548
62899a28 549 if (urb->iso_frame_desc[i].actual_length <= 0)
8b2f0795 550 continue;
62899a28 551
8b2f0795
DH
552 if (urb->iso_frame_desc[i].actual_length >
553 dev->max_pkt_size) {
554 au0828_isocdbg("packet bigger than packet size");
555 continue;
556 }
557
558 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
559 fbyte = p[0];
560 len = urb->iso_frame_desc[i].actual_length - 4;
561 p += 4;
562
563 if (fbyte & 0x80) {
564 len -= 4;
565 p += 4;
566 au0828_isocdbg("Video frame %s\n",
567 (fbyte & 0x40) ? "odd" : "even");
bde3bb9a 568 if (fbyte & 0x40) {
7f8eacd2
DH
569 /* VBI */
570 if (vbi_buf != NULL)
c5036d61 571 buffer_filled(dev, vbi_dma_q, vbi_buf);
7f8eacd2
DH
572 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
573 if (vbi_buf == NULL)
574 vbioutp = NULL;
575 else
05439b1a 576 vbioutp = vb2_plane_vaddr(
2d700715 577 &vbi_buf->vb.vb2_buf, 0);
7f8eacd2
DH
578
579 /* Video */
8b2f0795
DH
580 if (buf != NULL)
581 buffer_filled(dev, dma_q, buf);
582 get_next_buf(dma_q, &buf);
62899a28 583 if (buf == NULL)
8b2f0795 584 outp = NULL;
62899a28 585 else
2d700715
JS
586 outp = vb2_plane_vaddr(
587 &buf->vb.vb2_buf, 0);
78ca5005
DH
588
589 /* As long as isoc traffic is arriving, keep
590 resetting the timer */
591 if (dev->vid_timeout_running)
592 mod_timer(&dev->vid_timeout,
593 jiffies + (HZ / 10));
594 if (dev->vbi_timeout_running)
595 mod_timer(&dev->vbi_timeout,
596 jiffies + (HZ / 10));
8b2f0795
DH
597 }
598
599 if (buf != NULL) {
62899a28 600 if (fbyte & 0x40)
8b2f0795 601 buf->top_field = 1;
62899a28 602 else
8b2f0795 603 buf->top_field = 0;
8b2f0795
DH
604 }
605
7f8eacd2
DH
606 if (vbi_buf != NULL) {
607 if (fbyte & 0x40)
608 vbi_buf->top_field = 1;
609 else
610 vbi_buf->top_field = 0;
611 }
612
613 dev->vbi_read = 0;
614 vbi_dma_q->pos = 0;
8b2f0795
DH
615 dma_q->pos = 0;
616 }
7f8eacd2
DH
617
618 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
619 if (dev->vbi_read < vbi_field_size) {
620 remain = vbi_field_size - dev->vbi_read;
621 if (len < remain)
622 lencopy = len;
623 else
624 lencopy = remain;
625
626 if (vbi_buf != NULL)
627 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
628 vbioutp, len);
629
630 len -= lencopy;
631 p += lencopy;
632 dev->vbi_read += lencopy;
633 }
634
635 if (dev->vbi_read >= vbi_field_size && buf != NULL)
8b2f0795 636 au0828_copy_video(dev, dma_q, buf, p, outp, len);
8b2f0795
DH
637 }
638 return rc;
639}
640
bed69196
RLLC
641static int au0828_enable_analog_tuner(struct au0828_dev *dev)
642{
643#ifdef CONFIG_MEDIA_CONTROLLER
644 struct media_device *mdev = dev->media_dev;
20fe0319 645 struct media_entity *source;
bed69196 646 struct media_link *link, *found_link = NULL;
57208e5e 647 int ret, active_links = 0;
bed69196 648
0158e7b6 649 if (!mdev || !dev->decoder)
bed69196
RLLC
650 return 0;
651
652 /*
653 * This will find the tuner that is connected into the decoder.
654 * Technically, this is not 100% correct, as the device may be
655 * using an analog input instead of the tuner. However, as we can't
656 * do DVB streaming while the DMA engine is being used for V4L2,
657 * this should be enough for the actual needs.
658 */
57208e5e 659 list_for_each_entry(link, &dev->decoder->links, list) {
0158e7b6 660 if (link->sink->entity == dev->decoder) {
bed69196
RLLC
661 found_link = link;
662 if (link->flags & MEDIA_LNK_FL_ENABLED)
663 active_links++;
664 break;
665 }
666 }
667
668 if (active_links == 1 || !found_link)
669 return 0;
670
671 source = found_link->source->entity;
57208e5e 672 list_for_each_entry(link, &source->links, list) {
bed69196
RLLC
673 struct media_entity *sink;
674 int flags = 0;
675
bed69196
RLLC
676 sink = link->sink->entity;
677
20fe0319 678 if (sink == dev->decoder)
bed69196
RLLC
679 flags = MEDIA_LNK_FL_ENABLED;
680
681 ret = media_entity_setup_link(link, flags);
682 if (ret) {
683 pr_err(
684 "Couldn't change link %s->%s to %s. Error %d\n",
685 source->name, sink->name,
686 flags ? "enabled" : "disabled",
687 ret);
688 return ret;
689 } else
690 au0828_isocdbg(
691 "link %s->%s was %s\n",
692 source->name, sink->name,
693 flags ? "ENABLED" : "disabled");
694 }
695#endif
696 return 0;
697}
698
df9ecb0c 699static int queue_setup(struct vb2_queue *vq,
05439b1a
SK
700 unsigned int *nbuffers, unsigned int *nplanes,
701 unsigned int sizes[], void *alloc_ctxs[])
8b2f0795 702{
05439b1a 703 struct au0828_dev *dev = vb2_get_drv_priv(vq);
df9ecb0c 704 unsigned long size = dev->height * dev->bytesperline;
8b2f0795 705
df9ecb0c
HV
706 if (*nplanes)
707 return sizes[0] < size ? -EINVAL : 0;
05439b1a
SK
708 *nplanes = 1;
709 sizes[0] = size;
8b2f0795 710
bed69196
RLLC
711 au0828_enable_analog_tuner(dev);
712
05439b1a 713 return 0;
8b2f0795
DH
714}
715
716static int
05439b1a 717buffer_prepare(struct vb2_buffer *vb)
8b2f0795 718{
2d700715
JS
719 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
720 struct au0828_buffer *buf = container_of(vbuf,
721 struct au0828_buffer, vb);
05439b1a 722 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
8b2f0795 723
05439b1a 724 buf->length = dev->height * dev->bytesperline;
8b2f0795 725
05439b1a
SK
726 if (vb2_plane_size(vb, 0) < buf->length) {
727 pr_err("%s data will not fit into plane (%lu < %lu)\n",
728 __func__, vb2_plane_size(vb, 0), buf->length);
8b2f0795 729 return -EINVAL;
8b2f0795 730 }
2d700715 731 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
8b2f0795 732 return 0;
8b2f0795
DH
733}
734
735static void
05439b1a 736buffer_queue(struct vb2_buffer *vb)
8b2f0795 737{
2d700715
JS
738 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
739 struct au0828_buffer *buf = container_of(vbuf,
8b2f0795
DH
740 struct au0828_buffer,
741 vb);
05439b1a 742 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
8b2f0795 743 struct au0828_dmaqueue *vidq = &dev->vidq;
05439b1a 744 unsigned long flags = 0;
8b2f0795 745
05439b1a
SK
746 buf->mem = vb2_plane_vaddr(vb, 0);
747 buf->length = vb2_plane_size(vb, 0);
8b2f0795 748
05439b1a
SK
749 spin_lock_irqsave(&dev->slock, flags);
750 list_add_tail(&buf->list, &vidq->active);
751 spin_unlock_irqrestore(&dev->slock, flags);
8b2f0795
DH
752}
753
8b2f0795
DH
754static int au0828_i2s_init(struct au0828_dev *dev)
755{
756 /* Enable i2s mode */
757 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
758 return 0;
759}
760
761/*
762 * Auvitek au0828 analog stream enable
8b2f0795 763 */
a094ca46 764static int au0828_analog_stream_enable(struct au0828_dev *d)
8b2f0795 765{
64ea37bb 766 struct usb_interface *iface;
1fe3a8fe 767 int ret, h, w;
64ea37bb 768
8b2f0795 769 dprintk(1, "au0828_analog_stream_enable called\n");
64ea37bb
MCC
770
771 iface = usb_ifnum_to_if(d->usbdev, 0);
772 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
773 dprintk(1, "Changing intf#0 to alt 5\n");
774 /* set au0828 interface0 to AS5 here again */
775 ret = usb_set_interface(d->usbdev, 0, 5);
776 if (ret < 0) {
83afb32a 777 pr_info("Au0828 can't set alt setting to 5!\n");
64ea37bb
MCC
778 return -EBUSY;
779 }
780 }
781
1fe3a8fe
MCC
782 h = d->height / 2 + 2;
783 w = d->width * 2;
64ea37bb 784
8b2f0795
DH
785 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
786 au0828_writereg(d, 0x106, 0x00);
787 /* set x position */
788 au0828_writereg(d, 0x110, 0x00);
789 au0828_writereg(d, 0x111, 0x00);
1fe3a8fe
MCC
790 au0828_writereg(d, 0x114, w & 0xff);
791 au0828_writereg(d, 0x115, w >> 8);
8b2f0795 792 /* set y position */
7f8eacd2 793 au0828_writereg(d, 0x112, 0x00);
8b2f0795 794 au0828_writereg(d, 0x113, 0x00);
1fe3a8fe
MCC
795 au0828_writereg(d, 0x116, h & 0xff);
796 au0828_writereg(d, 0x117, h >> 8);
8b2f0795
DH
797 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
798
799 return 0;
800}
801
05439b1a 802static int au0828_analog_stream_disable(struct au0828_dev *d)
8b2f0795
DH
803{
804 dprintk(1, "au0828_analog_stream_disable called\n");
805 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
806 return 0;
807}
808
a094ca46 809static void au0828_analog_stream_reset(struct au0828_dev *dev)
8b2f0795
DH
810{
811 dprintk(1, "au0828_analog_stream_reset called\n");
812 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
813 mdelay(30);
814 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
815}
816
817/*
818 * Some operations needs to stop current streaming
819 */
820static int au0828_stream_interrupt(struct au0828_dev *dev)
821{
822 int ret = 0;
823
824 dev->stream_state = STREAM_INTERRUPT;
62899a28 825 if (dev->dev_state == DEV_DISCONNECTED)
8b2f0795 826 return -ENODEV;
62899a28 827 else if (ret) {
8b2f0795 828 dev->dev_state = DEV_MISCONFIGURED;
62899a28 829 dprintk(1, "%s device is misconfigured!\n", __func__);
8b2f0795
DH
830 return ret;
831 }
832 return 0;
833}
834
05439b1a 835int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
8b2f0795 836{
05439b1a
SK
837 struct au0828_dev *dev = vb2_get_drv_priv(vq);
838 int rc = 0;
8b2f0795 839
05439b1a
SK
840 dprintk(1, "au0828_start_analog_streaming called %d\n",
841 dev->streaming_users);
8b2f0795 842
05439b1a
SK
843 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
844 dev->frame_count = 0;
845 else
846 dev->vbi_frame_count = 0;
847
848 if (dev->streaming_users == 0) {
849 /* If we were doing ac97 instead of i2s, it would go here...*/
850 au0828_i2s_init(dev);
851 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
852 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
853 au0828_isoc_copy);
854 if (rc < 0) {
855 pr_info("au0828_init_isoc failed\n");
856 return rc;
857 }
8b2f0795 858
05439b1a
SK
859 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
860 v4l2_device_call_all(&dev->v4l2_dev, 0, video,
861 s_stream, 1);
862 dev->vid_timeout_running = 1;
863 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
864 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
865 dev->vbi_timeout_running = 1;
866 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
867 }
868 }
869 dev->streaming_users++;
870 return rc;
871}
8b2f0795 872
05439b1a 873static void au0828_stop_streaming(struct vb2_queue *vq)
8b2f0795 874{
05439b1a
SK
875 struct au0828_dev *dev = vb2_get_drv_priv(vq);
876 struct au0828_dmaqueue *vidq = &dev->vidq;
877 unsigned long flags = 0;
8b2f0795 878
05439b1a 879 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
8b2f0795 880
05439b1a
SK
881 if (dev->streaming_users-- == 1)
882 au0828_uninit_isoc(dev);
883
884 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
885 dev->vid_timeout_running = 0;
886 del_timer_sync(&dev->vid_timeout);
887
888 spin_lock_irqsave(&dev->slock, flags);
889 if (dev->isoc_ctl.buf != NULL) {
2d700715
JS
890 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
891 VB2_BUF_STATE_ERROR);
05439b1a 892 dev->isoc_ctl.buf = NULL;
7f8eacd2 893 }
05439b1a
SK
894 while (!list_empty(&vidq->active)) {
895 struct au0828_buffer *buf;
549ee4df 896
05439b1a 897 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
2d700715 898 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
05439b1a
SK
899 list_del(&buf->list);
900 }
901 spin_unlock_irqrestore(&dev->slock, flags);
7f8eacd2 902}
8b2f0795 903
05439b1a 904void au0828_stop_vbi_streaming(struct vb2_queue *vq)
7f8eacd2 905{
05439b1a
SK
906 struct au0828_dev *dev = vb2_get_drv_priv(vq);
907 struct au0828_dmaqueue *vbiq = &dev->vbiq;
908 unsigned long flags = 0;
8b2f0795 909
05439b1a
SK
910 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
911 dev->streaming_users);
8b2f0795 912
05439b1a
SK
913 if (dev->streaming_users-- == 1)
914 au0828_uninit_isoc(dev);
7f8eacd2 915
05439b1a
SK
916 spin_lock_irqsave(&dev->slock, flags);
917 if (dev->isoc_ctl.vbi_buf != NULL) {
2d700715 918 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
05439b1a
SK
919 VB2_BUF_STATE_ERROR);
920 dev->isoc_ctl.vbi_buf = NULL;
921 }
922 while (!list_empty(&vbiq->active)) {
923 struct au0828_buffer *buf;
924
925 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
926 list_del(&buf->list);
2d700715 927 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
05439b1a
SK
928 }
929 spin_unlock_irqrestore(&dev->slock, flags);
8b2f0795 930
05439b1a
SK
931 dev->vbi_timeout_running = 0;
932 del_timer_sync(&dev->vbi_timeout);
7f8eacd2
DH
933}
934
05439b1a
SK
935static struct vb2_ops au0828_video_qops = {
936 .queue_setup = queue_setup,
937 .buf_prepare = buffer_prepare,
938 .buf_queue = buffer_queue,
939 .start_streaming = au0828_start_analog_streaming,
940 .stop_streaming = au0828_stop_streaming,
941 .wait_prepare = vb2_ops_wait_prepare,
942 .wait_finish = vb2_ops_wait_finish,
943};
944
945/* ------------------------------------------------------------------
946 V4L2 interface
947 ------------------------------------------------------------------*/
948/*
949 * au0828_analog_unregister
950 * unregister v4l2 devices
951 */
952void au0828_analog_unregister(struct au0828_dev *dev)
7f8eacd2 953{
05439b1a
SK
954 dprintk(1, "au0828_analog_unregister called\n");
955 mutex_lock(&au0828_sysfs_lock);
41071bb8
SK
956 video_unregister_device(&dev->vdev);
957 video_unregister_device(&dev->vbi_dev);
05439b1a 958 mutex_unlock(&au0828_sysfs_lock);
8b2f0795
DH
959}
960
6e04b7b9
DH
961/* This function ensures that video frames continue to be delivered even if
962 the ITU-656 input isn't receiving any data (thereby preventing applications
963 such as tvtime from hanging) */
a094ca46 964static void au0828_vid_buffer_timeout(unsigned long data)
6e04b7b9
DH
965{
966 struct au0828_dev *dev = (struct au0828_dev *) data;
967 struct au0828_dmaqueue *dma_q = &dev->vidq;
968 struct au0828_buffer *buf;
969 unsigned char *vid_data;
78ca5005 970 unsigned long flags = 0;
6e04b7b9 971
78ca5005 972 spin_lock_irqsave(&dev->slock, flags);
6e04b7b9
DH
973
974 buf = dev->isoc_ctl.buf;
975 if (buf != NULL) {
2d700715 976 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
05439b1a 977 memset(vid_data, 0x00, buf->length); /* Blank green frame */
6e04b7b9 978 buffer_filled(dev, dma_q, buf);
6e04b7b9 979 }
78ca5005
DH
980 get_next_buf(dma_q, &buf);
981
982 if (dev->vid_timeout_running == 1)
983 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
6e04b7b9 984
78ca5005 985 spin_unlock_irqrestore(&dev->slock, flags);
6e04b7b9
DH
986}
987
a094ca46 988static void au0828_vbi_buffer_timeout(unsigned long data)
6e04b7b9
DH
989{
990 struct au0828_dev *dev = (struct au0828_dev *) data;
991 struct au0828_dmaqueue *dma_q = &dev->vbiq;
992 struct au0828_buffer *buf;
993 unsigned char *vbi_data;
78ca5005 994 unsigned long flags = 0;
6e04b7b9 995
78ca5005 996 spin_lock_irqsave(&dev->slock, flags);
6e04b7b9
DH
997
998 buf = dev->isoc_ctl.vbi_buf;
999 if (buf != NULL) {
2d700715 1000 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
05439b1a 1001 memset(vbi_data, 0x00, buf->length);
c5036d61 1002 buffer_filled(dev, dma_q, buf);
6e04b7b9 1003 }
78ca5005 1004 vbi_get_next_buf(dma_q, &buf);
6e04b7b9 1005
78ca5005
DH
1006 if (dev->vbi_timeout_running == 1)
1007 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1008 spin_unlock_irqrestore(&dev->slock, flags);
6e04b7b9
DH
1009}
1010
8b2f0795
DH
1011static int au0828_v4l2_open(struct file *filp)
1012{
63b0d5ad 1013 struct au0828_dev *dev = video_drvdata(filp);
05439b1a 1014 int ret;
8b2f0795 1015
05439b1a
SK
1016 dprintk(1,
1017 "%s called std_set %d dev_state %d stream users %d users %d\n",
1018 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1019 dev->streaming_users, dev->users);
8b2f0795 1020
05439b1a 1021 if (mutex_lock_interruptible(&dev->lock))
ea86968f 1022 return -ERESTARTSYS;
05439b1a
SK
1023
1024 ret = v4l2_fh_open(filp);
1025 if (ret) {
1026 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1027 __func__, ret);
1028 mutex_unlock(&dev->lock);
1029 return ret;
ea86968f 1030 }
05439b1a 1031
ea86968f 1032 if (dev->users == 0) {
8b2f0795
DH
1033 au0828_analog_stream_enable(dev);
1034 au0828_analog_stream_reset(dev);
8b2f0795
DH
1035 dev->stream_state = STREAM_OFF;
1036 dev->dev_state |= DEV_INITIALIZED;
1037 }
8b2f0795 1038 dev->users++;
ea86968f 1039 mutex_unlock(&dev->lock);
8b2f0795
DH
1040 return ret;
1041}
1042
1043static int au0828_v4l2_close(struct file *filp)
1044{
1045 int ret;
05439b1a
SK
1046 struct au0828_dev *dev = video_drvdata(filp);
1047 struct video_device *vdev = video_devdata(filp);
1048
1049 dprintk(1,
1050 "%s called std_set %d dev_state %d stream users %d users %d\n",
1051 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1052 dev->streaming_users, dev->users);
8b2f0795 1053
ea86968f 1054 mutex_lock(&dev->lock);
05439b1a 1055 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
78ca5005
DH
1056 /* Cancel timeout thread in case they didn't call streamoff */
1057 dev->vid_timeout_running = 0;
1058 del_timer_sync(&dev->vid_timeout);
05439b1a
SK
1059 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1060 dev->vbi_timeout_running) {
78ca5005
DH
1061 /* Cancel timeout thread in case they didn't call streamoff */
1062 dev->vbi_timeout_running = 0;
1063 del_timer_sync(&dev->vbi_timeout);
7f8eacd2
DH
1064 }
1065
05439b1a
SK
1066 if (dev->dev_state == DEV_DISCONNECTED)
1067 goto end;
8b2f0795 1068
05439b1a 1069 if (dev->users == 1) {
e4b8bc52 1070 /* Save some power by putting tuner to sleep */
622b828a 1071 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
ea86968f 1072 dev->std_set_in_tuner_core = 0;
e4b8bc52 1073
8b2f0795
DH
1074 /* When close the device, set the usb intf0 into alt0 to free
1075 USB bandwidth */
1076 ret = usb_set_interface(dev->usbdev, 0, 0);
62899a28 1077 if (ret < 0)
83afb32a 1078 pr_info("Au0828 can't set alternate to 0!\n");
8b2f0795 1079 }
05439b1a
SK
1080end:
1081 _vb2_fop_release(filp, NULL);
8b2f0795 1082 dev->users--;
05439b1a 1083 mutex_unlock(&dev->lock);
8b2f0795
DH
1084 return 0;
1085}
1086
ea86968f
HV
1087/* Must be called with dev->lock held */
1088static void au0828_init_tuner(struct au0828_dev *dev)
1089{
1090 struct v4l2_frequency f = {
1091 .frequency = dev->ctrl_freq,
1092 .type = V4L2_TUNER_ANALOG_TV,
1093 };
1094
05439b1a
SK
1095 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1096 dev->std_set_in_tuner_core, dev->dev_state);
1097
ea86968f
HV
1098 if (dev->std_set_in_tuner_core)
1099 return;
1100 dev->std_set_in_tuner_core = 1;
1101 i2c_gate_ctrl(dev, 1);
1102 /* If we've never sent the standard in tuner core, do so now.
1103 We don't do this at device probe because we don't want to
1104 incur the cost of a firmware load */
8774bed9 1105 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
ea86968f
HV
1106 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1107 i2c_gate_ctrl(dev, 0);
1108}
1109
8b2f0795
DH
1110static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1111 struct v4l2_format *format)
1112{
1113 int ret;
1114 int width = format->fmt.pix.width;
1115 int height = format->fmt.pix.height;
8b2f0795 1116
8b2f0795
DH
1117 /* If they are demanding a format other than the one we support,
1118 bail out (tvtime asks for UYVY and then retries with YUYV) */
62899a28 1119 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
8b2f0795 1120 return -EINVAL;
8b2f0795
DH
1121
1122 /* format->fmt.pix.width only support 720 and height 480 */
62899a28 1123 if (width != 720)
8b2f0795 1124 width = 720;
62899a28 1125 if (height != 480)
8b2f0795
DH
1126 height = 480;
1127
1128 format->fmt.pix.width = width;
1129 format->fmt.pix.height = height;
1130 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1131 format->fmt.pix.bytesperline = width * 2;
1132 format->fmt.pix.sizeimage = width * height * 2;
1133 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1134 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
8d86e4e8 1135 format->fmt.pix.priv = 0;
8b2f0795 1136
62899a28 1137 if (cmd == VIDIOC_TRY_FMT)
8b2f0795
DH
1138 return 0;
1139
1140 /* maybe set new image format, driver current only support 720*480 */
1141 dev->width = width;
1142 dev->height = height;
1143 dev->frame_size = width * height * 2;
1144 dev->field_size = width * height;
1145 dev->bytesperline = width * 2;
1146
62899a28 1147 if (dev->stream_state == STREAM_ON) {
8b2f0795 1148 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
62899a28
DH
1149 ret = au0828_stream_interrupt(dev);
1150 if (ret != 0) {
8b2f0795
DH
1151 dprintk(1, "error interrupting video stream!\n");
1152 return ret;
1153 }
1154 }
1155
8b2f0795
DH
1156 au0828_analog_stream_enable(dev);
1157
1158 return 0;
1159}
1160
8b2f0795
DH
1161static int vidioc_querycap(struct file *file, void *priv,
1162 struct v4l2_capability *cap)
1163{
59e05484 1164 struct video_device *vdev = video_devdata(file);
05439b1a
SK
1165 struct au0828_dev *dev = video_drvdata(file);
1166
1167 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1168 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795 1169
8b2f0795 1170 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
f1add5b5 1171 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
59e05484 1172 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
8b2f0795 1173
59e05484
HV
1174 /* set the device capabilities */
1175 cap->device_caps = V4L2_CAP_AUDIO |
8b2f0795
DH
1176 V4L2_CAP_READWRITE |
1177 V4L2_CAP_STREAMING |
1178 V4L2_CAP_TUNER;
59e05484
HV
1179 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1180 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1181 else
1182 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1183 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1184 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
8b2f0795
DH
1185 return 0;
1186}
1187
1188static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1189 struct v4l2_fmtdesc *f)
1190{
62899a28 1191 if (f->index)
8b2f0795
DH
1192 return -EINVAL;
1193
05439b1a
SK
1194 dprintk(1, "%s called\n", __func__);
1195
8b2f0795
DH
1196 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1197 strcpy(f->description, "Packed YUV2");
1198
1199 f->flags = 0;
1200 f->pixelformat = V4L2_PIX_FMT_UYVY;
1201
8b2f0795
DH
1202 return 0;
1203}
1204
1205static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1206 struct v4l2_format *f)
1207{
05439b1a
SK
1208 struct au0828_dev *dev = video_drvdata(file);
1209
1210 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1211 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795
DH
1212
1213 f->fmt.pix.width = dev->width;
1214 f->fmt.pix.height = dev->height;
1215 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1216 f->fmt.pix.bytesperline = dev->bytesperline;
1217 f->fmt.pix.sizeimage = dev->frame_size;
1218 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1219 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
8d86e4e8 1220 f->fmt.pix.priv = 0;
8b2f0795
DH
1221 return 0;
1222}
1223
1224static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1225 struct v4l2_format *f)
1226{
05439b1a
SK
1227 struct au0828_dev *dev = video_drvdata(file);
1228
1229 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1230 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795
DH
1231
1232 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1233}
1234
1235static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1236 struct v4l2_format *f)
1237{
05439b1a 1238 struct au0828_dev *dev = video_drvdata(file);
8b2f0795
DH
1239 int rc;
1240
05439b1a
SK
1241 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1242 dev->std_set_in_tuner_core, dev->dev_state);
1243
7f8eacd2
DH
1244 rc = check_dev(dev);
1245 if (rc < 0)
1246 return rc;
1247
05439b1a 1248 if (vb2_is_busy(&dev->vb_vidq)) {
83afb32a 1249 pr_info("%s queue busy\n", __func__);
8b2f0795
DH
1250 rc = -EBUSY;
1251 goto out;
1252 }
1253
7f8eacd2 1254 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
8b2f0795
DH
1255out:
1256 return rc;
1257}
1258
314527ac 1259static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
8b2f0795 1260{
05439b1a
SK
1261 struct au0828_dev *dev = video_drvdata(file);
1262
1263 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1264 dev->std_set_in_tuner_core, dev->dev_state);
1265
1266 if (norm == dev->std)
1267 return 0;
1268
1269 if (dev->streaming_users > 0)
1270 return -EBUSY;
8b2f0795 1271
ea86968f
HV
1272 dev->std = norm;
1273
1274 au0828_init_tuner(dev);
1275
fa09cb9a 1276 i2c_gate_ctrl(dev, 1);
e58071f0 1277
f2fd7ce6
MCC
1278 /*
1279 * FIXME: when we support something other than 60Hz standards,
1280 * we are going to have to make the au0828 bridge adjust the size
1281 * of its capture buffer, which is currently hardcoded at 720x480
1282 */
8b2f0795 1283
8774bed9 1284 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
e58071f0 1285
fa09cb9a 1286 i2c_gate_ctrl(dev, 0);
e58071f0 1287
8b2f0795
DH
1288 return 0;
1289}
1290
33b6b89b
HV
1291static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1292{
05439b1a
SK
1293 struct au0828_dev *dev = video_drvdata(file);
1294
1295 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1296 dev->std_set_in_tuner_core, dev->dev_state);
33b6b89b
HV
1297
1298 *norm = dev->std;
1299 return 0;
1300}
1301
8b2f0795
DH
1302static int vidioc_enum_input(struct file *file, void *priv,
1303 struct v4l2_input *input)
1304{
05439b1a 1305 struct au0828_dev *dev = video_drvdata(file);
8b2f0795
DH
1306 unsigned int tmp;
1307
1308 static const char *inames[] = {
3d62287e 1309 [AU0828_VMUX_UNDEFINED] = "Undefined",
8b2f0795
DH
1310 [AU0828_VMUX_COMPOSITE] = "Composite",
1311 [AU0828_VMUX_SVIDEO] = "S-Video",
1312 [AU0828_VMUX_CABLE] = "Cable TV",
1313 [AU0828_VMUX_TELEVISION] = "Television",
1314 [AU0828_VMUX_DVB] = "DVB",
1315 [AU0828_VMUX_DEBUG] = "tv debug"
1316 };
1317
05439b1a
SK
1318 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1319 dev->std_set_in_tuner_core, dev->dev_state);
1320
8b2f0795
DH
1321 tmp = input->index;
1322
f5e20c34 1323 if (tmp >= AU0828_MAX_INPUT)
8b2f0795 1324 return -EINVAL;
62899a28 1325 if (AUVI_INPUT(tmp).type == 0)
8b2f0795
DH
1326 return -EINVAL;
1327
8b2f0795 1328 input->index = tmp;
f1add5b5 1329 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
62899a28 1330 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
a2cf96f9 1331 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
8b2f0795 1332 input->type |= V4L2_INPUT_TYPE_TUNER;
a2cf96f9
HV
1333 input->audioset = 1;
1334 } else {
8b2f0795 1335 input->type |= V4L2_INPUT_TYPE_CAMERA;
a2cf96f9
HV
1336 input->audioset = 2;
1337 }
8b2f0795 1338
41071bb8 1339 input->std = dev->vdev.tvnorms;
8b2f0795
DH
1340
1341 return 0;
1342}
1343
1344static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1345{
05439b1a
SK
1346 struct au0828_dev *dev = video_drvdata(file);
1347
1348 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1349 dev->std_set_in_tuner_core, dev->dev_state);
1350
8b2f0795
DH
1351 *i = dev->ctrl_input;
1352 return 0;
1353}
1354
56230d1e 1355static void au0828_s_input(struct au0828_dev *dev, int index)
8b2f0795 1356{
8b2f0795 1357 int i;
8b2f0795 1358
05439b1a
SK
1359 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1360 dev->std_set_in_tuner_core, dev->dev_state);
1361
62899a28 1362 switch (AUVI_INPUT(index).type) {
8b2f0795 1363 case AU0828_VMUX_SVIDEO:
8b2f0795 1364 dev->input_type = AU0828_VMUX_SVIDEO;
a2cf96f9 1365 dev->ctrl_ainput = 1;
8b2f0795 1366 break;
8b2f0795 1367 case AU0828_VMUX_COMPOSITE:
8b2f0795 1368 dev->input_type = AU0828_VMUX_COMPOSITE;
a2cf96f9 1369 dev->ctrl_ainput = 1;
8b2f0795 1370 break;
8b2f0795 1371 case AU0828_VMUX_TELEVISION:
8b2f0795 1372 dev->input_type = AU0828_VMUX_TELEVISION;
a2cf96f9 1373 dev->ctrl_ainput = 0;
8b2f0795 1374 break;
8b2f0795 1375 default:
56230d1e 1376 dprintk(1, "unknown input type set [%d]\n",
a1094c4c
DH
1377 AUVI_INPUT(index).type);
1378 break;
8b2f0795
DH
1379 }
1380
5325b427
HV
1381 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1382 AUVI_INPUT(index).vmux, 0, 0);
8b2f0795
DH
1383
1384 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1385 int enable = 0;
62899a28 1386 if (AUVI_INPUT(i).audio_setup == NULL)
8b2f0795 1387 continue;
8b2f0795
DH
1388
1389 if (i == index)
1390 enable = 1;
1391 else
1392 enable = 0;
1393 if (enable) {
f1add5b5 1394 (AUVI_INPUT(i).audio_setup)(dev, enable);
8b2f0795
DH
1395 } else {
1396 /* Make sure we leave it turned on if some
1397 other input is routed to this callback */
f1add5b5
DH
1398 if ((AUVI_INPUT(i).audio_setup) !=
1399 ((AUVI_INPUT(index).audio_setup))) {
1400 (AUVI_INPUT(i).audio_setup)(dev, enable);
8b2f0795
DH
1401 }
1402 }
1403 }
1404
5325b427
HV
1405 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1406 AUVI_INPUT(index).amux, 0, 0);
56230d1e
HV
1407}
1408
1409static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1410{
05439b1a 1411 struct au0828_dev *dev = video_drvdata(file);
56230d1e
HV
1412
1413 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1414 index);
1415 if (index >= AU0828_MAX_INPUT)
1416 return -EINVAL;
1417 if (AUVI_INPUT(index).type == 0)
1418 return -EINVAL;
1419 dev->ctrl_input = index;
1420 au0828_s_input(dev, index);
8b2f0795
DH
1421 return 0;
1422}
1423
a2cf96f9
HV
1424static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1425{
1426 if (a->index > 1)
1427 return -EINVAL;
1428
05439b1a
SK
1429 dprintk(1, "%s called\n", __func__);
1430
a2cf96f9
HV
1431 if (a->index == 0)
1432 strcpy(a->name, "Television");
1433 else
1434 strcpy(a->name, "Line in");
1435
1436 a->capability = V4L2_AUDCAP_STEREO;
1437 return 0;
1438}
1439
8b2f0795
DH
1440static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1441{
05439b1a
SK
1442 struct au0828_dev *dev = video_drvdata(file);
1443
1444 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1445 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795 1446
a2cf96f9
HV
1447 a->index = dev->ctrl_ainput;
1448 if (a->index == 0)
8b2f0795
DH
1449 strcpy(a->name, "Television");
1450 else
1451 strcpy(a->name, "Line in");
1452
1453 a->capability = V4L2_AUDCAP_STEREO;
8b2f0795
DH
1454 return 0;
1455}
1456
0e8025b9 1457static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
8b2f0795 1458{
05439b1a 1459 struct au0828_dev *dev = video_drvdata(file);
a2cf96f9 1460
62899a28 1461 if (a->index != dev->ctrl_ainput)
8b2f0795 1462 return -EINVAL;
05439b1a
SK
1463
1464 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1465 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795
DH
1466 return 0;
1467}
1468
8b2f0795
DH
1469static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1470{
05439b1a 1471 struct au0828_dev *dev = video_drvdata(file);
8b2f0795 1472
62899a28 1473 if (t->index != 0)
8b2f0795
DH
1474 return -EINVAL;
1475
05439b1a
SK
1476 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1477 dev->std_set_in_tuner_core, dev->dev_state);
1478
8b2f0795 1479 strcpy(t->name, "Auvitek tuner");
ea86968f
HV
1480
1481 au0828_init_tuner(dev);
1482 i2c_gate_ctrl(dev, 1);
2689d3dc 1483 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
ea86968f 1484 i2c_gate_ctrl(dev, 0);
8b2f0795
DH
1485 return 0;
1486}
1487
1488static int vidioc_s_tuner(struct file *file, void *priv,
2f73c7c5 1489 const struct v4l2_tuner *t)
8b2f0795 1490{
05439b1a 1491 struct au0828_dev *dev = video_drvdata(file);
8b2f0795 1492
62899a28 1493 if (t->index != 0)
8b2f0795
DH
1494 return -EINVAL;
1495
05439b1a
SK
1496 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1497 dev->std_set_in_tuner_core, dev->dev_state);
1498
ea86968f 1499 au0828_init_tuner(dev);
fa09cb9a 1500 i2c_gate_ctrl(dev, 1);
2689d3dc 1501 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
fa09cb9a 1502 i2c_gate_ctrl(dev, 0);
e58071f0 1503
8b2f0795
DH
1504 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1505 t->afc);
e58071f0 1506
8b2f0795
DH
1507 return 0;
1508
1509}
1510
1511static int vidioc_g_frequency(struct file *file, void *priv,
1512 struct v4l2_frequency *freq)
1513{
05439b1a 1514 struct au0828_dev *dev = video_drvdata(file);
c888923d 1515
e1cf6587
HV
1516 if (freq->tuner != 0)
1517 return -EINVAL;
05439b1a
SK
1518 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1519 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795
DH
1520 freq->frequency = dev->ctrl_freq;
1521 return 0;
1522}
1523
1524static int vidioc_s_frequency(struct file *file, void *priv,
b530a447 1525 const struct v4l2_frequency *freq)
8b2f0795 1526{
05439b1a 1527 struct au0828_dev *dev = video_drvdata(file);
e1cf6587 1528 struct v4l2_frequency new_freq = *freq;
8b2f0795 1529
62899a28 1530 if (freq->tuner != 0)
8b2f0795 1531 return -EINVAL;
8b2f0795 1532
05439b1a
SK
1533 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1534 dev->std_set_in_tuner_core, dev->dev_state);
1535
ea86968f 1536 au0828_init_tuner(dev);
fa09cb9a 1537 i2c_gate_ctrl(dev, 1);
4a03dafc 1538
2689d3dc 1539 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
e1cf6587
HV
1540 /* Get the actual set (and possibly clamped) frequency */
1541 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1542 dev->ctrl_freq = new_freq.frequency;
8b2f0795 1543
fa09cb9a 1544 i2c_gate_ctrl(dev, 0);
4a03dafc 1545
8b2f0795
DH
1546 au0828_analog_stream_reset(dev);
1547
1548 return 0;
1549}
1550
7f8eacd2
DH
1551
1552/* RAW VBI ioctls */
1553
1554static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1555 struct v4l2_format *format)
1556{
05439b1a
SK
1557 struct au0828_dev *dev = video_drvdata(file);
1558
1559 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1560 dev->std_set_in_tuner_core, dev->dev_state);
7f8eacd2
DH
1561
1562 format->fmt.vbi.samples_per_line = dev->vbi_width;
1563 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1564 format->fmt.vbi.offset = 0;
1565 format->fmt.vbi.flags = 0;
1566 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1567
1568 format->fmt.vbi.count[0] = dev->vbi_height;
1569 format->fmt.vbi.count[1] = dev->vbi_height;
1570 format->fmt.vbi.start[0] = 21;
1571 format->fmt.vbi.start[1] = 284;
8d86e4e8 1572 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
7f8eacd2
DH
1573
1574 return 0;
1575}
1576
8b2f0795
DH
1577static int vidioc_cropcap(struct file *file, void *priv,
1578 struct v4l2_cropcap *cc)
1579{
05439b1a 1580 struct au0828_dev *dev = video_drvdata(file);
8b2f0795 1581
62899a28 1582 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
8b2f0795
DH
1583 return -EINVAL;
1584
05439b1a
SK
1585 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1586 dev->std_set_in_tuner_core, dev->dev_state);
1587
8b2f0795
DH
1588 cc->bounds.left = 0;
1589 cc->bounds.top = 0;
1590 cc->bounds.width = dev->width;
1591 cc->bounds.height = dev->height;
1592
1593 cc->defrect = cc->bounds;
1594
1595 cc->pixelaspect.numerator = 54;
1596 cc->pixelaspect.denominator = 59;
1597
1598 return 0;
1599}
1600
80c6e358 1601#ifdef CONFIG_VIDEO_ADV_DEBUG
8b2f0795
DH
1602static int vidioc_g_register(struct file *file, void *priv,
1603 struct v4l2_dbg_register *reg)
1604{
05439b1a
SK
1605 struct au0828_dev *dev = video_drvdata(file);
1606
1607 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1608 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795 1609
364d2db2 1610 reg->val = au0828_read(dev, reg->reg);
ead5bc4e 1611 reg->size = 1;
364d2db2 1612 return 0;
8b2f0795
DH
1613}
1614
1615static int vidioc_s_register(struct file *file, void *priv,
977ba3b1 1616 const struct v4l2_dbg_register *reg)
8b2f0795 1617{
05439b1a
SK
1618 struct au0828_dev *dev = video_drvdata(file);
1619
1620 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1621 dev->std_set_in_tuner_core, dev->dev_state);
8b2f0795 1622
364d2db2 1623 return au0828_writereg(dev, reg->reg, reg->val);
8b2f0795 1624}
80c6e358 1625#endif
8b2f0795 1626
83b09422
HV
1627static int vidioc_log_status(struct file *file, void *fh)
1628{
1629 struct video_device *vdev = video_devdata(file);
1630
05439b1a
SK
1631 dprintk(1, "%s called\n", __func__);
1632
83b09422
HV
1633 v4l2_ctrl_log_status(file, fh);
1634 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1635 return 0;
1636}
1637
1a1ba95e
MCC
1638void au0828_v4l2_suspend(struct au0828_dev *dev)
1639{
1640 struct urb *urb;
1641 int i;
1642
81187240
MCC
1643 pr_info("stopping V4L2\n");
1644
1a1ba95e 1645 if (dev->stream_state == STREAM_ON) {
81187240 1646 pr_info("stopping V4L2 active URBs\n");
1a1ba95e
MCC
1647 au0828_analog_stream_disable(dev);
1648 /* stop urbs */
1649 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1650 urb = dev->isoc_ctl.urb[i];
1651 if (urb) {
1652 if (!irqs_disabled())
1653 usb_kill_urb(urb);
1654 else
1655 usb_unlink_urb(urb);
1656 }
1657 }
1658 }
1659
1660 if (dev->vid_timeout_running)
1661 del_timer_sync(&dev->vid_timeout);
1662 if (dev->vbi_timeout_running)
1663 del_timer_sync(&dev->vbi_timeout);
1664}
1665
1666void au0828_v4l2_resume(struct au0828_dev *dev)
1667{
1668 int i, rc;
1669
81187240
MCC
1670 pr_info("restarting V4L2\n");
1671
1a1ba95e
MCC
1672 if (dev->stream_state == STREAM_ON) {
1673 au0828_stream_interrupt(dev);
1674 au0828_init_tuner(dev);
1675 }
1676
1677 if (dev->vid_timeout_running)
1678 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1679 if (dev->vbi_timeout_running)
1680 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1681
1682 /* If we were doing ac97 instead of i2s, it would go here...*/
1683 au0828_i2s_init(dev);
1684
1685 au0828_analog_stream_enable(dev);
1686
1687 if (!(dev->stream_state == STREAM_ON)) {
1688 au0828_analog_stream_reset(dev);
1689 /* submit urbs */
1690 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1691 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1692 if (rc) {
1693 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1694 i, rc);
1695 au0828_uninit_isoc(dev);
1696 }
1697 }
1698 }
1699}
1700
8b2f0795
DH
1701static struct v4l2_file_operations au0828_v4l_fops = {
1702 .owner = THIS_MODULE,
1703 .open = au0828_v4l2_open,
1704 .release = au0828_v4l2_close,
05439b1a
SK
1705 .read = vb2_fop_read,
1706 .poll = vb2_fop_poll,
1707 .mmap = vb2_fop_mmap,
549ee4df 1708 .unlocked_ioctl = video_ioctl2,
8b2f0795
DH
1709};
1710
1711static const struct v4l2_ioctl_ops video_ioctl_ops = {
1712 .vidioc_querycap = vidioc_querycap,
1713 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1714 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1715 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1716 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
5a5a4e16 1717 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
8d86e4e8 1718 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
7f8eacd2 1719 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
a2cf96f9 1720 .vidioc_enumaudio = vidioc_enumaudio,
8b2f0795
DH
1721 .vidioc_g_audio = vidioc_g_audio,
1722 .vidioc_s_audio = vidioc_s_audio,
1723 .vidioc_cropcap = vidioc_cropcap,
05439b1a
SK
1724
1725 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1726 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1727 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1728 .vidioc_querybuf = vb2_ioctl_querybuf,
1729 .vidioc_qbuf = vb2_ioctl_qbuf,
1730 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1731 .vidioc_expbuf = vb2_ioctl_expbuf,
1732
8b2f0795 1733 .vidioc_s_std = vidioc_s_std,
33b6b89b 1734 .vidioc_g_std = vidioc_g_std,
8b2f0795
DH
1735 .vidioc_enum_input = vidioc_enum_input,
1736 .vidioc_g_input = vidioc_g_input,
1737 .vidioc_s_input = vidioc_s_input,
05439b1a
SK
1738
1739 .vidioc_streamon = vb2_ioctl_streamon,
1740 .vidioc_streamoff = vb2_ioctl_streamoff,
1741
8b2f0795
DH
1742 .vidioc_g_tuner = vidioc_g_tuner,
1743 .vidioc_s_tuner = vidioc_s_tuner,
1744 .vidioc_g_frequency = vidioc_g_frequency,
1745 .vidioc_s_frequency = vidioc_s_frequency,
1746#ifdef CONFIG_VIDEO_ADV_DEBUG
1747 .vidioc_g_register = vidioc_g_register,
1748 .vidioc_s_register = vidioc_s_register,
8b2f0795 1749#endif
83b09422
HV
1750 .vidioc_log_status = vidioc_log_status,
1751 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1752 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
8b2f0795
DH
1753};
1754
1755static const struct video_device au0828_video_template = {
1756 .fops = &au0828_v4l_fops,
41071bb8 1757 .release = video_device_release_empty,
8b2f0795 1758 .ioctl_ops = &video_ioctl_ops,
f2fd7ce6 1759 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
8b2f0795
DH
1760};
1761
05439b1a
SK
1762static int au0828_vb2_setup(struct au0828_dev *dev)
1763{
1764 int rc;
1765 struct vb2_queue *q;
1766
1767 /* Setup Videobuf2 for Video capture */
1768 q = &dev->vb_vidq;
1769 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1770 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1771 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1772 q->drv_priv = dev;
1773 q->buf_struct_size = sizeof(struct au0828_buffer);
1774 q->ops = &au0828_video_qops;
1775 q->mem_ops = &vb2_vmalloc_memops;
1776
1777 rc = vb2_queue_init(q);
1778 if (rc < 0)
1779 return rc;
1780
1781 /* Setup Videobuf2 for VBI capture */
1782 q = &dev->vb_vbiq;
1783 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1784 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1785 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1786 q->drv_priv = dev;
1787 q->buf_struct_size = sizeof(struct au0828_buffer);
1788 q->ops = &au0828_vbi_qops;
1789 q->mem_ops = &vb2_vmalloc_memops;
1790
1791 rc = vb2_queue_init(q);
1792 if (rc < 0)
1793 return rc;
1794
1795 return 0;
1796}
1797
d1f33737
MCC
1798static void au0828_analog_create_entities(struct au0828_dev *dev)
1799{
1800#if defined(CONFIG_MEDIA_CONTROLLER)
1801 static const char * const inames[] = {
1802 [AU0828_VMUX_COMPOSITE] = "Composite",
1803 [AU0828_VMUX_SVIDEO] = "S-Video",
1804 [AU0828_VMUX_CABLE] = "Cable TV",
1805 [AU0828_VMUX_TELEVISION] = "Television",
1806 [AU0828_VMUX_DVB] = "DVB",
1807 [AU0828_VMUX_DEBUG] = "tv debug"
1808 };
1809 int ret, i;
1810
1811 /* Initialize Video and VBI pads */
1812 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1813 ret = media_entity_init(&dev->vdev.entity, 1, &dev->video_pad);
1814 if (ret < 0)
1815 pr_err("failed to initialize video media entity!\n");
1816
1817 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1818 ret = media_entity_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1819 if (ret < 0)
1820 pr_err("failed to initialize vbi media entity!\n");
1821
1822 /* Create entities for each input connector */
1823 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1824 struct media_entity *ent = &dev->input_ent[i];
1825
1826 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1827 break;
1828
1829 ent->name = inames[AUVI_INPUT(i).type];
1830 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1831 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1832
1833 switch (AUVI_INPUT(i).type) {
1834 case AU0828_VMUX_COMPOSITE:
4ca72efa 1835 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
d1f33737
MCC
1836 break;
1837 case AU0828_VMUX_SVIDEO:
4ca72efa 1838 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
d1f33737
MCC
1839 break;
1840 case AU0828_VMUX_CABLE:
1841 case AU0828_VMUX_TELEVISION:
1842 case AU0828_VMUX_DVB:
4ca72efa 1843 ent->function = MEDIA_ENT_F_CONN_RF;
d1f33737
MCC
1844 break;
1845 default: /* AU0828_VMUX_DEBUG */
4ca72efa 1846 ent->function = MEDIA_ENT_F_CONN_TEST;
d1f33737
MCC
1847 break;
1848 }
1849
1850 ret = media_entity_init(ent, 1, &dev->input_pad[i]);
1851 if (ret < 0)
1852 pr_err("failed to initialize input pad[%d]!\n", i);
1853
1854 ret = media_device_register_entity(dev->media_dev, ent);
1855 if (ret < 0)
1856 pr_err("failed to register input entity %d!\n", i);
1857 }
1858#endif
1859}
1860
8b2f0795
DH
1861/**************************************************************************/
1862
fc4ce6cd
DH
1863int au0828_analog_register(struct au0828_dev *dev,
1864 struct usb_interface *interface)
8b2f0795
DH
1865{
1866 int retval = -ENOMEM;
fc4ce6cd
DH
1867 struct usb_host_interface *iface_desc;
1868 struct usb_endpoint_descriptor *endpoint;
d63b21bf 1869 int i, ret;
8b2f0795 1870
1fe3a8fe
MCC
1871 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1872 interface->cur_altsetting->desc.bInterfaceNumber);
8b2f0795 1873
fc4ce6cd
DH
1874 /* set au0828 usb interface0 to as5 */
1875 retval = usb_set_interface(dev->usbdev,
62899a28 1876 interface->cur_altsetting->desc.bInterfaceNumber, 5);
fc4ce6cd 1877 if (retval != 0) {
83afb32a 1878 pr_info("Failure setting usb interface0 to as5\n");
fc4ce6cd
DH
1879 return retval;
1880 }
1881
1882 /* Figure out which endpoint has the isoc interface */
1883 iface_desc = interface->cur_altsetting;
62899a28 1884 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
fc4ce6cd 1885 endpoint = &iface_desc->endpoint[i].desc;
62899a28
DH
1886 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1887 == USB_DIR_IN) &&
1888 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1889 == USB_ENDPOINT_XFER_ISOC)) {
fc4ce6cd
DH
1890
1891 /* we find our isoc in endpoint */
1892 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
62899a28
DH
1893 dev->max_pkt_size = (tmp & 0x07ff) *
1894 (((tmp & 0x1800) >> 11) + 1);
fc4ce6cd 1895 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1fe3a8fe
MCC
1896 dprintk(1,
1897 "Found isoc endpoint 0x%02x, max size = %d\n",
1898 dev->isoc_in_endpointaddr, dev->max_pkt_size);
fc4ce6cd
DH
1899 }
1900 }
62899a28 1901 if (!(dev->isoc_in_endpointaddr)) {
83afb32a 1902 pr_info("Could not locate isoc endpoint\n");
fc4ce6cd
DH
1903 return -ENODEV;
1904 }
1905
8b2f0795
DH
1906 init_waitqueue_head(&dev->open);
1907 spin_lock_init(&dev->slock);
8b2f0795 1908
7f8eacd2 1909 /* init video dma queues */
8b2f0795 1910 INIT_LIST_HEAD(&dev->vidq.active);
7f8eacd2 1911 INIT_LIST_HEAD(&dev->vbiq.active);
8b2f0795 1912
30a8f2a0
JL
1913 setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
1914 (unsigned long)dev);
1915 setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
1916 (unsigned long)dev);
78ca5005 1917
8b2f0795
DH
1918 dev->width = NTSC_STD_W;
1919 dev->height = NTSC_STD_H;
1920 dev->field_size = dev->width * dev->height;
1921 dev->frame_size = dev->field_size << 1;
1922 dev->bytesperline = dev->width << 1;
de8d2bbf
HV
1923 dev->vbi_width = 720;
1924 dev->vbi_height = 1;
8b2f0795 1925 dev->ctrl_ainput = 0;
e1cf6587 1926 dev->ctrl_freq = 960;
33b6b89b 1927 dev->std = V4L2_STD_NTSC_M;
56230d1e 1928 au0828_s_input(dev, 0);
8b2f0795 1929
05439b1a
SK
1930 mutex_init(&dev->vb_queue_lock);
1931 mutex_init(&dev->vb_vbi_queue_lock);
1932
8b2f0795 1933 /* Fill the video capture device struct */
41071bb8
SK
1934 dev->vdev = au0828_video_template;
1935 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1936 dev->vdev.lock = &dev->lock;
1937 dev->vdev.queue = &dev->vb_vidq;
1938 dev->vdev.queue->lock = &dev->vb_queue_lock;
1939 strcpy(dev->vdev.name, "au0828a video");
8b2f0795
DH
1940
1941 /* Setup the VBI device */
41071bb8
SK
1942 dev->vbi_dev = au0828_video_template;
1943 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1944 dev->vbi_dev.lock = &dev->lock;
1945 dev->vbi_dev.queue = &dev->vb_vbiq;
1946 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1947 strcpy(dev->vbi_dev.name, "au0828a vbi");
8b2f0795 1948
d1f33737
MCC
1949 /* Init entities at the Media Controller */
1950 au0828_analog_create_entities(dev);
bed69196 1951
05439b1a
SK
1952 /* initialize videobuf2 stuff */
1953 retval = au0828_vb2_setup(dev);
1954 if (retval != 0) {
1955 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
1956 retval);
41071bb8 1957 return -ENODEV;
05439b1a
SK
1958 }
1959
8b2f0795 1960 /* Register the v4l2 device */
41071bb8
SK
1961 video_set_drvdata(&dev->vdev, dev);
1962 retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
62899a28
DH
1963 if (retval != 0) {
1964 dprintk(1, "unable to register video device (error = %d).\n",
1965 retval);
d63b21bf 1966 ret = -ENODEV;
05439b1a 1967 goto err_reg_vdev;
8b2f0795
DH
1968 }
1969
1970 /* Register the vbi device */
41071bb8
SK
1971 video_set_drvdata(&dev->vbi_dev, dev);
1972 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
62899a28
DH
1973 if (retval != 0) {
1974 dprintk(1, "unable to register vbi device (error = %d).\n",
1975 retval);
d63b21bf 1976 ret = -ENODEV;
05439b1a 1977 goto err_reg_vbi_dev;
8b2f0795
DH
1978 }
1979
62899a28 1980 dprintk(1, "%s completed!\n", __func__);
8b2f0795
DH
1981
1982 return 0;
d63b21bf 1983
05439b1a 1984err_reg_vbi_dev:
41071bb8 1985 video_unregister_device(&dev->vdev);
05439b1a
SK
1986err_reg_vdev:
1987 vb2_queue_release(&dev->vb_vidq);
1988 vb2_queue_release(&dev->vb_vbiq);
d63b21bf 1989 return ret;
8b2f0795
DH
1990}
1991
This page took 0.578038 seconds and 5 git commands to generate.