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

source code of linux/drivers/media/usb/au0828/au0828-video.c