blob: 5f337b118bffe36e9969c3a8963e9871034ffce1 [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 *
25 * VBI support is not yet working
26 * The hardware scaler supported is unimplemented
27 * AC97 audio support is unimplemented (only i2s audio mode)
28 *
29 */
30
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -030031#include "au0828.h"
32
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>
38#include <media/v4l2-ioctl.h>
Hans Verkuil83b09422013-02-15 09:14:00 -030039#include <media/v4l2-event.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030040#include <media/tuner.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030041#include "au0828-reg.h"
42
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030043static DEFINE_MUTEX(au0828_sysfs_lock);
44
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030045/* ------------------------------------------------------------------
46 Videobuf operations
47 ------------------------------------------------------------------*/
48
49static unsigned int isoc_debug;
50module_param(isoc_debug, int, 0644);
51MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
52
53#define au0828_isocdbg(fmt, arg...) \
54do {\
55 if (isoc_debug) { \
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -030056 pr_info("au0828 %s :"fmt, \
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030057 __func__ , ##arg); \
58 } \
59 } while (0)
60
Hans Verkuilfa09cb92013-03-11 16:10:33 -030061static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
62{
63 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
64 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
65}
66
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030067static inline void print_err_status(struct au0828_dev *dev,
68 int packet, int status)
69{
70 char *errmsg = "Unknown";
71
72 switch (status) {
73 case -ENOENT:
74 errmsg = "unlinked synchronuously";
75 break;
76 case -ECONNRESET:
77 errmsg = "unlinked asynchronuously";
78 break;
79 case -ENOSR:
80 errmsg = "Buffer error (overrun)";
81 break;
82 case -EPIPE:
83 errmsg = "Stalled (device not responding)";
84 break;
85 case -EOVERFLOW:
86 errmsg = "Babble (bad cable?)";
87 break;
88 case -EPROTO:
89 errmsg = "Bit-stuff error (bad cable?)";
90 break;
91 case -EILSEQ:
92 errmsg = "CRC/Timeout (could be anything)";
93 break;
94 case -ETIME:
95 errmsg = "Device does not respond";
96 break;
97 }
98 if (packet < 0) {
99 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
100 } else {
101 au0828_isocdbg("URB packet %d, status %d [%s].\n",
102 packet, status, errmsg);
103 }
104}
105
106static int check_dev(struct au0828_dev *dev)
107{
108 if (dev->dev_state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -0300109 pr_info("v4l2 ioctl: device not present\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300110 return -ENODEV;
111 }
112
113 if (dev->dev_state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -0300114 pr_info("v4l2 ioctl: device is misconfigured; "
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300115 "close and open it again\n");
116 return -EIO;
117 }
118 return 0;
119}
120
121/*
122 * IRQ callback, called by URB callback
123 */
124static void au0828_irq_callback(struct urb *urb)
125{
126 struct au0828_dmaqueue *dma_q = urb->context;
127 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
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
222 /* De-allocates all pending stuff */
223 au0828_uninit_isoc(dev);
224
225 dev->isoc_ctl.isoc_copy = isoc_copy;
226 dev->isoc_ctl.num_bufs = num_bufs;
227
228 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
229 if (!dev->isoc_ctl.urb) {
230 au0828_isocdbg("cannot alloc memory for usb buffers\n");
231 return -ENOMEM;
232 }
233
234 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
235 GFP_KERNEL);
236 if (!dev->isoc_ctl.transfer_buffer) {
237 au0828_isocdbg("cannot allocate memory for usb transfer\n");
238 kfree(dev->isoc_ctl.urb);
239 return -ENOMEM;
240 }
241
242 dev->isoc_ctl.max_pkt_size = max_pkt_size;
243 dev->isoc_ctl.buf = NULL;
244
245 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
246
247 /* allocate urbs and transfer buffers */
248 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
249 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
250 if (!urb) {
251 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
252 au0828_uninit_isoc(dev);
253 return -ENOMEM;
254 }
255 dev->isoc_ctl.urb[i] = urb;
256
Daniel Mack997ea582010-04-12 13:17:25 +0200257 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300258 sb_size, GFP_KERNEL, &urb->transfer_dma);
259 if (!dev->isoc_ctl.transfer_buffer[i]) {
260 printk("unable to allocate %i bytes for transfer"
261 " buffer %i%s\n",
262 sb_size, i,
263 in_interrupt() ? " while in int" : "");
264 au0828_uninit_isoc(dev);
265 return -ENOMEM;
266 }
267 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
268
269 pipe = usb_rcvisocpipe(dev->usbdev,
270 dev->isoc_in_endpointaddr),
271
272 usb_fill_int_urb(urb, dev->usbdev, pipe,
273 dev->isoc_ctl.transfer_buffer[i], sb_size,
274 au0828_irq_callback, dma_q, 1);
275
276 urb->number_of_packets = max_packets;
Devin Heitmuellerfadadb72009-03-22 23:42:26 -0300277 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300278
279 k = 0;
280 for (j = 0; j < max_packets; j++) {
281 urb->iso_frame_desc[j].offset = k;
282 urb->iso_frame_desc[j].length =
283 dev->isoc_ctl.max_pkt_size;
284 k += dev->isoc_ctl.max_pkt_size;
285 }
286 }
287
288 init_waitqueue_head(&dma_q->wq);
289
290 /* submit urbs and enables IRQ */
291 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
292 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
293 if (rc) {
294 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
295 i, rc);
296 au0828_uninit_isoc(dev);
297 return rc;
298 }
299 }
300
301 return 0;
302}
303
304/*
305 * Announces that a buffer were filled and request the next
306 */
307static inline void buffer_filled(struct au0828_dev *dev,
308 struct au0828_dmaqueue *dma_q,
309 struct au0828_buffer *buf)
310{
311 /* Advice that buffer was filled */
312 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
313
314 buf->vb.state = VIDEOBUF_DONE;
315 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300316 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300317
318 dev->isoc_ctl.buf = NULL;
319
320 list_del(&buf->vb.queue);
321 wake_up(&buf->vb.done);
322}
323
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300324static inline void vbi_buffer_filled(struct au0828_dev *dev,
325 struct au0828_dmaqueue *dma_q,
326 struct au0828_buffer *buf)
327{
328 /* Advice that buffer was filled */
329 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
330
331 buf->vb.state = VIDEOBUF_DONE;
332 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300333 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300334
335 dev->isoc_ctl.vbi_buf = NULL;
336
337 list_del(&buf->vb.queue);
338 wake_up(&buf->vb.done);
339}
340
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300341/*
342 * Identify the buffer header type and properly handles
343 */
344static void au0828_copy_video(struct au0828_dev *dev,
345 struct au0828_dmaqueue *dma_q,
346 struct au0828_buffer *buf,
347 unsigned char *p,
348 unsigned char *outp, unsigned long len)
349{
350 void *fieldstart, *startwrite, *startread;
351 int linesdone, currlinedone, offset, lencopy, remain;
352 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
353
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300354 if (len == 0)
355 return;
356
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300357 if (dma_q->pos + len > buf->vb.size)
358 len = buf->vb.size - dma_q->pos;
359
360 startread = p;
361 remain = len;
362
363 /* Interlaces frame */
364 if (buf->top_field)
365 fieldstart = outp;
366 else
367 fieldstart = outp + bytesperline;
368
369 linesdone = dma_q->pos / bytesperline;
370 currlinedone = dma_q->pos % bytesperline;
371 offset = linesdone * bytesperline * 2 + currlinedone;
372 startwrite = fieldstart + offset;
373 lencopy = bytesperline - currlinedone;
374 lencopy = lencopy > remain ? remain : lencopy;
375
376 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
377 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
378 ((char *)startwrite + lencopy) -
379 ((char *)outp + buf->vb.size));
380 remain = (char *)outp + buf->vb.size - (char *)startwrite;
381 lencopy = remain;
382 }
383 if (lencopy <= 0)
384 return;
385 memcpy(startwrite, startread, lencopy);
386
387 remain -= lencopy;
388
389 while (remain > 0) {
390 startwrite += lencopy + bytesperline;
391 startread += lencopy;
392 if (bytesperline > remain)
393 lencopy = remain;
394 else
395 lencopy = bytesperline;
396
397 if ((char *)startwrite + lencopy > (char *)outp +
398 buf->vb.size) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300399 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300400 ((char *)startwrite + lencopy) -
401 ((char *)outp + buf->vb.size));
402 lencopy = remain = (char *)outp + buf->vb.size -
403 (char *)startwrite;
404 }
405 if (lencopy <= 0)
406 break;
407
408 memcpy(startwrite, startread, lencopy);
409
410 remain -= lencopy;
411 }
412
413 if (offset > 1440) {
414 /* We have enough data to check for greenscreen */
Devin Heitmueller62899a22009-03-15 18:48:52 -0300415 if (outp[0] < 0x60 && outp[1440] < 0x60)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300416 dev->greenscreen_detected = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300417 }
418
419 dma_q->pos += len;
420}
421
422/*
423 * video-buf generic routine to get the next available buffer
424 */
425static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
426 struct au0828_buffer **buf)
427{
428 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
429
430 if (list_empty(&dma_q->active)) {
431 au0828_isocdbg("No active queue to serve\n");
432 dev->isoc_ctl.buf = NULL;
433 *buf = NULL;
434 return;
435 }
436
437 /* Get the next buffer */
438 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
439 dev->isoc_ctl.buf = *buf;
440
441 return;
442}
443
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300444static void au0828_copy_vbi(struct au0828_dev *dev,
445 struct au0828_dmaqueue *dma_q,
446 struct au0828_buffer *buf,
447 unsigned char *p,
448 unsigned char *outp, unsigned long len)
449{
450 unsigned char *startwrite, *startread;
Dan Carpenterb5f59332010-07-23 07:09:20 -0300451 int bytesperline;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300452 int i, j = 0;
453
454 if (dev == NULL) {
455 au0828_isocdbg("dev is null\n");
456 return;
457 }
458
459 if (dma_q == NULL) {
460 au0828_isocdbg("dma_q is null\n");
461 return;
462 }
463 if (buf == NULL)
464 return;
465 if (p == NULL) {
466 au0828_isocdbg("p is null\n");
467 return;
468 }
469 if (outp == NULL) {
470 au0828_isocdbg("outp is null\n");
471 return;
472 }
473
Dan Carpenterb5f59332010-07-23 07:09:20 -0300474 bytesperline = dev->vbi_width;
475
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300476 if (dma_q->pos + len > buf->vb.size)
477 len = buf->vb.size - dma_q->pos;
478
479 startread = p;
480 startwrite = outp + (dma_q->pos / 2);
481
482 /* Make sure the bottom field populates the second half of the frame */
483 if (buf->top_field == 0)
484 startwrite += bytesperline * dev->vbi_height;
485
486 for (i = 0; i < len; i += 2)
487 startwrite[j++] = startread[i+1];
488
489 dma_q->pos += len;
490}
491
492
493/*
494 * video-buf generic routine to get the next available VBI buffer
495 */
496static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
497 struct au0828_buffer **buf)
498{
499 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
500 char *outp;
501
502 if (list_empty(&dma_q->active)) {
503 au0828_isocdbg("No active queue to serve\n");
504 dev->isoc_ctl.vbi_buf = NULL;
505 *buf = NULL;
506 return;
507 }
508
509 /* Get the next buffer */
510 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300511 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300512 outp = videobuf_to_vmalloc(&(*buf)->vb);
513 memset(outp, 0x00, (*buf)->vb.size);
514
515 dev->isoc_ctl.vbi_buf = *buf;
516
517 return;
518}
519
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300520/*
521 * Controls the isoc copy of each urb packet
522 */
523static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
524{
525 struct au0828_buffer *buf;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300526 struct au0828_buffer *vbi_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300527 struct au0828_dmaqueue *dma_q = urb->context;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300528 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300529 unsigned char *outp = NULL;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300530 unsigned char *vbioutp = NULL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300531 int i, len = 0, rc = 1;
532 unsigned char *p;
533 unsigned char fbyte;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300534 unsigned int vbi_field_size;
535 unsigned int remain, lencopy;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300536
537 if (!dev)
538 return 0;
539
540 if ((dev->dev_state & DEV_DISCONNECTED) ||
541 (dev->dev_state & DEV_MISCONFIGURED))
542 return 0;
543
544 if (urb->status < 0) {
545 print_err_status(dev, -1, urb->status);
546 if (urb->status == -ENOENT)
547 return 0;
548 }
549
550 buf = dev->isoc_ctl.buf;
551 if (buf != NULL)
552 outp = videobuf_to_vmalloc(&buf->vb);
553
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300554 vbi_buf = dev->isoc_ctl.vbi_buf;
555 if (vbi_buf != NULL)
556 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
557
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300558 for (i = 0; i < urb->number_of_packets; i++) {
559 int status = urb->iso_frame_desc[i].status;
560
561 if (status < 0) {
562 print_err_status(dev, i, status);
563 if (urb->iso_frame_desc[i].status != -EPROTO)
564 continue;
565 }
566
Devin Heitmueller62899a22009-03-15 18:48:52 -0300567 if (urb->iso_frame_desc[i].actual_length <= 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300568 continue;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300569
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300570 if (urb->iso_frame_desc[i].actual_length >
571 dev->max_pkt_size) {
572 au0828_isocdbg("packet bigger than packet size");
573 continue;
574 }
575
576 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
577 fbyte = p[0];
578 len = urb->iso_frame_desc[i].actual_length - 4;
579 p += 4;
580
581 if (fbyte & 0x80) {
582 len -= 4;
583 p += 4;
584 au0828_isocdbg("Video frame %s\n",
585 (fbyte & 0x40) ? "odd" : "even");
Devin Heitmuellerbde3bb92010-07-05 13:05:16 -0300586 if (fbyte & 0x40) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300587 /* VBI */
588 if (vbi_buf != NULL)
589 vbi_buffer_filled(dev,
590 vbi_dma_q,
591 vbi_buf);
592 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
593 if (vbi_buf == NULL)
594 vbioutp = NULL;
595 else
596 vbioutp = videobuf_to_vmalloc(
597 &vbi_buf->vb);
598
599 /* Video */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300600 if (buf != NULL)
601 buffer_filled(dev, dma_q, buf);
602 get_next_buf(dma_q, &buf);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300603 if (buf == NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300604 outp = NULL;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300605 else
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300606 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300607
608 /* As long as isoc traffic is arriving, keep
609 resetting the timer */
610 if (dev->vid_timeout_running)
611 mod_timer(&dev->vid_timeout,
612 jiffies + (HZ / 10));
613 if (dev->vbi_timeout_running)
614 mod_timer(&dev->vbi_timeout,
615 jiffies + (HZ / 10));
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300616 }
617
618 if (buf != NULL) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300619 if (fbyte & 0x40)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300620 buf->top_field = 1;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300621 else
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300622 buf->top_field = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300623 }
624
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300625 if (vbi_buf != NULL) {
626 if (fbyte & 0x40)
627 vbi_buf->top_field = 1;
628 else
629 vbi_buf->top_field = 0;
630 }
631
632 dev->vbi_read = 0;
633 vbi_dma_q->pos = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300634 dma_q->pos = 0;
635 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300636
637 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
638 if (dev->vbi_read < vbi_field_size) {
639 remain = vbi_field_size - dev->vbi_read;
640 if (len < remain)
641 lencopy = len;
642 else
643 lencopy = remain;
644
645 if (vbi_buf != NULL)
646 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
647 vbioutp, len);
648
649 len -= lencopy;
650 p += lencopy;
651 dev->vbi_read += lencopy;
652 }
653
654 if (dev->vbi_read >= vbi_field_size && buf != NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300655 au0828_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300656 }
657 return rc;
658}
659
660static int
661buffer_setup(struct videobuf_queue *vq, unsigned int *count,
662 unsigned int *size)
663{
664 struct au0828_fh *fh = vq->priv_data;
665 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
666
667 if (0 == *count)
668 *count = AU0828_DEF_BUF;
669
670 if (*count < AU0828_MIN_BUF)
671 *count = AU0828_MIN_BUF;
672 return 0;
673}
674
675/* This is called *without* dev->slock held; please keep it that way */
676static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
677{
678 struct au0828_fh *fh = vq->priv_data;
679 struct au0828_dev *dev = fh->dev;
680 unsigned long flags = 0;
681 if (in_interrupt())
682 BUG();
683
684 /* We used to wait for the buffer to finish here, but this didn't work
685 because, as we were keeping the state as VIDEOBUF_QUEUED,
686 videobuf_queue_cancel marked it as finished for us.
687 (Also, it could wedge forever if the hardware was misconfigured.)
688
689 This should be safe; by the time we get here, the buffer isn't
690 queued anymore. If we ever start marking the buffers as
691 VIDEOBUF_ACTIVE, it won't be, though.
692 */
693 spin_lock_irqsave(&dev->slock, flags);
694 if (dev->isoc_ctl.buf == buf)
695 dev->isoc_ctl.buf = NULL;
696 spin_unlock_irqrestore(&dev->slock, flags);
697
698 videobuf_vmalloc_free(&buf->vb);
699 buf->vb.state = VIDEOBUF_NEEDS_INIT;
700}
701
702static int
703buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
704 enum v4l2_field field)
705{
706 struct au0828_fh *fh = vq->priv_data;
707 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
708 struct au0828_dev *dev = fh->dev;
709 int rc = 0, urb_init = 0;
710
711 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
712
713 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
714 return -EINVAL;
715
716 buf->vb.width = dev->width;
717 buf->vb.height = dev->height;
718 buf->vb.field = field;
719
720 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
721 rc = videobuf_iolock(vq, &buf->vb, NULL);
722 if (rc < 0) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -0300723 pr_info("videobuf_iolock failed\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300724 goto fail;
725 }
726 }
727
728 if (!dev->isoc_ctl.num_bufs)
729 urb_init = 1;
730
731 if (urb_init) {
732 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
733 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
734 au0828_isoc_copy);
735 if (rc < 0) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -0300736 pr_info("au0828_init_isoc failed\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300737 goto fail;
738 }
739 }
740
741 buf->vb.state = VIDEOBUF_PREPARED;
742 return 0;
743
744fail:
745 free_buffer(vq, buf);
746 return rc;
747}
748
749static void
750buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
751{
752 struct au0828_buffer *buf = container_of(vb,
753 struct au0828_buffer,
754 vb);
755 struct au0828_fh *fh = vq->priv_data;
756 struct au0828_dev *dev = fh->dev;
757 struct au0828_dmaqueue *vidq = &dev->vidq;
758
759 buf->vb.state = VIDEOBUF_QUEUED;
760 list_add_tail(&buf->vb.queue, &vidq->active);
761}
762
763static void buffer_release(struct videobuf_queue *vq,
764 struct videobuf_buffer *vb)
765{
766 struct au0828_buffer *buf = container_of(vb,
767 struct au0828_buffer,
768 vb);
769
770 free_buffer(vq, buf);
771}
772
773static struct videobuf_queue_ops au0828_video_qops = {
774 .buf_setup = buffer_setup,
775 .buf_prepare = buffer_prepare,
776 .buf_queue = buffer_queue,
777 .buf_release = buffer_release,
778};
779
780/* ------------------------------------------------------------------
781 V4L2 interface
782 ------------------------------------------------------------------*/
783
784static int au0828_i2s_init(struct au0828_dev *dev)
785{
786 /* Enable i2s mode */
787 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
788 return 0;
789}
790
791/*
792 * Auvitek au0828 analog stream enable
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300793 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300794static int au0828_analog_stream_enable(struct au0828_dev *d)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300795{
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300796 struct usb_interface *iface;
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300797 int ret, h, w;
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300798
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300799 dprintk(1, "au0828_analog_stream_enable called\n");
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300800
801 iface = usb_ifnum_to_if(d->usbdev, 0);
802 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
803 dprintk(1, "Changing intf#0 to alt 5\n");
804 /* set au0828 interface0 to AS5 here again */
805 ret = usb_set_interface(d->usbdev, 0, 5);
806 if (ret < 0) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -0300807 pr_info("Au0828 can't set alt setting to 5!\n");
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300808 return -EBUSY;
809 }
810 }
811
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300812 h = d->height / 2 + 2;
813 w = d->width * 2;
Mauro Carvalho Chehab64ea37b2014-06-08 13:54:57 -0300814
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300815 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
816 au0828_writereg(d, 0x106, 0x00);
817 /* set x position */
818 au0828_writereg(d, 0x110, 0x00);
819 au0828_writereg(d, 0x111, 0x00);
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300820 au0828_writereg(d, 0x114, w & 0xff);
821 au0828_writereg(d, 0x115, w >> 8);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300822 /* set y position */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300823 au0828_writereg(d, 0x112, 0x00);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300824 au0828_writereg(d, 0x113, 0x00);
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -0300825 au0828_writereg(d, 0x116, h & 0xff);
826 au0828_writereg(d, 0x117, h >> 8);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300827 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
828
829 return 0;
830}
831
832int au0828_analog_stream_disable(struct au0828_dev *d)
833{
834 dprintk(1, "au0828_analog_stream_disable called\n");
835 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
836 return 0;
837}
838
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300839static void au0828_analog_stream_reset(struct au0828_dev *dev)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300840{
841 dprintk(1, "au0828_analog_stream_reset called\n");
842 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
843 mdelay(30);
844 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
845}
846
847/*
848 * Some operations needs to stop current streaming
849 */
850static int au0828_stream_interrupt(struct au0828_dev *dev)
851{
852 int ret = 0;
853
854 dev->stream_state = STREAM_INTERRUPT;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300855 if (dev->dev_state == DEV_DISCONNECTED)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300856 return -ENODEV;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300857 else if (ret) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300858 dev->dev_state = DEV_MISCONFIGURED;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300859 dprintk(1, "%s device is misconfigured!\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300860 return ret;
861 }
862 return 0;
863}
864
865/*
866 * au0828_release_resources
867 * unregister v4l2 devices
868 */
869void au0828_analog_unregister(struct au0828_dev *dev)
870{
871 dprintk(1, "au0828_release_resources called\n");
872 mutex_lock(&au0828_sysfs_lock);
873
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200874 if (dev->vdev)
Devin Heitmueller5a5a4e12009-03-11 03:00:55 -0300875 video_unregister_device(dev->vdev);
876 if (dev->vbi_dev)
877 video_unregister_device(dev->vbi_dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300878
879 mutex_unlock(&au0828_sysfs_lock);
880}
881
882
883/* Usage lock check functions */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300884static int res_get(struct au0828_fh *fh, unsigned int bit)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300885{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300886 struct au0828_dev *dev = fh->dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300887
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300888 if (fh->resources & bit)
889 /* have it already allocated */
890 return 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300891
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300892 /* is it free? */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300893 if (dev->resources & bit) {
894 /* no, someone else uses it */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300895 return 0;
896 }
897 /* it's free, grab it */
898 fh->resources |= bit;
899 dev->resources |= bit;
900 dprintk(1, "res: get %d\n", bit);
Devin Heitmueller549ee4d2012-08-06 22:46:58 -0300901
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300902 return 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300903}
904
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300905static int res_check(struct au0828_fh *fh, unsigned int bit)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300906{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300907 return fh->resources & bit;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300908}
909
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300910static int res_locked(struct au0828_dev *dev, unsigned int bit)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300911{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300912 return dev->resources & bit;
913}
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300914
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300915static void res_free(struct au0828_fh *fh, unsigned int bits)
916{
917 struct au0828_dev *dev = fh->dev;
918
919 BUG_ON((fh->resources & bits) != bits);
920
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300921 fh->resources &= ~bits;
922 dev->resources &= ~bits;
923 dprintk(1, "res: put %d\n", bits);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300924}
925
926static int get_ressource(struct au0828_fh *fh)
927{
928 switch (fh->type) {
929 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
930 return AU0828_RESOURCE_VIDEO;
931 case V4L2_BUF_TYPE_VBI_CAPTURE:
932 return AU0828_RESOURCE_VBI;
933 default:
934 BUG();
935 return 0;
936 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300937}
938
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300939/* This function ensures that video frames continue to be delivered even if
940 the ITU-656 input isn't receiving any data (thereby preventing applications
941 such as tvtime from hanging) */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300942static void au0828_vid_buffer_timeout(unsigned long data)
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300943{
944 struct au0828_dev *dev = (struct au0828_dev *) data;
945 struct au0828_dmaqueue *dma_q = &dev->vidq;
946 struct au0828_buffer *buf;
947 unsigned char *vid_data;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300948 unsigned long flags = 0;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300949
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300950 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300951
952 buf = dev->isoc_ctl.buf;
953 if (buf != NULL) {
954 vid_data = videobuf_to_vmalloc(&buf->vb);
955 memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
956 buffer_filled(dev, dma_q, buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300957 }
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300958 get_next_buf(dma_q, &buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300959
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300960 if (dev->vid_timeout_running == 1)
961 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
962
963 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300964}
965
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300966static void au0828_vbi_buffer_timeout(unsigned long data)
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300967{
968 struct au0828_dev *dev = (struct au0828_dev *) data;
969 struct au0828_dmaqueue *dma_q = &dev->vbiq;
970 struct au0828_buffer *buf;
971 unsigned char *vbi_data;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300972 unsigned long flags = 0;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300973
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300974 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300975
976 buf = dev->isoc_ctl.vbi_buf;
977 if (buf != NULL) {
978 vbi_data = videobuf_to_vmalloc(&buf->vb);
979 memset(vbi_data, 0x00, buf->vb.size);
980 vbi_buffer_filled(dev, dma_q, buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300981 }
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300982 vbi_get_next_buf(dma_q, &buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300983
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300984 if (dev->vbi_timeout_running == 1)
985 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
986 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300987}
988
989
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300990static int au0828_v4l2_open(struct file *filp)
991{
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300992 int ret = 0;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300993 struct video_device *vdev = video_devdata(filp);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200994 struct au0828_dev *dev = video_drvdata(filp);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300995 struct au0828_fh *fh;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300996 int type;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300997
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300998 switch (vdev->vfl_type) {
999 case VFL_TYPE_GRABBER:
1000 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1001 break;
1002 case VFL_TYPE_VBI:
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001003 type = V4L2_BUF_TYPE_VBI_CAPTURE;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001004 break;
1005 default:
1006 return -EINVAL;
1007 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001008
1009 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001010 if (NULL == fh) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001011 dprintk(1, "Failed allocate au0828_fh struct!\n");
1012 return -ENOMEM;
1013 }
1014
1015 fh->type = type;
1016 fh->dev = dev;
Hans Verkuil83b09422013-02-15 09:14:00 -03001017 v4l2_fh_init(&fh->fh, vdev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001018 filp->private_data = fh;
1019
Hans Verkuilea86968f2013-03-11 16:22:13 -03001020 if (mutex_lock_interruptible(&dev->lock)) {
1021 kfree(fh);
1022 return -ERESTARTSYS;
1023 }
1024 if (dev->users == 0) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001025 au0828_analog_stream_enable(dev);
1026 au0828_analog_stream_reset(dev);
1027
1028 /* If we were doing ac97 instead of i2s, it would go here...*/
1029 au0828_i2s_init(dev);
1030
1031 dev->stream_state = STREAM_OFF;
1032 dev->dev_state |= DEV_INITIALIZED;
1033 }
1034
1035 dev->users++;
Hans Verkuilea86968f2013-03-11 16:22:13 -03001036 mutex_unlock(&dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001037
1038 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001039 NULL, &dev->slock,
1040 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001041 V4L2_FIELD_INTERLACED,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001042 sizeof(struct au0828_buffer), fh,
1043 &dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001044
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001045 /* VBI Setup */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001046 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1047 NULL, &dev->slock,
1048 V4L2_BUF_TYPE_VBI_CAPTURE,
1049 V4L2_FIELD_SEQ_TB,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001050 sizeof(struct au0828_buffer), fh,
1051 &dev->lock);
Hans Verkuil83b09422013-02-15 09:14:00 -03001052 v4l2_fh_add(&fh->fh);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001053 return ret;
1054}
1055
1056static int au0828_v4l2_close(struct file *filp)
1057{
1058 int ret;
1059 struct au0828_fh *fh = filp->private_data;
1060 struct au0828_dev *dev = fh->dev;
1061
Hans Verkuil83b09422013-02-15 09:14:00 -03001062 v4l2_fh_del(&fh->fh);
1063 v4l2_fh_exit(&fh->fh);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001064 mutex_lock(&dev->lock);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001065 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001066 /* Cancel timeout thread in case they didn't call streamoff */
1067 dev->vid_timeout_running = 0;
1068 del_timer_sync(&dev->vid_timeout);
1069
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001070 videobuf_stop(&fh->vb_vidq);
1071 res_free(fh, AU0828_RESOURCE_VIDEO);
1072 }
1073
1074 if (res_check(fh, AU0828_RESOURCE_VBI)) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001075 /* Cancel timeout thread in case they didn't call streamoff */
1076 dev->vbi_timeout_running = 0;
1077 del_timer_sync(&dev->vbi_timeout);
1078
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001079 videobuf_stop(&fh->vb_vbiq);
1080 res_free(fh, AU0828_RESOURCE_VBI);
1081 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001082
Hans Verkuil823beb72013-03-11 10:16:45 -03001083 if (dev->users == 1 && video_is_registered(video_devdata(filp))) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001084 au0828_analog_stream_disable(dev);
1085
1086 au0828_uninit_isoc(dev);
1087
Devin Heitmuellere4b8bc52009-05-06 20:54:00 -03001088 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001089 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001090 dev->std_set_in_tuner_core = 0;
Devin Heitmuellere4b8bc52009-05-06 20:54:00 -03001091
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001092 /* When close the device, set the usb intf0 into alt0 to free
1093 USB bandwidth */
1094 ret = usb_set_interface(dev->usbdev, 0, 0);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001095 if (ret < 0)
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -03001096 pr_info("Au0828 can't set alternate to 0!\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001097 }
Hans Verkuilea86968f2013-03-11 16:22:13 -03001098 mutex_unlock(&dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001099
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001100 videobuf_mmap_free(&fh->vb_vidq);
1101 videobuf_mmap_free(&fh->vb_vbiq);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001102 kfree(fh);
1103 dev->users--;
1104 wake_up_interruptible_nr(&dev->open, 1);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001105 return 0;
1106}
1107
Hans Verkuilea86968f2013-03-11 16:22:13 -03001108/* Must be called with dev->lock held */
1109static void au0828_init_tuner(struct au0828_dev *dev)
1110{
1111 struct v4l2_frequency f = {
1112 .frequency = dev->ctrl_freq,
1113 .type = V4L2_TUNER_ANALOG_TV,
1114 };
1115
1116 if (dev->std_set_in_tuner_core)
1117 return;
1118 dev->std_set_in_tuner_core = 1;
1119 i2c_gate_ctrl(dev, 1);
1120 /* If we've never sent the standard in tuner core, do so now.
1121 We don't do this at device probe because we don't want to
1122 incur the cost of a firmware load */
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001123 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001124 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1125 i2c_gate_ctrl(dev, 0);
1126}
1127
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001128static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1129 size_t count, loff_t *pos)
1130{
1131 struct au0828_fh *fh = filp->private_data;
1132 struct au0828_dev *dev = fh->dev;
1133 int rc;
1134
1135 rc = check_dev(dev);
1136 if (rc < 0)
1137 return rc;
1138
Hans Verkuilea86968f2013-03-11 16:22:13 -03001139 if (mutex_lock_interruptible(&dev->lock))
1140 return -ERESTARTSYS;
1141 au0828_init_tuner(dev);
1142 mutex_unlock(&dev->lock);
1143
Devin Heitmueller62899a22009-03-15 18:48:52 -03001144 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001145 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1146 return -EBUSY;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001147
1148 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1149 filp->f_flags & O_NONBLOCK);
1150 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001151
1152 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1153 if (!res_get(fh, AU0828_RESOURCE_VBI))
1154 return -EBUSY;
1155
Devin Heitmuellerbf797162010-10-09 15:09:17 -03001156 if (dev->vbi_timeout_running == 0) {
1157 /* Handle case where caller tries to read without
1158 calling streamon first */
1159 dev->vbi_timeout_running = 1;
1160 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1161 }
1162
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001163 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1164 filp->f_flags & O_NONBLOCK);
1165 }
1166
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001167 return 0;
1168}
1169
1170static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1171{
1172 struct au0828_fh *fh = filp->private_data;
1173 struct au0828_dev *dev = fh->dev;
Hans Verkuil83b09422013-02-15 09:14:00 -03001174 unsigned long req_events = poll_requested_events(wait);
1175 unsigned int res;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001176
Hans Verkuil83b09422013-02-15 09:14:00 -03001177 if (check_dev(dev) < 0)
1178 return POLLERR;
1179
1180 res = v4l2_ctrl_poll(filp, wait);
1181 if (!(req_events & (POLLIN | POLLRDNORM)))
1182 return res;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001183
Hans Verkuilea86968f2013-03-11 16:22:13 -03001184 if (mutex_lock_interruptible(&dev->lock))
1185 return -ERESTARTSYS;
1186 au0828_init_tuner(dev);
1187 mutex_unlock(&dev->lock);
1188
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001189 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1190 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1191 return POLLERR;
Hans Verkuil83b09422013-02-15 09:14:00 -03001192 return res | videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1193 }
1194 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001195 if (!res_get(fh, AU0828_RESOURCE_VBI))
1196 return POLLERR;
Hans Verkuil83b09422013-02-15 09:14:00 -03001197 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001198 }
Hans Verkuil83b09422013-02-15 09:14:00 -03001199 return POLLERR;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001200}
1201
1202static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1203{
1204 struct au0828_fh *fh = filp->private_data;
1205 struct au0828_dev *dev = fh->dev;
1206 int rc;
1207
1208 rc = check_dev(dev);
1209 if (rc < 0)
1210 return rc;
1211
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001212 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1213 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1214 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1215 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001216
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001217 return rc;
1218}
1219
1220static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1221 struct v4l2_format *format)
1222{
1223 int ret;
1224 int width = format->fmt.pix.width;
1225 int height = format->fmt.pix.height;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001226
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001227 /* If they are demanding a format other than the one we support,
1228 bail out (tvtime asks for UYVY and then retries with YUYV) */
Devin Heitmueller62899a22009-03-15 18:48:52 -03001229 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001230 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001231
1232 /* format->fmt.pix.width only support 720 and height 480 */
Devin Heitmueller62899a22009-03-15 18:48:52 -03001233 if (width != 720)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001234 width = 720;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001235 if (height != 480)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001236 height = 480;
1237
1238 format->fmt.pix.width = width;
1239 format->fmt.pix.height = height;
1240 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1241 format->fmt.pix.bytesperline = width * 2;
1242 format->fmt.pix.sizeimage = width * height * 2;
1243 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1244 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001245 format->fmt.pix.priv = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001246
Devin Heitmueller62899a22009-03-15 18:48:52 -03001247 if (cmd == VIDIOC_TRY_FMT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001248 return 0;
1249
1250 /* maybe set new image format, driver current only support 720*480 */
1251 dev->width = width;
1252 dev->height = height;
1253 dev->frame_size = width * height * 2;
1254 dev->field_size = width * height;
1255 dev->bytesperline = width * 2;
1256
Devin Heitmueller62899a22009-03-15 18:48:52 -03001257 if (dev->stream_state == STREAM_ON) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001258 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
Devin Heitmueller62899a22009-03-15 18:48:52 -03001259 ret = au0828_stream_interrupt(dev);
1260 if (ret != 0) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001261 dprintk(1, "error interrupting video stream!\n");
1262 return ret;
1263 }
1264 }
1265
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001266 au0828_analog_stream_enable(dev);
1267
1268 return 0;
1269}
1270
1271
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001272static int vidioc_querycap(struct file *file, void *priv,
1273 struct v4l2_capability *cap)
1274{
Hans Verkuil59e05482013-02-15 08:11:04 -03001275 struct video_device *vdev = video_devdata(file);
1276 struct au0828_fh *fh = priv;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001277 struct au0828_dev *dev = fh->dev;
1278
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001279 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001280 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
Hans Verkuil59e05482013-02-15 08:11:04 -03001281 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001282
Hans Verkuil59e05482013-02-15 08:11:04 -03001283 /* set the device capabilities */
1284 cap->device_caps = V4L2_CAP_AUDIO |
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001285 V4L2_CAP_READWRITE |
1286 V4L2_CAP_STREAMING |
1287 V4L2_CAP_TUNER;
Hans Verkuil59e05482013-02-15 08:11:04 -03001288 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1289 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1290 else
1291 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1292 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1293 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001294 return 0;
1295}
1296
1297static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1298 struct v4l2_fmtdesc *f)
1299{
Devin Heitmueller62899a22009-03-15 18:48:52 -03001300 if (f->index)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001301 return -EINVAL;
1302
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001303 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1304 strcpy(f->description, "Packed YUV2");
1305
1306 f->flags = 0;
1307 f->pixelformat = V4L2_PIX_FMT_UYVY;
1308
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001309 return 0;
1310}
1311
1312static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1313 struct v4l2_format *f)
1314{
1315 struct au0828_fh *fh = priv;
1316 struct au0828_dev *dev = fh->dev;
1317
1318 f->fmt.pix.width = dev->width;
1319 f->fmt.pix.height = dev->height;
1320 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1321 f->fmt.pix.bytesperline = dev->bytesperline;
1322 f->fmt.pix.sizeimage = dev->frame_size;
1323 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1324 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001325 f->fmt.pix.priv = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001326 return 0;
1327}
1328
1329static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1330 struct v4l2_format *f)
1331{
1332 struct au0828_fh *fh = priv;
1333 struct au0828_dev *dev = fh->dev;
1334
1335 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1336}
1337
1338static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1339 struct v4l2_format *f)
1340{
1341 struct au0828_fh *fh = priv;
1342 struct au0828_dev *dev = fh->dev;
1343 int rc;
1344
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001345 rc = check_dev(dev);
1346 if (rc < 0)
1347 return rc;
1348
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001349 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -03001350 pr_info("%s queue busy\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001351 rc = -EBUSY;
1352 goto out;
1353 }
1354
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001355 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001356out:
1357 return rc;
1358}
1359
Hans Verkuil314527a2013-03-15 06:10:40 -03001360static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001361{
1362 struct au0828_fh *fh = priv;
1363 struct au0828_dev *dev = fh->dev;
1364
Hans Verkuilea86968f2013-03-11 16:22:13 -03001365 dev->std = norm;
1366
1367 au0828_init_tuner(dev);
1368
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001369 i2c_gate_ctrl(dev, 1);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001370
Mauro Carvalho Chehabf2fd7ce2014-06-08 13:54:56 -03001371 /*
1372 * FIXME: when we support something other than 60Hz standards,
1373 * we are going to have to make the au0828 bridge adjust the size
1374 * of its capture buffer, which is currently hardcoded at 720x480
1375 */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001376
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001377 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001378
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001379 i2c_gate_ctrl(dev, 0);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001380
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001381 return 0;
1382}
1383
Hans Verkuil33b6b892013-02-15 09:22:37 -03001384static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1385{
1386 struct au0828_fh *fh = priv;
1387 struct au0828_dev *dev = fh->dev;
1388
1389 *norm = dev->std;
1390 return 0;
1391}
1392
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001393static int vidioc_enum_input(struct file *file, void *priv,
1394 struct v4l2_input *input)
1395{
1396 struct au0828_fh *fh = priv;
1397 struct au0828_dev *dev = fh->dev;
1398 unsigned int tmp;
1399
1400 static const char *inames[] = {
Devin Heitmueller3d622872009-03-15 17:48:26 -03001401 [AU0828_VMUX_UNDEFINED] = "Undefined",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001402 [AU0828_VMUX_COMPOSITE] = "Composite",
1403 [AU0828_VMUX_SVIDEO] = "S-Video",
1404 [AU0828_VMUX_CABLE] = "Cable TV",
1405 [AU0828_VMUX_TELEVISION] = "Television",
1406 [AU0828_VMUX_DVB] = "DVB",
1407 [AU0828_VMUX_DEBUG] = "tv debug"
1408 };
1409
1410 tmp = input->index;
1411
Dan Carpenterf5e20c32010-03-28 08:21:18 -03001412 if (tmp >= AU0828_MAX_INPUT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001413 return -EINVAL;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001414 if (AUVI_INPUT(tmp).type == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001415 return -EINVAL;
1416
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001417 input->index = tmp;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001418 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001419 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001420 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001421 input->type |= V4L2_INPUT_TYPE_TUNER;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001422 input->audioset = 1;
1423 } else {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001424 input->type |= V4L2_INPUT_TYPE_CAMERA;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001425 input->audioset = 2;
1426 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001427
1428 input->std = dev->vdev->tvnorms;
1429
1430 return 0;
1431}
1432
1433static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1434{
1435 struct au0828_fh *fh = priv;
1436 struct au0828_dev *dev = fh->dev;
1437 *i = dev->ctrl_input;
1438 return 0;
1439}
1440
Hans Verkuil56230d12013-03-11 16:18:31 -03001441static void au0828_s_input(struct au0828_dev *dev, int index)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001442{
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001443 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001444
Devin Heitmueller62899a22009-03-15 18:48:52 -03001445 switch (AUVI_INPUT(index).type) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001446 case AU0828_VMUX_SVIDEO:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001447 dev->input_type = AU0828_VMUX_SVIDEO;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001448 dev->ctrl_ainput = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001449 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001450 case AU0828_VMUX_COMPOSITE:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001451 dev->input_type = AU0828_VMUX_COMPOSITE;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001452 dev->ctrl_ainput = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001453 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001454 case AU0828_VMUX_TELEVISION:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001455 dev->input_type = AU0828_VMUX_TELEVISION;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001456 dev->ctrl_ainput = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001457 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001458 default:
Hans Verkuil56230d12013-03-11 16:18:31 -03001459 dprintk(1, "unknown input type set [%d]\n",
Devin Heitmuellera1094c42009-03-15 17:43:13 -03001460 AUVI_INPUT(index).type);
1461 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001462 }
1463
Hans Verkuil5325b422009-04-02 11:26:22 -03001464 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1465 AUVI_INPUT(index).vmux, 0, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001466
1467 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1468 int enable = 0;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001469 if (AUVI_INPUT(i).audio_setup == NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001470 continue;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001471
1472 if (i == index)
1473 enable = 1;
1474 else
1475 enable = 0;
1476 if (enable) {
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001477 (AUVI_INPUT(i).audio_setup)(dev, enable);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001478 } else {
1479 /* Make sure we leave it turned on if some
1480 other input is routed to this callback */
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001481 if ((AUVI_INPUT(i).audio_setup) !=
1482 ((AUVI_INPUT(index).audio_setup))) {
1483 (AUVI_INPUT(i).audio_setup)(dev, enable);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001484 }
1485 }
1486 }
1487
Hans Verkuil5325b422009-04-02 11:26:22 -03001488 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1489 AUVI_INPUT(index).amux, 0, 0);
Hans Verkuil56230d12013-03-11 16:18:31 -03001490}
1491
1492static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1493{
1494 struct au0828_fh *fh = priv;
1495 struct au0828_dev *dev = fh->dev;
1496
1497 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1498 index);
1499 if (index >= AU0828_MAX_INPUT)
1500 return -EINVAL;
1501 if (AUVI_INPUT(index).type == 0)
1502 return -EINVAL;
1503 dev->ctrl_input = index;
1504 au0828_s_input(dev, index);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001505 return 0;
1506}
1507
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001508static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001509{
Devin Heitmueller62899a22009-03-15 18:48:52 -03001510 if (a->index > 1)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001511 return -EINVAL;
1512
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001513 if (a->index == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001514 strcpy(a->name, "Television");
1515 else
1516 strcpy(a->name, "Line in");
1517
1518 a->capability = V4L2_AUDCAP_STEREO;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001519 return 0;
1520}
1521
1522static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1523{
1524 struct au0828_fh *fh = priv;
1525 struct au0828_dev *dev = fh->dev;
1526
1527 a->index = dev->ctrl_ainput;
1528 if (a->index == 0)
1529 strcpy(a->name, "Television");
1530 else
1531 strcpy(a->name, "Line in");
1532
1533 a->capability = V4L2_AUDCAP_STEREO;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001534 return 0;
1535}
1536
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001537static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001538{
1539 struct au0828_fh *fh = priv;
1540 struct au0828_dev *dev = fh->dev;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001541
Devin Heitmueller62899a22009-03-15 18:48:52 -03001542 if (a->index != dev->ctrl_ainput)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001543 return -EINVAL;
1544 return 0;
1545}
1546
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001547static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1548{
1549 struct au0828_fh *fh = priv;
1550 struct au0828_dev *dev = fh->dev;
1551
Devin Heitmueller62899a22009-03-15 18:48:52 -03001552 if (t->index != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001553 return -EINVAL;
1554
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001555 strcpy(t->name, "Auvitek tuner");
Hans Verkuilea86968f2013-03-11 16:22:13 -03001556
1557 au0828_init_tuner(dev);
1558 i2c_gate_ctrl(dev, 1);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001559 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Hans Verkuilea86968f2013-03-11 16:22:13 -03001560 i2c_gate_ctrl(dev, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001561 return 0;
1562}
1563
1564static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001565 const struct v4l2_tuner *t)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001566{
1567 struct au0828_fh *fh = priv;
1568 struct au0828_dev *dev = fh->dev;
1569
Devin Heitmueller62899a22009-03-15 18:48:52 -03001570 if (t->index != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001571 return -EINVAL;
1572
Hans Verkuilea86968f2013-03-11 16:22:13 -03001573 au0828_init_tuner(dev);
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001574 i2c_gate_ctrl(dev, 1);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001575 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001576 i2c_gate_ctrl(dev, 0);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001577
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001578 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1579 t->afc);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001580
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001581 return 0;
1582
1583}
1584
1585static int vidioc_g_frequency(struct file *file, void *priv,
1586 struct v4l2_frequency *freq)
1587{
1588 struct au0828_fh *fh = priv;
1589 struct au0828_dev *dev = fh->dev;
Devin Heitmuellerc8889232009-03-15 17:38:47 -03001590
Hans Verkuile1cf6582013-03-22 13:32:20 -03001591 if (freq->tuner != 0)
1592 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001593 freq->frequency = dev->ctrl_freq;
1594 return 0;
1595}
1596
1597static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001598 const struct v4l2_frequency *freq)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001599{
1600 struct au0828_fh *fh = priv;
1601 struct au0828_dev *dev = fh->dev;
Hans Verkuile1cf6582013-03-22 13:32:20 -03001602 struct v4l2_frequency new_freq = *freq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001603
Devin Heitmueller62899a22009-03-15 18:48:52 -03001604 if (freq->tuner != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001605 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001606
Hans Verkuilea86968f2013-03-11 16:22:13 -03001607 au0828_init_tuner(dev);
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001608 i2c_gate_ctrl(dev, 1);
Devin Heitmueller4a03daf2012-08-06 22:47:02 -03001609
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001610 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
Hans Verkuile1cf6582013-03-22 13:32:20 -03001611 /* Get the actual set (and possibly clamped) frequency */
1612 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1613 dev->ctrl_freq = new_freq.frequency;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001614
Hans Verkuilfa09cb92013-03-11 16:10:33 -03001615 i2c_gate_ctrl(dev, 0);
Devin Heitmueller4a03daf2012-08-06 22:47:02 -03001616
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001617 au0828_analog_stream_reset(dev);
1618
1619 return 0;
1620}
1621
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001622
1623/* RAW VBI ioctls */
1624
1625static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1626 struct v4l2_format *format)
1627{
1628 struct au0828_fh *fh = priv;
1629 struct au0828_dev *dev = fh->dev;
1630
1631 format->fmt.vbi.samples_per_line = dev->vbi_width;
1632 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1633 format->fmt.vbi.offset = 0;
1634 format->fmt.vbi.flags = 0;
1635 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1636
1637 format->fmt.vbi.count[0] = dev->vbi_height;
1638 format->fmt.vbi.count[1] = dev->vbi_height;
1639 format->fmt.vbi.start[0] = 21;
1640 format->fmt.vbi.start[1] = 284;
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001641 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001642
1643 return 0;
1644}
1645
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001646static int vidioc_cropcap(struct file *file, void *priv,
1647 struct v4l2_cropcap *cc)
1648{
1649 struct au0828_fh *fh = priv;
1650 struct au0828_dev *dev = fh->dev;
1651
Devin Heitmueller62899a22009-03-15 18:48:52 -03001652 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001653 return -EINVAL;
1654
1655 cc->bounds.left = 0;
1656 cc->bounds.top = 0;
1657 cc->bounds.width = dev->width;
1658 cc->bounds.height = dev->height;
1659
1660 cc->defrect = cc->bounds;
1661
1662 cc->pixelaspect.numerator = 54;
1663 cc->pixelaspect.denominator = 59;
1664
1665 return 0;
1666}
1667
1668static int vidioc_streamon(struct file *file, void *priv,
1669 enum v4l2_buf_type type)
1670{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001671 struct au0828_fh *fh = priv;
1672 struct au0828_dev *dev = fh->dev;
1673 int rc = -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001674
1675 rc = check_dev(dev);
1676 if (rc < 0)
1677 return rc;
1678
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001679 if (unlikely(type != fh->type))
1680 return -EINVAL;
1681
1682 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1683 fh, type, fh->resources, dev->resources);
1684
1685 if (unlikely(!res_get(fh, get_ressource(fh))))
1686 return -EBUSY;
1687
Hans Verkuilea86968f2013-03-11 16:22:13 -03001688 au0828_init_tuner(dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001689 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1690 au0828_analog_stream_enable(dev);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001691 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001692 }
1693
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001694 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001695 rc = videobuf_streamon(&fh->vb_vidq);
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001696 dev->vid_timeout_running = 1;
1697 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1698 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001699 rc = videobuf_streamon(&fh->vb_vbiq);
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001700 dev->vbi_timeout_running = 1;
1701 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1702 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001703
1704 return rc;
1705}
1706
1707static int vidioc_streamoff(struct file *file, void *priv,
1708 enum v4l2_buf_type type)
1709{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001710 struct au0828_fh *fh = priv;
1711 struct au0828_dev *dev = fh->dev;
1712 int rc;
1713 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001714
1715 rc = check_dev(dev);
1716 if (rc < 0)
1717 return rc;
1718
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001719 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1720 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001721 return -EINVAL;
1722 if (type != fh->type)
1723 return -EINVAL;
1724
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001725 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1726 fh, type, fh->resources, dev->resources);
1727
1728 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001729 dev->vid_timeout_running = 0;
1730 del_timer_sync(&dev->vid_timeout);
1731
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -03001732 au0828_analog_stream_disable(dev);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001733 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001734 rc = au0828_stream_interrupt(dev);
1735 if (rc != 0)
1736 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001737
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001738 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1739 if (AUVI_INPUT(i).audio_setup == NULL)
1740 continue;
1741 (AUVI_INPUT(i).audio_setup)(dev, 0);
1742 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001743
Devin Heitmuellera595c1c2012-08-06 22:47:03 -03001744 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1745 videobuf_streamoff(&fh->vb_vidq);
1746 res_free(fh, AU0828_RESOURCE_VIDEO);
1747 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001748 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001749 dev->vbi_timeout_running = 0;
1750 del_timer_sync(&dev->vbi_timeout);
1751
Devin Heitmuellera595c1c2012-08-06 22:47:03 -03001752 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1753 videobuf_streamoff(&fh->vb_vbiq);
1754 res_free(fh, AU0828_RESOURCE_VBI);
1755 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001756 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001757
1758 return 0;
1759}
1760
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001761#ifdef CONFIG_VIDEO_ADV_DEBUG
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001762static int vidioc_g_register(struct file *file, void *priv,
1763 struct v4l2_dbg_register *reg)
1764{
1765 struct au0828_fh *fh = priv;
1766 struct au0828_dev *dev = fh->dev;
1767
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001768 reg->val = au0828_read(dev, reg->reg);
Hans Verkuilead5bc42013-05-29 07:00:03 -03001769 reg->size = 1;
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001770 return 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001771}
1772
1773static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001774 const struct v4l2_dbg_register *reg)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001775{
1776 struct au0828_fh *fh = priv;
1777 struct au0828_dev *dev = fh->dev;
1778
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001779 return au0828_writereg(dev, reg->reg, reg->val);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001780}
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001781#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001782
Hans Verkuil83b09422013-02-15 09:14:00 -03001783static int vidioc_log_status(struct file *file, void *fh)
1784{
1785 struct video_device *vdev = video_devdata(file);
1786
1787 v4l2_ctrl_log_status(file, fh);
1788 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1789 return 0;
1790}
1791
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001792static int vidioc_reqbufs(struct file *file, void *priv,
1793 struct v4l2_requestbuffers *rb)
1794{
1795 struct au0828_fh *fh = priv;
1796 struct au0828_dev *dev = fh->dev;
1797 int rc;
1798
1799 rc = check_dev(dev);
1800 if (rc < 0)
1801 return rc;
1802
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001803 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1804 rc = videobuf_reqbufs(&fh->vb_vidq, rb);
1805 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1806 rc = videobuf_reqbufs(&fh->vb_vbiq, rb);
1807
1808 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001809}
1810
1811static int vidioc_querybuf(struct file *file, void *priv,
1812 struct v4l2_buffer *b)
1813{
1814 struct au0828_fh *fh = priv;
1815 struct au0828_dev *dev = fh->dev;
1816 int rc;
1817
1818 rc = check_dev(dev);
1819 if (rc < 0)
1820 return rc;
1821
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001822 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1823 rc = videobuf_querybuf(&fh->vb_vidq, b);
1824 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1825 rc = videobuf_querybuf(&fh->vb_vbiq, b);
1826
1827 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001828}
1829
1830static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1831{
1832 struct au0828_fh *fh = priv;
1833 struct au0828_dev *dev = fh->dev;
1834 int rc;
1835
1836 rc = check_dev(dev);
1837 if (rc < 0)
1838 return rc;
1839
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001840 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1841 rc = videobuf_qbuf(&fh->vb_vidq, b);
1842 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1843 rc = videobuf_qbuf(&fh->vb_vbiq, b);
1844
1845 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001846}
1847
1848static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1849{
1850 struct au0828_fh *fh = priv;
1851 struct au0828_dev *dev = fh->dev;
1852 int rc;
1853
1854 rc = check_dev(dev);
1855 if (rc < 0)
1856 return rc;
1857
1858 /* Workaround for a bug in the au0828 hardware design that sometimes
1859 results in the colorspace being inverted */
1860 if (dev->greenscreen_detected == 1) {
1861 dprintk(1, "Detected green frame. Resetting stream...\n");
1862 au0828_analog_stream_reset(dev);
1863 dev->greenscreen_detected = 0;
1864 }
1865
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001866 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1867 rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1868 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1869 rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK);
1870
1871 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001872}
1873
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001874void au0828_v4l2_suspend(struct au0828_dev *dev)
1875{
1876 struct urb *urb;
1877 int i;
1878
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -03001879 pr_info("stopping V4L2\n");
1880
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001881 if (dev->stream_state == STREAM_ON) {
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -03001882 pr_info("stopping V4L2 active URBs\n");
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001883 au0828_analog_stream_disable(dev);
1884 /* stop urbs */
1885 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1886 urb = dev->isoc_ctl.urb[i];
1887 if (urb) {
1888 if (!irqs_disabled())
1889 usb_kill_urb(urb);
1890 else
1891 usb_unlink_urb(urb);
1892 }
1893 }
1894 }
1895
1896 if (dev->vid_timeout_running)
1897 del_timer_sync(&dev->vid_timeout);
1898 if (dev->vbi_timeout_running)
1899 del_timer_sync(&dev->vbi_timeout);
1900}
1901
1902void au0828_v4l2_resume(struct au0828_dev *dev)
1903{
1904 int i, rc;
1905
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -03001906 pr_info("restarting V4L2\n");
1907
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -03001908 if (dev->stream_state == STREAM_ON) {
1909 au0828_stream_interrupt(dev);
1910 au0828_init_tuner(dev);
1911 }
1912
1913 if (dev->vid_timeout_running)
1914 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1915 if (dev->vbi_timeout_running)
1916 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1917
1918 /* If we were doing ac97 instead of i2s, it would go here...*/
1919 au0828_i2s_init(dev);
1920
1921 au0828_analog_stream_enable(dev);
1922
1923 if (!(dev->stream_state == STREAM_ON)) {
1924 au0828_analog_stream_reset(dev);
1925 /* submit urbs */
1926 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1927 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1928 if (rc) {
1929 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1930 i, rc);
1931 au0828_uninit_isoc(dev);
1932 }
1933 }
1934 }
1935}
1936
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001937static struct v4l2_file_operations au0828_v4l_fops = {
1938 .owner = THIS_MODULE,
1939 .open = au0828_v4l2_open,
1940 .release = au0828_v4l2_close,
1941 .read = au0828_v4l2_read,
1942 .poll = au0828_v4l2_poll,
1943 .mmap = au0828_v4l2_mmap,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001944 .unlocked_ioctl = video_ioctl2,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001945};
1946
1947static const struct v4l2_ioctl_ops video_ioctl_ops = {
1948 .vidioc_querycap = vidioc_querycap,
1949 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1950 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1951 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1952 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller5a5a4e12009-03-11 03:00:55 -03001953 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil8d86e4e2013-03-11 17:48:34 -03001954 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001955 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001956 .vidioc_enumaudio = vidioc_enumaudio,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001957 .vidioc_g_audio = vidioc_g_audio,
1958 .vidioc_s_audio = vidioc_s_audio,
1959 .vidioc_cropcap = vidioc_cropcap,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001960 .vidioc_reqbufs = vidioc_reqbufs,
1961 .vidioc_querybuf = vidioc_querybuf,
1962 .vidioc_qbuf = vidioc_qbuf,
1963 .vidioc_dqbuf = vidioc_dqbuf,
1964 .vidioc_s_std = vidioc_s_std,
Hans Verkuil33b6b892013-02-15 09:22:37 -03001965 .vidioc_g_std = vidioc_g_std,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001966 .vidioc_enum_input = vidioc_enum_input,
1967 .vidioc_g_input = vidioc_g_input,
1968 .vidioc_s_input = vidioc_s_input,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001969 .vidioc_streamon = vidioc_streamon,
1970 .vidioc_streamoff = vidioc_streamoff,
1971 .vidioc_g_tuner = vidioc_g_tuner,
1972 .vidioc_s_tuner = vidioc_s_tuner,
1973 .vidioc_g_frequency = vidioc_g_frequency,
1974 .vidioc_s_frequency = vidioc_s_frequency,
1975#ifdef CONFIG_VIDEO_ADV_DEBUG
1976 .vidioc_g_register = vidioc_g_register,
1977 .vidioc_s_register = vidioc_s_register,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001978#endif
Hans Verkuil83b09422013-02-15 09:14:00 -03001979 .vidioc_log_status = vidioc_log_status,
1980 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1981 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001982};
1983
1984static const struct video_device au0828_video_template = {
1985 .fops = &au0828_v4l_fops,
1986 .release = video_device_release,
1987 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehabf2fd7ce2014-06-08 13:54:56 -03001988 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001989};
1990
1991/**************************************************************************/
1992
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001993int au0828_analog_register(struct au0828_dev *dev,
1994 struct usb_interface *interface)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001995{
1996 int retval = -ENOMEM;
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001997 struct usb_host_interface *iface_desc;
1998 struct usb_endpoint_descriptor *endpoint;
Julia Lawalld63b21b2012-04-22 07:54:42 -03001999 int i, ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002000
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -03002001 dprintk(1, "au0828_analog_register called for intf#%d!\n",
2002 interface->cur_altsetting->desc.bInterfaceNumber);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002003
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002004 /* set au0828 usb interface0 to as5 */
2005 retval = usb_set_interface(dev->usbdev,
Devin Heitmueller62899a22009-03-15 18:48:52 -03002006 interface->cur_altsetting->desc.bInterfaceNumber, 5);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002007 if (retval != 0) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -03002008 pr_info("Failure setting usb interface0 to as5\n");
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002009 return retval;
2010 }
2011
2012 /* Figure out which endpoint has the isoc interface */
2013 iface_desc = interface->cur_altsetting;
Devin Heitmueller62899a22009-03-15 18:48:52 -03002014 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002015 endpoint = &iface_desc->endpoint[i].desc;
Devin Heitmueller62899a22009-03-15 18:48:52 -03002016 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2017 == USB_DIR_IN) &&
2018 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2019 == USB_ENDPOINT_XFER_ISOC)) {
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002020
2021 /* we find our isoc in endpoint */
2022 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
Devin Heitmueller62899a22009-03-15 18:48:52 -03002023 dev->max_pkt_size = (tmp & 0x07ff) *
2024 (((tmp & 0x1800) >> 11) + 1);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002025 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
Mauro Carvalho Chehab1fe3a8f2014-06-08 13:54:58 -03002026 dprintk(1,
2027 "Found isoc endpoint 0x%02x, max size = %d\n",
2028 dev->isoc_in_endpointaddr, dev->max_pkt_size);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002029 }
2030 }
Devin Heitmueller62899a22009-03-15 18:48:52 -03002031 if (!(dev->isoc_in_endpointaddr)) {
Mauro Carvalho Chehab83afb32a2014-08-09 21:47:17 -03002032 pr_info("Could not locate isoc endpoint\n");
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03002033 kfree(dev);
2034 return -ENODEV;
2035 }
2036
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002037 init_waitqueue_head(&dev->open);
2038 spin_lock_init(&dev->slock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002039
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03002040 /* init video dma queues */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002041 INIT_LIST_HEAD(&dev->vidq.active);
2042 INIT_LIST_HEAD(&dev->vidq.queued);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03002043 INIT_LIST_HEAD(&dev->vbiq.active);
2044 INIT_LIST_HEAD(&dev->vbiq.queued);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002045
Devin Heitmueller78ca5002010-10-09 14:43:53 -03002046 dev->vid_timeout.function = au0828_vid_buffer_timeout;
2047 dev->vid_timeout.data = (unsigned long) dev;
2048 init_timer(&dev->vid_timeout);
2049
2050 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
2051 dev->vbi_timeout.data = (unsigned long) dev;
2052 init_timer(&dev->vbi_timeout);
2053
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002054 dev->width = NTSC_STD_W;
2055 dev->height = NTSC_STD_H;
2056 dev->field_size = dev->width * dev->height;
2057 dev->frame_size = dev->field_size << 1;
2058 dev->bytesperline = dev->width << 1;
Hans Verkuilde8d2bb2013-03-11 16:12:17 -03002059 dev->vbi_width = 720;
2060 dev->vbi_height = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002061 dev->ctrl_ainput = 0;
Hans Verkuile1cf6582013-03-22 13:32:20 -03002062 dev->ctrl_freq = 960;
Hans Verkuil33b6b892013-02-15 09:22:37 -03002063 dev->std = V4L2_STD_NTSC_M;
Hans Verkuil56230d12013-03-11 16:18:31 -03002064 au0828_s_input(dev, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002065
2066 /* allocate and fill v4l2 video struct */
2067 dev->vdev = video_device_alloc();
Devin Heitmueller62899a22009-03-15 18:48:52 -03002068 if (NULL == dev->vdev) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002069 dprintk(1, "Can't allocate video_device.\n");
2070 return -ENOMEM;
2071 }
2072
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03002073 /* allocate the VBI struct */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002074 dev->vbi_dev = video_device_alloc();
Devin Heitmueller62899a22009-03-15 18:48:52 -03002075 if (NULL == dev->vbi_dev) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002076 dprintk(1, "Can't allocate vbi_device.\n");
Julia Lawalld63b21b2012-04-22 07:54:42 -03002077 ret = -ENOMEM;
2078 goto err_vdev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002079 }
2080
2081 /* Fill the video capture device struct */
2082 *dev->vdev = au0828_video_template;
Hans Verkuile8c26f42013-02-15 08:55:41 -03002083 dev->vdev->v4l2_dev = &dev->v4l2_dev;
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03002084 dev->vdev->lock = &dev->lock;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002085 strcpy(dev->vdev->name, "au0828a video");
2086
2087 /* Setup the VBI device */
2088 *dev->vbi_dev = au0828_video_template;
Hans Verkuile8c26f42013-02-15 08:55:41 -03002089 dev->vbi_dev->v4l2_dev = &dev->v4l2_dev;
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03002090 dev->vbi_dev->lock = &dev->lock;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002091 strcpy(dev->vbi_dev->name, "au0828a vbi");
2092
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002093 /* Register the v4l2 device */
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002094 video_set_drvdata(dev->vdev, dev);
Devin Heitmueller62899a22009-03-15 18:48:52 -03002095 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
2096 if (retval != 0) {
2097 dprintk(1, "unable to register video device (error = %d).\n",
2098 retval);
Julia Lawalld63b21b2012-04-22 07:54:42 -03002099 ret = -ENODEV;
2100 goto err_vbi_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002101 }
2102
2103 /* Register the vbi device */
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002104 video_set_drvdata(dev->vbi_dev, dev);
Devin Heitmueller62899a22009-03-15 18:48:52 -03002105 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
2106 if (retval != 0) {
2107 dprintk(1, "unable to register vbi device (error = %d).\n",
2108 retval);
Julia Lawalld63b21b2012-04-22 07:54:42 -03002109 ret = -ENODEV;
2110 goto err_vbi_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002111 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002112
Devin Heitmueller62899a22009-03-15 18:48:52 -03002113 dprintk(1, "%s completed!\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002114
2115 return 0;
Julia Lawalld63b21b2012-04-22 07:54:42 -03002116
2117err_vbi_dev:
2118 video_device_release(dev->vbi_dev);
2119err_vdev:
2120 video_device_release(dev->vdev);
2121 return ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002122}
2123