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