blob: 13b8387082f277ba63218c95c95396eb669d3f30 [file] [log] [blame]
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001/*
2 * Auvitek AU0828 USB Bridge (Analog video support)
3 *
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * As published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
23/* Developer Notes:
24 *
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030025 * The hardware scaler supported is unimplemented
26 * AC97 audio support is unimplemented (only i2s audio mode)
27 *
28 */
29
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030030#include "au0828.h"
Shuah Khan50512332016-02-11 21:41:28 -020031#include "au8522.h"
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030032
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030033#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030035#include <linux/init.h>
36#include <linux/device.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030037#include <media/v4l2-common.h>
Mauro Carvalho Chehab9822f412016-03-02 10:11:41 -030038#include <media/v4l2-mc.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030039#include <media/v4l2-ioctl.h>
Hans Verkuil83b09422013-02-15 09:14:00 -030040#include <media/v4l2-event.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030041#include <media/tuner.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030042#include "au0828-reg.h"
43
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030044static DEFINE_MUTEX(au0828_sysfs_lock);
45
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030046/* ------------------------------------------------------------------
47 Videobuf operations
48 ------------------------------------------------------------------*/
49
50static unsigned int isoc_debug;
51module_param(isoc_debug, int, 0644);
52MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
53
54#define au0828_isocdbg(fmt, arg...) \
55do {\
56 if (isoc_debug) { \
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030057 pr_info("au0828 %s :"fmt, \
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030058 __func__ , ##arg); \
59 } \
60 } while (0)
61
Hans Verkuilfa09cb92013-03-11 16:10:33 -030062static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
63{
64 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
65 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
66}
67
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030068static inline void print_err_status(struct au0828_dev *dev,
69 int packet, int status)
70{
71 char *errmsg = "Unknown";
72
73 switch (status) {
74 case -ENOENT:
75 errmsg = "unlinked synchronuously";
76 break;
77 case -ECONNRESET:
78 errmsg = "unlinked asynchronuously";
79 break;
80 case -ENOSR:
81 errmsg = "Buffer error (overrun)";
82 break;
83 case -EPIPE:
84 errmsg = "Stalled (device not responding)";
85 break;
86 case -EOVERFLOW:
87 errmsg = "Babble (bad cable?)";
88 break;
89 case -EPROTO:
90 errmsg = "Bit-stuff error (bad cable?)";
91 break;
92 case -EILSEQ:
93 errmsg = "CRC/Timeout (could be anything)";
94 break;
95 case -ETIME:
96 errmsg = "Device does not respond";
97 break;
98 }
99 if (packet < 0) {
100 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
101 } else {
102 au0828_isocdbg("URB packet %d, status %d [%s].\n",
103 packet, status, errmsg);
104 }
105}
106
107static int check_dev(struct au0828_dev *dev)
108{
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -0300109 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300110 pr_info("v4l2 ioctl: device not present\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300111 return -ENODEV;
112 }
113
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -0300114 if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
115 pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300116 return -EIO;
117 }
118 return 0;
119}
120
121/*
122 * IRQ callback, called by URB callback
123 */
124static void au0828_irq_callback(struct urb *urb)
125{
126 struct au0828_dmaqueue *dma_q = urb->context;
127 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300128 unsigned long flags = 0;
Hans Verkuile92ba282012-05-14 10:17:35 -0300129 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300130
131 switch (urb->status) {
132 case 0: /* success */
133 case -ETIMEDOUT: /* NAK */
134 break;
135 case -ECONNRESET: /* kill */
136 case -ENOENT:
137 case -ESHUTDOWN:
138 au0828_isocdbg("au0828_irq_callback called: status kill\n");
139 return;
140 default: /* unknown error */
141 au0828_isocdbg("urb completition error %d.\n", urb->status);
142 break;
143 }
144
145 /* Copy data from URB */
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300146 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuile92ba282012-05-14 10:17:35 -0300147 dev->isoc_ctl.isoc_copy(dev, urb);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300148 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300149
150 /* Reset urb buffers */
151 for (i = 0; i < urb->number_of_packets; i++) {
152 urb->iso_frame_desc[i].status = 0;
153 urb->iso_frame_desc[i].actual_length = 0;
154 }
155 urb->status = 0;
156
157 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
158 if (urb->status) {
159 au0828_isocdbg("urb resubmit failed (error=%i)\n",
160 urb->status);
161 }
Mauro Carvalho Chehabe2147d02014-08-09 21:47:14 -0300162 dev->stream_state = STREAM_ON;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300163}
164
165/*
166 * Stop and Deallocate URBs
167 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300168static void au0828_uninit_isoc(struct au0828_dev *dev)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300169{
170 struct urb *urb;
171 int i;
172
173 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
174
175 dev->isoc_ctl.nfields = -1;
176 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
177 urb = dev->isoc_ctl.urb[i];
178 if (urb) {
179 if (!irqs_disabled())
180 usb_kill_urb(urb);
181 else
182 usb_unlink_urb(urb);
183
184 if (dev->isoc_ctl.transfer_buffer[i]) {
Daniel Mack997ea582010-04-12 13:17:25 +0200185 usb_free_coherent(dev->usbdev,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300186 urb->transfer_buffer_length,
187 dev->isoc_ctl.transfer_buffer[i],
188 urb->transfer_dma);
189 }
190 usb_free_urb(urb);
191 dev->isoc_ctl.urb[i] = NULL;
192 }
193 dev->isoc_ctl.transfer_buffer[i] = NULL;
194 }
195
196 kfree(dev->isoc_ctl.urb);
197 kfree(dev->isoc_ctl.transfer_buffer);
198
199 dev->isoc_ctl.urb = NULL;
200 dev->isoc_ctl.transfer_buffer = NULL;
201 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehabe2147d02014-08-09 21:47:14 -0300202
203 dev->stream_state = STREAM_OFF;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300204}
205
206/*
207 * Allocate URBs and start IRQ
208 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300209static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
210 int num_bufs, int max_pkt_size,
211 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300212{
213 struct au0828_dmaqueue *dma_q = &dev->vidq;
214 int i;
215 int sb_size, pipe;
216 struct urb *urb;
217 int j, k;
218 int rc;
219
220 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
221
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300222 dev->isoc_ctl.isoc_copy = isoc_copy;
223 dev->isoc_ctl.num_bufs = num_bufs;
224
225 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
226 if (!dev->isoc_ctl.urb) {
227 au0828_isocdbg("cannot alloc memory for usb buffers\n");
228 return -ENOMEM;
229 }
230
231 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
232 GFP_KERNEL);
233 if (!dev->isoc_ctl.transfer_buffer) {
234 au0828_isocdbg("cannot allocate memory for usb transfer\n");
235 kfree(dev->isoc_ctl.urb);
236 return -ENOMEM;
237 }
238
239 dev->isoc_ctl.max_pkt_size = max_pkt_size;
240 dev->isoc_ctl.buf = NULL;
241
242 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
243
244 /* allocate urbs and transfer buffers */
245 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
246 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
247 if (!urb) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300248 au0828_uninit_isoc(dev);
249 return -ENOMEM;
250 }
251 dev->isoc_ctl.urb[i] = urb;
252
Daniel Mack997ea582010-04-12 13:17:25 +0200253 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300254 sb_size, GFP_KERNEL, &urb->transfer_dma);
255 if (!dev->isoc_ctl.transfer_buffer[i]) {
256 printk("unable to allocate %i bytes for transfer"
257 " buffer %i%s\n",
258 sb_size, i,
259 in_interrupt() ? " while in int" : "");
260 au0828_uninit_isoc(dev);
261 return -ENOMEM;
262 }
263 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
264
265 pipe = usb_rcvisocpipe(dev->usbdev,
266 dev->isoc_in_endpointaddr),
267
268 usb_fill_int_urb(urb, dev->usbdev, pipe,
269 dev->isoc_ctl.transfer_buffer[i], sb_size,
270 au0828_irq_callback, dma_q, 1);
271
272 urb->number_of_packets = max_packets;
Devin Heitmuellerfadadb72009-03-22 23:42:26 -0300273 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300274
275 k = 0;
276 for (j = 0; j < max_packets; j++) {
277 urb->iso_frame_desc[j].offset = k;
278 urb->iso_frame_desc[j].length =
279 dev->isoc_ctl.max_pkt_size;
280 k += dev->isoc_ctl.max_pkt_size;
281 }
282 }
283
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300284 /* submit urbs and enables IRQ */
285 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
286 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
287 if (rc) {
288 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
289 i, rc);
290 au0828_uninit_isoc(dev);
291 return rc;
292 }
293 }
294
295 return 0;
296}
297
298/*
299 * Announces that a buffer were filled and request the next
300 */
301static inline void buffer_filled(struct au0828_dev *dev,
Lad, Prabhakarc5036d62015-02-24 10:00:29 -0300302 struct au0828_dmaqueue *dma_q,
303 struct au0828_buffer *buf)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300304{
Junghak Sung2d700712015-09-22 10:30:30 -0300305 struct vb2_v4l2_buffer *vb = &buf->vb;
306 struct vb2_queue *q = vb->vb2_buf.vb2_queue;
Lad, Prabhakarc5036d62015-02-24 10:00:29 -0300307
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300308 /* Advice that buffer was filled */
Shuah Khan05439b12015-01-29 13:41:32 -0300309 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300310
Lad, Prabhakarc5036d62015-02-24 10:00:29 -0300311 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Junghak Sung2d700712015-09-22 10:30:30 -0300312 vb->sequence = dev->frame_count++;
Lad, Prabhakarc5036d62015-02-24 10:00:29 -0300313 else
Junghak Sung2d700712015-09-22 10:30:30 -0300314 vb->sequence = dev->vbi_frame_count++;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300315
Junghak Sung2d700712015-09-22 10:30:30 -0300316 vb->field = V4L2_FIELD_INTERLACED;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200317 vb->vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300318 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300319}
320
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300321/*
322 * Identify the buffer header type and properly handles
323 */
324static void au0828_copy_video(struct au0828_dev *dev,
325 struct au0828_dmaqueue *dma_q,
326 struct au0828_buffer *buf,
327 unsigned char *p,
328 unsigned char *outp, unsigned long len)
329{
330 void *fieldstart, *startwrite, *startread;
331 int linesdone, currlinedone, offset, lencopy, remain;
332 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
333
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300334 if (len == 0)
335 return;
336
Shuah Khan05439b12015-01-29 13:41:32 -0300337 if (dma_q->pos + len > buf->length)
338 len = buf->length - dma_q->pos;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300339
340 startread = p;
341 remain = len;
342
343 /* Interlaces frame */
344 if (buf->top_field)
345 fieldstart = outp;
346 else
347 fieldstart = outp + bytesperline;
348
349 linesdone = dma_q->pos / bytesperline;
350 currlinedone = dma_q->pos % bytesperline;
351 offset = linesdone * bytesperline * 2 + currlinedone;
352 startwrite = fieldstart + offset;
353 lencopy = bytesperline - currlinedone;
354 lencopy = lencopy > remain ? remain : lencopy;
355
Shuah Khan05439b12015-01-29 13:41:32 -0300356 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300357 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
358 ((char *)startwrite + lencopy) -
Shuah Khan05439b12015-01-29 13:41:32 -0300359 ((char *)outp + buf->length));
360 remain = (char *)outp + buf->length - (char *)startwrite;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300361 lencopy = remain;
362 }
363 if (lencopy <= 0)
364 return;
365 memcpy(startwrite, startread, lencopy);
366
367 remain -= lencopy;
368
369 while (remain > 0) {
370 startwrite += lencopy + bytesperline;
371 startread += lencopy;
372 if (bytesperline > remain)
373 lencopy = remain;
374 else
375 lencopy = bytesperline;
376
377 if ((char *)startwrite + lencopy > (char *)outp +
Shuah Khan05439b12015-01-29 13:41:32 -0300378 buf->length) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300379 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300380 ((char *)startwrite + lencopy) -
Shuah Khan05439b12015-01-29 13:41:32 -0300381 ((char *)outp + buf->length));
382 lencopy = remain = (char *)outp + buf->length -
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300383 (char *)startwrite;
384 }
385 if (lencopy <= 0)
386 break;
387
388 memcpy(startwrite, startread, lencopy);
389
390 remain -= lencopy;
391 }
392
393 if (offset > 1440) {
394 /* We have enough data to check for greenscreen */
Devin Heitmueller62899a22009-03-15 18:48:52 -0300395 if (outp[0] < 0x60 && outp[1440] < 0x60)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300396 dev->greenscreen_detected = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300397 }
398
399 dma_q->pos += len;
400}
401
402/*
403 * video-buf generic routine to get the next available buffer
404 */
405static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
406 struct au0828_buffer **buf)
407{
408 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
409
410 if (list_empty(&dma_q->active)) {
411 au0828_isocdbg("No active queue to serve\n");
412 dev->isoc_ctl.buf = NULL;
413 *buf = NULL;
414 return;
415 }
416
417 /* Get the next buffer */
Shuah Khan05439b12015-01-29 13:41:32 -0300418 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
419 /* Cleans up buffer - Useful for testing for frame/URB loss */
420 list_del(&(*buf)->list);
421 dma_q->pos = 0;
422 (*buf)->vb_buf = (*buf)->mem;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300423 dev->isoc_ctl.buf = *buf;
424
425 return;
426}
427
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300428static void au0828_copy_vbi(struct au0828_dev *dev,
429 struct au0828_dmaqueue *dma_q,
430 struct au0828_buffer *buf,
431 unsigned char *p,
432 unsigned char *outp, unsigned long len)
433{
434 unsigned char *startwrite, *startread;
Dan Carpenterb5f59332010-07-23 07:09:20 -0300435 int bytesperline;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300436 int i, j = 0;
437
438 if (dev == NULL) {
439 au0828_isocdbg("dev is null\n");
440 return;
441 }
442
443 if (dma_q == NULL) {
444 au0828_isocdbg("dma_q is null\n");
445 return;
446 }
447 if (buf == NULL)
448 return;
449 if (p == NULL) {
450 au0828_isocdbg("p is null\n");
451 return;
452 }
453 if (outp == NULL) {
454 au0828_isocdbg("outp is null\n");
455 return;
456 }
457
Dan Carpenterb5f59332010-07-23 07:09:20 -0300458 bytesperline = dev->vbi_width;
459
Shuah Khan05439b12015-01-29 13:41:32 -0300460 if (dma_q->pos + len > buf->length)
461 len = buf->length - dma_q->pos;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300462
463 startread = p;
464 startwrite = outp + (dma_q->pos / 2);
465
466 /* Make sure the bottom field populates the second half of the frame */
467 if (buf->top_field == 0)
468 startwrite += bytesperline * dev->vbi_height;
469
470 for (i = 0; i < len; i += 2)
471 startwrite[j++] = startread[i+1];
472
473 dma_q->pos += len;
474}
475
476
477/*
478 * video-buf generic routine to get the next available VBI buffer
479 */
480static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
481 struct au0828_buffer **buf)
482{
483 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300484
485 if (list_empty(&dma_q->active)) {
486 au0828_isocdbg("No active queue to serve\n");
487 dev->isoc_ctl.vbi_buf = NULL;
488 *buf = NULL;
489 return;
490 }
491
492 /* Get the next buffer */
Shuah Khan05439b12015-01-29 13:41:32 -0300493 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300494 /* Cleans up buffer - Useful for testing for frame/URB loss */
Shuah Khan05439b12015-01-29 13:41:32 -0300495 list_del(&(*buf)->list);
496 dma_q->pos = 0;
497 (*buf)->vb_buf = (*buf)->mem;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300498 dev->isoc_ctl.vbi_buf = *buf;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300499 return;
500}
501
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300502/*
503 * Controls the isoc copy of each urb packet
504 */
505static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
506{
507 struct au0828_buffer *buf;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300508 struct au0828_buffer *vbi_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300509 struct au0828_dmaqueue *dma_q = urb->context;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300510 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300511 unsigned char *outp = NULL;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300512 unsigned char *vbioutp = NULL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300513 int i, len = 0, rc = 1;
514 unsigned char *p;
515 unsigned char fbyte;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300516 unsigned int vbi_field_size;
517 unsigned int remain, lencopy;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300518
519 if (!dev)
520 return 0;
521
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -0300522 if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
523 test_bit(DEV_MISCONFIGURED, &dev->dev_state))
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300524 return 0;
525
526 if (urb->status < 0) {
527 print_err_status(dev, -1, urb->status);
528 if (urb->status == -ENOENT)
529 return 0;
530 }
531
532 buf = dev->isoc_ctl.buf;
533 if (buf != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300534 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300535
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300536 vbi_buf = dev->isoc_ctl.vbi_buf;
537 if (vbi_buf != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300538 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300539
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300540 for (i = 0; i < urb->number_of_packets; i++) {
541 int status = urb->iso_frame_desc[i].status;
542
543 if (status < 0) {
544 print_err_status(dev, i, status);
545 if (urb->iso_frame_desc[i].status != -EPROTO)
546 continue;
547 }
548
Devin Heitmueller62899a22009-03-15 18:48:52 -0300549 if (urb->iso_frame_desc[i].actual_length <= 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300550 continue;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300551
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300552 if (urb->iso_frame_desc[i].actual_length >
553 dev->max_pkt_size) {
554 au0828_isocdbg("packet bigger than packet size");
555 continue;
556 }
557
558 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
559 fbyte = p[0];
560 len = urb->iso_frame_desc[i].actual_length - 4;
561 p += 4;
562
563 if (fbyte & 0x80) {
564 len -= 4;
565 p += 4;
566 au0828_isocdbg("Video frame %s\n",
567 (fbyte & 0x40) ? "odd" : "even");
Devin Heitmuellerbde3bb92010-07-05 13:05:16 -0300568 if (fbyte & 0x40) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300569 /* VBI */
570 if (vbi_buf != NULL)
Lad, Prabhakarc5036d62015-02-24 10:00:29 -0300571 buffer_filled(dev, vbi_dma_q, vbi_buf);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300572 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
573 if (vbi_buf == NULL)
574 vbioutp = NULL;
575 else
Shuah Khan05439b12015-01-29 13:41:32 -0300576 vbioutp = vb2_plane_vaddr(
Junghak Sung2d700712015-09-22 10:30:30 -0300577 &vbi_buf->vb.vb2_buf, 0);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300578
579 /* Video */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300580 if (buf != NULL)
581 buffer_filled(dev, dma_q, buf);
582 get_next_buf(dma_q, &buf);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300583 if (buf == NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300584 outp = NULL;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300585 else
Junghak Sung2d700712015-09-22 10:30:30 -0300586 outp = vb2_plane_vaddr(
587 &buf->vb.vb2_buf, 0);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300588
589 /* As long as isoc traffic is arriving, keep
590 resetting the timer */
591 if (dev->vid_timeout_running)
592 mod_timer(&dev->vid_timeout,
593 jiffies + (HZ / 10));
594 if (dev->vbi_timeout_running)
595 mod_timer(&dev->vbi_timeout,
596 jiffies + (HZ / 10));
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300597 }
598
599 if (buf != NULL) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300600 if (fbyte & 0x40)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300601 buf->top_field = 1;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300602 else
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300603 buf->top_field = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300604 }
605
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300606 if (vbi_buf != NULL) {
607 if (fbyte & 0x40)
608 vbi_buf->top_field = 1;
609 else
610 vbi_buf->top_field = 0;
611 }
612
613 dev->vbi_read = 0;
614 vbi_dma_q->pos = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300615 dma_q->pos = 0;
616 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300617
618 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
619 if (dev->vbi_read < vbi_field_size) {
620 remain = vbi_field_size - dev->vbi_read;
621 if (len < remain)
622 lencopy = len;
623 else
624 lencopy = remain;
625
626 if (vbi_buf != NULL)
627 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
628 vbioutp, len);
629
630 len -= lencopy;
631 p += lencopy;
632 dev->vbi_read += lencopy;
633 }
634
635 if (dev->vbi_read >= vbi_field_size && buf != NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300636 au0828_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300637 }
638 return rc;
639}
640
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200641void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
642{
643#ifdef CONFIG_MEDIA_CONTROLLER
644 int i;
645
646 for (i = 0; i < AU0828_MAX_INPUT; i++) {
647 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
648 return;
649 media_device_unregister_entity(&dev->input_ent[i]);
650 }
651#endif
652}
653
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200654static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
655{
656 struct au0828_dev *dev =
657 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
658
659 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
660 v4l2_device_unregister(&dev->v4l2_dev);
661 au0828_usb_v4l2_media_release(dev);
662 au0828_usb_release(dev);
663}
664
665int au0828_v4l2_device_register(struct usb_interface *interface,
666 struct au0828_dev *dev)
667{
668 int retval;
669
670 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
671 return 0;
672
673 /* Create the v4l2_device */
674#ifdef CONFIG_MEDIA_CONTROLLER
675 dev->v4l2_dev.mdev = dev->media_dev;
676#endif
677 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
678 if (retval) {
679 pr_err("%s() v4l2_device_register failed\n",
680 __func__);
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200681 return retval;
682 }
683
684 dev->v4l2_dev.release = au0828_usb_v4l2_release;
685
686 /* This control handler will inherit the controls from au8522 */
687 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
688 if (retval) {
689 pr_err("%s() v4l2_ctrl_handler_init failed\n",
690 __func__);
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200691 return retval;
692 }
693 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
694
695 return 0;
696}
697
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200698static int queue_setup(struct vb2_queue *vq,
Shuah Khan05439b12015-01-29 13:41:32 -0300699 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300700 unsigned int sizes[], struct device *alloc_devs[])
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300701{
Shuah Khan05439b12015-01-29 13:41:32 -0300702 struct au0828_dev *dev = vb2_get_drv_priv(vq);
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200703 unsigned long size = dev->height * dev->bytesperline;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300704
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200705 if (*nplanes)
706 return sizes[0] < size ? -EINVAL : 0;
Shuah Khan05439b12015-01-29 13:41:32 -0300707 *nplanes = 1;
708 sizes[0] = size;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300709 return 0;
Shuah Khan05439b12015-01-29 13:41:32 -0300710}
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300711
Shuah Khan05439b12015-01-29 13:41:32 -0300712static int
713buffer_prepare(struct vb2_buffer *vb)
714{
Junghak Sung2d700712015-09-22 10:30:30 -0300715 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
716 struct au0828_buffer *buf = container_of(vbuf,
717 struct au0828_buffer, vb);
Shuah Khan05439b12015-01-29 13:41:32 -0300718 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
719
720 buf->length = dev->height * dev->bytesperline;
721
722 if (vb2_plane_size(vb, 0) < buf->length) {
723 pr_err("%s data will not fit into plane (%lu < %lu)\n",
724 __func__, vb2_plane_size(vb, 0), buf->length);
725 return -EINVAL;
726 }
Junghak Sung2d700712015-09-22 10:30:30 -0300727 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
Shuah Khan05439b12015-01-29 13:41:32 -0300728 return 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300729}
730
731static void
Shuah Khan05439b12015-01-29 13:41:32 -0300732buffer_queue(struct vb2_buffer *vb)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300733{
Junghak Sung2d700712015-09-22 10:30:30 -0300734 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
735 struct au0828_buffer *buf = container_of(vbuf,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300736 struct au0828_buffer,
737 vb);
Shuah Khan05439b12015-01-29 13:41:32 -0300738 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300739 struct au0828_dmaqueue *vidq = &dev->vidq;
Shuah Khan05439b12015-01-29 13:41:32 -0300740 unsigned long flags = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300741
Shuah Khan05439b12015-01-29 13:41:32 -0300742 buf->mem = vb2_plane_vaddr(vb, 0);
743 buf->length = vb2_plane_size(vb, 0);
744
745 spin_lock_irqsave(&dev->slock, flags);
746 list_add_tail(&buf->list, &vidq->active);
747 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300748}
749
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300750static int au0828_i2s_init(struct au0828_dev *dev)
751{
752 /* Enable i2s mode */
753 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
754 return 0;
755}
756
757/*
758 * Auvitek au0828 analog stream enable
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300759 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300760static int au0828_analog_stream_enable(struct au0828_dev *d)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300761{
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300762 struct usb_interface *iface;
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300763 int ret, h, w;
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300764
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300765 dprintk(1, "au0828_analog_stream_enable called\n");
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300766
767 iface = usb_ifnum_to_if(d->usbdev, 0);
768 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
769 dprintk(1, "Changing intf#0 to alt 5\n");
770 /* set au0828 interface0 to AS5 here again */
771 ret = usb_set_interface(d->usbdev, 0, 5);
772 if (ret < 0) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300773 pr_info("Au0828 can't set alt setting to 5!\n");
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300774 return -EBUSY;
775 }
776 }
777
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300778 h = d->height / 2 + 2;
779 w = d->width * 2;
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300780
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300781 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
782 au0828_writereg(d, 0x106, 0x00);
783 /* set x position */
784 au0828_writereg(d, 0x110, 0x00);
785 au0828_writereg(d, 0x111, 0x00);
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300786 au0828_writereg(d, 0x114, w & 0xff);
787 au0828_writereg(d, 0x115, w >> 8);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300788 /* set y position */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300789 au0828_writereg(d, 0x112, 0x00);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300790 au0828_writereg(d, 0x113, 0x00);
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300791 au0828_writereg(d, 0x116, h & 0xff);
792 au0828_writereg(d, 0x117, h >> 8);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300793 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
794
795 return 0;
796}
797
Shuah Khan05439b12015-01-29 13:41:32 -0300798static int au0828_analog_stream_disable(struct au0828_dev *d)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300799{
800 dprintk(1, "au0828_analog_stream_disable called\n");
801 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
802 return 0;
803}
804
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300805static void au0828_analog_stream_reset(struct au0828_dev *dev)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300806{
807 dprintk(1, "au0828_analog_stream_reset called\n");
808 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
809 mdelay(30);
810 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
811}
812
813/*
814 * Some operations needs to stop current streaming
815 */
816static int au0828_stream_interrupt(struct au0828_dev *dev)
817{
818 int ret = 0;
819
820 dev->stream_state = STREAM_INTERRUPT;
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -0300821 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300822 return -ENODEV;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300823 else if (ret) {
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -0300824 set_bit(DEV_MISCONFIGURED, &dev->dev_state);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300825 dprintk(1, "%s device is misconfigured!\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300826 return ret;
827 }
828 return 0;
829}
830
Shuah Khan05439b12015-01-29 13:41:32 -0300831int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
832{
833 struct au0828_dev *dev = vb2_get_drv_priv(vq);
834 int rc = 0;
835
836 dprintk(1, "au0828_start_analog_streaming called %d\n",
837 dev->streaming_users);
838
839 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
840 dev->frame_count = 0;
841 else
842 dev->vbi_frame_count = 0;
843
844 if (dev->streaming_users == 0) {
845 /* If we were doing ac97 instead of i2s, it would go here...*/
846 au0828_i2s_init(dev);
847 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
848 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
849 au0828_isoc_copy);
850 if (rc < 0) {
851 pr_info("au0828_init_isoc failed\n");
852 return rc;
853 }
854
855 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
856 v4l2_device_call_all(&dev->v4l2_dev, 0, video,
857 s_stream, 1);
858 dev->vid_timeout_running = 1;
859 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
860 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
861 dev->vbi_timeout_running = 1;
862 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
863 }
864 }
865 dev->streaming_users++;
866 return rc;
867}
868
869static void au0828_stop_streaming(struct vb2_queue *vq)
870{
871 struct au0828_dev *dev = vb2_get_drv_priv(vq);
872 struct au0828_dmaqueue *vidq = &dev->vidq;
873 unsigned long flags = 0;
874
875 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
876
877 if (dev->streaming_users-- == 1)
878 au0828_uninit_isoc(dev);
879
880 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
881 dev->vid_timeout_running = 0;
882 del_timer_sync(&dev->vid_timeout);
883
884 spin_lock_irqsave(&dev->slock, flags);
885 if (dev->isoc_ctl.buf != NULL) {
Junghak Sung2d700712015-09-22 10:30:30 -0300886 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
887 VB2_BUF_STATE_ERROR);
Shuah Khan05439b12015-01-29 13:41:32 -0300888 dev->isoc_ctl.buf = NULL;
889 }
890 while (!list_empty(&vidq->active)) {
891 struct au0828_buffer *buf;
892
893 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
Junghak Sung2d700712015-09-22 10:30:30 -0300894 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Shuah Khan05439b12015-01-29 13:41:32 -0300895 list_del(&buf->list);
896 }
897 spin_unlock_irqrestore(&dev->slock, flags);
898}
899
900void au0828_stop_vbi_streaming(struct vb2_queue *vq)
901{
902 struct au0828_dev *dev = vb2_get_drv_priv(vq);
903 struct au0828_dmaqueue *vbiq = &dev->vbiq;
904 unsigned long flags = 0;
905
906 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
907 dev->streaming_users);
908
909 if (dev->streaming_users-- == 1)
910 au0828_uninit_isoc(dev);
911
912 spin_lock_irqsave(&dev->slock, flags);
913 if (dev->isoc_ctl.vbi_buf != NULL) {
Junghak Sung2d700712015-09-22 10:30:30 -0300914 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
Shuah Khan05439b12015-01-29 13:41:32 -0300915 VB2_BUF_STATE_ERROR);
916 dev->isoc_ctl.vbi_buf = NULL;
917 }
918 while (!list_empty(&vbiq->active)) {
919 struct au0828_buffer *buf;
920
921 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
922 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300923 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Shuah Khan05439b12015-01-29 13:41:32 -0300924 }
925 spin_unlock_irqrestore(&dev->slock, flags);
926
927 dev->vbi_timeout_running = 0;
928 del_timer_sync(&dev->vbi_timeout);
929}
930
931static struct vb2_ops au0828_video_qops = {
932 .queue_setup = queue_setup,
933 .buf_prepare = buffer_prepare,
934 .buf_queue = buffer_queue,
935 .start_streaming = au0828_start_analog_streaming,
936 .stop_streaming = au0828_stop_streaming,
937 .wait_prepare = vb2_ops_wait_prepare,
938 .wait_finish = vb2_ops_wait_finish,
939};
940
941/* ------------------------------------------------------------------
942 V4L2 interface
943 ------------------------------------------------------------------*/
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300944/*
Shuah Khan05439b12015-01-29 13:41:32 -0300945 * au0828_analog_unregister
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300946 * unregister v4l2 devices
947 */
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200948int au0828_analog_unregister(struct au0828_dev *dev)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300949{
Shuah Khan05439b12015-01-29 13:41:32 -0300950 dprintk(1, "au0828_analog_unregister called\n");
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200951
952 /* No analog TV */
953 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
954 return 0;
955
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300956 mutex_lock(&au0828_sysfs_lock);
Shuah Khan41071bb2015-02-26 19:33:13 -0300957 video_unregister_device(&dev->vdev);
958 video_unregister_device(&dev->vbi_dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300959 mutex_unlock(&au0828_sysfs_lock);
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200960
961 v4l2_device_disconnect(&dev->v4l2_dev);
962 v4l2_device_put(&dev->v4l2_dev);
963
964 return 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300965}
966
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300967/* This function ensures that video frames continue to be delivered even if
968 the ITU-656 input isn't receiving any data (thereby preventing applications
969 such as tvtime from hanging) */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300970static void au0828_vid_buffer_timeout(unsigned long data)
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300971{
972 struct au0828_dev *dev = (struct au0828_dev *) data;
973 struct au0828_dmaqueue *dma_q = &dev->vidq;
974 struct au0828_buffer *buf;
975 unsigned char *vid_data;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300976 unsigned long flags = 0;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300977
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300978 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300979
980 buf = dev->isoc_ctl.buf;
981 if (buf != NULL) {
Junghak Sung2d700712015-09-22 10:30:30 -0300982 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
Shuah Khan05439b12015-01-29 13:41:32 -0300983 memset(vid_data, 0x00, buf->length); /* Blank green frame */
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300984 buffer_filled(dev, dma_q, buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300985 }
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300986 get_next_buf(dma_q, &buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300987
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300988 if (dev->vid_timeout_running == 1)
989 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
990
991 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300992}
993
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300994static void au0828_vbi_buffer_timeout(unsigned long data)
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300995{
996 struct au0828_dev *dev = (struct au0828_dev *) data;
997 struct au0828_dmaqueue *dma_q = &dev->vbiq;
998 struct au0828_buffer *buf;
999 unsigned char *vbi_data;
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001000 unsigned long flags = 0;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -03001001
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001002 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -03001003
1004 buf = dev->isoc_ctl.vbi_buf;
1005 if (buf != NULL) {
Junghak Sung2d700712015-09-22 10:30:30 -03001006 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
Shuah Khan05439b12015-01-29 13:41:32 -03001007 memset(vbi_data, 0x00, buf->length);
Lad, Prabhakarc5036d62015-02-24 10:00:29 -03001008 buffer_filled(dev, dma_q, buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -03001009 }
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001010 vbi_get_next_buf(dma_q, &buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -03001011
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001012 if (dev->vbi_timeout_running == 1)
1013 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1014 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -03001015}
1016
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001017static int au0828_v4l2_open(struct file *filp)
1018{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001019 struct au0828_dev *dev = video_drvdata(filp);
Shuah Khan05439b12015-01-29 13:41:32 -03001020 int ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001021
Shuah Khan05439b12015-01-29 13:41:32 -03001022 dprintk(1,
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001023 "%s called std_set %d dev_state %ld stream users %d users %d\n",
Shuah Khan05439b12015-01-29 13:41:32 -03001024 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1025 dev->streaming_users, dev->users);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001026
Shuah Khan05439b12015-01-29 13:41:32 -03001027 if (mutex_lock_interruptible(&dev->lock))
Hans Verkuilea86968f2013-03-11 16:22:13 -03001028 return -ERESTARTSYS;
Shuah Khan05439b12015-01-29 13:41:32 -03001029
1030 ret = v4l2_fh_open(filp);
1031 if (ret) {
1032 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1033 __func__, ret);
1034 mutex_unlock(&dev->lock);
1035 return ret;
Hans Verkuilea86968f2013-03-11 16:22:13 -03001036 }
Shuah Khan05439b12015-01-29 13:41:32 -03001037
Hans Verkuilea86968f2013-03-11 16:22:13 -03001038 if (dev->users == 0) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001039 au0828_analog_stream_enable(dev);
1040 au0828_analog_stream_reset(dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001041 dev->stream_state = STREAM_OFF;
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001042 set_bit(DEV_INITIALIZED, &dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001043 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001044 dev->users++;
Hans Verkuilea86968f2013-03-11 16:22:13 -03001045 mutex_unlock(&dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001046 return ret;
1047}
1048
1049static int au0828_v4l2_close(struct file *filp)
1050{
1051 int ret;
Shuah Khan05439b12015-01-29 13:41:32 -03001052 struct au0828_dev *dev = video_drvdata(filp);
1053 struct video_device *vdev = video_devdata(filp);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001054
Shuah Khan05439b12015-01-29 13:41:32 -03001055 dprintk(1,
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001056 "%s called std_set %d dev_state %ld stream users %d users %d\n",
Shuah Khan05439b12015-01-29 13:41:32 -03001057 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1058 dev->streaming_users, dev->users);
1059
Hans Verkuilea86968f2013-03-11 16:22:13 -03001060 mutex_lock(&dev->lock);
Shuah Khan05439b12015-01-29 13:41:32 -03001061 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001062 /* Cancel timeout thread in case they didn't call streamoff */
1063 dev->vid_timeout_running = 0;
1064 del_timer_sync(&dev->vid_timeout);
Shuah Khan05439b12015-01-29 13:41:32 -03001065 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1066 dev->vbi_timeout_running) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001067 /* Cancel timeout thread in case they didn't call streamoff */
1068 dev->vbi_timeout_running = 0;
1069 del_timer_sync(&dev->vbi_timeout);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001070 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001071
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001072 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
Shuah Khan05439b12015-01-29 13:41:32 -03001073 goto end;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001074
Shuah Khan05439b12015-01-29 13:41:32 -03001075 if (dev->users == 1) {
Shuah Khanb19581a2016-02-11 21:41:37 -02001076 /*
1077 * Avoid putting tuner in sleep if DVB or ALSA are
1078 * streaming.
1079 *
1080 * On most USB devices like au0828 the tuner can
1081 * be safely put in sleep stare here if ALSA isn't
1082 * streaming. Exceptions are some very old USB tuner
1083 * models such as em28xx-based WinTV USB2 which have
1084 * a separate audio output jack. The devices that have
1085 * a separate audio output jack have analog tuners,
1086 * like Philips FM1236. Those devices are always on,
1087 * so the s_power callback are silently ignored.
1088 * So, the current logic here does the following:
1089 * Disable (put tuner to sleep) when
1090 * - ALSA and DVB aren't not streaming;
1091 * - the last V4L2 file handler is closed.
1092 *
1093 * FIXME:
1094 *
1095 * Additionally, this logic could be improved to
1096 * disable the media source if the above conditions
1097 * are met and if the device:
1098 * - doesn't have a separate audio out plug (or
1099 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1100 *
1101 * Once this additional logic is in place, a callback
1102 * is needed to enable the media source and power on
1103 * the tuner, for radio to work.
1104 */
1105 ret = v4l_enable_media_source(vdev);
1106 if (ret == 0)
1107 v4l2_device_call_all(&dev->v4l2_dev, 0, core,
1108 s_power, 0);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001109 dev->std_set_in_tuner_core = 0;
Devin Heitmuellere4b8bc52009-05-06 20:54:00 -03001110
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001111 /* When close the device, set the usb intf0 into alt0 to free
1112 USB bandwidth */
1113 ret = usb_set_interface(dev->usbdev, 0, 0);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001114 if (ret < 0)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -03001115 pr_info("Au0828 can't set alternate to 0!\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001116 }
Shuah Khan05439b12015-01-29 13:41:32 -03001117end:
1118 _vb2_fop_release(filp, NULL);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001119 dev->users--;
Shuah Khan05439b12015-01-29 13:41:32 -03001120 mutex_unlock(&dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001121 return 0;
1122}
1123
Hans Verkuilea86968f2013-03-11 16:22:13 -03001124/* Must be called with dev->lock held */
1125static void au0828_init_tuner(struct au0828_dev *dev)
1126{
1127 struct v4l2_frequency f = {
1128 .frequency = dev->ctrl_freq,
1129 .type = V4L2_TUNER_ANALOG_TV,
1130 };
1131
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001132 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001133 dev->std_set_in_tuner_core, dev->dev_state);
1134
Hans Verkuilea86968f2013-03-11 16:22:13 -03001135 if (dev->std_set_in_tuner_core)
1136 return;
1137 dev->std_set_in_tuner_core = 1;
1138 i2c_gate_ctrl(dev, 1);
1139 /* If we've never sent the standard in tuner core, do so now.
1140 We don't do this at device probe because we don't want to
1141 incur the cost of a firmware load */
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001142 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001143 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1144 i2c_gate_ctrl(dev, 0);
1145}
1146
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001147static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1148 struct v4l2_format *format)
1149{
1150 int ret;
1151 int width = format->fmt.pix.width;
1152 int height = format->fmt.pix.height;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001153
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001154 /* If they are demanding a format other than the one we support,
1155 bail out (tvtime asks for UYVY and then retries with YUYV) */
Devin Heitmueller62899a22009-03-15 18:48:52 -03001156 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001157 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001158
1159 /* format->fmt.pix.width only support 720 and height 480 */
Devin Heitmueller62899a22009-03-15 18:48:52 -03001160 if (width != 720)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001161 width = 720;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001162 if (height != 480)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001163 height = 480;
1164
1165 format->fmt.pix.width = width;
1166 format->fmt.pix.height = height;
1167 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1168 format->fmt.pix.bytesperline = width * 2;
1169 format->fmt.pix.sizeimage = width * height * 2;
1170 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1171 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001172 format->fmt.pix.priv = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001173
Devin Heitmueller62899a22009-03-15 18:48:52 -03001174 if (cmd == VIDIOC_TRY_FMT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001175 return 0;
1176
1177 /* maybe set new image format, driver current only support 720*480 */
1178 dev->width = width;
1179 dev->height = height;
1180 dev->frame_size = width * height * 2;
1181 dev->field_size = width * height;
1182 dev->bytesperline = width * 2;
1183
Devin Heitmueller62899a22009-03-15 18:48:52 -03001184 if (dev->stream_state == STREAM_ON) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001185 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
Devin Heitmueller62899a22009-03-15 18:48:52 -03001186 ret = au0828_stream_interrupt(dev);
1187 if (ret != 0) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001188 dprintk(1, "error interrupting video stream!\n");
1189 return ret;
1190 }
1191 }
1192
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001193 au0828_analog_stream_enable(dev);
1194
1195 return 0;
1196}
1197
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001198static int vidioc_querycap(struct file *file, void *priv,
1199 struct v4l2_capability *cap)
1200{
Hans Verkuil59e05482013-02-15 08:11:04 -03001201 struct video_device *vdev = video_devdata(file);
Shuah Khan05439b12015-01-29 13:41:32 -03001202 struct au0828_dev *dev = video_drvdata(file);
1203
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001204 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001205 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001206
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001207 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001208 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
Hans Verkuil59e05482013-02-15 08:11:04 -03001209 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001210
Hans Verkuil59e05482013-02-15 08:11:04 -03001211 /* set the device capabilities */
1212 cap->device_caps = V4L2_CAP_AUDIO |
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001213 V4L2_CAP_READWRITE |
1214 V4L2_CAP_STREAMING |
1215 V4L2_CAP_TUNER;
Hans Verkuil59e05482013-02-15 08:11:04 -03001216 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1217 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1218 else
1219 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1220 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1221 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001222 return 0;
1223}
1224
1225static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1226 struct v4l2_fmtdesc *f)
1227{
Devin Heitmueller62899a22009-03-15 18:48:52 -03001228 if (f->index)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001229 return -EINVAL;
1230
Shuah Khan05439b12015-01-29 13:41:32 -03001231 dprintk(1, "%s called\n", __func__);
1232
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001233 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1234 strcpy(f->description, "Packed YUV2");
1235
1236 f->flags = 0;
1237 f->pixelformat = V4L2_PIX_FMT_UYVY;
1238
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001239 return 0;
1240}
1241
1242static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1243 struct v4l2_format *f)
1244{
Shuah Khan05439b12015-01-29 13:41:32 -03001245 struct au0828_dev *dev = video_drvdata(file);
1246
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001247 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001248 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001249
1250 f->fmt.pix.width = dev->width;
1251 f->fmt.pix.height = dev->height;
1252 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1253 f->fmt.pix.bytesperline = dev->bytesperline;
1254 f->fmt.pix.sizeimage = dev->frame_size;
1255 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1256 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001257 f->fmt.pix.priv = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001258 return 0;
1259}
1260
1261static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1262 struct v4l2_format *f)
1263{
Shuah Khan05439b12015-01-29 13:41:32 -03001264 struct au0828_dev *dev = video_drvdata(file);
1265
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001266 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001267 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001268
1269 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1270}
1271
1272static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1273 struct v4l2_format *f)
1274{
Shuah Khan05439b12015-01-29 13:41:32 -03001275 struct au0828_dev *dev = video_drvdata(file);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001276 int rc;
1277
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001278 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001279 dev->std_set_in_tuner_core, dev->dev_state);
1280
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001281 rc = check_dev(dev);
1282 if (rc < 0)
1283 return rc;
1284
Shuah Khan05439b12015-01-29 13:41:32 -03001285 if (vb2_is_busy(&dev->vb_vidq)) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -03001286 pr_info("%s queue busy\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001287 rc = -EBUSY;
1288 goto out;
1289 }
1290
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001291 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001292out:
1293 return rc;
1294}
1295
Hans Verkuil314527a2013-03-15 06:10:40 -03001296static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001297{
Shuah Khan05439b12015-01-29 13:41:32 -03001298 struct au0828_dev *dev = video_drvdata(file);
1299
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001300 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001301 dev->std_set_in_tuner_core, dev->dev_state);
1302
1303 if (norm == dev->std)
1304 return 0;
1305
1306 if (dev->streaming_users > 0)
1307 return -EBUSY;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001308
Hans Verkuilea86968f2013-03-11 16:22:13 -03001309 dev->std = norm;
1310
1311 au0828_init_tuner(dev);
1312
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001313 i2c_gate_ctrl(dev, 1);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001314
Mauro Carvalho Chehabf2fd7ce2014-06-08 13:54:56 -03001315 /*
1316 * FIXME: when we support something other than 60Hz standards,
1317 * we are going to have to make the au0828 bridge adjust the size
1318 * of its capture buffer, which is currently hardcoded at 720x480
1319 */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001320
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001321 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001322
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001323 i2c_gate_ctrl(dev, 0);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001324
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001325 return 0;
1326}
1327
Hans Verkuil33b6b892013-02-15 09:22:37 -03001328static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1329{
Shuah Khan05439b12015-01-29 13:41:32 -03001330 struct au0828_dev *dev = video_drvdata(file);
1331
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001332 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001333 dev->std_set_in_tuner_core, dev->dev_state);
Hans Verkuil33b6b892013-02-15 09:22:37 -03001334
1335 *norm = dev->std;
1336 return 0;
1337}
1338
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001339static int vidioc_enum_input(struct file *file, void *priv,
1340 struct v4l2_input *input)
1341{
Shuah Khan05439b12015-01-29 13:41:32 -03001342 struct au0828_dev *dev = video_drvdata(file);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001343 unsigned int tmp;
1344
1345 static const char *inames[] = {
Devin Heitmueller3d622872009-03-15 17:48:26 -03001346 [AU0828_VMUX_UNDEFINED] = "Undefined",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001347 [AU0828_VMUX_COMPOSITE] = "Composite",
1348 [AU0828_VMUX_SVIDEO] = "S-Video",
1349 [AU0828_VMUX_CABLE] = "Cable TV",
1350 [AU0828_VMUX_TELEVISION] = "Television",
1351 [AU0828_VMUX_DVB] = "DVB",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001352 };
1353
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001354 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001355 dev->std_set_in_tuner_core, dev->dev_state);
1356
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001357 tmp = input->index;
1358
Dan Carpenterf5e20c32010-03-28 08:21:18 -03001359 if (tmp >= AU0828_MAX_INPUT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001360 return -EINVAL;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001361 if (AUVI_INPUT(tmp).type == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001362 return -EINVAL;
1363
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001364 input->index = tmp;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001365 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001366 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001367 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001368 input->type |= V4L2_INPUT_TYPE_TUNER;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001369 input->audioset = 1;
1370 } else {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001371 input->type |= V4L2_INPUT_TYPE_CAMERA;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001372 input->audioset = 2;
1373 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001374
Shuah Khan41071bb2015-02-26 19:33:13 -03001375 input->std = dev->vdev.tvnorms;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001376
1377 return 0;
1378}
1379
1380static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1381{
Shuah Khan05439b12015-01-29 13:41:32 -03001382 struct au0828_dev *dev = video_drvdata(file);
1383
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001384 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001385 dev->std_set_in_tuner_core, dev->dev_state);
1386
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001387 *i = dev->ctrl_input;
1388 return 0;
1389}
1390
Hans Verkuil56230d12013-03-11 16:18:31 -03001391static void au0828_s_input(struct au0828_dev *dev, int index)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001392{
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001393 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001394
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001395 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001396 dev->std_set_in_tuner_core, dev->dev_state);
1397
Devin Heitmueller62899a22009-03-15 18:48:52 -03001398 switch (AUVI_INPUT(index).type) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001399 case AU0828_VMUX_SVIDEO:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001400 dev->input_type = AU0828_VMUX_SVIDEO;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001401 dev->ctrl_ainput = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001402 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001403 case AU0828_VMUX_COMPOSITE:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001404 dev->input_type = AU0828_VMUX_COMPOSITE;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001405 dev->ctrl_ainput = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001406 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001407 case AU0828_VMUX_TELEVISION:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001408 dev->input_type = AU0828_VMUX_TELEVISION;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001409 dev->ctrl_ainput = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001410 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001411 default:
Hans Verkuil56230d12013-03-11 16:18:31 -03001412 dprintk(1, "unknown input type set [%d]\n",
Devin Heitmuellera1094c42009-03-15 17:43:13 -03001413 AUVI_INPUT(index).type);
Shuah Khan174ced22016-02-12 21:18:03 -02001414 return;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001415 }
1416
Shuah Khan174ced22016-02-12 21:18:03 -02001417 dev->ctrl_input = index;
1418
Hans Verkuil5325b422009-04-02 11:26:22 -03001419 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1420 AUVI_INPUT(index).vmux, 0, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001421
1422 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1423 int enable = 0;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001424 if (AUVI_INPUT(i).audio_setup == NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001425 continue;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001426
1427 if (i == index)
1428 enable = 1;
1429 else
1430 enable = 0;
1431 if (enable) {
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001432 (AUVI_INPUT(i).audio_setup)(dev, enable);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001433 } else {
1434 /* Make sure we leave it turned on if some
1435 other input is routed to this callback */
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001436 if ((AUVI_INPUT(i).audio_setup) !=
1437 ((AUVI_INPUT(index).audio_setup))) {
1438 (AUVI_INPUT(i).audio_setup)(dev, enable);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001439 }
1440 }
1441 }
1442
Hans Verkuil5325b422009-04-02 11:26:22 -03001443 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1444 AUVI_INPUT(index).amux, 0, 0);
Hans Verkuil56230d12013-03-11 16:18:31 -03001445}
1446
1447static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1448{
Shuah Khan05439b12015-01-29 13:41:32 -03001449 struct au0828_dev *dev = video_drvdata(file);
Shuah Khan050b6c982016-02-18 22:28:48 -02001450 struct video_device *vfd = video_devdata(file);
Hans Verkuil56230d12013-03-11 16:18:31 -03001451
1452 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1453 index);
1454 if (index >= AU0828_MAX_INPUT)
1455 return -EINVAL;
1456 if (AUVI_INPUT(index).type == 0)
1457 return -EINVAL;
Shuah Khan174ced22016-02-12 21:18:03 -02001458
1459 if (dev->ctrl_input == index)
1460 return 0;
1461
Hans Verkuil56230d12013-03-11 16:18:31 -03001462 au0828_s_input(dev, index);
Shuah Khan050b6c982016-02-18 22:28:48 -02001463
1464 /*
1465 * Input has been changed. Disable the media source
1466 * associated with the old input and enable source
1467 * for the newly set input
1468 */
1469 v4l_disable_media_source(vfd);
1470 return v4l_enable_media_source(vfd);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001471}
1472
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001473static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001474{
Devin Heitmueller62899a22009-03-15 18:48:52 -03001475 if (a->index > 1)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001476 return -EINVAL;
1477
Shuah Khan05439b12015-01-29 13:41:32 -03001478 dprintk(1, "%s called\n", __func__);
1479
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001480 if (a->index == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001481 strcpy(a->name, "Television");
1482 else
1483 strcpy(a->name, "Line in");
1484
1485 a->capability = V4L2_AUDCAP_STEREO;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001486 return 0;
1487}
1488
1489static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1490{
Shuah Khan05439b12015-01-29 13:41:32 -03001491 struct au0828_dev *dev = video_drvdata(file);
1492
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001493 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001494 dev->std_set_in_tuner_core, dev->dev_state);
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001495
1496 a->index = dev->ctrl_ainput;
1497 if (a->index == 0)
1498 strcpy(a->name, "Television");
1499 else
1500 strcpy(a->name, "Line in");
1501
1502 a->capability = V4L2_AUDCAP_STEREO;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001503 return 0;
1504}
1505
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001506static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001507{
Shuah Khan05439b12015-01-29 13:41:32 -03001508 struct au0828_dev *dev = video_drvdata(file);
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001509
Devin Heitmueller62899a22009-03-15 18:48:52 -03001510 if (a->index != dev->ctrl_ainput)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001511 return -EINVAL;
Shuah Khan05439b12015-01-29 13:41:32 -03001512
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001513 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001514 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001515 return 0;
1516}
1517
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001518static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1519{
Shuah Khan05439b12015-01-29 13:41:32 -03001520 struct au0828_dev *dev = video_drvdata(file);
Shuah Khanb19581a2016-02-11 21:41:37 -02001521 struct video_device *vfd = video_devdata(file);
1522 int ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001523
Devin Heitmueller62899a22009-03-15 18:48:52 -03001524 if (t->index != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001525 return -EINVAL;
1526
Shuah Khanb19581a2016-02-11 21:41:37 -02001527 ret = v4l_enable_media_source(vfd);
1528 if (ret)
1529 return ret;
1530
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001531 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001532 dev->std_set_in_tuner_core, dev->dev_state);
1533
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001534 strcpy(t->name, "Auvitek tuner");
Hans Verkuilea86968f2013-03-11 16:22:13 -03001535
1536 au0828_init_tuner(dev);
1537 i2c_gate_ctrl(dev, 1);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001538 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001539 i2c_gate_ctrl(dev, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001540 return 0;
1541}
1542
1543static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001544 const struct v4l2_tuner *t)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001545{
Shuah Khan05439b12015-01-29 13:41:32 -03001546 struct au0828_dev *dev = video_drvdata(file);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001547
Devin Heitmueller62899a22009-03-15 18:48:52 -03001548 if (t->index != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001549 return -EINVAL;
1550
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001551 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001552 dev->std_set_in_tuner_core, dev->dev_state);
1553
Hans Verkuilea86968f2013-03-11 16:22:13 -03001554 au0828_init_tuner(dev);
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001555 i2c_gate_ctrl(dev, 1);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001556 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001557 i2c_gate_ctrl(dev, 0);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001558
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001559 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1560 t->afc);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001561
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001562 return 0;
1563
1564}
1565
1566static int vidioc_g_frequency(struct file *file, void *priv,
1567 struct v4l2_frequency *freq)
1568{
Shuah Khan05439b12015-01-29 13:41:32 -03001569 struct au0828_dev *dev = video_drvdata(file);
Devin Heitmuellerc8889232009-03-15 17:38:47 -03001570
Hans Verkuile1cf6582013-03-22 13:32:20 -03001571 if (freq->tuner != 0)
1572 return -EINVAL;
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001573 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001574 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001575 freq->frequency = dev->ctrl_freq;
1576 return 0;
1577}
1578
1579static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001580 const struct v4l2_frequency *freq)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001581{
Shuah Khan05439b12015-01-29 13:41:32 -03001582 struct au0828_dev *dev = video_drvdata(file);
Hans Verkuile1cf6582013-03-22 13:32:20 -03001583 struct v4l2_frequency new_freq = *freq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001584
Devin Heitmueller62899a22009-03-15 18:48:52 -03001585 if (freq->tuner != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001586 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001587
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001588 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001589 dev->std_set_in_tuner_core, dev->dev_state);
1590
Hans Verkuilea86968f2013-03-11 16:22:13 -03001591 au0828_init_tuner(dev);
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001592 i2c_gate_ctrl(dev, 1);
Devin Heitmueller4a03daf2012-08-06 22:47:02 -03001593
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001594 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
Hans Verkuile1cf6582013-03-22 13:32:20 -03001595 /* Get the actual set (and possibly clamped) frequency */
1596 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1597 dev->ctrl_freq = new_freq.frequency;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001598
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001599 i2c_gate_ctrl(dev, 0);
Devin Heitmueller4a03daf2012-08-06 22:47:02 -03001600
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001601 au0828_analog_stream_reset(dev);
1602
1603 return 0;
1604}
1605
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001606
1607/* RAW VBI ioctls */
1608
1609static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1610 struct v4l2_format *format)
1611{
Shuah Khan05439b12015-01-29 13:41:32 -03001612 struct au0828_dev *dev = video_drvdata(file);
1613
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001614 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001615 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001616
1617 format->fmt.vbi.samples_per_line = dev->vbi_width;
1618 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1619 format->fmt.vbi.offset = 0;
1620 format->fmt.vbi.flags = 0;
1621 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1622
1623 format->fmt.vbi.count[0] = dev->vbi_height;
1624 format->fmt.vbi.count[1] = dev->vbi_height;
1625 format->fmt.vbi.start[0] = 21;
1626 format->fmt.vbi.start[1] = 284;
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001627 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001628
1629 return 0;
1630}
1631
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001632static int vidioc_cropcap(struct file *file, void *priv,
1633 struct v4l2_cropcap *cc)
1634{
Shuah Khan05439b12015-01-29 13:41:32 -03001635 struct au0828_dev *dev = video_drvdata(file);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001636
Devin Heitmueller62899a22009-03-15 18:48:52 -03001637 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001638 return -EINVAL;
1639
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001640 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001641 dev->std_set_in_tuner_core, dev->dev_state);
1642
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001643 cc->bounds.left = 0;
1644 cc->bounds.top = 0;
1645 cc->bounds.width = dev->width;
1646 cc->bounds.height = dev->height;
1647
1648 cc->defrect = cc->bounds;
1649
1650 cc->pixelaspect.numerator = 54;
1651 cc->pixelaspect.denominator = 59;
1652
1653 return 0;
1654}
1655
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001656#ifdef CONFIG_VIDEO_ADV_DEBUG
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001657static int vidioc_g_register(struct file *file, void *priv,
1658 struct v4l2_dbg_register *reg)
1659{
Shuah Khan05439b12015-01-29 13:41:32 -03001660 struct au0828_dev *dev = video_drvdata(file);
1661
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001662 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001663 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001664
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001665 reg->val = au0828_read(dev, reg->reg);
Hans Verkuilead5bc42013-05-29 07:00:03 -03001666 reg->size = 1;
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001667 return 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001668}
1669
1670static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001671 const struct v4l2_dbg_register *reg)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001672{
Shuah Khan05439b12015-01-29 13:41:32 -03001673 struct au0828_dev *dev = video_drvdata(file);
1674
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -03001675 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
Shuah Khan05439b12015-01-29 13:41:32 -03001676 dev->std_set_in_tuner_core, dev->dev_state);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001677
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001678 return au0828_writereg(dev, reg->reg, reg->val);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001679}
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001680#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001681
Hans Verkuil83b09422013-02-15 09:14:00 -03001682static int vidioc_log_status(struct file *file, void *fh)
1683{
1684 struct video_device *vdev = video_devdata(file);
1685
Shuah Khan05439b12015-01-29 13:41:32 -03001686 dprintk(1, "%s called\n", __func__);
1687
Hans Verkuil83b09422013-02-15 09:14:00 -03001688 v4l2_ctrl_log_status(file, fh);
1689 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1690 return 0;
1691}
1692
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001693void au0828_v4l2_suspend(struct au0828_dev *dev)
1694{
1695 struct urb *urb;
1696 int i;
1697
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -03001698 pr_info("stopping V4L2\n");
1699
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001700 if (dev->stream_state == STREAM_ON) {
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -03001701 pr_info("stopping V4L2 active URBs\n");
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001702 au0828_analog_stream_disable(dev);
1703 /* stop urbs */
1704 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1705 urb = dev->isoc_ctl.urb[i];
1706 if (urb) {
1707 if (!irqs_disabled())
1708 usb_kill_urb(urb);
1709 else
1710 usb_unlink_urb(urb);
1711 }
1712 }
1713 }
1714
1715 if (dev->vid_timeout_running)
1716 del_timer_sync(&dev->vid_timeout);
1717 if (dev->vbi_timeout_running)
1718 del_timer_sync(&dev->vbi_timeout);
1719}
1720
1721void au0828_v4l2_resume(struct au0828_dev *dev)
1722{
1723 int i, rc;
1724
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -03001725 pr_info("restarting V4L2\n");
1726
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001727 if (dev->stream_state == STREAM_ON) {
1728 au0828_stream_interrupt(dev);
1729 au0828_init_tuner(dev);
1730 }
1731
1732 if (dev->vid_timeout_running)
1733 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1734 if (dev->vbi_timeout_running)
1735 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1736
1737 /* If we were doing ac97 instead of i2s, it would go here...*/
1738 au0828_i2s_init(dev);
1739
1740 au0828_analog_stream_enable(dev);
1741
1742 if (!(dev->stream_state == STREAM_ON)) {
1743 au0828_analog_stream_reset(dev);
1744 /* submit urbs */
1745 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1746 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1747 if (rc) {
1748 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1749 i, rc);
1750 au0828_uninit_isoc(dev);
1751 }
1752 }
1753 }
1754}
1755
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001756static struct v4l2_file_operations au0828_v4l_fops = {
1757 .owner = THIS_MODULE,
1758 .open = au0828_v4l2_open,
1759 .release = au0828_v4l2_close,
Shuah Khan05439b12015-01-29 13:41:32 -03001760 .read = vb2_fop_read,
1761 .poll = vb2_fop_poll,
1762 .mmap = vb2_fop_mmap,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001763 .unlocked_ioctl = video_ioctl2,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001764};
1765
1766static const struct v4l2_ioctl_ops video_ioctl_ops = {
1767 .vidioc_querycap = vidioc_querycap,
1768 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1769 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1770 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1771 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller5a5a4e12009-03-11 03:00:55 -03001772 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001773 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001774 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001775 .vidioc_enumaudio = vidioc_enumaudio,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001776 .vidioc_g_audio = vidioc_g_audio,
1777 .vidioc_s_audio = vidioc_s_audio,
1778 .vidioc_cropcap = vidioc_cropcap,
Shuah Khan05439b12015-01-29 13:41:32 -03001779
1780 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1781 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1782 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1783 .vidioc_querybuf = vb2_ioctl_querybuf,
1784 .vidioc_qbuf = vb2_ioctl_qbuf,
1785 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1786 .vidioc_expbuf = vb2_ioctl_expbuf,
1787
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001788 .vidioc_s_std = vidioc_s_std,
Hans Verkuil33b6b892013-02-15 09:22:37 -03001789 .vidioc_g_std = vidioc_g_std,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001790 .vidioc_enum_input = vidioc_enum_input,
1791 .vidioc_g_input = vidioc_g_input,
1792 .vidioc_s_input = vidioc_s_input,
Shuah Khan05439b12015-01-29 13:41:32 -03001793
1794 .vidioc_streamon = vb2_ioctl_streamon,
1795 .vidioc_streamoff = vb2_ioctl_streamoff,
1796
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001797 .vidioc_g_tuner = vidioc_g_tuner,
1798 .vidioc_s_tuner = vidioc_s_tuner,
1799 .vidioc_g_frequency = vidioc_g_frequency,
1800 .vidioc_s_frequency = vidioc_s_frequency,
1801#ifdef CONFIG_VIDEO_ADV_DEBUG
1802 .vidioc_g_register = vidioc_g_register,
1803 .vidioc_s_register = vidioc_s_register,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001804#endif
Hans Verkuil83b09422013-02-15 09:14:00 -03001805 .vidioc_log_status = vidioc_log_status,
1806 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1807 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001808};
1809
1810static const struct video_device au0828_video_template = {
1811 .fops = &au0828_v4l_fops,
Shuah Khan41071bb2015-02-26 19:33:13 -03001812 .release = video_device_release_empty,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001813 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehabf2fd7ce2014-06-08 13:54:56 -03001814 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001815};
1816
Shuah Khan05439b12015-01-29 13:41:32 -03001817static int au0828_vb2_setup(struct au0828_dev *dev)
1818{
1819 int rc;
1820 struct vb2_queue *q;
1821
1822 /* Setup Videobuf2 for Video capture */
1823 q = &dev->vb_vidq;
1824 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1825 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1826 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1827 q->drv_priv = dev;
1828 q->buf_struct_size = sizeof(struct au0828_buffer);
1829 q->ops = &au0828_video_qops;
1830 q->mem_ops = &vb2_vmalloc_memops;
1831
1832 rc = vb2_queue_init(q);
1833 if (rc < 0)
1834 return rc;
1835
1836 /* Setup Videobuf2 for VBI capture */
1837 q = &dev->vb_vbiq;
1838 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1839 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1840 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1841 q->drv_priv = dev;
1842 q->buf_struct_size = sizeof(struct au0828_buffer);
1843 q->ops = &au0828_vbi_qops;
1844 q->mem_ops = &vb2_vmalloc_memops;
1845
1846 rc = vb2_queue_init(q);
1847 if (rc < 0)
1848 return rc;
1849
1850 return 0;
1851}
1852
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001853static void au0828_analog_create_entities(struct au0828_dev *dev)
1854{
1855#if defined(CONFIG_MEDIA_CONTROLLER)
1856 static const char * const inames[] = {
1857 [AU0828_VMUX_COMPOSITE] = "Composite",
1858 [AU0828_VMUX_SVIDEO] = "S-Video",
1859 [AU0828_VMUX_CABLE] = "Cable TV",
1860 [AU0828_VMUX_TELEVISION] = "Television",
1861 [AU0828_VMUX_DVB] = "DVB",
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001862 };
1863 int ret, i;
1864
1865 /* Initialize Video and VBI pads */
1866 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001867 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001868 if (ret < 0)
1869 pr_err("failed to initialize video media entity!\n");
1870
1871 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001872 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001873 if (ret < 0)
1874 pr_err("failed to initialize vbi media entity!\n");
1875
1876 /* Create entities for each input connector */
1877 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1878 struct media_entity *ent = &dev->input_ent[i];
1879
1880 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1881 break;
1882
1883 ent->name = inames[AUVI_INPUT(i).type];
1884 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1885 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1886
1887 switch (AUVI_INPUT(i).type) {
1888 case AU0828_VMUX_COMPOSITE:
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -02001889 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001890 break;
1891 case AU0828_VMUX_SVIDEO:
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -02001892 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001893 break;
1894 case AU0828_VMUX_CABLE:
1895 case AU0828_VMUX_TELEVISION:
1896 case AU0828_VMUX_DVB:
Mauro Carvalho Chehab34ac2532016-02-12 09:15:41 -02001897 default: /* Just to shut up a warning */
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -02001898 ent->function = MEDIA_ENT_F_CONN_RF;
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001899 break;
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001900 }
1901
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001902 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03001903 if (ret < 0)
1904 pr_err("failed to initialize input pad[%d]!\n", i);
1905
1906 ret = media_device_register_entity(dev->media_dev, ent);
1907 if (ret < 0)
1908 pr_err("failed to register input entity %d!\n", i);
1909 }
1910#endif
1911}
1912
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001913/**************************************************************************/
1914
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001915int au0828_analog_register(struct au0828_dev *dev,
1916 struct usb_interface *interface)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001917{
1918 int retval = -ENOMEM;
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001919 struct usb_host_interface *iface_desc;
1920 struct usb_endpoint_descriptor *endpoint;
Julia Lawalld63b21b2012-04-22 07:54:42 -03001921 int i, ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001922
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -03001923 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1924 interface->cur_altsetting->desc.bInterfaceNumber);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001925
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -02001926 /* No analog TV */
1927 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1928 return 0;
1929
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001930 /* set au0828 usb interface0 to as5 */
1931 retval = usb_set_interface(dev->usbdev,
Devin Heitmueller62899a22009-03-15 18:48:52 -03001932 interface->cur_altsetting->desc.bInterfaceNumber, 5);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001933 if (retval != 0) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -03001934 pr_info("Failure setting usb interface0 to as5\n");
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001935 return retval;
1936 }
1937
1938 /* Figure out which endpoint has the isoc interface */
1939 iface_desc = interface->cur_altsetting;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001940 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001941 endpoint = &iface_desc->endpoint[i].desc;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001942 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1943 == USB_DIR_IN) &&
1944 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1945 == USB_ENDPOINT_XFER_ISOC)) {
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001946
1947 /* we find our isoc in endpoint */
1948 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001949 dev->max_pkt_size = (tmp & 0x07ff) *
1950 (((tmp & 0x1800) >> 11) + 1);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001951 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -03001952 dprintk(1,
1953 "Found isoc endpoint 0x%02x, max size = %d\n",
1954 dev->isoc_in_endpointaddr, dev->max_pkt_size);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001955 }
1956 }
Devin Heitmueller62899a22009-03-15 18:48:52 -03001957 if (!(dev->isoc_in_endpointaddr)) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -03001958 pr_info("Could not locate isoc endpoint\n");
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001959 return -ENODEV;
1960 }
1961
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001962 init_waitqueue_head(&dev->open);
1963 spin_lock_init(&dev->slock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001964
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001965 /* init video dma queues */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001966 INIT_LIST_HEAD(&dev->vidq.active);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001967 INIT_LIST_HEAD(&dev->vbiq.active);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001968
Julia Lawall30a8f2a2014-12-26 11:35:32 -03001969 setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
1970 (unsigned long)dev);
1971 setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
1972 (unsigned long)dev);
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001973
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001974 dev->width = NTSC_STD_W;
1975 dev->height = NTSC_STD_H;
1976 dev->field_size = dev->width * dev->height;
1977 dev->frame_size = dev->field_size << 1;
1978 dev->bytesperline = dev->width << 1;
Hans Verkuilde8d2bb2013-03-11 16:12:17 -03001979 dev->vbi_width = 720;
1980 dev->vbi_height = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001981 dev->ctrl_ainput = 0;
Hans Verkuile1cf6582013-03-22 13:32:20 -03001982 dev->ctrl_freq = 960;
Hans Verkuil33b6b892013-02-15 09:22:37 -03001983 dev->std = V4L2_STD_NTSC_M;
Shuah Khan174ced22016-02-12 21:18:03 -02001984 /* Default input is TV Tuner */
Hans Verkuil56230d12013-03-11 16:18:31 -03001985 au0828_s_input(dev, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001986
Shuah Khan05439b12015-01-29 13:41:32 -03001987 mutex_init(&dev->vb_queue_lock);
1988 mutex_init(&dev->vb_vbi_queue_lock);
1989
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001990 /* Fill the video capture device struct */
Shuah Khan41071bb2015-02-26 19:33:13 -03001991 dev->vdev = au0828_video_template;
1992 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1993 dev->vdev.lock = &dev->lock;
1994 dev->vdev.queue = &dev->vb_vidq;
1995 dev->vdev.queue->lock = &dev->vb_queue_lock;
1996 strcpy(dev->vdev.name, "au0828a video");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001997
1998 /* Setup the VBI device */
Shuah Khan41071bb2015-02-26 19:33:13 -03001999 dev->vbi_dev = au0828_video_template;
2000 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
2001 dev->vbi_dev.lock = &dev->lock;
2002 dev->vbi_dev.queue = &dev->vb_vbiq;
2003 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
2004 strcpy(dev->vbi_dev.name, "au0828a vbi");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002005
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -03002006 /* Init entities at the Media Controller */
2007 au0828_analog_create_entities(dev);
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -03002008
Shuah Khan05439b12015-01-29 13:41:32 -03002009 /* initialize videobuf2 stuff */
2010 retval = au0828_vb2_setup(dev);
2011 if (retval != 0) {
2012 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2013 retval);
Shuah Khan41071bb2015-02-26 19:33:13 -03002014 return -ENODEV;
Shuah Khan05439b12015-01-29 13:41:32 -03002015 }
2016
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002017 /* Register the v4l2 device */
Shuah Khan41071bb2015-02-26 19:33:13 -03002018 video_set_drvdata(&dev->vdev, dev);
2019 retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
Devin Heitmueller62899a22009-03-15 18:48:52 -03002020 if (retval != 0) {
2021 dprintk(1, "unable to register video device (error = %d).\n",
2022 retval);
Julia Lawalld63b21b2012-04-22 07:54:42 -03002023 ret = -ENODEV;
Shuah Khan05439b12015-01-29 13:41:32 -03002024 goto err_reg_vdev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002025 }
2026
2027 /* Register the vbi device */
Shuah Khan41071bb2015-02-26 19:33:13 -03002028 video_set_drvdata(&dev->vbi_dev, dev);
2029 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
Devin Heitmueller62899a22009-03-15 18:48:52 -03002030 if (retval != 0) {
2031 dprintk(1, "unable to register vbi device (error = %d).\n",
2032 retval);
Julia Lawalld63b21b2012-04-22 07:54:42 -03002033 ret = -ENODEV;
Shuah Khan05439b12015-01-29 13:41:32 -03002034 goto err_reg_vbi_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002035 }
Mauro Carvalho Chehab9822f412016-03-02 10:11:41 -03002036
2037#ifdef CONFIG_MEDIA_CONTROLLER
2038 retval = v4l2_mc_create_media_graph(dev->media_dev);
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -02002039 if (retval) {
2040 pr_err("%s() au0282_dev_register failed to create graph\n",
2041 __func__);
2042 ret = -ENODEV;
2043 goto err_reg_vbi_dev;
2044 }
Mauro Carvalho Chehab9822f412016-03-02 10:11:41 -03002045#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002046
Devin Heitmueller62899a22009-03-15 18:48:52 -03002047 dprintk(1, "%s completed!\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002048
2049 return 0;
Julia Lawalld63b21b2012-04-22 07:54:42 -03002050
Shuah Khan05439b12015-01-29 13:41:32 -03002051err_reg_vbi_dev:
Shuah Khan41071bb2015-02-26 19:33:13 -03002052 video_unregister_device(&dev->vdev);
Shuah Khan05439b12015-01-29 13:41:32 -03002053err_reg_vdev:
2054 vb2_queue_release(&dev->vb_vidq);
2055 vb2_queue_release(&dev->vb_vbiq);
Julia Lawalld63b21b2012-04-22 07:54:42 -03002056 return ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002057}
2058