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