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