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
30 static DEFINE_MUTEX(au0828_sysfs_lock);
31
32 /* ------------------------------------------------------------------
33 Videobuf operations
34 ------------------------------------------------------------------*/
35
36 static unsigned int isoc_debug;
37 module_param(isoc_debug, int, 0644);
38 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
39
40 #define au0828_isocdbg(fmt, arg...) \
41 do {\
42 if (isoc_debug) { \
43 pr_info("au0828 %s :"fmt, \
44 __func__ , ##arg); \
45 } \
46 } while (0)
47
i2c_gate_ctrl(struct au0828_dev * dev,int val)48 static 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
print_err_status(struct au0828_dev * dev,int packet,int status)54 static 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
check_dev(struct au0828_dev * dev)93 static 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 */
au0828_irq_callback(struct urb * urb)110 static 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(&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 */
au0828_uninit_isoc(struct au0828_dev * dev)154 static 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->usbdev,
172 urb->transfer_buffer_length,
173 dev->isoc_ctl.transfer_buffer[i],
174 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(dev->isoc_ctl.urb);
183 kfree(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 */
au0828_init_isoc(struct au0828_dev * dev,int max_packets,int num_bufs,int max_pkt_size,int (* isoc_copy)(struct au0828_dev * dev,struct urb * urb))195 static 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(num_bufs, 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(num_bufs, sizeof(void *),
218 GFP_KERNEL);
219 if (!dev->isoc_ctl.transfer_buffer) {
220 au0828_isocdbg("cannot allocate memory for usb transfer\n");
221 kfree(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(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->usbdev,
241 sb_size, GFP_KERNEL, &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->usbdev, pipe,
253 dev->isoc_ctl.transfer_buffer[i], sb_size,
254 au0828_irq_callback, dma_q, 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(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 */
buffer_filled(struct au0828_dev * dev,struct au0828_dmaqueue * dma_q,struct au0828_buffer * buf)285 static 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->vb2_buf, VB2_BUF_STATE_DONE);
303 }
304
305 /*
306 * Identify the buffer header type and properly handles
307 */
au0828_copy_video(struct au0828_dev * dev,struct au0828_dmaqueue * dma_q,struct au0828_buffer * buf,unsigned char * p,unsigned char * outp,unsigned long len)308 static 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 */
get_next_buf(struct au0828_dmaqueue * dma_q,struct au0828_buffer ** buf)389 static 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(&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(&(*buf)->list);
405 dma_q->pos = 0;
406 (*buf)->vb_buf = (*buf)->mem;
407 dev->isoc_ctl.buf = *buf;
408
409 return;
410 }
411
au0828_copy_vbi(struct au0828_dev * dev,struct au0828_dmaqueue * dma_q,struct au0828_buffer * buf,unsigned char * p,unsigned char * outp,unsigned long len)412 static 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 */
vbi_get_next_buf(struct au0828_dmaqueue * dma_q,struct au0828_buffer ** buf)464 static 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(&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(&(*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 */
au0828_isoc_copy(struct au0828_dev * dev,struct urb * urb)489 static 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, -1, 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(&buf->vb.vb2_buf, 0);
519
520 vbi_buf = dev->isoc_ctl.vbi_buf;
521 if (vbi_buf != NULL)
522 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 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, 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, vbi_dma_q, vbi_buf);
556 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
557 if (vbi_buf == NULL)
558 vbioutp = NULL;
559 else
560 vbioutp = vb2_plane_vaddr(
561 &vbi_buf->vb.vb2_buf, 0);
562
563 /* Video */
564 if (buf != NULL)
565 buffer_filled(dev, dma_q, buf);
566 get_next_buf(dma_q, &buf);
567 if (buf == NULL)
568 outp = NULL;
569 else
570 outp = vb2_plane_vaddr(
571 &buf->vb.vb2_buf, 0);
572
573 /* As long as isoc traffic is arriving, keep
574 resetting the timer */
575 if (dev->vid_timeout_running)
576 mod_timer(&dev->vid_timeout,
577 jiffies + (HZ / 10));
578 if (dev->vbi_timeout_running)
579 mod_timer(&dev->vbi_timeout,
580 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 lencopy = umin(len, remain);
606
607 if (vbi_buf != NULL)
608 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
609 vbioutp, len);
610
611 len -= lencopy;
612 p += lencopy;
613 dev->vbi_read += lencopy;
614 }
615
616 if (dev->vbi_read >= vbi_field_size && buf != NULL)
617 au0828_copy_video(dev, dma_q, buf, p, outp, len);
618 }
619 return rc;
620 }
621
au0828_usb_v4l2_media_release(struct au0828_dev * dev)622 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
623 {
624 #ifdef CONFIG_MEDIA_CONTROLLER
625 int i;
626
627 for (i = 0; i < AU0828_MAX_INPUT; i++) {
628 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
629 return;
630 media_device_unregister_entity(&dev->input_ent[i]);
631 }
632 #endif
633 }
634
au0828_usb_v4l2_release(struct v4l2_device * v4l2_dev)635 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
636 {
637 struct au0828_dev *dev =
638 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
639
640 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
641 v4l2_device_unregister(&dev->v4l2_dev);
642 au0828_usb_v4l2_media_release(dev);
643 au0828_usb_release(dev);
644 }
645
au0828_v4l2_device_register(struct usb_interface * interface,struct au0828_dev * dev)646 int au0828_v4l2_device_register(struct usb_interface *interface,
647 struct au0828_dev *dev)
648 {
649 int retval;
650
651 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
652 return 0;
653
654 /* Create the v4l2_device */
655 #ifdef CONFIG_MEDIA_CONTROLLER
656 dev->v4l2_dev.mdev = dev->media_dev;
657 #endif
658 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
659 if (retval) {
660 pr_err("%s() v4l2_device_register failed\n",
661 __func__);
662 return retval;
663 }
664
665 dev->v4l2_dev.release = au0828_usb_v4l2_release;
666
667 /* This control handler will inherit the controls from au8522 */
668 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
669 if (retval) {
670 pr_err("%s() v4l2_ctrl_handler_init failed\n",
671 __func__);
672 return retval;
673 }
674 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
675
676 return 0;
677 }
678
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])679 static int queue_setup(struct vb2_queue *vq,
680 unsigned int *nbuffers, unsigned int *nplanes,
681 unsigned int sizes[], struct device *alloc_devs[])
682 {
683 struct au0828_dev *dev = vb2_get_drv_priv(vq);
684 unsigned long size = dev->height * dev->bytesperline;
685
686 if (*nplanes)
687 return sizes[0] < size ? -EINVAL : 0;
688 *nplanes = 1;
689 sizes[0] = size;
690 return 0;
691 }
692
693 static int
buffer_prepare(struct vb2_buffer * vb)694 buffer_prepare(struct vb2_buffer *vb)
695 {
696 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
697 struct au0828_buffer *buf = container_of(vbuf,
698 struct au0828_buffer, vb);
699 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
700
701 buf->length = dev->height * dev->bytesperline;
702
703 if (vb2_plane_size(vb, 0) < buf->length) {
704 pr_err("%s data will not fit into plane (%lu < %lu)\n",
705 __func__, vb2_plane_size(vb, 0), buf->length);
706 return -EINVAL;
707 }
708 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
709 return 0;
710 }
711
712 static void
buffer_queue(struct vb2_buffer * vb)713 buffer_queue(struct vb2_buffer *vb)
714 {
715 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
716 struct au0828_buffer *buf = container_of(vbuf,
717 struct au0828_buffer,
718 vb);
719 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
720 struct au0828_dmaqueue *vidq = &dev->vidq;
721 unsigned long flags = 0;
722
723 buf->mem = vb2_plane_vaddr(vb, 0);
724 buf->length = vb2_plane_size(vb, 0);
725
726 spin_lock_irqsave(&dev->slock, flags);
727 list_add_tail(&buf->list, &vidq->active);
728 spin_unlock_irqrestore(&dev->slock, flags);
729 }
730
au0828_i2s_init(struct au0828_dev * dev)731 static int au0828_i2s_init(struct au0828_dev *dev)
732 {
733 /* Enable i2s mode */
734 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
735 return 0;
736 }
737
738 /*
739 * Auvitek au0828 analog stream enable
740 */
au0828_analog_stream_enable(struct au0828_dev * d)741 static int au0828_analog_stream_enable(struct au0828_dev *d)
742 {
743 struct usb_interface *iface;
744 int ret, h, w;
745
746 dprintk(1, "au0828_analog_stream_enable called\n");
747
748 if (test_bit(DEV_DISCONNECTED, &d->dev_state))
749 return -ENODEV;
750
751 iface = usb_ifnum_to_if(d->usbdev, 0);
752 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
753 dprintk(1, "Changing intf#0 to alt 5\n");
754 /* set au0828 interface0 to AS5 here again */
755 ret = usb_set_interface(d->usbdev, 0, 5);
756 if (ret < 0) {
757 pr_info("Au0828 can't set alt setting to 5!\n");
758 return -EBUSY;
759 }
760 }
761
762 h = d->height / 2 + 2;
763 w = d->width * 2;
764
765 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
766 au0828_writereg(d, 0x106, 0x00);
767 /* set x position */
768 au0828_writereg(d, 0x110, 0x00);
769 au0828_writereg(d, 0x111, 0x00);
770 au0828_writereg(d, 0x114, w & 0xff);
771 au0828_writereg(d, 0x115, w >> 8);
772 /* set y position */
773 au0828_writereg(d, 0x112, 0x00);
774 au0828_writereg(d, 0x113, 0x00);
775 au0828_writereg(d, 0x116, h & 0xff);
776 au0828_writereg(d, 0x117, h >> 8);
777 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
778
779 return 0;
780 }
781
au0828_analog_stream_disable(struct au0828_dev * d)782 static int au0828_analog_stream_disable(struct au0828_dev *d)
783 {
784 dprintk(1, "au0828_analog_stream_disable called\n");
785 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
786 return 0;
787 }
788
au0828_analog_stream_reset(struct au0828_dev * dev)789 static void au0828_analog_stream_reset(struct au0828_dev *dev)
790 {
791 dprintk(1, "au0828_analog_stream_reset called\n");
792 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
793 mdelay(30);
794 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
795 }
796
797 /*
798 * Some operations needs to stop current streaming
799 */
au0828_stream_interrupt(struct au0828_dev * dev)800 static int au0828_stream_interrupt(struct au0828_dev *dev)
801 {
802 dev->stream_state = STREAM_INTERRUPT;
803 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
804 return -ENODEV;
805 return 0;
806 }
807
au0828_start_analog_streaming(struct vb2_queue * vq,unsigned int count)808 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
809 {
810 struct au0828_dev *dev = vb2_get_drv_priv(vq);
811 int rc = 0;
812
813 dprintk(1, "au0828_start_analog_streaming called %d\n",
814 dev->streaming_users);
815
816 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
817 dev->frame_count = 0;
818 else
819 dev->vbi_frame_count = 0;
820
821 if (dev->streaming_users == 0) {
822 /* If we were doing ac97 instead of i2s, it would go here...*/
823 au0828_i2s_init(dev);
824 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
825 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
826 au0828_isoc_copy);
827 if (rc < 0) {
828 pr_info("au0828_init_isoc failed\n");
829 return rc;
830 }
831
832 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
833
834 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
835 dev->vid_timeout_running = 1;
836 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
837 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
838 dev->vbi_timeout_running = 1;
839 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
840 }
841 }
842 dev->streaming_users++;
843 return rc;
844 }
845
au0828_stop_streaming(struct vb2_queue * vq)846 static void au0828_stop_streaming(struct vb2_queue *vq)
847 {
848 struct au0828_dev *dev = vb2_get_drv_priv(vq);
849 struct au0828_dmaqueue *vidq = &dev->vidq;
850 unsigned long flags = 0;
851
852 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
853
854 if (dev->streaming_users-- == 1) {
855 au0828_uninit_isoc(dev);
856 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
857 }
858
859 dev->vid_timeout_running = 0;
860 del_timer_sync(&dev->vid_timeout);
861
862 spin_lock_irqsave(&dev->slock, flags);
863 if (dev->isoc_ctl.buf != NULL) {
864 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
865 VB2_BUF_STATE_ERROR);
866 dev->isoc_ctl.buf = NULL;
867 }
868 while (!list_empty(&vidq->active)) {
869 struct au0828_buffer *buf;
870
871 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
872 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
873 list_del(&buf->list);
874 }
875 spin_unlock_irqrestore(&dev->slock, flags);
876 }
877
au0828_stop_vbi_streaming(struct vb2_queue * vq)878 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
879 {
880 struct au0828_dev *dev = vb2_get_drv_priv(vq);
881 struct au0828_dmaqueue *vbiq = &dev->vbiq;
882 unsigned long flags = 0;
883
884 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
885 dev->streaming_users);
886
887 if (dev->streaming_users-- == 1) {
888 au0828_uninit_isoc(dev);
889 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
890 }
891
892 spin_lock_irqsave(&dev->slock, flags);
893 if (dev->isoc_ctl.vbi_buf != NULL) {
894 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
895 VB2_BUF_STATE_ERROR);
896 dev->isoc_ctl.vbi_buf = NULL;
897 }
898 while (!list_empty(&vbiq->active)) {
899 struct au0828_buffer *buf;
900
901 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
902 list_del(&buf->list);
903 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
904 }
905 spin_unlock_irqrestore(&dev->slock, flags);
906
907 dev->vbi_timeout_running = 0;
908 del_timer_sync(&dev->vbi_timeout);
909 }
910
911 static const struct vb2_ops au0828_video_qops = {
912 .queue_setup = queue_setup,
913 .buf_prepare = buffer_prepare,
914 .buf_queue = buffer_queue,
915 .prepare_streaming = v4l_vb2q_enable_media_source,
916 .start_streaming = au0828_start_analog_streaming,
917 .stop_streaming = au0828_stop_streaming,
918 .wait_prepare = vb2_ops_wait_prepare,
919 .wait_finish = vb2_ops_wait_finish,
920 };
921
922 /* ------------------------------------------------------------------
923 V4L2 interface
924 ------------------------------------------------------------------*/
925 /*
926 * au0828_analog_unregister
927 * unregister v4l2 devices
928 */
au0828_analog_unregister(struct au0828_dev * dev)929 int au0828_analog_unregister(struct au0828_dev *dev)
930 {
931 dprintk(1, "au0828_analog_unregister called\n");
932
933 /* No analog TV */
934 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
935 return 0;
936
937 mutex_lock(&au0828_sysfs_lock);
938 vb2_video_unregister_device(&dev->vdev);
939 vb2_video_unregister_device(&dev->vbi_dev);
940 mutex_unlock(&au0828_sysfs_lock);
941
942 v4l2_device_disconnect(&dev->v4l2_dev);
943 v4l2_device_put(&dev->v4l2_dev);
944
945 return 1;
946 }
947
948 /* This function ensures that video frames continue to be delivered even if
949 the ITU-656 input isn't receiving any data (thereby preventing applications
950 such as tvtime from hanging) */
au0828_vid_buffer_timeout(struct timer_list * t)951 static void au0828_vid_buffer_timeout(struct timer_list *t)
952 {
953 struct au0828_dev *dev = from_timer(dev, t, vid_timeout);
954 struct au0828_dmaqueue *dma_q = &dev->vidq;
955 struct au0828_buffer *buf;
956 unsigned char *vid_data;
957 unsigned long flags = 0;
958
959 spin_lock_irqsave(&dev->slock, flags);
960
961 buf = dev->isoc_ctl.buf;
962 if (buf != NULL) {
963 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
964 memset(vid_data, 0x00, buf->length); /* Blank green frame */
965 buffer_filled(dev, dma_q, buf);
966 }
967 get_next_buf(dma_q, &buf);
968
969 if (dev->vid_timeout_running == 1)
970 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
971
972 spin_unlock_irqrestore(&dev->slock, flags);
973 }
974
au0828_vbi_buffer_timeout(struct timer_list * t)975 static void au0828_vbi_buffer_timeout(struct timer_list *t)
976 {
977 struct au0828_dev *dev = from_timer(dev, t, vbi_timeout);
978 struct au0828_dmaqueue *dma_q = &dev->vbiq;
979 struct au0828_buffer *buf;
980 unsigned char *vbi_data;
981 unsigned long flags = 0;
982
983 spin_lock_irqsave(&dev->slock, flags);
984
985 buf = dev->isoc_ctl.vbi_buf;
986 if (buf != NULL) {
987 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
988 memset(vbi_data, 0x00, buf->length);
989 buffer_filled(dev, dma_q, buf);
990 }
991 vbi_get_next_buf(dma_q, &buf);
992
993 if (dev->vbi_timeout_running == 1)
994 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
995 spin_unlock_irqrestore(&dev->slock, flags);
996 }
997
au0828_v4l2_open(struct file * filp)998 static int au0828_v4l2_open(struct file *filp)
999 {
1000 struct au0828_dev *dev = video_drvdata(filp);
1001 int ret;
1002
1003 dprintk(1,
1004 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1005 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1006 dev->streaming_users, dev->users);
1007
1008 if (mutex_lock_interruptible(&dev->lock))
1009 return -ERESTARTSYS;
1010
1011 ret = v4l2_fh_open(filp);
1012 if (ret) {
1013 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1014 __func__, ret);
1015 mutex_unlock(&dev->lock);
1016 return ret;
1017 }
1018
1019 if (dev->users == 0) {
1020 au0828_analog_stream_enable(dev);
1021 au0828_analog_stream_reset(dev);
1022 dev->stream_state = STREAM_OFF;
1023 set_bit(DEV_INITIALIZED, &dev->dev_state);
1024 }
1025 dev->users++;
1026 mutex_unlock(&dev->lock);
1027 return ret;
1028 }
1029
au0828_v4l2_close(struct file * filp)1030 static int au0828_v4l2_close(struct file *filp)
1031 {
1032 int ret;
1033 struct au0828_dev *dev = video_drvdata(filp);
1034 struct video_device *vdev = video_devdata(filp);
1035
1036 dprintk(1,
1037 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1038 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1039 dev->streaming_users, dev->users);
1040
1041 mutex_lock(&dev->lock);
1042 if (vdev->vfl_type == VFL_TYPE_VIDEO && dev->vid_timeout_running) {
1043 /* Cancel timeout thread in case they didn't call streamoff */
1044 dev->vid_timeout_running = 0;
1045 del_timer_sync(&dev->vid_timeout);
1046 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1047 dev->vbi_timeout_running) {
1048 /* Cancel timeout thread in case they didn't call streamoff */
1049 dev->vbi_timeout_running = 0;
1050 del_timer_sync(&dev->vbi_timeout);
1051 }
1052
1053 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1054 goto end;
1055
1056 if (dev->users == 1) {
1057 /*
1058 * Avoid putting tuner in sleep if DVB or ALSA are
1059 * streaming.
1060 *
1061 * On most USB devices like au0828 the tuner can
1062 * be safely put in sleep state here if ALSA isn't
1063 * streaming. Exceptions are some very old USB tuner
1064 * models such as em28xx-based WinTV USB2 which have
1065 * a separate audio output jack. The devices that have
1066 * a separate audio output jack have analog tuners,
1067 * like Philips FM1236. Those devices are always on,
1068 * so the s_power callback are silently ignored.
1069 * So, the current logic here does the following:
1070 * Disable (put tuner to sleep) when
1071 * - ALSA and DVB aren't streaming.
1072 * - the last V4L2 file handler is closed.
1073 *
1074 * FIXME:
1075 *
1076 * Additionally, this logic could be improved to
1077 * disable the media source if the above conditions
1078 * are met and if the device:
1079 * - doesn't have a separate audio out plug (or
1080 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1081 *
1082 * Once this additional logic is in place, a callback
1083 * is needed to enable the media source and power on
1084 * the tuner, for radio to work.
1085 */
1086 ret = v4l_enable_media_source(vdev);
1087 if (ret == 0)
1088 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
1089 standby);
1090 dev->std_set_in_tuner_core = 0;
1091
1092 /* When close the device, set the usb intf0 into alt0 to free
1093 USB bandwidth */
1094 ret = usb_set_interface(dev->usbdev, 0, 0);
1095 if (ret < 0)
1096 pr_info("Au0828 can't set alternate to 0!\n");
1097 }
1098 end:
1099 _vb2_fop_release(filp, NULL);
1100 dev->users--;
1101 mutex_unlock(&dev->lock);
1102 return 0;
1103 }
1104
1105 /* Must be called with dev->lock held */
au0828_init_tuner(struct au0828_dev * dev)1106 static void au0828_init_tuner(struct au0828_dev *dev)
1107 {
1108 struct v4l2_frequency f = {
1109 .frequency = dev->ctrl_freq,
1110 .type = V4L2_TUNER_ANALOG_TV,
1111 };
1112
1113 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1114 dev->std_set_in_tuner_core, dev->dev_state);
1115
1116 if (dev->std_set_in_tuner_core)
1117 return;
1118 dev->std_set_in_tuner_core = 1;
1119 i2c_gate_ctrl(dev, 1);
1120 /* If we've never sent the standard in tuner core, do so now.
1121 We don't do this at device probe because we don't want to
1122 incur the cost of a firmware load */
1123 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1124 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1125 i2c_gate_ctrl(dev, 0);
1126 }
1127
au0828_set_format(struct au0828_dev * dev,unsigned int cmd,struct v4l2_format * format)1128 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1129 struct v4l2_format *format)
1130 {
1131 int ret;
1132 int width = format->fmt.pix.width;
1133 int height = format->fmt.pix.height;
1134
1135 /* If they are demanding a format other than the one we support,
1136 bail out (tvtime asks for UYVY and then retries with YUYV) */
1137 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1138 return -EINVAL;
1139
1140 /* format->fmt.pix.width only support 720 and height 480 */
1141 if (width != 720)
1142 width = 720;
1143 if (height != 480)
1144 height = 480;
1145
1146 format->fmt.pix.width = width;
1147 format->fmt.pix.height = height;
1148 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1149 format->fmt.pix.bytesperline = width * 2;
1150 format->fmt.pix.sizeimage = width * height * 2;
1151 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1152 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1153
1154 if (cmd == VIDIOC_TRY_FMT)
1155 return 0;
1156
1157 /* maybe set new image format, driver current only support 720*480 */
1158 dev->width = width;
1159 dev->height = height;
1160 dev->frame_size = width * height * 2;
1161 dev->field_size = width * height;
1162 dev->bytesperline = width * 2;
1163
1164 if (dev->stream_state == STREAM_ON) {
1165 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1166 ret = au0828_stream_interrupt(dev);
1167 if (ret != 0) {
1168 dprintk(1, "error interrupting video stream!\n");
1169 return ret;
1170 }
1171 }
1172
1173 au0828_analog_stream_enable(dev);
1174
1175 return 0;
1176 }
1177
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1178 static int vidioc_querycap(struct file *file, void *priv,
1179 struct v4l2_capability *cap)
1180 {
1181 struct au0828_dev *dev = video_drvdata(file);
1182
1183 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1184 dev->std_set_in_tuner_core, dev->dev_state);
1185
1186 strscpy(cap->driver, "au0828", sizeof(cap->driver));
1187 strscpy(cap->card, dev->board.name, sizeof(cap->card));
1188 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1189
1190 /* set the device capabilities */
1191 cap->capabilities =
1192 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1193 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1194 V4L2_CAP_DEVICE_CAPS;
1195 return 0;
1196 }
1197
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)1198 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1199 struct v4l2_fmtdesc *f)
1200 {
1201 if (f->index)
1202 return -EINVAL;
1203
1204 dprintk(1, "%s called\n", __func__);
1205
1206 f->pixelformat = V4L2_PIX_FMT_UYVY;
1207
1208 return 0;
1209 }
1210
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1211 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1212 struct v4l2_format *f)
1213 {
1214 struct au0828_dev *dev = video_drvdata(file);
1215
1216 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1217 dev->std_set_in_tuner_core, dev->dev_state);
1218
1219 f->fmt.pix.width = dev->width;
1220 f->fmt.pix.height = dev->height;
1221 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1222 f->fmt.pix.bytesperline = dev->bytesperline;
1223 f->fmt.pix.sizeimage = dev->frame_size;
1224 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1225 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1226 return 0;
1227 }
1228
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1229 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1230 struct v4l2_format *f)
1231 {
1232 struct au0828_dev *dev = video_drvdata(file);
1233
1234 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1235 dev->std_set_in_tuner_core, dev->dev_state);
1236
1237 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1238 }
1239
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1240 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1241 struct v4l2_format *f)
1242 {
1243 struct au0828_dev *dev = video_drvdata(file);
1244 int rc;
1245
1246 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1247 dev->std_set_in_tuner_core, dev->dev_state);
1248
1249 rc = check_dev(dev);
1250 if (rc < 0)
1251 return rc;
1252
1253 if (vb2_is_busy(&dev->vb_vidq)) {
1254 pr_info("%s queue busy\n", __func__);
1255 rc = -EBUSY;
1256 goto out;
1257 }
1258
1259 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1260 out:
1261 return rc;
1262 }
1263
vidioc_s_std(struct file * file,void * priv,v4l2_std_id norm)1264 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1265 {
1266 struct au0828_dev *dev = video_drvdata(file);
1267
1268 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1269 dev->std_set_in_tuner_core, dev->dev_state);
1270
1271 if (norm == dev->std)
1272 return 0;
1273
1274 if (dev->streaming_users > 0)
1275 return -EBUSY;
1276
1277 dev->std = norm;
1278
1279 au0828_init_tuner(dev);
1280
1281 i2c_gate_ctrl(dev, 1);
1282
1283 /*
1284 * FIXME: when we support something other than 60Hz standards,
1285 * we are going to have to make the au0828 bridge adjust the size
1286 * of its capture buffer, which is currently hardcoded at 720x480
1287 */
1288
1289 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1290
1291 i2c_gate_ctrl(dev, 0);
1292
1293 return 0;
1294 }
1295
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * norm)1296 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1297 {
1298 struct au0828_dev *dev = video_drvdata(file);
1299
1300 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1301 dev->std_set_in_tuner_core, dev->dev_state);
1302
1303 *norm = dev->std;
1304 return 0;
1305 }
1306
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * input)1307 static int vidioc_enum_input(struct file *file, void *priv,
1308 struct v4l2_input *input)
1309 {
1310 struct au0828_dev *dev = video_drvdata(file);
1311 unsigned int tmp;
1312
1313 static const char *inames[] = {
1314 [AU0828_VMUX_UNDEFINED] = "Undefined",
1315 [AU0828_VMUX_COMPOSITE] = "Composite",
1316 [AU0828_VMUX_SVIDEO] = "S-Video",
1317 [AU0828_VMUX_CABLE] = "Cable TV",
1318 [AU0828_VMUX_TELEVISION] = "Television",
1319 [AU0828_VMUX_DVB] = "DVB",
1320 };
1321
1322 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1323 dev->std_set_in_tuner_core, dev->dev_state);
1324
1325 tmp = input->index;
1326
1327 if (tmp >= AU0828_MAX_INPUT)
1328 return -EINVAL;
1329 if (AUVI_INPUT(tmp).type == 0)
1330 return -EINVAL;
1331
1332 input->index = tmp;
1333 strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name));
1334 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1335 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1336 input->type |= V4L2_INPUT_TYPE_TUNER;
1337 input->audioset = 1;
1338 } else {
1339 input->type |= V4L2_INPUT_TYPE_CAMERA;
1340 input->audioset = 2;
1341 }
1342
1343 input->std = dev->vdev.tvnorms;
1344
1345 return 0;
1346 }
1347
vidioc_g_input(struct file * file,void * priv,unsigned int * i)1348 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1349 {
1350 struct au0828_dev *dev = video_drvdata(file);
1351
1352 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1353 dev->std_set_in_tuner_core, dev->dev_state);
1354
1355 *i = dev->ctrl_input;
1356 return 0;
1357 }
1358
au0828_s_input(struct au0828_dev * dev,int index)1359 static void au0828_s_input(struct au0828_dev *dev, int index)
1360 {
1361 int i;
1362
1363 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1364 dev->std_set_in_tuner_core, dev->dev_state);
1365
1366 switch (AUVI_INPUT(index).type) {
1367 case AU0828_VMUX_SVIDEO:
1368 dev->input_type = AU0828_VMUX_SVIDEO;
1369 dev->ctrl_ainput = 1;
1370 break;
1371 case AU0828_VMUX_COMPOSITE:
1372 dev->input_type = AU0828_VMUX_COMPOSITE;
1373 dev->ctrl_ainput = 1;
1374 break;
1375 case AU0828_VMUX_TELEVISION:
1376 dev->input_type = AU0828_VMUX_TELEVISION;
1377 dev->ctrl_ainput = 0;
1378 break;
1379 default:
1380 dprintk(1, "unknown input type set [%d]\n",
1381 AUVI_INPUT(index).type);
1382 return;
1383 }
1384
1385 dev->ctrl_input = index;
1386
1387 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1388 AUVI_INPUT(index).vmux, 0, 0);
1389
1390 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1391 int enable = 0;
1392 if (AUVI_INPUT(i).audio_setup == NULL)
1393 continue;
1394
1395 if (i == index)
1396 enable = 1;
1397 else
1398 enable = 0;
1399 if (enable) {
1400 (AUVI_INPUT(i).audio_setup)(dev, enable);
1401 } else {
1402 /* Make sure we leave it turned on if some
1403 other input is routed to this callback */
1404 if ((AUVI_INPUT(i).audio_setup) !=
1405 ((AUVI_INPUT(index).audio_setup))) {
1406 (AUVI_INPUT(i).audio_setup)(dev, enable);
1407 }
1408 }
1409 }
1410
1411 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1412 AUVI_INPUT(index).amux, 0, 0);
1413 }
1414
vidioc_s_input(struct file * file,void * priv,unsigned int index)1415 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1416 {
1417 struct au0828_dev *dev = video_drvdata(file);
1418 struct video_device *vfd = video_devdata(file);
1419
1420 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1421 index);
1422 if (index >= AU0828_MAX_INPUT)
1423 return -EINVAL;
1424 if (AUVI_INPUT(index).type == 0)
1425 return -EINVAL;
1426
1427 if (dev->ctrl_input == index)
1428 return 0;
1429
1430 au0828_s_input(dev, index);
1431
1432 /*
1433 * Input has been changed. Disable the media source
1434 * associated with the old input and enable source
1435 * for the newly set input
1436 */
1437 v4l_disable_media_source(vfd);
1438 return v4l_enable_media_source(vfd);
1439 }
1440
vidioc_enumaudio(struct file * file,void * priv,struct v4l2_audio * a)1441 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1442 {
1443 if (a->index > 1)
1444 return -EINVAL;
1445
1446 dprintk(1, "%s called\n", __func__);
1447
1448 if (a->index == 0)
1449 strscpy(a->name, "Television", sizeof(a->name));
1450 else
1451 strscpy(a->name, "Line in", sizeof(a->name));
1452
1453 a->capability = V4L2_AUDCAP_STEREO;
1454 return 0;
1455 }
1456
vidioc_g_audio(struct file * file,void * priv,struct v4l2_audio * a)1457 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1458 {
1459 struct au0828_dev *dev = video_drvdata(file);
1460
1461 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1462 dev->std_set_in_tuner_core, dev->dev_state);
1463
1464 a->index = dev->ctrl_ainput;
1465 if (a->index == 0)
1466 strscpy(a->name, "Television", sizeof(a->name));
1467 else
1468 strscpy(a->name, "Line in", sizeof(a->name));
1469
1470 a->capability = V4L2_AUDCAP_STEREO;
1471 return 0;
1472 }
1473
vidioc_s_audio(struct file * file,void * priv,const struct v4l2_audio * a)1474 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1475 {
1476 struct au0828_dev *dev = video_drvdata(file);
1477
1478 if (a->index != dev->ctrl_ainput)
1479 return -EINVAL;
1480
1481 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1482 dev->std_set_in_tuner_core, dev->dev_state);
1483 return 0;
1484 }
1485
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1486 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1487 {
1488 struct au0828_dev *dev = video_drvdata(file);
1489 struct video_device *vfd = video_devdata(file);
1490 int ret;
1491
1492 if (t->index != 0)
1493 return -EINVAL;
1494
1495 ret = v4l_enable_media_source(vfd);
1496 if (ret)
1497 return ret;
1498
1499 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1500 dev->std_set_in_tuner_core, dev->dev_state);
1501
1502 strscpy(t->name, "Auvitek tuner", sizeof(t->name));
1503
1504 au0828_init_tuner(dev);
1505 i2c_gate_ctrl(dev, 1);
1506 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1507 i2c_gate_ctrl(dev, 0);
1508 return 0;
1509 }
1510
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1511 static int vidioc_s_tuner(struct file *file, void *priv,
1512 const struct v4l2_tuner *t)
1513 {
1514 struct au0828_dev *dev = video_drvdata(file);
1515
1516 if (t->index != 0)
1517 return -EINVAL;
1518
1519 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1520 dev->std_set_in_tuner_core, dev->dev_state);
1521
1522 au0828_init_tuner(dev);
1523 i2c_gate_ctrl(dev, 1);
1524 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1525 i2c_gate_ctrl(dev, 0);
1526
1527 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1528 t->afc);
1529
1530 return 0;
1531
1532 }
1533
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * freq)1534 static int vidioc_g_frequency(struct file *file, void *priv,
1535 struct v4l2_frequency *freq)
1536 {
1537 struct au0828_dev *dev = video_drvdata(file);
1538
1539 if (freq->tuner != 0)
1540 return -EINVAL;
1541 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1542 dev->std_set_in_tuner_core, dev->dev_state);
1543 freq->frequency = dev->ctrl_freq;
1544 return 0;
1545 }
1546
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * freq)1547 static int vidioc_s_frequency(struct file *file, void *priv,
1548 const struct v4l2_frequency *freq)
1549 {
1550 struct au0828_dev *dev = video_drvdata(file);
1551 struct v4l2_frequency new_freq = *freq;
1552
1553 if (freq->tuner != 0)
1554 return -EINVAL;
1555
1556 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1557 dev->std_set_in_tuner_core, dev->dev_state);
1558
1559 au0828_init_tuner(dev);
1560 i2c_gate_ctrl(dev, 1);
1561
1562 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1563 /* Get the actual set (and possibly clamped) frequency */
1564 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1565 dev->ctrl_freq = new_freq.frequency;
1566
1567 i2c_gate_ctrl(dev, 0);
1568
1569 au0828_analog_stream_reset(dev);
1570
1571 return 0;
1572 }
1573
1574
1575 /* RAW VBI ioctls */
1576
vidioc_g_fmt_vbi_cap(struct file * file,void * priv,struct v4l2_format * format)1577 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1578 struct v4l2_format *format)
1579 {
1580 struct au0828_dev *dev = video_drvdata(file);
1581
1582 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1583 dev->std_set_in_tuner_core, dev->dev_state);
1584
1585 format->fmt.vbi.samples_per_line = dev->vbi_width;
1586 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1587 format->fmt.vbi.offset = 0;
1588 format->fmt.vbi.flags = 0;
1589 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1590
1591 format->fmt.vbi.count[0] = dev->vbi_height;
1592 format->fmt.vbi.count[1] = dev->vbi_height;
1593 format->fmt.vbi.start[0] = 21;
1594 format->fmt.vbi.start[1] = 284;
1595 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1596
1597 return 0;
1598 }
1599
vidioc_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)1600 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1601 int type, struct v4l2_fract *f)
1602 {
1603 struct au0828_dev *dev = video_drvdata(file);
1604
1605 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1606 return -EINVAL;
1607
1608 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1609 dev->std_set_in_tuner_core, dev->dev_state);
1610
1611 f->numerator = 54;
1612 f->denominator = 59;
1613
1614 return 0;
1615 }
1616
vidioc_g_selection(struct file * file,void * priv,struct v4l2_selection * s)1617 static int vidioc_g_selection(struct file *file, void *priv,
1618 struct v4l2_selection *s)
1619 {
1620 struct au0828_dev *dev = video_drvdata(file);
1621
1622 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1623 return -EINVAL;
1624
1625 switch (s->target) {
1626 case V4L2_SEL_TGT_CROP_BOUNDS:
1627 case V4L2_SEL_TGT_CROP_DEFAULT:
1628 s->r.left = 0;
1629 s->r.top = 0;
1630 s->r.width = dev->width;
1631 s->r.height = dev->height;
1632 break;
1633 default:
1634 return -EINVAL;
1635 }
1636 return 0;
1637 }
1638
1639 #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_register(struct file * file,void * priv,struct v4l2_dbg_register * reg)1640 static int vidioc_g_register(struct file *file, void *priv,
1641 struct v4l2_dbg_register *reg)
1642 {
1643 struct au0828_dev *dev = video_drvdata(file);
1644
1645 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1646 dev->std_set_in_tuner_core, dev->dev_state);
1647
1648 reg->val = au0828_read(dev, reg->reg);
1649 reg->size = 1;
1650 return 0;
1651 }
1652
vidioc_s_register(struct file * file,void * priv,const struct v4l2_dbg_register * reg)1653 static int vidioc_s_register(struct file *file, void *priv,
1654 const struct v4l2_dbg_register *reg)
1655 {
1656 struct au0828_dev *dev = video_drvdata(file);
1657
1658 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1659 dev->std_set_in_tuner_core, dev->dev_state);
1660
1661 return au0828_writereg(dev, reg->reg, reg->val);
1662 }
1663 #endif
1664
vidioc_log_status(struct file * file,void * fh)1665 static int vidioc_log_status(struct file *file, void *fh)
1666 {
1667 struct video_device *vdev = video_devdata(file);
1668
1669 dprintk(1, "%s called\n", __func__);
1670
1671 v4l2_ctrl_log_status(file, fh);
1672 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1673 return 0;
1674 }
1675
au0828_v4l2_suspend(struct au0828_dev * dev)1676 void au0828_v4l2_suspend(struct au0828_dev *dev)
1677 {
1678 struct urb *urb;
1679 int i;
1680
1681 pr_info("stopping V4L2\n");
1682
1683 if (dev->stream_state == STREAM_ON) {
1684 pr_info("stopping V4L2 active URBs\n");
1685 au0828_analog_stream_disable(dev);
1686 /* stop urbs */
1687 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1688 urb = dev->isoc_ctl.urb[i];
1689 if (urb) {
1690 if (!irqs_disabled())
1691 usb_kill_urb(urb);
1692 else
1693 usb_unlink_urb(urb);
1694 }
1695 }
1696 }
1697
1698 if (dev->vid_timeout_running)
1699 del_timer_sync(&dev->vid_timeout);
1700 if (dev->vbi_timeout_running)
1701 del_timer_sync(&dev->vbi_timeout);
1702 }
1703
au0828_v4l2_resume(struct au0828_dev * dev)1704 void au0828_v4l2_resume(struct au0828_dev *dev)
1705 {
1706 int i, rc;
1707
1708 pr_info("restarting V4L2\n");
1709
1710 if (dev->stream_state == STREAM_ON) {
1711 au0828_stream_interrupt(dev);
1712 au0828_init_tuner(dev);
1713 }
1714
1715 if (dev->vid_timeout_running)
1716 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1717 if (dev->vbi_timeout_running)
1718 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1719
1720 /* If we were doing ac97 instead of i2s, it would go here...*/
1721 au0828_i2s_init(dev);
1722
1723 au0828_analog_stream_enable(dev);
1724
1725 if (!(dev->stream_state == STREAM_ON)) {
1726 au0828_analog_stream_reset(dev);
1727 /* submit urbs */
1728 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1729 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1730 if (rc) {
1731 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1732 i, rc);
1733 au0828_uninit_isoc(dev);
1734 }
1735 }
1736 }
1737 }
1738
1739 static const struct v4l2_file_operations au0828_v4l_fops = {
1740 .owner = THIS_MODULE,
1741 .open = au0828_v4l2_open,
1742 .release = au0828_v4l2_close,
1743 .read = vb2_fop_read,
1744 .poll = vb2_fop_poll,
1745 .mmap = vb2_fop_mmap,
1746 .unlocked_ioctl = video_ioctl2,
1747 };
1748
1749 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1750 .vidioc_querycap = vidioc_querycap,
1751 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1752 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1753 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1754 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1755 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1756 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1757 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1758 .vidioc_enumaudio = vidioc_enumaudio,
1759 .vidioc_g_audio = vidioc_g_audio,
1760 .vidioc_s_audio = vidioc_s_audio,
1761 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1762 .vidioc_g_selection = vidioc_g_selection,
1763
1764 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1765 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1766 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1767 .vidioc_querybuf = vb2_ioctl_querybuf,
1768 .vidioc_qbuf = vb2_ioctl_qbuf,
1769 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1770 .vidioc_expbuf = vb2_ioctl_expbuf,
1771
1772 .vidioc_s_std = vidioc_s_std,
1773 .vidioc_g_std = vidioc_g_std,
1774 .vidioc_enum_input = vidioc_enum_input,
1775 .vidioc_g_input = vidioc_g_input,
1776 .vidioc_s_input = vidioc_s_input,
1777
1778 .vidioc_streamon = vb2_ioctl_streamon,
1779 .vidioc_streamoff = vb2_ioctl_streamoff,
1780
1781 .vidioc_g_tuner = vidioc_g_tuner,
1782 .vidioc_s_tuner = vidioc_s_tuner,
1783 .vidioc_g_frequency = vidioc_g_frequency,
1784 .vidioc_s_frequency = vidioc_s_frequency,
1785 #ifdef CONFIG_VIDEO_ADV_DEBUG
1786 .vidioc_g_register = vidioc_g_register,
1787 .vidioc_s_register = vidioc_s_register,
1788 #endif
1789 .vidioc_log_status = vidioc_log_status,
1790 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1791 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1792 };
1793
1794 static const struct video_device au0828_video_template = {
1795 .fops = &au0828_v4l_fops,
1796 .release = video_device_release_empty,
1797 .ioctl_ops = &video_ioctl_ops,
1798 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1799 };
1800
au0828_vb2_setup(struct au0828_dev * dev)1801 static int au0828_vb2_setup(struct au0828_dev *dev)
1802 {
1803 int rc;
1804 struct vb2_queue *q;
1805
1806 /* Setup Videobuf2 for Video capture */
1807 q = &dev->vb_vidq;
1808 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1809 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1810 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1811 q->drv_priv = dev;
1812 q->buf_struct_size = sizeof(struct au0828_buffer);
1813 q->ops = &au0828_video_qops;
1814 q->mem_ops = &vb2_vmalloc_memops;
1815
1816 rc = vb2_queue_init(q);
1817 if (rc < 0)
1818 return rc;
1819
1820 /* Setup Videobuf2 for VBI capture */
1821 q = &dev->vb_vbiq;
1822 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1823 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1824 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1825 q->drv_priv = dev;
1826 q->buf_struct_size = sizeof(struct au0828_buffer);
1827 q->ops = &au0828_vbi_qops;
1828 q->mem_ops = &vb2_vmalloc_memops;
1829
1830 rc = vb2_queue_init(q);
1831 if (rc < 0)
1832 return rc;
1833
1834 return 0;
1835 }
1836
au0828_analog_create_entities(struct au0828_dev * dev)1837 static void au0828_analog_create_entities(struct au0828_dev *dev)
1838 {
1839 #if defined(CONFIG_MEDIA_CONTROLLER)
1840 static const char * const inames[] = {
1841 [AU0828_VMUX_COMPOSITE] = "Composite",
1842 [AU0828_VMUX_SVIDEO] = "S-Video",
1843 [AU0828_VMUX_CABLE] = "Cable TV",
1844 [AU0828_VMUX_TELEVISION] = "Television",
1845 [AU0828_VMUX_DVB] = "DVB",
1846 };
1847 int ret, i;
1848
1849 /* Initialize Video and VBI pads */
1850 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1851 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1852 if (ret < 0)
1853 pr_err("failed to initialize video media entity!\n");
1854
1855 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1856 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1857 if (ret < 0)
1858 pr_err("failed to initialize vbi media entity!\n");
1859
1860 /* Create entities for each input connector */
1861 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1862 struct media_entity *ent = &dev->input_ent[i];
1863
1864 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1865 break;
1866
1867 ent->name = inames[AUVI_INPUT(i).type];
1868 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1869 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1870
1871 switch (AUVI_INPUT(i).type) {
1872 case AU0828_VMUX_COMPOSITE:
1873 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1874 break;
1875 case AU0828_VMUX_SVIDEO:
1876 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1877 break;
1878 case AU0828_VMUX_CABLE:
1879 case AU0828_VMUX_TELEVISION:
1880 case AU0828_VMUX_DVB:
1881 default: /* Just to shut up a warning */
1882 ent->function = MEDIA_ENT_F_CONN_RF;
1883 break;
1884 }
1885
1886 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1887 if (ret < 0)
1888 pr_err("failed to initialize input pad[%d]!\n", i);
1889
1890 ret = media_device_register_entity(dev->media_dev, ent);
1891 if (ret < 0)
1892 pr_err("failed to register input entity %d!\n", i);
1893 }
1894 #endif
1895 }
1896
1897 /**************************************************************************/
1898
au0828_analog_register(struct au0828_dev * dev,struct usb_interface * interface)1899 int au0828_analog_register(struct au0828_dev *dev,
1900 struct usb_interface *interface)
1901 {
1902 int retval = -ENOMEM;
1903 struct usb_host_interface *iface_desc;
1904 struct usb_endpoint_descriptor *endpoint;
1905 int i, ret;
1906
1907 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1908 interface->cur_altsetting->desc.bInterfaceNumber);
1909
1910 /* No analog TV */
1911 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1912 return 0;
1913
1914 /* set au0828 usb interface0 to as5 */
1915 retval = usb_set_interface(dev->usbdev,
1916 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1917 if (retval != 0) {
1918 pr_info("Failure setting usb interface0 to as5\n");
1919 return retval;
1920 }
1921
1922 /* Figure out which endpoint has the isoc interface */
1923 iface_desc = interface->cur_altsetting;
1924 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1925 endpoint = &iface_desc->endpoint[i].desc;
1926 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1927 == USB_DIR_IN) &&
1928 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1929 == USB_ENDPOINT_XFER_ISOC)) {
1930
1931 /* we find our isoc in endpoint */
1932 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1933 dev->max_pkt_size = (tmp & 0x07ff) *
1934 (((tmp & 0x1800) >> 11) + 1);
1935 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1936 dprintk(1,
1937 "Found isoc endpoint 0x%02x, max size = %d\n",
1938 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1939 }
1940 }
1941 if (!(dev->isoc_in_endpointaddr)) {
1942 pr_info("Could not locate isoc endpoint\n");
1943 return -ENODEV;
1944 }
1945
1946 init_waitqueue_head(&dev->open);
1947 spin_lock_init(&dev->slock);
1948
1949 /* init video dma queues */
1950 INIT_LIST_HEAD(&dev->vidq.active);
1951 INIT_LIST_HEAD(&dev->vbiq.active);
1952
1953 timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
1954 timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
1955
1956 dev->width = NTSC_STD_W;
1957 dev->height = NTSC_STD_H;
1958 dev->field_size = dev->width * dev->height;
1959 dev->frame_size = dev->field_size << 1;
1960 dev->bytesperline = dev->width << 1;
1961 dev->vbi_width = 720;
1962 dev->vbi_height = 1;
1963 dev->ctrl_ainput = 0;
1964 dev->ctrl_freq = 960;
1965 dev->std = V4L2_STD_NTSC_M;
1966 /* Default input is TV Tuner */
1967 au0828_s_input(dev, 0);
1968
1969 mutex_init(&dev->vb_queue_lock);
1970 mutex_init(&dev->vb_vbi_queue_lock);
1971
1972 /* Fill the video capture device struct */
1973 dev->vdev = au0828_video_template;
1974 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1975 dev->vdev.lock = &dev->lock;
1976 dev->vdev.queue = &dev->vb_vidq;
1977 dev->vdev.queue->lock = &dev->vb_queue_lock;
1978 dev->vdev.device_caps =
1979 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1980 V4L2_CAP_TUNER | V4L2_CAP_VIDEO_CAPTURE;
1981 strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name));
1982
1983 /* Setup the VBI device */
1984 dev->vbi_dev = au0828_video_template;
1985 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1986 dev->vbi_dev.lock = &dev->lock;
1987 dev->vbi_dev.queue = &dev->vb_vbiq;
1988 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1989 dev->vbi_dev.device_caps =
1990 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1991 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE;
1992 strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name));
1993
1994 /* Init entities at the Media Controller */
1995 au0828_analog_create_entities(dev);
1996
1997 /* initialize videobuf2 stuff */
1998 retval = au0828_vb2_setup(dev);
1999 if (retval != 0) {
2000 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2001 retval);
2002 return -ENODEV;
2003 }
2004
2005 /* Register the v4l2 device */
2006 video_set_drvdata(&dev->vdev, dev);
2007 retval = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
2008 if (retval != 0) {
2009 dprintk(1, "unable to register video device (error = %d).\n",
2010 retval);
2011 return -ENODEV;
2012 }
2013
2014 /* Register the vbi device */
2015 video_set_drvdata(&dev->vbi_dev, dev);
2016 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2017 if (retval != 0) {
2018 dprintk(1, "unable to register vbi device (error = %d).\n",
2019 retval);
2020 ret = -ENODEV;
2021 goto err_reg_vbi_dev;
2022 }
2023
2024 #ifdef CONFIG_MEDIA_CONTROLLER
2025 retval = v4l2_mc_create_media_graph(dev->media_dev);
2026 if (retval) {
2027 pr_err("%s() au0282_dev_register failed to create graph\n",
2028 __func__);
2029 ret = -ENODEV;
2030 goto err_reg_vbi_dev;
2031 }
2032 #endif
2033
2034 dprintk(1, "%s completed!\n", __func__);
2035
2036 return 0;
2037
2038 err_reg_vbi_dev:
2039 vb2_video_unregister_device(&dev->vdev);
2040 return ret;
2041 }
2042
2043