blob: afb600f9daa6f05af5b5fb0094182e560e2394d4 [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
31#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030033#include <linux/init.h>
34#include <linux/device.h>
35#include <linux/suspend.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030036#include <media/v4l2-common.h>
37#include <media/v4l2-ioctl.h>
38#include <media/v4l2-chip-ident.h>
39#include <media/tuner.h>
40#include "au0828.h"
41#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) { \
56 printk(KERN_INFO "au0828 %s :"fmt, \
57 __func__ , ##arg); \
58 } \
59 } while (0)
60
61static inline void print_err_status(struct au0828_dev *dev,
62 int packet, int status)
63{
64 char *errmsg = "Unknown";
65
66 switch (status) {
67 case -ENOENT:
68 errmsg = "unlinked synchronuously";
69 break;
70 case -ECONNRESET:
71 errmsg = "unlinked asynchronuously";
72 break;
73 case -ENOSR:
74 errmsg = "Buffer error (overrun)";
75 break;
76 case -EPIPE:
77 errmsg = "Stalled (device not responding)";
78 break;
79 case -EOVERFLOW:
80 errmsg = "Babble (bad cable?)";
81 break;
82 case -EPROTO:
83 errmsg = "Bit-stuff error (bad cable?)";
84 break;
85 case -EILSEQ:
86 errmsg = "CRC/Timeout (could be anything)";
87 break;
88 case -ETIME:
89 errmsg = "Device does not respond";
90 break;
91 }
92 if (packet < 0) {
93 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
94 } else {
95 au0828_isocdbg("URB packet %d, status %d [%s].\n",
96 packet, status, errmsg);
97 }
98}
99
100static int check_dev(struct au0828_dev *dev)
101{
102 if (dev->dev_state & DEV_DISCONNECTED) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300103 printk(KERN_INFO "v4l2 ioctl: device not present\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300104 return -ENODEV;
105 }
106
107 if (dev->dev_state & DEV_MISCONFIGURED) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300108 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300109 "close and open it again\n");
110 return -EIO;
111 }
112 return 0;
113}
114
115/*
116 * IRQ callback, called by URB callback
117 */
118static void au0828_irq_callback(struct urb *urb)
119{
120 struct au0828_dmaqueue *dma_q = urb->context;
121 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300122 unsigned long flags = 0;
Hans Verkuile92ba282012-05-14 10:17:35 -0300123 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300124
125 switch (urb->status) {
126 case 0: /* success */
127 case -ETIMEDOUT: /* NAK */
128 break;
129 case -ECONNRESET: /* kill */
130 case -ENOENT:
131 case -ESHUTDOWN:
132 au0828_isocdbg("au0828_irq_callback called: status kill\n");
133 return;
134 default: /* unknown error */
135 au0828_isocdbg("urb completition error %d.\n", urb->status);
136 break;
137 }
138
139 /* Copy data from URB */
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300140 spin_lock_irqsave(&dev->slock, flags);
Hans Verkuile92ba282012-05-14 10:17:35 -0300141 dev->isoc_ctl.isoc_copy(dev, urb);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300142 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300143
144 /* Reset urb buffers */
145 for (i = 0; i < urb->number_of_packets; i++) {
146 urb->iso_frame_desc[i].status = 0;
147 urb->iso_frame_desc[i].actual_length = 0;
148 }
149 urb->status = 0;
150
151 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
152 if (urb->status) {
153 au0828_isocdbg("urb resubmit failed (error=%i)\n",
154 urb->status);
155 }
156}
157
158/*
159 * Stop and Deallocate URBs
160 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300161static void au0828_uninit_isoc(struct au0828_dev *dev)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300162{
163 struct urb *urb;
164 int i;
165
166 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
167
168 dev->isoc_ctl.nfields = -1;
169 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
170 urb = dev->isoc_ctl.urb[i];
171 if (urb) {
172 if (!irqs_disabled())
173 usb_kill_urb(urb);
174 else
175 usb_unlink_urb(urb);
176
177 if (dev->isoc_ctl.transfer_buffer[i]) {
Daniel Mack997ea582010-04-12 13:17:25 +0200178 usb_free_coherent(dev->usbdev,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300179 urb->transfer_buffer_length,
180 dev->isoc_ctl.transfer_buffer[i],
181 urb->transfer_dma);
182 }
183 usb_free_urb(urb);
184 dev->isoc_ctl.urb[i] = NULL;
185 }
186 dev->isoc_ctl.transfer_buffer[i] = NULL;
187 }
188
189 kfree(dev->isoc_ctl.urb);
190 kfree(dev->isoc_ctl.transfer_buffer);
191
192 dev->isoc_ctl.urb = NULL;
193 dev->isoc_ctl.transfer_buffer = NULL;
194 dev->isoc_ctl.num_bufs = 0;
195}
196
197/*
198 * Allocate URBs and start IRQ
199 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300200static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
201 int num_bufs, int max_pkt_size,
202 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300203{
204 struct au0828_dmaqueue *dma_q = &dev->vidq;
205 int i;
206 int sb_size, pipe;
207 struct urb *urb;
208 int j, k;
209 int rc;
210
211 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
212
213 /* De-allocates all pending stuff */
214 au0828_uninit_isoc(dev);
215
216 dev->isoc_ctl.isoc_copy = isoc_copy;
217 dev->isoc_ctl.num_bufs = num_bufs;
218
219 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
220 if (!dev->isoc_ctl.urb) {
221 au0828_isocdbg("cannot alloc memory for usb buffers\n");
222 return -ENOMEM;
223 }
224
225 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
226 GFP_KERNEL);
227 if (!dev->isoc_ctl.transfer_buffer) {
228 au0828_isocdbg("cannot allocate memory for usb transfer\n");
229 kfree(dev->isoc_ctl.urb);
230 return -ENOMEM;
231 }
232
233 dev->isoc_ctl.max_pkt_size = max_pkt_size;
234 dev->isoc_ctl.buf = NULL;
235
236 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
237
238 /* allocate urbs and transfer buffers */
239 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
240 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
241 if (!urb) {
242 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
243 au0828_uninit_isoc(dev);
244 return -ENOMEM;
245 }
246 dev->isoc_ctl.urb[i] = urb;
247
Daniel Mack997ea582010-04-12 13:17:25 +0200248 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300249 sb_size, GFP_KERNEL, &urb->transfer_dma);
250 if (!dev->isoc_ctl.transfer_buffer[i]) {
251 printk("unable to allocate %i bytes for transfer"
252 " buffer %i%s\n",
253 sb_size, i,
254 in_interrupt() ? " while in int" : "");
255 au0828_uninit_isoc(dev);
256 return -ENOMEM;
257 }
258 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
259
260 pipe = usb_rcvisocpipe(dev->usbdev,
261 dev->isoc_in_endpointaddr),
262
263 usb_fill_int_urb(urb, dev->usbdev, pipe,
264 dev->isoc_ctl.transfer_buffer[i], sb_size,
265 au0828_irq_callback, dma_q, 1);
266
267 urb->number_of_packets = max_packets;
Devin Heitmuellerfadadb72009-03-22 23:42:26 -0300268 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300269
270 k = 0;
271 for (j = 0; j < max_packets; j++) {
272 urb->iso_frame_desc[j].offset = k;
273 urb->iso_frame_desc[j].length =
274 dev->isoc_ctl.max_pkt_size;
275 k += dev->isoc_ctl.max_pkt_size;
276 }
277 }
278
279 init_waitqueue_head(&dma_q->wq);
280
281 /* submit urbs and enables IRQ */
282 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
283 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
284 if (rc) {
285 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
286 i, rc);
287 au0828_uninit_isoc(dev);
288 return rc;
289 }
290 }
291
292 return 0;
293}
294
295/*
296 * Announces that a buffer were filled and request the next
297 */
298static inline void buffer_filled(struct au0828_dev *dev,
299 struct au0828_dmaqueue *dma_q,
300 struct au0828_buffer *buf)
301{
302 /* Advice that buffer was filled */
303 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
304
305 buf->vb.state = VIDEOBUF_DONE;
306 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300307 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300308
309 dev->isoc_ctl.buf = NULL;
310
311 list_del(&buf->vb.queue);
312 wake_up(&buf->vb.done);
313}
314
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300315static inline void vbi_buffer_filled(struct au0828_dev *dev,
316 struct au0828_dmaqueue *dma_q,
317 struct au0828_buffer *buf)
318{
319 /* Advice that buffer was filled */
320 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
321
322 buf->vb.state = VIDEOBUF_DONE;
323 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300324 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300325
326 dev->isoc_ctl.vbi_buf = NULL;
327
328 list_del(&buf->vb.queue);
329 wake_up(&buf->vb.done);
330}
331
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300332/*
333 * Identify the buffer header type and properly handles
334 */
335static void au0828_copy_video(struct au0828_dev *dev,
336 struct au0828_dmaqueue *dma_q,
337 struct au0828_buffer *buf,
338 unsigned char *p,
339 unsigned char *outp, unsigned long len)
340{
341 void *fieldstart, *startwrite, *startread;
342 int linesdone, currlinedone, offset, lencopy, remain;
343 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
344
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300345 if (len == 0)
346 return;
347
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300348 if (dma_q->pos + len > buf->vb.size)
349 len = buf->vb.size - dma_q->pos;
350
351 startread = p;
352 remain = len;
353
354 /* Interlaces frame */
355 if (buf->top_field)
356 fieldstart = outp;
357 else
358 fieldstart = outp + bytesperline;
359
360 linesdone = dma_q->pos / bytesperline;
361 currlinedone = dma_q->pos % bytesperline;
362 offset = linesdone * bytesperline * 2 + currlinedone;
363 startwrite = fieldstart + offset;
364 lencopy = bytesperline - currlinedone;
365 lencopy = lencopy > remain ? remain : lencopy;
366
367 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
368 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
369 ((char *)startwrite + lencopy) -
370 ((char *)outp + buf->vb.size));
371 remain = (char *)outp + buf->vb.size - (char *)startwrite;
372 lencopy = remain;
373 }
374 if (lencopy <= 0)
375 return;
376 memcpy(startwrite, startread, lencopy);
377
378 remain -= lencopy;
379
380 while (remain > 0) {
381 startwrite += lencopy + bytesperline;
382 startread += lencopy;
383 if (bytesperline > remain)
384 lencopy = remain;
385 else
386 lencopy = bytesperline;
387
388 if ((char *)startwrite + lencopy > (char *)outp +
389 buf->vb.size) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300390 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300391 ((char *)startwrite + lencopy) -
392 ((char *)outp + buf->vb.size));
393 lencopy = remain = (char *)outp + buf->vb.size -
394 (char *)startwrite;
395 }
396 if (lencopy <= 0)
397 break;
398
399 memcpy(startwrite, startread, lencopy);
400
401 remain -= lencopy;
402 }
403
404 if (offset > 1440) {
405 /* We have enough data to check for greenscreen */
Devin Heitmueller62899a22009-03-15 18:48:52 -0300406 if (outp[0] < 0x60 && outp[1440] < 0x60)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300407 dev->greenscreen_detected = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300408 }
409
410 dma_q->pos += len;
411}
412
413/*
414 * video-buf generic routine to get the next available buffer
415 */
416static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
417 struct au0828_buffer **buf)
418{
419 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
420
421 if (list_empty(&dma_q->active)) {
422 au0828_isocdbg("No active queue to serve\n");
423 dev->isoc_ctl.buf = NULL;
424 *buf = NULL;
425 return;
426 }
427
428 /* Get the next buffer */
429 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
430 dev->isoc_ctl.buf = *buf;
431
432 return;
433}
434
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300435static void au0828_copy_vbi(struct au0828_dev *dev,
436 struct au0828_dmaqueue *dma_q,
437 struct au0828_buffer *buf,
438 unsigned char *p,
439 unsigned char *outp, unsigned long len)
440{
441 unsigned char *startwrite, *startread;
Dan Carpenterb5f59332010-07-23 07:09:20 -0300442 int bytesperline;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300443 int i, j = 0;
444
445 if (dev == NULL) {
446 au0828_isocdbg("dev is null\n");
447 return;
448 }
449
450 if (dma_q == NULL) {
451 au0828_isocdbg("dma_q is null\n");
452 return;
453 }
454 if (buf == NULL)
455 return;
456 if (p == NULL) {
457 au0828_isocdbg("p is null\n");
458 return;
459 }
460 if (outp == NULL) {
461 au0828_isocdbg("outp is null\n");
462 return;
463 }
464
Dan Carpenterb5f59332010-07-23 07:09:20 -0300465 bytesperline = dev->vbi_width;
466
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300467 if (dma_q->pos + len > buf->vb.size)
468 len = buf->vb.size - dma_q->pos;
469
470 startread = p;
471 startwrite = outp + (dma_q->pos / 2);
472
473 /* Make sure the bottom field populates the second half of the frame */
474 if (buf->top_field == 0)
475 startwrite += bytesperline * dev->vbi_height;
476
477 for (i = 0; i < len; i += 2)
478 startwrite[j++] = startread[i+1];
479
480 dma_q->pos += len;
481}
482
483
484/*
485 * video-buf generic routine to get the next available VBI buffer
486 */
487static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
488 struct au0828_buffer **buf)
489{
490 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
491 char *outp;
492
493 if (list_empty(&dma_q->active)) {
494 au0828_isocdbg("No active queue to serve\n");
495 dev->isoc_ctl.vbi_buf = NULL;
496 *buf = NULL;
497 return;
498 }
499
500 /* Get the next buffer */
501 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300502 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300503 outp = videobuf_to_vmalloc(&(*buf)->vb);
504 memset(outp, 0x00, (*buf)->vb.size);
505
506 dev->isoc_ctl.vbi_buf = *buf;
507
508 return;
509}
510
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300511/*
512 * Controls the isoc copy of each urb packet
513 */
514static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
515{
516 struct au0828_buffer *buf;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300517 struct au0828_buffer *vbi_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300518 struct au0828_dmaqueue *dma_q = urb->context;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300519 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300520 unsigned char *outp = NULL;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300521 unsigned char *vbioutp = NULL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300522 int i, len = 0, rc = 1;
523 unsigned char *p;
524 unsigned char fbyte;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300525 unsigned int vbi_field_size;
526 unsigned int remain, lencopy;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300527
528 if (!dev)
529 return 0;
530
531 if ((dev->dev_state & DEV_DISCONNECTED) ||
532 (dev->dev_state & DEV_MISCONFIGURED))
533 return 0;
534
535 if (urb->status < 0) {
536 print_err_status(dev, -1, urb->status);
537 if (urb->status == -ENOENT)
538 return 0;
539 }
540
541 buf = dev->isoc_ctl.buf;
542 if (buf != NULL)
543 outp = videobuf_to_vmalloc(&buf->vb);
544
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300545 vbi_buf = dev->isoc_ctl.vbi_buf;
546 if (vbi_buf != NULL)
547 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
548
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300549 for (i = 0; i < urb->number_of_packets; i++) {
550 int status = urb->iso_frame_desc[i].status;
551
552 if (status < 0) {
553 print_err_status(dev, i, status);
554 if (urb->iso_frame_desc[i].status != -EPROTO)
555 continue;
556 }
557
Devin Heitmueller62899a22009-03-15 18:48:52 -0300558 if (urb->iso_frame_desc[i].actual_length <= 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300559 continue;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300560
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300561 if (urb->iso_frame_desc[i].actual_length >
562 dev->max_pkt_size) {
563 au0828_isocdbg("packet bigger than packet size");
564 continue;
565 }
566
567 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
568 fbyte = p[0];
569 len = urb->iso_frame_desc[i].actual_length - 4;
570 p += 4;
571
572 if (fbyte & 0x80) {
573 len -= 4;
574 p += 4;
575 au0828_isocdbg("Video frame %s\n",
576 (fbyte & 0x40) ? "odd" : "even");
Devin Heitmuellerbde3bb92010-07-05 13:05:16 -0300577 if (fbyte & 0x40) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300578 /* VBI */
579 if (vbi_buf != NULL)
580 vbi_buffer_filled(dev,
581 vbi_dma_q,
582 vbi_buf);
583 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
584 if (vbi_buf == NULL)
585 vbioutp = NULL;
586 else
587 vbioutp = videobuf_to_vmalloc(
588 &vbi_buf->vb);
589
590 /* Video */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300591 if (buf != NULL)
592 buffer_filled(dev, dma_q, buf);
593 get_next_buf(dma_q, &buf);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300594 if (buf == NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300595 outp = NULL;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300596 else
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300597 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300598
599 /* As long as isoc traffic is arriving, keep
600 resetting the timer */
601 if (dev->vid_timeout_running)
602 mod_timer(&dev->vid_timeout,
603 jiffies + (HZ / 10));
604 if (dev->vbi_timeout_running)
605 mod_timer(&dev->vbi_timeout,
606 jiffies + (HZ / 10));
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300607 }
608
609 if (buf != NULL) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300610 if (fbyte & 0x40)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300611 buf->top_field = 1;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300612 else
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300613 buf->top_field = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300614 }
615
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300616 if (vbi_buf != NULL) {
617 if (fbyte & 0x40)
618 vbi_buf->top_field = 1;
619 else
620 vbi_buf->top_field = 0;
621 }
622
623 dev->vbi_read = 0;
624 vbi_dma_q->pos = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300625 dma_q->pos = 0;
626 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300627
628 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
629 if (dev->vbi_read < vbi_field_size) {
630 remain = vbi_field_size - dev->vbi_read;
631 if (len < remain)
632 lencopy = len;
633 else
634 lencopy = remain;
635
636 if (vbi_buf != NULL)
637 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
638 vbioutp, len);
639
640 len -= lencopy;
641 p += lencopy;
642 dev->vbi_read += lencopy;
643 }
644
645 if (dev->vbi_read >= vbi_field_size && buf != NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300646 au0828_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300647 }
648 return rc;
649}
650
651static int
652buffer_setup(struct videobuf_queue *vq, unsigned int *count,
653 unsigned int *size)
654{
655 struct au0828_fh *fh = vq->priv_data;
656 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
657
658 if (0 == *count)
659 *count = AU0828_DEF_BUF;
660
661 if (*count < AU0828_MIN_BUF)
662 *count = AU0828_MIN_BUF;
663 return 0;
664}
665
666/* This is called *without* dev->slock held; please keep it that way */
667static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
668{
669 struct au0828_fh *fh = vq->priv_data;
670 struct au0828_dev *dev = fh->dev;
671 unsigned long flags = 0;
672 if (in_interrupt())
673 BUG();
674
675 /* We used to wait for the buffer to finish here, but this didn't work
676 because, as we were keeping the state as VIDEOBUF_QUEUED,
677 videobuf_queue_cancel marked it as finished for us.
678 (Also, it could wedge forever if the hardware was misconfigured.)
679
680 This should be safe; by the time we get here, the buffer isn't
681 queued anymore. If we ever start marking the buffers as
682 VIDEOBUF_ACTIVE, it won't be, though.
683 */
684 spin_lock_irqsave(&dev->slock, flags);
685 if (dev->isoc_ctl.buf == buf)
686 dev->isoc_ctl.buf = NULL;
687 spin_unlock_irqrestore(&dev->slock, flags);
688
689 videobuf_vmalloc_free(&buf->vb);
690 buf->vb.state = VIDEOBUF_NEEDS_INIT;
691}
692
693static int
694buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
695 enum v4l2_field field)
696{
697 struct au0828_fh *fh = vq->priv_data;
698 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
699 struct au0828_dev *dev = fh->dev;
700 int rc = 0, urb_init = 0;
701
702 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
703
704 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
705 return -EINVAL;
706
707 buf->vb.width = dev->width;
708 buf->vb.height = dev->height;
709 buf->vb.field = field;
710
711 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
712 rc = videobuf_iolock(vq, &buf->vb, NULL);
713 if (rc < 0) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300714 printk(KERN_INFO "videobuf_iolock failed\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300715 goto fail;
716 }
717 }
718
719 if (!dev->isoc_ctl.num_bufs)
720 urb_init = 1;
721
722 if (urb_init) {
723 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
724 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
725 au0828_isoc_copy);
726 if (rc < 0) {
Devin Heitmueller62899a22009-03-15 18:48:52 -0300727 printk(KERN_INFO "au0828_init_isoc failed\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300728 goto fail;
729 }
730 }
731
732 buf->vb.state = VIDEOBUF_PREPARED;
733 return 0;
734
735fail:
736 free_buffer(vq, buf);
737 return rc;
738}
739
740static void
741buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
742{
743 struct au0828_buffer *buf = container_of(vb,
744 struct au0828_buffer,
745 vb);
746 struct au0828_fh *fh = vq->priv_data;
747 struct au0828_dev *dev = fh->dev;
748 struct au0828_dmaqueue *vidq = &dev->vidq;
749
750 buf->vb.state = VIDEOBUF_QUEUED;
751 list_add_tail(&buf->vb.queue, &vidq->active);
752}
753
754static void buffer_release(struct videobuf_queue *vq,
755 struct videobuf_buffer *vb)
756{
757 struct au0828_buffer *buf = container_of(vb,
758 struct au0828_buffer,
759 vb);
760
761 free_buffer(vq, buf);
762}
763
764static struct videobuf_queue_ops au0828_video_qops = {
765 .buf_setup = buffer_setup,
766 .buf_prepare = buffer_prepare,
767 .buf_queue = buffer_queue,
768 .buf_release = buffer_release,
769};
770
771/* ------------------------------------------------------------------
772 V4L2 interface
773 ------------------------------------------------------------------*/
774
775static int au0828_i2s_init(struct au0828_dev *dev)
776{
777 /* Enable i2s mode */
778 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
779 return 0;
780}
781
782/*
783 * Auvitek au0828 analog stream enable
784 * Please set interface0 to AS5 before enable the stream
785 */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300786static int au0828_analog_stream_enable(struct au0828_dev *d)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300787{
788 dprintk(1, "au0828_analog_stream_enable called\n");
789 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
790 au0828_writereg(d, 0x106, 0x00);
791 /* set x position */
792 au0828_writereg(d, 0x110, 0x00);
793 au0828_writereg(d, 0x111, 0x00);
794 au0828_writereg(d, 0x114, 0xa0);
795 au0828_writereg(d, 0x115, 0x05);
796 /* set y position */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300797 au0828_writereg(d, 0x112, 0x00);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300798 au0828_writereg(d, 0x113, 0x00);
799 au0828_writereg(d, 0x116, 0xf2);
800 au0828_writereg(d, 0x117, 0x00);
801 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
802
803 return 0;
804}
805
806int au0828_analog_stream_disable(struct au0828_dev *d)
807{
808 dprintk(1, "au0828_analog_stream_disable called\n");
809 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
810 return 0;
811}
812
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300813static void au0828_analog_stream_reset(struct au0828_dev *dev)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300814{
815 dprintk(1, "au0828_analog_stream_reset called\n");
816 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
817 mdelay(30);
818 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
819}
820
821/*
822 * Some operations needs to stop current streaming
823 */
824static int au0828_stream_interrupt(struct au0828_dev *dev)
825{
826 int ret = 0;
827
828 dev->stream_state = STREAM_INTERRUPT;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300829 if (dev->dev_state == DEV_DISCONNECTED)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300830 return -ENODEV;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300831 else if (ret) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300832 dev->dev_state = DEV_MISCONFIGURED;
Devin Heitmueller62899a22009-03-15 18:48:52 -0300833 dprintk(1, "%s device is misconfigured!\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300834 return ret;
835 }
836 return 0;
837}
838
839/*
840 * au0828_release_resources
841 * unregister v4l2 devices
842 */
843void au0828_analog_unregister(struct au0828_dev *dev)
844{
845 dprintk(1, "au0828_release_resources called\n");
846 mutex_lock(&au0828_sysfs_lock);
847
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200848 if (dev->vdev)
Devin Heitmueller5a5a4e12009-03-11 03:00:55 -0300849 video_unregister_device(dev->vdev);
850 if (dev->vbi_dev)
851 video_unregister_device(dev->vbi_dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300852
853 mutex_unlock(&au0828_sysfs_lock);
854}
855
856
857/* Usage lock check functions */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300858static int res_get(struct au0828_fh *fh, unsigned int bit)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300859{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300860 struct au0828_dev *dev = fh->dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300861
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300862 if (fh->resources & bit)
863 /* have it already allocated */
864 return 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300865
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300866 /* is it free? */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300867 if (dev->resources & bit) {
868 /* no, someone else uses it */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300869 return 0;
870 }
871 /* it's free, grab it */
872 fh->resources |= bit;
873 dev->resources |= bit;
874 dprintk(1, "res: get %d\n", bit);
Devin Heitmueller549ee4d2012-08-06 22:46:58 -0300875
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300876 return 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300877}
878
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300879static int res_check(struct au0828_fh *fh, unsigned int bit)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300880{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300881 return fh->resources & bit;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300882}
883
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300884static int res_locked(struct au0828_dev *dev, unsigned int bit)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300885{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300886 return dev->resources & bit;
887}
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300888
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300889static void res_free(struct au0828_fh *fh, unsigned int bits)
890{
891 struct au0828_dev *dev = fh->dev;
892
893 BUG_ON((fh->resources & bits) != bits);
894
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300895 fh->resources &= ~bits;
896 dev->resources &= ~bits;
897 dprintk(1, "res: put %d\n", bits);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300898}
899
900static int get_ressource(struct au0828_fh *fh)
901{
902 switch (fh->type) {
903 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
904 return AU0828_RESOURCE_VIDEO;
905 case V4L2_BUF_TYPE_VBI_CAPTURE:
906 return AU0828_RESOURCE_VBI;
907 default:
908 BUG();
909 return 0;
910 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300911}
912
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300913/* This function ensures that video frames continue to be delivered even if
914 the ITU-656 input isn't receiving any data (thereby preventing applications
915 such as tvtime from hanging) */
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300916static void au0828_vid_buffer_timeout(unsigned long data)
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300917{
918 struct au0828_dev *dev = (struct au0828_dev *) data;
919 struct au0828_dmaqueue *dma_q = &dev->vidq;
920 struct au0828_buffer *buf;
921 unsigned char *vid_data;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300922 unsigned long flags = 0;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300923
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300924 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300925
926 buf = dev->isoc_ctl.buf;
927 if (buf != NULL) {
928 vid_data = videobuf_to_vmalloc(&buf->vb);
929 memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
930 buffer_filled(dev, dma_q, buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300931 }
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300932 get_next_buf(dma_q, &buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300933
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300934 if (dev->vid_timeout_running == 1)
935 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
936
937 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300938}
939
Mauro Carvalho Chehaba094ca42012-10-27 14:00:30 -0300940static void au0828_vbi_buffer_timeout(unsigned long data)
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300941{
942 struct au0828_dev *dev = (struct au0828_dev *) data;
943 struct au0828_dmaqueue *dma_q = &dev->vbiq;
944 struct au0828_buffer *buf;
945 unsigned char *vbi_data;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300946 unsigned long flags = 0;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300947
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300948 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300949
950 buf = dev->isoc_ctl.vbi_buf;
951 if (buf != NULL) {
952 vbi_data = videobuf_to_vmalloc(&buf->vb);
953 memset(vbi_data, 0x00, buf->vb.size);
954 vbi_buffer_filled(dev, dma_q, buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300955 }
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300956 vbi_get_next_buf(dma_q, &buf);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300957
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300958 if (dev->vbi_timeout_running == 1)
959 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
960 spin_unlock_irqrestore(&dev->slock, flags);
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300961}
962
963
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300964static int au0828_v4l2_open(struct file *filp)
965{
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300966 int ret = 0;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300967 struct video_device *vdev = video_devdata(filp);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200968 struct au0828_dev *dev = video_drvdata(filp);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300969 struct au0828_fh *fh;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300970 int type;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300971
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300972 switch (vdev->vfl_type) {
973 case VFL_TYPE_GRABBER:
974 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
975 break;
976 case VFL_TYPE_VBI:
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200977 type = V4L2_BUF_TYPE_VBI_CAPTURE;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300978 break;
979 default:
980 return -EINVAL;
981 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300982
983 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300984 if (NULL == fh) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300985 dprintk(1, "Failed allocate au0828_fh struct!\n");
986 return -ENOMEM;
987 }
988
989 fh->type = type;
990 fh->dev = dev;
991 filp->private_data = fh;
992
Devin Heitmueller62899a22009-03-15 18:48:52 -0300993 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300994 /* set au0828 interface0 to AS5 here again */
995 ret = usb_set_interface(dev->usbdev, 0, 5);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300996 if (ret < 0) {
997 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300998 return -EBUSY;
999 }
1000 dev->width = NTSC_STD_W;
1001 dev->height = NTSC_STD_H;
1002 dev->frame_size = dev->width * dev->height * 2;
1003 dev->field_size = dev->width * dev->height;
1004 dev->bytesperline = dev->width * 2;
1005
1006 au0828_analog_stream_enable(dev);
1007 au0828_analog_stream_reset(dev);
1008
1009 /* If we were doing ac97 instead of i2s, it would go here...*/
1010 au0828_i2s_init(dev);
1011
1012 dev->stream_state = STREAM_OFF;
1013 dev->dev_state |= DEV_INITIALIZED;
1014 }
1015
1016 dev->users++;
1017
1018 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001019 NULL, &dev->slock,
1020 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001021 V4L2_FIELD_INTERLACED,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001022 sizeof(struct au0828_buffer), fh,
1023 &dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001024
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001025 /* VBI Setup */
1026 dev->vbi_width = 720;
1027 dev->vbi_height = 1;
1028 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1029 NULL, &dev->slock,
1030 V4L2_BUF_TYPE_VBI_CAPTURE,
1031 V4L2_FIELD_SEQ_TB,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001032 sizeof(struct au0828_buffer), fh,
1033 &dev->lock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001034 return ret;
1035}
1036
1037static int au0828_v4l2_close(struct file *filp)
1038{
1039 int ret;
1040 struct au0828_fh *fh = filp->private_data;
1041 struct au0828_dev *dev = fh->dev;
1042
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001043 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001044 /* Cancel timeout thread in case they didn't call streamoff */
1045 dev->vid_timeout_running = 0;
1046 del_timer_sync(&dev->vid_timeout);
1047
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001048 videobuf_stop(&fh->vb_vidq);
1049 res_free(fh, AU0828_RESOURCE_VIDEO);
1050 }
1051
1052 if (res_check(fh, AU0828_RESOURCE_VBI)) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001053 /* Cancel timeout thread in case they didn't call streamoff */
1054 dev->vbi_timeout_running = 0;
1055 del_timer_sync(&dev->vbi_timeout);
1056
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001057 videobuf_stop(&fh->vb_vbiq);
1058 res_free(fh, AU0828_RESOURCE_VBI);
1059 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001060
Devin Heitmueller62899a22009-03-15 18:48:52 -03001061 if (dev->users == 1) {
Devin Heitmueller62899a22009-03-15 18:48:52 -03001062 if (dev->dev_state & DEV_DISCONNECTED) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001063 au0828_analog_unregister(dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001064 kfree(dev);
1065 return 0;
1066 }
1067
1068 au0828_analog_stream_disable(dev);
1069
1070 au0828_uninit_isoc(dev);
1071
Devin Heitmuellere4b8bc52009-05-06 20:54:00 -03001072 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001073 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Devin Heitmuellere4b8bc52009-05-06 20:54:00 -03001074
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001075 /* When close the device, set the usb intf0 into alt0 to free
1076 USB bandwidth */
1077 ret = usb_set_interface(dev->usbdev, 0, 0);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001078 if (ret < 0)
1079 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001080 }
1081
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001082 videobuf_mmap_free(&fh->vb_vidq);
1083 videobuf_mmap_free(&fh->vb_vbiq);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001084 kfree(fh);
1085 dev->users--;
1086 wake_up_interruptible_nr(&dev->open, 1);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001087 return 0;
1088}
1089
1090static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1091 size_t count, loff_t *pos)
1092{
1093 struct au0828_fh *fh = filp->private_data;
1094 struct au0828_dev *dev = fh->dev;
1095 int rc;
1096
1097 rc = check_dev(dev);
1098 if (rc < 0)
1099 return rc;
1100
Devin Heitmueller62899a22009-03-15 18:48:52 -03001101 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001102 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1103 return -EBUSY;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001104
1105 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1106 filp->f_flags & O_NONBLOCK);
1107 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001108
1109 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1110 if (!res_get(fh, AU0828_RESOURCE_VBI))
1111 return -EBUSY;
1112
Devin Heitmuellerbf797162010-10-09 15:09:17 -03001113 if (dev->vbi_timeout_running == 0) {
1114 /* Handle case where caller tries to read without
1115 calling streamon first */
1116 dev->vbi_timeout_running = 1;
1117 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1118 }
1119
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001120 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1121 filp->f_flags & O_NONBLOCK);
1122 }
1123
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001124 return 0;
1125}
1126
1127static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1128{
1129 struct au0828_fh *fh = filp->private_data;
1130 struct au0828_dev *dev = fh->dev;
1131 int rc;
1132
1133 rc = check_dev(dev);
1134 if (rc < 0)
1135 return rc;
1136
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001137 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1138 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1139 return POLLERR;
1140 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1141 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1142 if (!res_get(fh, AU0828_RESOURCE_VBI))
1143 return POLLERR;
1144 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1145 } else {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001146 return POLLERR;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001147 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001148}
1149
1150static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1151{
1152 struct au0828_fh *fh = filp->private_data;
1153 struct au0828_dev *dev = fh->dev;
1154 int rc;
1155
1156 rc = check_dev(dev);
1157 if (rc < 0)
1158 return rc;
1159
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001160 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1161 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1162 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1163 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001164
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001165 return rc;
1166}
1167
1168static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1169 struct v4l2_format *format)
1170{
1171 int ret;
1172 int width = format->fmt.pix.width;
1173 int height = format->fmt.pix.height;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001174
Devin Heitmueller62899a22009-03-15 18:48:52 -03001175 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001176 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001177
1178 /* If they are demanding a format other than the one we support,
1179 bail out (tvtime asks for UYVY and then retries with YUYV) */
Devin Heitmueller62899a22009-03-15 18:48:52 -03001180 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001181 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001182
1183 /* format->fmt.pix.width only support 720 and height 480 */
Devin Heitmueller62899a22009-03-15 18:48:52 -03001184 if (width != 720)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001185 width = 720;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001186 if (height != 480)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001187 height = 480;
1188
1189 format->fmt.pix.width = width;
1190 format->fmt.pix.height = height;
1191 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1192 format->fmt.pix.bytesperline = width * 2;
1193 format->fmt.pix.sizeimage = width * height * 2;
1194 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1195 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1196
Devin Heitmueller62899a22009-03-15 18:48:52 -03001197 if (cmd == VIDIOC_TRY_FMT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001198 return 0;
1199
1200 /* maybe set new image format, driver current only support 720*480 */
1201 dev->width = width;
1202 dev->height = height;
1203 dev->frame_size = width * height * 2;
1204 dev->field_size = width * height;
1205 dev->bytesperline = width * 2;
1206
Devin Heitmueller62899a22009-03-15 18:48:52 -03001207 if (dev->stream_state == STREAM_ON) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001208 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
Devin Heitmueller62899a22009-03-15 18:48:52 -03001209 ret = au0828_stream_interrupt(dev);
1210 if (ret != 0) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001211 dprintk(1, "error interrupting video stream!\n");
1212 return ret;
1213 }
1214 }
1215
1216 /* set au0828 interface0 to AS5 here again */
1217 ret = usb_set_interface(dev->usbdev, 0, 5);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001218 if (ret < 0) {
1219 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001220 return -EBUSY;
1221 }
1222
1223 au0828_analog_stream_enable(dev);
1224
1225 return 0;
1226}
1227
1228
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001229static int vidioc_querycap(struct file *file, void *priv,
1230 struct v4l2_capability *cap)
1231{
Hans Verkuil59e05482013-02-15 08:11:04 -03001232 struct video_device *vdev = video_devdata(file);
1233 struct au0828_fh *fh = priv;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001234 struct au0828_dev *dev = fh->dev;
1235
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001236 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001237 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
Hans Verkuil59e05482013-02-15 08:11:04 -03001238 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001239
Hans Verkuil59e05482013-02-15 08:11:04 -03001240 /* set the device capabilities */
1241 cap->device_caps = V4L2_CAP_AUDIO |
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001242 V4L2_CAP_READWRITE |
1243 V4L2_CAP_STREAMING |
1244 V4L2_CAP_TUNER;
Hans Verkuil59e05482013-02-15 08:11:04 -03001245 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1246 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1247 else
1248 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1249 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1250 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001251 return 0;
1252}
1253
1254static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1255 struct v4l2_fmtdesc *f)
1256{
Devin Heitmueller62899a22009-03-15 18:48:52 -03001257 if (f->index)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001258 return -EINVAL;
1259
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001260 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1261 strcpy(f->description, "Packed YUV2");
1262
1263 f->flags = 0;
1264 f->pixelformat = V4L2_PIX_FMT_UYVY;
1265
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001266 return 0;
1267}
1268
1269static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1270 struct v4l2_format *f)
1271{
1272 struct au0828_fh *fh = priv;
1273 struct au0828_dev *dev = fh->dev;
1274
1275 f->fmt.pix.width = dev->width;
1276 f->fmt.pix.height = dev->height;
1277 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1278 f->fmt.pix.bytesperline = dev->bytesperline;
1279 f->fmt.pix.sizeimage = dev->frame_size;
1280 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1281 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1282 return 0;
1283}
1284
1285static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1286 struct v4l2_format *f)
1287{
1288 struct au0828_fh *fh = priv;
1289 struct au0828_dev *dev = fh->dev;
1290
1291 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1292}
1293
1294static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1295 struct v4l2_format *f)
1296{
1297 struct au0828_fh *fh = priv;
1298 struct au0828_dev *dev = fh->dev;
1299 int rc;
1300
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001301 rc = check_dev(dev);
1302 if (rc < 0)
1303 return rc;
1304
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001305 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
Devin Heitmueller62899a22009-03-15 18:48:52 -03001306 printk(KERN_INFO "%s queue busy\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001307 rc = -EBUSY;
1308 goto out;
1309 }
1310
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001311 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001312out:
1313 return rc;
1314}
1315
Hans Verkuil314527a2013-03-15 06:10:40 -03001316static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001317{
1318 struct au0828_fh *fh = priv;
1319 struct au0828_dev *dev = fh->dev;
1320
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001321 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1322 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);
1323
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001324 /* FIXME: when we support something other than NTSC, we are going to
1325 have to make the au0828 bridge adjust the size of its capture
1326 buffer, which is currently hardcoded at 720x480 */
1327
Hans Verkuil314527a2013-03-15 06:10:40 -03001328 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, norm);
Devin Heitmuellerc98bc032012-08-06 22:46:59 -03001329 dev->std_set_in_tuner_core = 1;
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001330
1331 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1332 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);
1333
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001334 return 0;
1335}
1336
1337static int vidioc_enum_input(struct file *file, void *priv,
1338 struct v4l2_input *input)
1339{
1340 struct au0828_fh *fh = priv;
1341 struct au0828_dev *dev = fh->dev;
1342 unsigned int tmp;
1343
1344 static const char *inames[] = {
Devin Heitmueller3d622872009-03-15 17:48:26 -03001345 [AU0828_VMUX_UNDEFINED] = "Undefined",
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001346 [AU0828_VMUX_COMPOSITE] = "Composite",
1347 [AU0828_VMUX_SVIDEO] = "S-Video",
1348 [AU0828_VMUX_CABLE] = "Cable TV",
1349 [AU0828_VMUX_TELEVISION] = "Television",
1350 [AU0828_VMUX_DVB] = "DVB",
1351 [AU0828_VMUX_DEBUG] = "tv debug"
1352 };
1353
1354 tmp = input->index;
1355
Dan Carpenterf5e20c32010-03-28 08:21:18 -03001356 if (tmp >= AU0828_MAX_INPUT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001357 return -EINVAL;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001358 if (AUVI_INPUT(tmp).type == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001359 return -EINVAL;
1360
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001361 input->index = tmp;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001362 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001363 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001364 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001365 input->type |= V4L2_INPUT_TYPE_TUNER;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001366 input->audioset = 1;
1367 } else {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001368 input->type |= V4L2_INPUT_TYPE_CAMERA;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001369 input->audioset = 2;
1370 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001371
1372 input->std = dev->vdev->tvnorms;
1373
1374 return 0;
1375}
1376
1377static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1378{
1379 struct au0828_fh *fh = priv;
1380 struct au0828_dev *dev = fh->dev;
1381 *i = dev->ctrl_input;
1382 return 0;
1383}
1384
1385static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1386{
1387 struct au0828_fh *fh = priv;
1388 struct au0828_dev *dev = fh->dev;
1389 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001390
Devin Heitmueller62899a22009-03-15 18:48:52 -03001391 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001392 index);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001393 if (index >= AU0828_MAX_INPUT)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001394 return -EINVAL;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001395 if (AUVI_INPUT(index).type == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001396 return -EINVAL;
1397 dev->ctrl_input = index;
1398
Devin Heitmueller62899a22009-03-15 18:48:52 -03001399 switch (AUVI_INPUT(index).type) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001400 case AU0828_VMUX_SVIDEO:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001401 dev->input_type = AU0828_VMUX_SVIDEO;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001402 dev->ctrl_ainput = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001403 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001404 case AU0828_VMUX_COMPOSITE:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001405 dev->input_type = AU0828_VMUX_COMPOSITE;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001406 dev->ctrl_ainput = 1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001407 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001408 case AU0828_VMUX_TELEVISION:
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001409 dev->input_type = AU0828_VMUX_TELEVISION;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001410 dev->ctrl_ainput = 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001411 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001412 default:
Devin Heitmuellera1094c42009-03-15 17:43:13 -03001413 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1414 AUVI_INPUT(index).type);
1415 break;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001416 }
1417
Hans Verkuil5325b422009-04-02 11:26:22 -03001418 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1419 AUVI_INPUT(index).vmux, 0, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001420
1421 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1422 int enable = 0;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001423 if (AUVI_INPUT(i).audio_setup == NULL)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001424 continue;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001425
1426 if (i == index)
1427 enable = 1;
1428 else
1429 enable = 0;
1430 if (enable) {
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001431 (AUVI_INPUT(i).audio_setup)(dev, enable);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001432 } else {
1433 /* Make sure we leave it turned on if some
1434 other input is routed to this callback */
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -03001435 if ((AUVI_INPUT(i).audio_setup) !=
1436 ((AUVI_INPUT(index).audio_setup))) {
1437 (AUVI_INPUT(i).audio_setup)(dev, enable);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001438 }
1439 }
1440 }
1441
Hans Verkuil5325b422009-04-02 11:26:22 -03001442 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1443 AUVI_INPUT(index).amux, 0, 0);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001444 return 0;
1445}
1446
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001447static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001448{
Devin Heitmueller62899a22009-03-15 18:48:52 -03001449 if (a->index > 1)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001450 return -EINVAL;
1451
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001452 if (a->index == 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001453 strcpy(a->name, "Television");
1454 else
1455 strcpy(a->name, "Line in");
1456
1457 a->capability = V4L2_AUDCAP_STEREO;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001458 return 0;
1459}
1460
1461static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1462{
1463 struct au0828_fh *fh = priv;
1464 struct au0828_dev *dev = fh->dev;
1465
1466 a->index = dev->ctrl_ainput;
1467 if (a->index == 0)
1468 strcpy(a->name, "Television");
1469 else
1470 strcpy(a->name, "Line in");
1471
1472 a->capability = V4L2_AUDCAP_STEREO;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001473 return 0;
1474}
1475
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001476static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001477{
1478 struct au0828_fh *fh = priv;
1479 struct au0828_dev *dev = fh->dev;
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001480
Devin Heitmueller62899a22009-03-15 18:48:52 -03001481 if (a->index != dev->ctrl_ainput)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001482 return -EINVAL;
1483 return 0;
1484}
1485
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001486static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1487{
1488 struct au0828_fh *fh = priv;
1489 struct au0828_dev *dev = fh->dev;
1490
Devin Heitmueller62899a22009-03-15 18:48:52 -03001491 if (t->index != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001492 return -EINVAL;
1493
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001494 strcpy(t->name, "Auvitek tuner");
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001495 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001496 return 0;
1497}
1498
1499static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001500 const struct v4l2_tuner *t)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001501{
1502 struct au0828_fh *fh = priv;
1503 struct au0828_dev *dev = fh->dev;
1504
Devin Heitmueller62899a22009-03-15 18:48:52 -03001505 if (t->index != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001506 return -EINVAL;
1507
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001508 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1509 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);
1510
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001511 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001512
1513 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1514 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);
1515
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001516 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1517 t->afc);
Devin Heitmuellere58071f2012-08-06 22:47:12 -03001518
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001519 return 0;
1520
1521}
1522
1523static int vidioc_g_frequency(struct file *file, void *priv,
1524 struct v4l2_frequency *freq)
1525{
1526 struct au0828_fh *fh = priv;
1527 struct au0828_dev *dev = fh->dev;
Devin Heitmuellerc8889232009-03-15 17:38:47 -03001528
Hans Verkuile1cf6582013-03-22 13:32:20 -03001529 if (freq->tuner != 0)
1530 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001531 freq->frequency = dev->ctrl_freq;
1532 return 0;
1533}
1534
1535static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001536 const struct v4l2_frequency *freq)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001537{
1538 struct au0828_fh *fh = priv;
1539 struct au0828_dev *dev = fh->dev;
Hans Verkuile1cf6582013-03-22 13:32:20 -03001540 struct v4l2_frequency new_freq = *freq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001541
Devin Heitmueller62899a22009-03-15 18:48:52 -03001542 if (freq->tuner != 0)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001543 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001544
Devin Heitmueller4a03daf2012-08-06 22:47:02 -03001545 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1546 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);
1547
Devin Heitmuellerc98bc032012-08-06 22:46:59 -03001548 if (dev->std_set_in_tuner_core == 0) {
Hans Verkuil38823b22013-02-15 08:28:27 -03001549 /* If we've never sent the standard in tuner core, do so now.
1550 We don't do this at device probe because we don't want to
1551 incur the cost of a firmware load */
1552 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
1553 dev->vdev->tvnorms);
1554 dev->std_set_in_tuner_core = 1;
Devin Heitmuellerc98bc032012-08-06 22:46:59 -03001555 }
1556
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001557 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
Hans Verkuile1cf6582013-03-22 13:32:20 -03001558 /* Get the actual set (and possibly clamped) frequency */
1559 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1560 dev->ctrl_freq = new_freq.frequency;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001561
Devin Heitmueller4a03daf2012-08-06 22:47:02 -03001562 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1563 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);
1564
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001565 au0828_analog_stream_reset(dev);
1566
1567 return 0;
1568}
1569
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001570
1571/* RAW VBI ioctls */
1572
1573static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1574 struct v4l2_format *format)
1575{
1576 struct au0828_fh *fh = priv;
1577 struct au0828_dev *dev = fh->dev;
1578
1579 format->fmt.vbi.samples_per_line = dev->vbi_width;
1580 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1581 format->fmt.vbi.offset = 0;
1582 format->fmt.vbi.flags = 0;
1583 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1584
1585 format->fmt.vbi.count[0] = dev->vbi_height;
1586 format->fmt.vbi.count[1] = dev->vbi_height;
1587 format->fmt.vbi.start[0] = 21;
1588 format->fmt.vbi.start[1] = 284;
1589
1590 return 0;
1591}
1592
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001593static int vidioc_g_chip_ident(struct file *file, void *priv,
1594 struct v4l2_dbg_chip_ident *chip)
1595{
1596 struct au0828_fh *fh = priv;
1597 struct au0828_dev *dev = fh->dev;
1598 chip->ident = V4L2_IDENT_NONE;
1599 chip->revision = 0;
1600
Devin Heitmuellerd9109be2009-03-11 03:00:58 -03001601 if (v4l2_chip_match_host(&chip->match)) {
1602 chip->ident = V4L2_IDENT_AU0828;
1603 return 0;
1604 }
1605
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001606 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Devin Heitmuellerd9109be2009-03-11 03:00:58 -03001607 if (chip->ident == V4L2_IDENT_NONE)
1608 return -EINVAL;
1609
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001610 return 0;
1611}
1612
1613static int vidioc_cropcap(struct file *file, void *priv,
1614 struct v4l2_cropcap *cc)
1615{
1616 struct au0828_fh *fh = priv;
1617 struct au0828_dev *dev = fh->dev;
1618
Devin Heitmueller62899a22009-03-15 18:48:52 -03001619 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001620 return -EINVAL;
1621
1622 cc->bounds.left = 0;
1623 cc->bounds.top = 0;
1624 cc->bounds.width = dev->width;
1625 cc->bounds.height = dev->height;
1626
1627 cc->defrect = cc->bounds;
1628
1629 cc->pixelaspect.numerator = 54;
1630 cc->pixelaspect.denominator = 59;
1631
1632 return 0;
1633}
1634
1635static int vidioc_streamon(struct file *file, void *priv,
1636 enum v4l2_buf_type type)
1637{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001638 struct au0828_fh *fh = priv;
1639 struct au0828_dev *dev = fh->dev;
1640 int rc = -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001641
1642 rc = check_dev(dev);
1643 if (rc < 0)
1644 return rc;
1645
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001646 if (unlikely(type != fh->type))
1647 return -EINVAL;
1648
1649 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1650 fh, type, fh->resources, dev->resources);
1651
1652 if (unlikely(!res_get(fh, get_ressource(fh))))
1653 return -EBUSY;
1654
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001655 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1656 au0828_analog_stream_enable(dev);
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001657 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001658 }
1659
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001660 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001661 rc = videobuf_streamon(&fh->vb_vidq);
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001662 dev->vid_timeout_running = 1;
1663 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1664 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001665 rc = videobuf_streamon(&fh->vb_vbiq);
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001666 dev->vbi_timeout_running = 1;
1667 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1668 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001669
1670 return rc;
1671}
1672
1673static int vidioc_streamoff(struct file *file, void *priv,
1674 enum v4l2_buf_type type)
1675{
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001676 struct au0828_fh *fh = priv;
1677 struct au0828_dev *dev = fh->dev;
1678 int rc;
1679 int i;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001680
1681 rc = check_dev(dev);
1682 if (rc < 0)
1683 return rc;
1684
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001685 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1686 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001687 return -EINVAL;
1688 if (type != fh->type)
1689 return -EINVAL;
1690
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001691 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1692 fh, type, fh->resources, dev->resources);
1693
1694 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001695 dev->vid_timeout_running = 0;
1696 del_timer_sync(&dev->vid_timeout);
1697
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001698 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001699 rc = au0828_stream_interrupt(dev);
1700 if (rc != 0)
1701 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001702
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001703 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1704 if (AUVI_INPUT(i).audio_setup == NULL)
1705 continue;
1706 (AUVI_INPUT(i).audio_setup)(dev, 0);
1707 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001708
Devin Heitmuellera595c1c2012-08-06 22:47:03 -03001709 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1710 videobuf_streamoff(&fh->vb_vidq);
1711 res_free(fh, AU0828_RESOURCE_VIDEO);
1712 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001713 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001714 dev->vbi_timeout_running = 0;
1715 del_timer_sync(&dev->vbi_timeout);
1716
Devin Heitmuellera595c1c2012-08-06 22:47:03 -03001717 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1718 videobuf_streamoff(&fh->vb_vbiq);
1719 res_free(fh, AU0828_RESOURCE_VBI);
1720 }
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001721 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001722
1723 return 0;
1724}
1725
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001726#ifdef CONFIG_VIDEO_ADV_DEBUG
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001727static int vidioc_g_register(struct file *file, void *priv,
1728 struct v4l2_dbg_register *reg)
1729{
1730 struct au0828_fh *fh = priv;
1731 struct au0828_dev *dev = fh->dev;
1732
1733 switch (reg->match.type) {
1734 case V4L2_CHIP_MATCH_I2C_DRIVER:
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001735 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001736 return 0;
1737 default:
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001738 if (!v4l2_chip_match_host(&reg->match))
1739 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001740 }
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001741
1742 reg->val = au0828_read(dev, reg->reg);
1743 return 0;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001744}
1745
1746static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001747 const struct v4l2_dbg_register *reg)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001748{
1749 struct au0828_fh *fh = priv;
1750 struct au0828_dev *dev = fh->dev;
1751
1752 switch (reg->match.type) {
1753 case V4L2_CHIP_MATCH_I2C_DRIVER:
Devin Heitmueller2689d3d2009-03-15 20:01:53 -03001754 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001755 return 0;
1756 default:
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001757 if (!v4l2_chip_match_host(&reg->match))
1758 return -EINVAL;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001759 }
Devin Heitmueller364d2db2012-08-06 22:46:54 -03001760 return au0828_writereg(dev, reg->reg, reg->val);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001761}
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001762#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001763
1764static int vidioc_reqbufs(struct file *file, void *priv,
1765 struct v4l2_requestbuffers *rb)
1766{
1767 struct au0828_fh *fh = priv;
1768 struct au0828_dev *dev = fh->dev;
1769 int rc;
1770
1771 rc = check_dev(dev);
1772 if (rc < 0)
1773 return rc;
1774
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001775 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1776 rc = videobuf_reqbufs(&fh->vb_vidq, rb);
1777 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1778 rc = videobuf_reqbufs(&fh->vb_vbiq, rb);
1779
1780 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001781}
1782
1783static int vidioc_querybuf(struct file *file, void *priv,
1784 struct v4l2_buffer *b)
1785{
1786 struct au0828_fh *fh = priv;
1787 struct au0828_dev *dev = fh->dev;
1788 int rc;
1789
1790 rc = check_dev(dev);
1791 if (rc < 0)
1792 return rc;
1793
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001794 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1795 rc = videobuf_querybuf(&fh->vb_vidq, b);
1796 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1797 rc = videobuf_querybuf(&fh->vb_vbiq, b);
1798
1799 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001800}
1801
1802static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1803{
1804 struct au0828_fh *fh = priv;
1805 struct au0828_dev *dev = fh->dev;
1806 int rc;
1807
1808 rc = check_dev(dev);
1809 if (rc < 0)
1810 return rc;
1811
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001812 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1813 rc = videobuf_qbuf(&fh->vb_vidq, b);
1814 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1815 rc = videobuf_qbuf(&fh->vb_vbiq, b);
1816
1817 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001818}
1819
1820static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1821{
1822 struct au0828_fh *fh = priv;
1823 struct au0828_dev *dev = fh->dev;
1824 int rc;
1825
1826 rc = check_dev(dev);
1827 if (rc < 0)
1828 return rc;
1829
1830 /* Workaround for a bug in the au0828 hardware design that sometimes
1831 results in the colorspace being inverted */
1832 if (dev->greenscreen_detected == 1) {
1833 dprintk(1, "Detected green frame. Resetting stream...\n");
1834 au0828_analog_stream_reset(dev);
1835 dev->greenscreen_detected = 0;
1836 }
1837
Devin Heitmueller54ebb8b2011-01-23 19:12:27 -03001838 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1839 rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1840 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1841 rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK);
1842
1843 return rc;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001844}
1845
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001846static struct v4l2_file_operations au0828_v4l_fops = {
1847 .owner = THIS_MODULE,
1848 .open = au0828_v4l2_open,
1849 .release = au0828_v4l2_close,
1850 .read = au0828_v4l2_read,
1851 .poll = au0828_v4l2_poll,
1852 .mmap = au0828_v4l2_mmap,
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001853 .unlocked_ioctl = video_ioctl2,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001854};
1855
1856static const struct v4l2_ioctl_ops video_ioctl_ops = {
1857 .vidioc_querycap = vidioc_querycap,
1858 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1859 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1860 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1861 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller5a5a4e12009-03-11 03:00:55 -03001862 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001863 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuila2cf96f2013-02-15 08:41:24 -03001864 .vidioc_enumaudio = vidioc_enumaudio,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001865 .vidioc_g_audio = vidioc_g_audio,
1866 .vidioc_s_audio = vidioc_s_audio,
1867 .vidioc_cropcap = vidioc_cropcap,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001868 .vidioc_reqbufs = vidioc_reqbufs,
1869 .vidioc_querybuf = vidioc_querybuf,
1870 .vidioc_qbuf = vidioc_qbuf,
1871 .vidioc_dqbuf = vidioc_dqbuf,
1872 .vidioc_s_std = vidioc_s_std,
1873 .vidioc_enum_input = vidioc_enum_input,
1874 .vidioc_g_input = vidioc_g_input,
1875 .vidioc_s_input = vidioc_s_input,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001876 .vidioc_streamon = vidioc_streamon,
1877 .vidioc_streamoff = vidioc_streamoff,
1878 .vidioc_g_tuner = vidioc_g_tuner,
1879 .vidioc_s_tuner = vidioc_s_tuner,
1880 .vidioc_g_frequency = vidioc_g_frequency,
1881 .vidioc_s_frequency = vidioc_s_frequency,
1882#ifdef CONFIG_VIDEO_ADV_DEBUG
1883 .vidioc_g_register = vidioc_g_register,
1884 .vidioc_s_register = vidioc_s_register,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001885#endif
Mauro Carvalho Chehab80c6e352009-03-19 19:26:23 -03001886 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001887};
1888
1889static const struct video_device au0828_video_template = {
1890 .fops = &au0828_v4l_fops,
1891 .release = video_device_release,
1892 .ioctl_ops = &video_ioctl_ops,
Devin Heitmueller0ef210712009-03-11 03:00:53 -03001893 .tvnorms = V4L2_STD_NTSC_M,
1894 .current_norm = V4L2_STD_NTSC_M,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001895};
1896
1897/**************************************************************************/
1898
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001899int au0828_analog_register(struct au0828_dev *dev,
1900 struct usb_interface *interface)
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001901{
1902 int retval = -ENOMEM;
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001903 struct usb_host_interface *iface_desc;
1904 struct usb_endpoint_descriptor *endpoint;
Julia Lawalld63b21b2012-04-22 07:54:42 -03001905 int i, ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001906
1907 dprintk(1, "au0828_analog_register called!\n");
1908
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001909 /* set au0828 usb interface0 to as5 */
1910 retval = usb_set_interface(dev->usbdev,
Devin Heitmueller62899a22009-03-15 18:48:52 -03001911 interface->cur_altsetting->desc.bInterfaceNumber, 5);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001912 if (retval != 0) {
Devin Heitmueller62899a22009-03-15 18:48:52 -03001913 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001914 return retval;
1915 }
1916
1917 /* Figure out which endpoint has the isoc interface */
1918 iface_desc = interface->cur_altsetting;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001919 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001920 endpoint = &iface_desc->endpoint[i].desc;
Devin Heitmueller62899a22009-03-15 18:48:52 -03001921 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1922 == USB_DIR_IN) &&
1923 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1924 == USB_ENDPOINT_XFER_ISOC)) {
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001925
1926 /* we find our isoc in endpoint */
1927 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001928 dev->max_pkt_size = (tmp & 0x07ff) *
1929 (((tmp & 0x1800) >> 11) + 1);
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001930 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1931 }
1932 }
Devin Heitmueller62899a22009-03-15 18:48:52 -03001933 if (!(dev->isoc_in_endpointaddr)) {
1934 printk(KERN_INFO "Could not locate isoc endpoint\n");
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -03001935 kfree(dev);
1936 return -ENODEV;
1937 }
1938
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001939 init_waitqueue_head(&dev->open);
1940 spin_lock_init(&dev->slock);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001941
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001942 /* init video dma queues */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001943 INIT_LIST_HEAD(&dev->vidq.active);
1944 INIT_LIST_HEAD(&dev->vidq.queued);
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001945 INIT_LIST_HEAD(&dev->vbiq.active);
1946 INIT_LIST_HEAD(&dev->vbiq.queued);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001947
Devin Heitmueller78ca5002010-10-09 14:43:53 -03001948 dev->vid_timeout.function = au0828_vid_buffer_timeout;
1949 dev->vid_timeout.data = (unsigned long) dev;
1950 init_timer(&dev->vid_timeout);
1951
1952 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
1953 dev->vbi_timeout.data = (unsigned long) dev;
1954 init_timer(&dev->vbi_timeout);
1955
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001956 dev->width = NTSC_STD_W;
1957 dev->height = NTSC_STD_H;
1958 dev->field_size = dev->width * dev->height;
1959 dev->frame_size = dev->field_size << 1;
1960 dev->bytesperline = dev->width << 1;
1961 dev->ctrl_ainput = 0;
Hans Verkuile1cf6582013-03-22 13:32:20 -03001962 dev->ctrl_freq = 960;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001963
1964 /* allocate and fill v4l2 video struct */
1965 dev->vdev = video_device_alloc();
Devin Heitmueller62899a22009-03-15 18:48:52 -03001966 if (NULL == dev->vdev) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001967 dprintk(1, "Can't allocate video_device.\n");
1968 return -ENOMEM;
1969 }
1970
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -03001971 /* allocate the VBI struct */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001972 dev->vbi_dev = video_device_alloc();
Devin Heitmueller62899a22009-03-15 18:48:52 -03001973 if (NULL == dev->vbi_dev) {
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001974 dprintk(1, "Can't allocate vbi_device.\n");
Julia Lawalld63b21b2012-04-22 07:54:42 -03001975 ret = -ENOMEM;
1976 goto err_vdev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001977 }
1978
1979 /* Fill the video capture device struct */
1980 *dev->vdev = au0828_video_template;
Hans Verkuile8c26f42013-02-15 08:55:41 -03001981 dev->vdev->v4l2_dev = &dev->v4l2_dev;
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001982 dev->vdev->lock = &dev->lock;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001983 strcpy(dev->vdev->name, "au0828a video");
1984
1985 /* Setup the VBI device */
1986 *dev->vbi_dev = au0828_video_template;
Hans Verkuile8c26f42013-02-15 08:55:41 -03001987 dev->vbi_dev->v4l2_dev = &dev->v4l2_dev;
Devin Heitmueller549ee4d2012-08-06 22:46:58 -03001988 dev->vbi_dev->lock = &dev->lock;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001989 strcpy(dev->vbi_dev->name, "au0828a vbi");
1990
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001991 /* Register the v4l2 device */
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001992 video_set_drvdata(dev->vdev, dev);
Devin Heitmueller62899a22009-03-15 18:48:52 -03001993 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1994 if (retval != 0) {
1995 dprintk(1, "unable to register video device (error = %d).\n",
1996 retval);
Julia Lawalld63b21b2012-04-22 07:54:42 -03001997 ret = -ENODEV;
1998 goto err_vbi_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03001999 }
2000
2001 /* Register the vbi device */
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002002 video_set_drvdata(dev->vbi_dev, dev);
Devin Heitmueller62899a22009-03-15 18:48:52 -03002003 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
2004 if (retval != 0) {
2005 dprintk(1, "unable to register vbi device (error = %d).\n",
2006 retval);
Julia Lawalld63b21b2012-04-22 07:54:42 -03002007 ret = -ENODEV;
2008 goto err_vbi_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002009 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002010
Devin Heitmueller62899a22009-03-15 18:48:52 -03002011 dprintk(1, "%s completed!\n", __func__);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002012
2013 return 0;
Julia Lawalld63b21b2012-04-22 07:54:42 -03002014
2015err_vbi_dev:
2016 video_device_release(dev->vbi_dev);
2017err_vdev:
2018 video_device_release(dev->vdev);
2019 return ret;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -03002020}
2021