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