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