Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Auvitek AU0828 USB Bridge (Analog video support)
0004  *
0005  * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
0006  * Copyright (C) 2005-2008 Auvitek International, Ltd.
0007  */
0008 
0009 /* Developer Notes:
0010  *
0011  * The hardware scaler supported is unimplemented
0012  * AC97 audio support is unimplemented (only i2s audio mode)
0013  *
0014  */
0015 
0016 #include "au0828.h"
0017 #include "au8522.h"
0018 
0019 #include <linux/module.h>
0020 #include <linux/slab.h>
0021 #include <linux/init.h>
0022 #include <linux/device.h>
0023 #include <media/v4l2-common.h>
0024 #include <media/v4l2-mc.h>
0025 #include <media/v4l2-ioctl.h>
0026 #include <media/v4l2-event.h>
0027 #include <media/tuner.h>
0028 #include "au0828-reg.h"
0029 
0030 static DEFINE_MUTEX(au0828_sysfs_lock);
0031 
0032 /* ------------------------------------------------------------------
0033     Videobuf operations
0034    ------------------------------------------------------------------*/
0035 
0036 static unsigned int isoc_debug;
0037 module_param(isoc_debug, int, 0644);
0038 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
0039 
0040 #define au0828_isocdbg(fmt, arg...) \
0041 do {\
0042     if (isoc_debug) { \
0043         pr_info("au0828 %s :"fmt, \
0044                __func__ , ##arg);      \
0045     } \
0046   } while (0)
0047 
0048 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
0049 {
0050     if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
0051         dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
0052 }
0053 
0054 static inline void print_err_status(struct au0828_dev *dev,
0055                     int packet, int status)
0056 {
0057     char *errmsg = "Unknown";
0058 
0059     switch (status) {
0060     case -ENOENT:
0061         errmsg = "unlinked synchronously";
0062         break;
0063     case -ECONNRESET:
0064         errmsg = "unlinked asynchronously";
0065         break;
0066     case -ENOSR:
0067         errmsg = "Buffer error (overrun)";
0068         break;
0069     case -EPIPE:
0070         errmsg = "Stalled (device not responding)";
0071         break;
0072     case -EOVERFLOW:
0073         errmsg = "Babble (bad cable?)";
0074         break;
0075     case -EPROTO:
0076         errmsg = "Bit-stuff error (bad cable?)";
0077         break;
0078     case -EILSEQ:
0079         errmsg = "CRC/Timeout (could be anything)";
0080         break;
0081     case -ETIME:
0082         errmsg = "Device does not respond";
0083         break;
0084     }
0085     if (packet < 0) {
0086         au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
0087     } else {
0088         au0828_isocdbg("URB packet %d, status %d [%s].\n",
0089                    packet, status, errmsg);
0090     }
0091 }
0092 
0093 static int check_dev(struct au0828_dev *dev)
0094 {
0095     if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
0096         pr_info("v4l2 ioctl: device not present\n");
0097         return -ENODEV;
0098     }
0099 
0100     if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
0101         pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
0102         return -EIO;
0103     }
0104     return 0;
0105 }
0106 
0107 /*
0108  * IRQ callback, called by URB callback
0109  */
0110 static void au0828_irq_callback(struct urb *urb)
0111 {
0112     struct au0828_dmaqueue  *dma_q = urb->context;
0113     struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
0114     unsigned long flags = 0;
0115     int i;
0116 
0117     switch (urb->status) {
0118     case 0:             /* success */
0119     case -ETIMEDOUT:    /* NAK */
0120         break;
0121     case -ECONNRESET:   /* kill */
0122     case -ENOENT:
0123     case -ESHUTDOWN:
0124         au0828_isocdbg("au0828_irq_callback called: status kill\n");
0125         return;
0126     default:            /* unknown error */
0127         au0828_isocdbg("urb completion error %d.\n", urb->status);
0128         break;
0129     }
0130 
0131     /* Copy data from URB */
0132     spin_lock_irqsave(&dev->slock, flags);
0133     dev->isoc_ctl.isoc_copy(dev, urb);
0134     spin_unlock_irqrestore(&dev->slock, flags);
0135 
0136     /* Reset urb buffers */
0137     for (i = 0; i < urb->number_of_packets; i++) {
0138         urb->iso_frame_desc[i].status = 0;
0139         urb->iso_frame_desc[i].actual_length = 0;
0140     }
0141     urb->status = 0;
0142 
0143     urb->status = usb_submit_urb(urb, GFP_ATOMIC);
0144     if (urb->status) {
0145         au0828_isocdbg("urb resubmit failed (error=%i)\n",
0146                    urb->status);
0147     }
0148     dev->stream_state = STREAM_ON;
0149 }
0150 
0151 /*
0152  * Stop and Deallocate URBs
0153  */
0154 static void au0828_uninit_isoc(struct au0828_dev *dev)
0155 {
0156     struct urb *urb;
0157     int i;
0158 
0159     au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
0160 
0161     dev->isoc_ctl.nfields = -1;
0162     for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
0163         urb = dev->isoc_ctl.urb[i];
0164         if (urb) {
0165             if (!irqs_disabled())
0166                 usb_kill_urb(urb);
0167             else
0168                 usb_unlink_urb(urb);
0169 
0170             if (dev->isoc_ctl.transfer_buffer[i]) {
0171                 usb_free_coherent(dev->usbdev,
0172                     urb->transfer_buffer_length,
0173                     dev->isoc_ctl.transfer_buffer[i],
0174                     urb->transfer_dma);
0175             }
0176             usb_free_urb(urb);
0177             dev->isoc_ctl.urb[i] = NULL;
0178         }
0179         dev->isoc_ctl.transfer_buffer[i] = NULL;
0180     }
0181 
0182     kfree(dev->isoc_ctl.urb);
0183     kfree(dev->isoc_ctl.transfer_buffer);
0184 
0185     dev->isoc_ctl.urb = NULL;
0186     dev->isoc_ctl.transfer_buffer = NULL;
0187     dev->isoc_ctl.num_bufs = 0;
0188 
0189     dev->stream_state = STREAM_OFF;
0190 }
0191 
0192 /*
0193  * Allocate URBs and start IRQ
0194  */
0195 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
0196                 int num_bufs, int max_pkt_size,
0197                 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
0198 {
0199     struct au0828_dmaqueue *dma_q = &dev->vidq;
0200     int i;
0201     int sb_size, pipe;
0202     struct urb *urb;
0203     int j, k;
0204     int rc;
0205 
0206     au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
0207 
0208     dev->isoc_ctl.isoc_copy = isoc_copy;
0209     dev->isoc_ctl.num_bufs = num_bufs;
0210 
0211     dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *),  GFP_KERNEL);
0212     if (!dev->isoc_ctl.urb) {
0213         au0828_isocdbg("cannot alloc memory for usb buffers\n");
0214         return -ENOMEM;
0215     }
0216 
0217     dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
0218                         GFP_KERNEL);
0219     if (!dev->isoc_ctl.transfer_buffer) {
0220         au0828_isocdbg("cannot allocate memory for usb transfer\n");
0221         kfree(dev->isoc_ctl.urb);
0222         return -ENOMEM;
0223     }
0224 
0225     dev->isoc_ctl.max_pkt_size = max_pkt_size;
0226     dev->isoc_ctl.buf = NULL;
0227 
0228     sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
0229 
0230     /* allocate urbs and transfer buffers */
0231     for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
0232         urb = usb_alloc_urb(max_packets, GFP_KERNEL);
0233         if (!urb) {
0234             au0828_isocdbg("cannot allocate URB\n");
0235             au0828_uninit_isoc(dev);
0236             return -ENOMEM;
0237         }
0238         dev->isoc_ctl.urb[i] = urb;
0239 
0240         dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
0241             sb_size, GFP_KERNEL, &urb->transfer_dma);
0242         if (!dev->isoc_ctl.transfer_buffer[i]) {
0243             au0828_isocdbg("cannot allocate transfer buffer\n");
0244             au0828_uninit_isoc(dev);
0245             return -ENOMEM;
0246         }
0247         memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
0248 
0249         pipe = usb_rcvisocpipe(dev->usbdev,
0250                        dev->isoc_in_endpointaddr);
0251 
0252         usb_fill_int_urb(urb, dev->usbdev, pipe,
0253                  dev->isoc_ctl.transfer_buffer[i], sb_size,
0254                  au0828_irq_callback, dma_q, 1);
0255 
0256         urb->number_of_packets = max_packets;
0257         urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
0258 
0259         k = 0;
0260         for (j = 0; j < max_packets; j++) {
0261             urb->iso_frame_desc[j].offset = k;
0262             urb->iso_frame_desc[j].length =
0263                         dev->isoc_ctl.max_pkt_size;
0264             k += dev->isoc_ctl.max_pkt_size;
0265         }
0266     }
0267 
0268     /* submit urbs and enables IRQ */
0269     for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
0270         rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
0271         if (rc) {
0272             au0828_isocdbg("submit of urb %i failed (error=%i)\n",
0273                        i, rc);
0274             au0828_uninit_isoc(dev);
0275             return rc;
0276         }
0277     }
0278 
0279     return 0;
0280 }
0281 
0282 /*
0283  * Announces that a buffer were filled and request the next
0284  */
0285 static inline void buffer_filled(struct au0828_dev *dev,
0286                  struct au0828_dmaqueue *dma_q,
0287                  struct au0828_buffer *buf)
0288 {
0289     struct vb2_v4l2_buffer *vb = &buf->vb;
0290     struct vb2_queue *q = vb->vb2_buf.vb2_queue;
0291 
0292     /* Advice that buffer was filled */
0293     au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
0294 
0295     if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0296         vb->sequence = dev->frame_count++;
0297     else
0298         vb->sequence = dev->vbi_frame_count++;
0299 
0300     vb->field = V4L2_FIELD_INTERLACED;
0301     vb->vb2_buf.timestamp = ktime_get_ns();
0302     vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
0303 }
0304 
0305 /*
0306  * Identify the buffer header type and properly handles
0307  */
0308 static void au0828_copy_video(struct au0828_dev *dev,
0309                   struct au0828_dmaqueue  *dma_q,
0310                   struct au0828_buffer *buf,
0311                   unsigned char *p,
0312                   unsigned char *outp, unsigned long len)
0313 {
0314     void *fieldstart, *startwrite, *startread;
0315     int  linesdone, currlinedone, offset, lencopy, remain;
0316     int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
0317 
0318     if (len == 0)
0319         return;
0320 
0321     if (dma_q->pos + len > buf->length)
0322         len = buf->length - dma_q->pos;
0323 
0324     startread = p;
0325     remain = len;
0326 
0327     /* Interlaces frame */
0328     if (buf->top_field)
0329         fieldstart = outp;
0330     else
0331         fieldstart = outp + bytesperline;
0332 
0333     linesdone = dma_q->pos / bytesperline;
0334     currlinedone = dma_q->pos % bytesperline;
0335     offset = linesdone * bytesperline * 2 + currlinedone;
0336     startwrite = fieldstart + offset;
0337     lencopy = bytesperline - currlinedone;
0338     lencopy = lencopy > remain ? remain : lencopy;
0339 
0340     if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
0341         au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
0342                    ((char *)startwrite + lencopy) -
0343                    ((char *)outp + buf->length));
0344         remain = (char *)outp + buf->length - (char *)startwrite;
0345         lencopy = remain;
0346     }
0347     if (lencopy <= 0)
0348         return;
0349     memcpy(startwrite, startread, lencopy);
0350 
0351     remain -= lencopy;
0352 
0353     while (remain > 0) {
0354         startwrite += lencopy + bytesperline;
0355         startread += lencopy;
0356         if (bytesperline > remain)
0357             lencopy = remain;
0358         else
0359             lencopy = bytesperline;
0360 
0361         if ((char *)startwrite + lencopy > (char *)outp +
0362             buf->length) {
0363             au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
0364                        ((char *)startwrite + lencopy) -
0365                        ((char *)outp + buf->length));
0366             lencopy = remain = (char *)outp + buf->length -
0367                        (char *)startwrite;
0368         }
0369         if (lencopy <= 0)
0370             break;
0371 
0372         memcpy(startwrite, startread, lencopy);
0373 
0374         remain -= lencopy;
0375     }
0376 
0377     if (offset > 1440) {
0378         /* We have enough data to check for greenscreen */
0379         if (outp[0] < 0x60 && outp[1440] < 0x60)
0380             dev->greenscreen_detected = 1;
0381     }
0382 
0383     dma_q->pos += len;
0384 }
0385 
0386 /*
0387  * video-buf generic routine to get the next available buffer
0388  */
0389 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
0390                 struct au0828_buffer **buf)
0391 {
0392     struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
0393 
0394     if (list_empty(&dma_q->active)) {
0395         au0828_isocdbg("No active queue to serve\n");
0396         dev->isoc_ctl.buf = NULL;
0397         *buf = NULL;
0398         return;
0399     }
0400 
0401     /* Get the next buffer */
0402     *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
0403     /* Cleans up buffer - Useful for testing for frame/URB loss */
0404     list_del(&(*buf)->list);
0405     dma_q->pos = 0;
0406     (*buf)->vb_buf = (*buf)->mem;
0407     dev->isoc_ctl.buf = *buf;
0408 
0409     return;
0410 }
0411 
0412 static void au0828_copy_vbi(struct au0828_dev *dev,
0413                   struct au0828_dmaqueue  *dma_q,
0414                   struct au0828_buffer *buf,
0415                   unsigned char *p,
0416                   unsigned char *outp, unsigned long len)
0417 {
0418     unsigned char *startwrite, *startread;
0419     int bytesperline;
0420     int i, j = 0;
0421 
0422     if (dev == NULL) {
0423         au0828_isocdbg("dev is null\n");
0424         return;
0425     }
0426 
0427     if (dma_q == NULL) {
0428         au0828_isocdbg("dma_q is null\n");
0429         return;
0430     }
0431     if (buf == NULL)
0432         return;
0433     if (p == NULL) {
0434         au0828_isocdbg("p is null\n");
0435         return;
0436     }
0437     if (outp == NULL) {
0438         au0828_isocdbg("outp is null\n");
0439         return;
0440     }
0441 
0442     bytesperline = dev->vbi_width;
0443 
0444     if (dma_q->pos + len > buf->length)
0445         len = buf->length - dma_q->pos;
0446 
0447     startread = p;
0448     startwrite = outp + (dma_q->pos / 2);
0449 
0450     /* Make sure the bottom field populates the second half of the frame */
0451     if (buf->top_field == 0)
0452         startwrite += bytesperline * dev->vbi_height;
0453 
0454     for (i = 0; i < len; i += 2)
0455         startwrite[j++] = startread[i+1];
0456 
0457     dma_q->pos += len;
0458 }
0459 
0460 
0461 /*
0462  * video-buf generic routine to get the next available VBI buffer
0463  */
0464 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
0465                     struct au0828_buffer **buf)
0466 {
0467     struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
0468 
0469     if (list_empty(&dma_q->active)) {
0470         au0828_isocdbg("No active queue to serve\n");
0471         dev->isoc_ctl.vbi_buf = NULL;
0472         *buf = NULL;
0473         return;
0474     }
0475 
0476     /* Get the next buffer */
0477     *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
0478     /* Cleans up buffer - Useful for testing for frame/URB loss */
0479     list_del(&(*buf)->list);
0480     dma_q->pos = 0;
0481     (*buf)->vb_buf = (*buf)->mem;
0482     dev->isoc_ctl.vbi_buf = *buf;
0483     return;
0484 }
0485 
0486 /*
0487  * Controls the isoc copy of each urb packet
0488  */
0489 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
0490 {
0491     struct au0828_buffer    *buf;
0492     struct au0828_buffer    *vbi_buf;
0493     struct au0828_dmaqueue  *dma_q = urb->context;
0494     struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
0495     unsigned char *outp = NULL;
0496     unsigned char *vbioutp = NULL;
0497     int i, len = 0, rc = 1;
0498     unsigned char *p;
0499     unsigned char fbyte;
0500     unsigned int vbi_field_size;
0501     unsigned int remain, lencopy;
0502 
0503     if (!dev)
0504         return 0;
0505 
0506     if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
0507         test_bit(DEV_MISCONFIGURED, &dev->dev_state))
0508         return 0;
0509 
0510     if (urb->status < 0) {
0511         print_err_status(dev, -1, urb->status);
0512         if (urb->status == -ENOENT)
0513             return 0;
0514     }
0515 
0516     buf = dev->isoc_ctl.buf;
0517     if (buf != NULL)
0518         outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
0519 
0520     vbi_buf = dev->isoc_ctl.vbi_buf;
0521     if (vbi_buf != NULL)
0522         vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
0523 
0524     for (i = 0; i < urb->number_of_packets; i++) {
0525         int status = urb->iso_frame_desc[i].status;
0526 
0527         if (status < 0) {
0528             print_err_status(dev, i, status);
0529             if (urb->iso_frame_desc[i].status != -EPROTO)
0530                 continue;
0531         }
0532 
0533         if (urb->iso_frame_desc[i].actual_length <= 0)
0534             continue;
0535 
0536         if (urb->iso_frame_desc[i].actual_length >
0537                         dev->max_pkt_size) {
0538             au0828_isocdbg("packet bigger than packet size");
0539             continue;
0540         }
0541 
0542         p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
0543         fbyte = p[0];
0544         len = urb->iso_frame_desc[i].actual_length - 4;
0545         p += 4;
0546 
0547         if (fbyte & 0x80) {
0548             len -= 4;
0549             p += 4;
0550             au0828_isocdbg("Video frame %s\n",
0551                        (fbyte & 0x40) ? "odd" : "even");
0552             if (fbyte & 0x40) {
0553                 /* VBI */
0554                 if (vbi_buf != NULL)
0555                     buffer_filled(dev, vbi_dma_q, vbi_buf);
0556                 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
0557                 if (vbi_buf == NULL)
0558                     vbioutp = NULL;
0559                 else
0560                     vbioutp = vb2_plane_vaddr(
0561                         &vbi_buf->vb.vb2_buf, 0);
0562 
0563                 /* Video */
0564                 if (buf != NULL)
0565                     buffer_filled(dev, dma_q, buf);
0566                 get_next_buf(dma_q, &buf);
0567                 if (buf == NULL)
0568                     outp = NULL;
0569                 else
0570                     outp = vb2_plane_vaddr(
0571                         &buf->vb.vb2_buf, 0);
0572 
0573                 /* As long as isoc traffic is arriving, keep
0574                    resetting the timer */
0575                 if (dev->vid_timeout_running)
0576                     mod_timer(&dev->vid_timeout,
0577                           jiffies + (HZ / 10));
0578                 if (dev->vbi_timeout_running)
0579                     mod_timer(&dev->vbi_timeout,
0580                           jiffies + (HZ / 10));
0581             }
0582 
0583             if (buf != NULL) {
0584                 if (fbyte & 0x40)
0585                     buf->top_field = 1;
0586                 else
0587                     buf->top_field = 0;
0588             }
0589 
0590             if (vbi_buf != NULL) {
0591                 if (fbyte & 0x40)
0592                     vbi_buf->top_field = 1;
0593                 else
0594                     vbi_buf->top_field = 0;
0595             }
0596 
0597             dev->vbi_read = 0;
0598             vbi_dma_q->pos = 0;
0599             dma_q->pos = 0;
0600         }
0601 
0602         vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
0603         if (dev->vbi_read < vbi_field_size) {
0604             remain  = vbi_field_size - dev->vbi_read;
0605             if (len < remain)
0606                 lencopy = len;
0607             else
0608                 lencopy = remain;
0609 
0610             if (vbi_buf != NULL)
0611                 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
0612                         vbioutp, len);
0613 
0614             len -= lencopy;
0615             p += lencopy;
0616             dev->vbi_read += lencopy;
0617         }
0618 
0619         if (dev->vbi_read >= vbi_field_size && buf != NULL)
0620             au0828_copy_video(dev, dma_q, buf, p, outp, len);
0621     }
0622     return rc;
0623 }
0624 
0625 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
0626 {
0627 #ifdef CONFIG_MEDIA_CONTROLLER
0628     int i;
0629 
0630     for (i = 0; i < AU0828_MAX_INPUT; i++) {
0631         if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
0632             return;
0633         media_device_unregister_entity(&dev->input_ent[i]);
0634     }
0635 #endif
0636 }
0637 
0638 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
0639 {
0640     struct au0828_dev *dev =
0641         container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
0642 
0643     v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
0644     v4l2_device_unregister(&dev->v4l2_dev);
0645     au0828_usb_v4l2_media_release(dev);
0646     au0828_usb_release(dev);
0647 }
0648 
0649 int au0828_v4l2_device_register(struct usb_interface *interface,
0650                 struct au0828_dev *dev)
0651 {
0652     int retval;
0653 
0654     if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
0655         return 0;
0656 
0657     /* Create the v4l2_device */
0658 #ifdef CONFIG_MEDIA_CONTROLLER
0659     dev->v4l2_dev.mdev = dev->media_dev;
0660 #endif
0661     retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
0662     if (retval) {
0663         pr_err("%s() v4l2_device_register failed\n",
0664                __func__);
0665         return retval;
0666     }
0667 
0668     dev->v4l2_dev.release = au0828_usb_v4l2_release;
0669 
0670     /* This control handler will inherit the controls from au8522 */
0671     retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
0672     if (retval) {
0673         pr_err("%s() v4l2_ctrl_handler_init failed\n",
0674                __func__);
0675         return retval;
0676     }
0677     dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
0678 
0679     return 0;
0680 }
0681 
0682 static int queue_setup(struct vb2_queue *vq,
0683                unsigned int *nbuffers, unsigned int *nplanes,
0684                unsigned int sizes[], struct device *alloc_devs[])
0685 {
0686     struct au0828_dev *dev = vb2_get_drv_priv(vq);
0687     unsigned long size = dev->height * dev->bytesperline;
0688 
0689     if (*nplanes)
0690         return sizes[0] < size ? -EINVAL : 0;
0691     *nplanes = 1;
0692     sizes[0] = size;
0693     return 0;
0694 }
0695 
0696 static int
0697 buffer_prepare(struct vb2_buffer *vb)
0698 {
0699     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0700     struct au0828_buffer *buf = container_of(vbuf,
0701                 struct au0828_buffer, vb);
0702     struct au0828_dev    *dev = vb2_get_drv_priv(vb->vb2_queue);
0703 
0704     buf->length = dev->height * dev->bytesperline;
0705 
0706     if (vb2_plane_size(vb, 0) < buf->length) {
0707         pr_err("%s data will not fit into plane (%lu < %lu)\n",
0708             __func__, vb2_plane_size(vb, 0), buf->length);
0709         return -EINVAL;
0710     }
0711     vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
0712     return 0;
0713 }
0714 
0715 static void
0716 buffer_queue(struct vb2_buffer *vb)
0717 {
0718     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0719     struct au0828_buffer    *buf     = container_of(vbuf,
0720                             struct au0828_buffer,
0721                             vb);
0722     struct au0828_dev       *dev     = vb2_get_drv_priv(vb->vb2_queue);
0723     struct au0828_dmaqueue  *vidq    = &dev->vidq;
0724     unsigned long flags = 0;
0725 
0726     buf->mem = vb2_plane_vaddr(vb, 0);
0727     buf->length = vb2_plane_size(vb, 0);
0728 
0729     spin_lock_irqsave(&dev->slock, flags);
0730     list_add_tail(&buf->list, &vidq->active);
0731     spin_unlock_irqrestore(&dev->slock, flags);
0732 }
0733 
0734 static int au0828_i2s_init(struct au0828_dev *dev)
0735 {
0736     /* Enable i2s mode */
0737     au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
0738     return 0;
0739 }
0740 
0741 /*
0742  * Auvitek au0828 analog stream enable
0743  */
0744 static int au0828_analog_stream_enable(struct au0828_dev *d)
0745 {
0746     struct usb_interface *iface;
0747     int ret, h, w;
0748 
0749     dprintk(1, "au0828_analog_stream_enable called\n");
0750 
0751     if (test_bit(DEV_DISCONNECTED, &d->dev_state))
0752         return -ENODEV;
0753 
0754     iface = usb_ifnum_to_if(d->usbdev, 0);
0755     if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
0756         dprintk(1, "Changing intf#0 to alt 5\n");
0757         /* set au0828 interface0 to AS5 here again */
0758         ret = usb_set_interface(d->usbdev, 0, 5);
0759         if (ret < 0) {
0760             pr_info("Au0828 can't set alt setting to 5!\n");
0761             return -EBUSY;
0762         }
0763     }
0764 
0765     h = d->height / 2 + 2;
0766     w = d->width * 2;
0767 
0768     au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
0769     au0828_writereg(d, 0x106, 0x00);
0770     /* set x position */
0771     au0828_writereg(d, 0x110, 0x00);
0772     au0828_writereg(d, 0x111, 0x00);
0773     au0828_writereg(d, 0x114, w & 0xff);
0774     au0828_writereg(d, 0x115, w >> 8);
0775     /* set y position */
0776     au0828_writereg(d, 0x112, 0x00);
0777     au0828_writereg(d, 0x113, 0x00);
0778     au0828_writereg(d, 0x116, h & 0xff);
0779     au0828_writereg(d, 0x117, h >> 8);
0780     au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
0781 
0782     return 0;
0783 }
0784 
0785 static int au0828_analog_stream_disable(struct au0828_dev *d)
0786 {
0787     dprintk(1, "au0828_analog_stream_disable called\n");
0788     au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
0789     return 0;
0790 }
0791 
0792 static void au0828_analog_stream_reset(struct au0828_dev *dev)
0793 {
0794     dprintk(1, "au0828_analog_stream_reset called\n");
0795     au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
0796     mdelay(30);
0797     au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
0798 }
0799 
0800 /*
0801  * Some operations needs to stop current streaming
0802  */
0803 static int au0828_stream_interrupt(struct au0828_dev *dev)
0804 {
0805     dev->stream_state = STREAM_INTERRUPT;
0806     if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
0807         return -ENODEV;
0808     return 0;
0809 }
0810 
0811 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
0812 {
0813     struct au0828_dev *dev = vb2_get_drv_priv(vq);
0814     int rc = 0;
0815 
0816     dprintk(1, "au0828_start_analog_streaming called %d\n",
0817         dev->streaming_users);
0818 
0819     if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0820         dev->frame_count = 0;
0821     else
0822         dev->vbi_frame_count = 0;
0823 
0824     if (dev->streaming_users == 0) {
0825         /* If we were doing ac97 instead of i2s, it would go here...*/
0826         au0828_i2s_init(dev);
0827         rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
0828                    AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
0829                    au0828_isoc_copy);
0830         if (rc < 0) {
0831             pr_info("au0828_init_isoc failed\n");
0832             return rc;
0833         }
0834 
0835         v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
0836 
0837         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
0838             dev->vid_timeout_running = 1;
0839             mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
0840         } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
0841             dev->vbi_timeout_running = 1;
0842             mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
0843         }
0844     }
0845     dev->streaming_users++;
0846     return rc;
0847 }
0848 
0849 static void au0828_stop_streaming(struct vb2_queue *vq)
0850 {
0851     struct au0828_dev *dev = vb2_get_drv_priv(vq);
0852     struct au0828_dmaqueue *vidq = &dev->vidq;
0853     unsigned long flags = 0;
0854 
0855     dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
0856 
0857     if (dev->streaming_users-- == 1) {
0858         au0828_uninit_isoc(dev);
0859         v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
0860     }
0861 
0862     dev->vid_timeout_running = 0;
0863     del_timer_sync(&dev->vid_timeout);
0864 
0865     spin_lock_irqsave(&dev->slock, flags);
0866     if (dev->isoc_ctl.buf != NULL) {
0867         vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
0868                 VB2_BUF_STATE_ERROR);
0869         dev->isoc_ctl.buf = NULL;
0870     }
0871     while (!list_empty(&vidq->active)) {
0872         struct au0828_buffer *buf;
0873 
0874         buf = list_entry(vidq->active.next, struct au0828_buffer, list);
0875         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0876         list_del(&buf->list);
0877     }
0878     spin_unlock_irqrestore(&dev->slock, flags);
0879 }
0880 
0881 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
0882 {
0883     struct au0828_dev *dev = vb2_get_drv_priv(vq);
0884     struct au0828_dmaqueue *vbiq = &dev->vbiq;
0885     unsigned long flags = 0;
0886 
0887     dprintk(1, "au0828_stop_vbi_streaming called %d\n",
0888         dev->streaming_users);
0889 
0890     if (dev->streaming_users-- == 1) {
0891         au0828_uninit_isoc(dev);
0892         v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
0893     }
0894 
0895     spin_lock_irqsave(&dev->slock, flags);
0896     if (dev->isoc_ctl.vbi_buf != NULL) {
0897         vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
0898                 VB2_BUF_STATE_ERROR);
0899         dev->isoc_ctl.vbi_buf = NULL;
0900     }
0901     while (!list_empty(&vbiq->active)) {
0902         struct au0828_buffer *buf;
0903 
0904         buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
0905         list_del(&buf->list);
0906         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0907     }
0908     spin_unlock_irqrestore(&dev->slock, flags);
0909 
0910     dev->vbi_timeout_running = 0;
0911     del_timer_sync(&dev->vbi_timeout);
0912 }
0913 
0914 static const struct vb2_ops au0828_video_qops = {
0915     .queue_setup     = queue_setup,
0916     .buf_prepare     = buffer_prepare,
0917     .buf_queue       = buffer_queue,
0918     .start_streaming = au0828_start_analog_streaming,
0919     .stop_streaming  = au0828_stop_streaming,
0920     .wait_prepare    = vb2_ops_wait_prepare,
0921     .wait_finish     = vb2_ops_wait_finish,
0922 };
0923 
0924 /* ------------------------------------------------------------------
0925    V4L2 interface
0926    ------------------------------------------------------------------*/
0927 /*
0928  * au0828_analog_unregister
0929  * unregister v4l2 devices
0930  */
0931 int au0828_analog_unregister(struct au0828_dev *dev)
0932 {
0933     dprintk(1, "au0828_analog_unregister called\n");
0934 
0935     /* No analog TV */
0936     if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
0937         return 0;
0938 
0939     mutex_lock(&au0828_sysfs_lock);
0940     vb2_video_unregister_device(&dev->vdev);
0941     vb2_video_unregister_device(&dev->vbi_dev);
0942     mutex_unlock(&au0828_sysfs_lock);
0943 
0944     v4l2_device_disconnect(&dev->v4l2_dev);
0945     v4l2_device_put(&dev->v4l2_dev);
0946 
0947     return 1;
0948 }
0949 
0950 /* This function ensures that video frames continue to be delivered even if
0951    the ITU-656 input isn't receiving any data (thereby preventing applications
0952    such as tvtime from hanging) */
0953 static void au0828_vid_buffer_timeout(struct timer_list *t)
0954 {
0955     struct au0828_dev *dev = from_timer(dev, t, vid_timeout);
0956     struct au0828_dmaqueue *dma_q = &dev->vidq;
0957     struct au0828_buffer *buf;
0958     unsigned char *vid_data;
0959     unsigned long flags = 0;
0960 
0961     spin_lock_irqsave(&dev->slock, flags);
0962 
0963     buf = dev->isoc_ctl.buf;
0964     if (buf != NULL) {
0965         vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
0966         memset(vid_data, 0x00, buf->length); /* Blank green frame */
0967         buffer_filled(dev, dma_q, buf);
0968     }
0969     get_next_buf(dma_q, &buf);
0970 
0971     if (dev->vid_timeout_running == 1)
0972         mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
0973 
0974     spin_unlock_irqrestore(&dev->slock, flags);
0975 }
0976 
0977 static void au0828_vbi_buffer_timeout(struct timer_list *t)
0978 {
0979     struct au0828_dev *dev = from_timer(dev, t, vbi_timeout);
0980     struct au0828_dmaqueue *dma_q = &dev->vbiq;
0981     struct au0828_buffer *buf;
0982     unsigned char *vbi_data;
0983     unsigned long flags = 0;
0984 
0985     spin_lock_irqsave(&dev->slock, flags);
0986 
0987     buf = dev->isoc_ctl.vbi_buf;
0988     if (buf != NULL) {
0989         vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
0990         memset(vbi_data, 0x00, buf->length);
0991         buffer_filled(dev, dma_q, buf);
0992     }
0993     vbi_get_next_buf(dma_q, &buf);
0994 
0995     if (dev->vbi_timeout_running == 1)
0996         mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
0997     spin_unlock_irqrestore(&dev->slock, flags);
0998 }
0999 
1000 static int au0828_v4l2_open(struct file *filp)
1001 {
1002     struct au0828_dev *dev = video_drvdata(filp);
1003     int ret;
1004 
1005     dprintk(1,
1006         "%s called std_set %d dev_state %ld stream users %d users %d\n",
1007         __func__, dev->std_set_in_tuner_core, dev->dev_state,
1008         dev->streaming_users, dev->users);
1009 
1010     if (mutex_lock_interruptible(&dev->lock))
1011         return -ERESTARTSYS;
1012 
1013     ret = v4l2_fh_open(filp);
1014     if (ret) {
1015         au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1016                 __func__, ret);
1017         mutex_unlock(&dev->lock);
1018         return ret;
1019     }
1020 
1021     if (dev->users == 0) {
1022         au0828_analog_stream_enable(dev);
1023         au0828_analog_stream_reset(dev);
1024         dev->stream_state = STREAM_OFF;
1025         set_bit(DEV_INITIALIZED, &dev->dev_state);
1026     }
1027     dev->users++;
1028     mutex_unlock(&dev->lock);
1029     return ret;
1030 }
1031 
1032 static int au0828_v4l2_close(struct file *filp)
1033 {
1034     int ret;
1035     struct au0828_dev *dev = video_drvdata(filp);
1036     struct video_device *vdev = video_devdata(filp);
1037 
1038     dprintk(1,
1039         "%s called std_set %d dev_state %ld stream users %d users %d\n",
1040         __func__, dev->std_set_in_tuner_core, dev->dev_state,
1041         dev->streaming_users, dev->users);
1042 
1043     mutex_lock(&dev->lock);
1044     if (vdev->vfl_type == VFL_TYPE_VIDEO && dev->vid_timeout_running) {
1045         /* Cancel timeout thread in case they didn't call streamoff */
1046         dev->vid_timeout_running = 0;
1047         del_timer_sync(&dev->vid_timeout);
1048     } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1049             dev->vbi_timeout_running) {
1050         /* Cancel timeout thread in case they didn't call streamoff */
1051         dev->vbi_timeout_running = 0;
1052         del_timer_sync(&dev->vbi_timeout);
1053     }
1054 
1055     if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1056         goto end;
1057 
1058     if (dev->users == 1) {
1059         /*
1060          * Avoid putting tuner in sleep if DVB or ALSA are
1061          * streaming.
1062          *
1063          * On most USB devices  like au0828 the tuner can
1064          * be safely put in sleep state here if ALSA isn't
1065          * streaming. Exceptions are some very old USB tuner
1066          * models such as em28xx-based WinTV USB2 which have
1067          * a separate audio output jack. The devices that have
1068          * a separate audio output jack have analog tuners,
1069          * like Philips FM1236. Those devices are always on,
1070          * so the s_power callback are silently ignored.
1071          * So, the current logic here does the following:
1072          * Disable (put tuner to sleep) when
1073          * - ALSA and DVB aren't streaming.
1074          * - the last V4L2 file handler is closed.
1075          *
1076          * FIXME:
1077          *
1078          * Additionally, this logic could be improved to
1079          * disable the media source if the above conditions
1080          * are met and if the device:
1081          * - doesn't have a separate audio out plug (or
1082          * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1083          *
1084          * Once this additional logic is in place, a callback
1085          * is needed to enable the media source and power on
1086          * the tuner, for radio to work.
1087         */
1088         ret = v4l_enable_media_source(vdev);
1089         if (ret == 0)
1090             v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
1091                          standby);
1092         dev->std_set_in_tuner_core = 0;
1093 
1094         /* When close the device, set the usb intf0 into alt0 to free
1095            USB bandwidth */
1096         ret = usb_set_interface(dev->usbdev, 0, 0);
1097         if (ret < 0)
1098             pr_info("Au0828 can't set alternate to 0!\n");
1099     }
1100 end:
1101     _vb2_fop_release(filp, NULL);
1102     dev->users--;
1103     mutex_unlock(&dev->lock);
1104     return 0;
1105 }
1106 
1107 /* Must be called with dev->lock held */
1108 static void au0828_init_tuner(struct au0828_dev *dev)
1109 {
1110     struct v4l2_frequency f = {
1111         .frequency = dev->ctrl_freq,
1112         .type = V4L2_TUNER_ANALOG_TV,
1113     };
1114 
1115     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1116         dev->std_set_in_tuner_core, dev->dev_state);
1117 
1118     if (dev->std_set_in_tuner_core)
1119         return;
1120     dev->std_set_in_tuner_core = 1;
1121     i2c_gate_ctrl(dev, 1);
1122     /* If we've never sent the standard in tuner core, do so now.
1123        We don't do this at device probe because we don't want to
1124        incur the cost of a firmware load */
1125     v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1126     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1127     i2c_gate_ctrl(dev, 0);
1128 }
1129 
1130 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1131                  struct v4l2_format *format)
1132 {
1133     int ret;
1134     int width = format->fmt.pix.width;
1135     int height = format->fmt.pix.height;
1136 
1137     /* If they are demanding a format other than the one we support,
1138        bail out (tvtime asks for UYVY and then retries with YUYV) */
1139     if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1140         return -EINVAL;
1141 
1142     /* format->fmt.pix.width only support 720 and height 480 */
1143     if (width != 720)
1144         width = 720;
1145     if (height != 480)
1146         height = 480;
1147 
1148     format->fmt.pix.width = width;
1149     format->fmt.pix.height = height;
1150     format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1151     format->fmt.pix.bytesperline = width * 2;
1152     format->fmt.pix.sizeimage = width * height * 2;
1153     format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1154     format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1155 
1156     if (cmd == VIDIOC_TRY_FMT)
1157         return 0;
1158 
1159     /* maybe set new image format, driver current only support 720*480 */
1160     dev->width = width;
1161     dev->height = height;
1162     dev->frame_size = width * height * 2;
1163     dev->field_size = width * height;
1164     dev->bytesperline = width * 2;
1165 
1166     if (dev->stream_state == STREAM_ON) {
1167         dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1168         ret = au0828_stream_interrupt(dev);
1169         if (ret != 0) {
1170             dprintk(1, "error interrupting video stream!\n");
1171             return ret;
1172         }
1173     }
1174 
1175     au0828_analog_stream_enable(dev);
1176 
1177     return 0;
1178 }
1179 
1180 static int vidioc_querycap(struct file *file, void  *priv,
1181                struct v4l2_capability *cap)
1182 {
1183     struct au0828_dev *dev = video_drvdata(file);
1184 
1185     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1186         dev->std_set_in_tuner_core, dev->dev_state);
1187 
1188     strscpy(cap->driver, "au0828", sizeof(cap->driver));
1189     strscpy(cap->card, dev->board.name, sizeof(cap->card));
1190     usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1191 
1192     /* set the device capabilities */
1193     cap->capabilities =
1194         V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1195         V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1196         V4L2_CAP_DEVICE_CAPS;
1197     return 0;
1198 }
1199 
1200 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1201                     struct v4l2_fmtdesc *f)
1202 {
1203     if (f->index)
1204         return -EINVAL;
1205 
1206     dprintk(1, "%s called\n", __func__);
1207 
1208     f->pixelformat = V4L2_PIX_FMT_UYVY;
1209 
1210     return 0;
1211 }
1212 
1213 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1214                     struct v4l2_format *f)
1215 {
1216     struct au0828_dev *dev = video_drvdata(file);
1217 
1218     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1219         dev->std_set_in_tuner_core, dev->dev_state);
1220 
1221     f->fmt.pix.width = dev->width;
1222     f->fmt.pix.height = dev->height;
1223     f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1224     f->fmt.pix.bytesperline = dev->bytesperline;
1225     f->fmt.pix.sizeimage = dev->frame_size;
1226     f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1227     f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1228     return 0;
1229 }
1230 
1231 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1232                   struct v4l2_format *f)
1233 {
1234     struct au0828_dev *dev = video_drvdata(file);
1235 
1236     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1237         dev->std_set_in_tuner_core, dev->dev_state);
1238 
1239     return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1240 }
1241 
1242 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1243                 struct v4l2_format *f)
1244 {
1245     struct au0828_dev *dev = video_drvdata(file);
1246     int rc;
1247 
1248     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1249         dev->std_set_in_tuner_core, dev->dev_state);
1250 
1251     rc = check_dev(dev);
1252     if (rc < 0)
1253         return rc;
1254 
1255     if (vb2_is_busy(&dev->vb_vidq)) {
1256         pr_info("%s queue busy\n", __func__);
1257         rc = -EBUSY;
1258         goto out;
1259     }
1260 
1261     rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1262 out:
1263     return rc;
1264 }
1265 
1266 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1267 {
1268     struct au0828_dev *dev = video_drvdata(file);
1269 
1270     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1271         dev->std_set_in_tuner_core, dev->dev_state);
1272 
1273     if (norm == dev->std)
1274         return 0;
1275 
1276     if (dev->streaming_users > 0)
1277         return -EBUSY;
1278 
1279     dev->std = norm;
1280 
1281     au0828_init_tuner(dev);
1282 
1283     i2c_gate_ctrl(dev, 1);
1284 
1285     /*
1286      * FIXME: when we support something other than 60Hz standards,
1287      * we are going to have to make the au0828 bridge adjust the size
1288      * of its capture buffer, which is currently hardcoded at 720x480
1289      */
1290 
1291     v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1292 
1293     i2c_gate_ctrl(dev, 0);
1294 
1295     return 0;
1296 }
1297 
1298 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1299 {
1300     struct au0828_dev *dev = video_drvdata(file);
1301 
1302     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1303         dev->std_set_in_tuner_core, dev->dev_state);
1304 
1305     *norm = dev->std;
1306     return 0;
1307 }
1308 
1309 static int vidioc_enum_input(struct file *file, void *priv,
1310                 struct v4l2_input *input)
1311 {
1312     struct au0828_dev *dev = video_drvdata(file);
1313     unsigned int tmp;
1314 
1315     static const char *inames[] = {
1316         [AU0828_VMUX_UNDEFINED] = "Undefined",
1317         [AU0828_VMUX_COMPOSITE] = "Composite",
1318         [AU0828_VMUX_SVIDEO] = "S-Video",
1319         [AU0828_VMUX_CABLE] = "Cable TV",
1320         [AU0828_VMUX_TELEVISION] = "Television",
1321         [AU0828_VMUX_DVB] = "DVB",
1322     };
1323 
1324     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1325         dev->std_set_in_tuner_core, dev->dev_state);
1326 
1327     tmp = input->index;
1328 
1329     if (tmp >= AU0828_MAX_INPUT)
1330         return -EINVAL;
1331     if (AUVI_INPUT(tmp).type == 0)
1332         return -EINVAL;
1333 
1334     input->index = tmp;
1335     strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name));
1336     if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1337         (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1338         input->type |= V4L2_INPUT_TYPE_TUNER;
1339         input->audioset = 1;
1340     } else {
1341         input->type |= V4L2_INPUT_TYPE_CAMERA;
1342         input->audioset = 2;
1343     }
1344 
1345     input->std = dev->vdev.tvnorms;
1346 
1347     return 0;
1348 }
1349 
1350 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1351 {
1352     struct au0828_dev *dev = video_drvdata(file);
1353 
1354     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1355         dev->std_set_in_tuner_core, dev->dev_state);
1356 
1357     *i = dev->ctrl_input;
1358     return 0;
1359 }
1360 
1361 static void au0828_s_input(struct au0828_dev *dev, int index)
1362 {
1363     int i;
1364 
1365     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1366         dev->std_set_in_tuner_core, dev->dev_state);
1367 
1368     switch (AUVI_INPUT(index).type) {
1369     case AU0828_VMUX_SVIDEO:
1370         dev->input_type = AU0828_VMUX_SVIDEO;
1371         dev->ctrl_ainput = 1;
1372         break;
1373     case AU0828_VMUX_COMPOSITE:
1374         dev->input_type = AU0828_VMUX_COMPOSITE;
1375         dev->ctrl_ainput = 1;
1376         break;
1377     case AU0828_VMUX_TELEVISION:
1378         dev->input_type = AU0828_VMUX_TELEVISION;
1379         dev->ctrl_ainput = 0;
1380         break;
1381     default:
1382         dprintk(1, "unknown input type set [%d]\n",
1383             AUVI_INPUT(index).type);
1384         return;
1385     }
1386 
1387     dev->ctrl_input = index;
1388 
1389     v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1390             AUVI_INPUT(index).vmux, 0, 0);
1391 
1392     for (i = 0; i < AU0828_MAX_INPUT; i++) {
1393         int enable = 0;
1394         if (AUVI_INPUT(i).audio_setup == NULL)
1395             continue;
1396 
1397         if (i == index)
1398             enable = 1;
1399         else
1400             enable = 0;
1401         if (enable) {
1402             (AUVI_INPUT(i).audio_setup)(dev, enable);
1403         } else {
1404             /* Make sure we leave it turned on if some
1405                other input is routed to this callback */
1406             if ((AUVI_INPUT(i).audio_setup) !=
1407                 ((AUVI_INPUT(index).audio_setup))) {
1408                 (AUVI_INPUT(i).audio_setup)(dev, enable);
1409             }
1410         }
1411     }
1412 
1413     v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1414             AUVI_INPUT(index).amux, 0, 0);
1415 }
1416 
1417 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1418 {
1419     struct au0828_dev *dev = video_drvdata(file);
1420     struct video_device *vfd = video_devdata(file);
1421 
1422     dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1423         index);
1424     if (index >= AU0828_MAX_INPUT)
1425         return -EINVAL;
1426     if (AUVI_INPUT(index).type == 0)
1427         return -EINVAL;
1428 
1429     if (dev->ctrl_input == index)
1430         return 0;
1431 
1432     au0828_s_input(dev, index);
1433 
1434     /*
1435      * Input has been changed. Disable the media source
1436      * associated with the old input and enable source
1437      * for the newly set input
1438      */
1439     v4l_disable_media_source(vfd);
1440     return v4l_enable_media_source(vfd);
1441 }
1442 
1443 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1444 {
1445     if (a->index > 1)
1446         return -EINVAL;
1447 
1448     dprintk(1, "%s called\n", __func__);
1449 
1450     if (a->index == 0)
1451         strscpy(a->name, "Television", sizeof(a->name));
1452     else
1453         strscpy(a->name, "Line in", sizeof(a->name));
1454 
1455     a->capability = V4L2_AUDCAP_STEREO;
1456     return 0;
1457 }
1458 
1459 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1460 {
1461     struct au0828_dev *dev = video_drvdata(file);
1462 
1463     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1464         dev->std_set_in_tuner_core, dev->dev_state);
1465 
1466     a->index = dev->ctrl_ainput;
1467     if (a->index == 0)
1468         strscpy(a->name, "Television", sizeof(a->name));
1469     else
1470         strscpy(a->name, "Line in", sizeof(a->name));
1471 
1472     a->capability = V4L2_AUDCAP_STEREO;
1473     return 0;
1474 }
1475 
1476 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1477 {
1478     struct au0828_dev *dev = video_drvdata(file);
1479 
1480     if (a->index != dev->ctrl_ainput)
1481         return -EINVAL;
1482 
1483     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1484         dev->std_set_in_tuner_core, dev->dev_state);
1485     return 0;
1486 }
1487 
1488 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1489 {
1490     struct au0828_dev *dev = video_drvdata(file);
1491     struct video_device *vfd = video_devdata(file);
1492     int ret;
1493 
1494     if (t->index != 0)
1495         return -EINVAL;
1496 
1497     ret = v4l_enable_media_source(vfd);
1498     if (ret)
1499         return ret;
1500 
1501     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1502         dev->std_set_in_tuner_core, dev->dev_state);
1503 
1504     strscpy(t->name, "Auvitek tuner", sizeof(t->name));
1505 
1506     au0828_init_tuner(dev);
1507     i2c_gate_ctrl(dev, 1);
1508     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1509     i2c_gate_ctrl(dev, 0);
1510     return 0;
1511 }
1512 
1513 static int vidioc_s_tuner(struct file *file, void *priv,
1514                 const struct v4l2_tuner *t)
1515 {
1516     struct au0828_dev *dev = video_drvdata(file);
1517 
1518     if (t->index != 0)
1519         return -EINVAL;
1520 
1521     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1522         dev->std_set_in_tuner_core, dev->dev_state);
1523 
1524     au0828_init_tuner(dev);
1525     i2c_gate_ctrl(dev, 1);
1526     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1527     i2c_gate_ctrl(dev, 0);
1528 
1529     dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1530         t->afc);
1531 
1532     return 0;
1533 
1534 }
1535 
1536 static int vidioc_g_frequency(struct file *file, void *priv,
1537                 struct v4l2_frequency *freq)
1538 {
1539     struct au0828_dev *dev = video_drvdata(file);
1540 
1541     if (freq->tuner != 0)
1542         return -EINVAL;
1543     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1544         dev->std_set_in_tuner_core, dev->dev_state);
1545     freq->frequency = dev->ctrl_freq;
1546     return 0;
1547 }
1548 
1549 static int vidioc_s_frequency(struct file *file, void *priv,
1550                 const struct v4l2_frequency *freq)
1551 {
1552     struct au0828_dev *dev = video_drvdata(file);
1553     struct v4l2_frequency new_freq = *freq;
1554 
1555     if (freq->tuner != 0)
1556         return -EINVAL;
1557 
1558     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1559         dev->std_set_in_tuner_core, dev->dev_state);
1560 
1561     au0828_init_tuner(dev);
1562     i2c_gate_ctrl(dev, 1);
1563 
1564     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1565     /* Get the actual set (and possibly clamped) frequency */
1566     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1567     dev->ctrl_freq = new_freq.frequency;
1568 
1569     i2c_gate_ctrl(dev, 0);
1570 
1571     au0828_analog_stream_reset(dev);
1572 
1573     return 0;
1574 }
1575 
1576 
1577 /* RAW VBI ioctls */
1578 
1579 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1580                 struct v4l2_format *format)
1581 {
1582     struct au0828_dev *dev = video_drvdata(file);
1583 
1584     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1585         dev->std_set_in_tuner_core, dev->dev_state);
1586 
1587     format->fmt.vbi.samples_per_line = dev->vbi_width;
1588     format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1589     format->fmt.vbi.offset = 0;
1590     format->fmt.vbi.flags = 0;
1591     format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1592 
1593     format->fmt.vbi.count[0] = dev->vbi_height;
1594     format->fmt.vbi.count[1] = dev->vbi_height;
1595     format->fmt.vbi.start[0] = 21;
1596     format->fmt.vbi.start[1] = 284;
1597     memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1598 
1599     return 0;
1600 }
1601 
1602 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1603                 int type, struct v4l2_fract *f)
1604 {
1605     struct au0828_dev *dev = video_drvdata(file);
1606 
1607     if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1608         return -EINVAL;
1609 
1610     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1611         dev->std_set_in_tuner_core, dev->dev_state);
1612 
1613     f->numerator = 54;
1614     f->denominator = 59;
1615 
1616     return 0;
1617 }
1618 
1619 static int vidioc_g_selection(struct file *file, void *priv,
1620                   struct v4l2_selection *s)
1621 {
1622     struct au0828_dev *dev = video_drvdata(file);
1623 
1624     if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1625         return -EINVAL;
1626 
1627     switch (s->target) {
1628     case V4L2_SEL_TGT_CROP_BOUNDS:
1629     case V4L2_SEL_TGT_CROP_DEFAULT:
1630         s->r.left = 0;
1631         s->r.top = 0;
1632         s->r.width = dev->width;
1633         s->r.height = dev->height;
1634         break;
1635     default:
1636         return -EINVAL;
1637     }
1638     return 0;
1639 }
1640 
1641 #ifdef CONFIG_VIDEO_ADV_DEBUG
1642 static int vidioc_g_register(struct file *file, void *priv,
1643                  struct v4l2_dbg_register *reg)
1644 {
1645     struct au0828_dev *dev = video_drvdata(file);
1646 
1647     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1648         dev->std_set_in_tuner_core, dev->dev_state);
1649 
1650     reg->val = au0828_read(dev, reg->reg);
1651     reg->size = 1;
1652     return 0;
1653 }
1654 
1655 static int vidioc_s_register(struct file *file, void *priv,
1656                  const struct v4l2_dbg_register *reg)
1657 {
1658     struct au0828_dev *dev = video_drvdata(file);
1659 
1660     dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1661         dev->std_set_in_tuner_core, dev->dev_state);
1662 
1663     return au0828_writereg(dev, reg->reg, reg->val);
1664 }
1665 #endif
1666 
1667 static int vidioc_log_status(struct file *file, void *fh)
1668 {
1669     struct video_device *vdev = video_devdata(file);
1670 
1671     dprintk(1, "%s called\n", __func__);
1672 
1673     v4l2_ctrl_log_status(file, fh);
1674     v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1675     return 0;
1676 }
1677 
1678 void au0828_v4l2_suspend(struct au0828_dev *dev)
1679 {
1680     struct urb *urb;
1681     int i;
1682 
1683     pr_info("stopping V4L2\n");
1684 
1685     if (dev->stream_state == STREAM_ON) {
1686         pr_info("stopping V4L2 active URBs\n");
1687         au0828_analog_stream_disable(dev);
1688         /* stop urbs */
1689         for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1690             urb = dev->isoc_ctl.urb[i];
1691             if (urb) {
1692                 if (!irqs_disabled())
1693                     usb_kill_urb(urb);
1694                 else
1695                     usb_unlink_urb(urb);
1696             }
1697         }
1698     }
1699 
1700     if (dev->vid_timeout_running)
1701         del_timer_sync(&dev->vid_timeout);
1702     if (dev->vbi_timeout_running)
1703         del_timer_sync(&dev->vbi_timeout);
1704 }
1705 
1706 void au0828_v4l2_resume(struct au0828_dev *dev)
1707 {
1708     int i, rc;
1709 
1710     pr_info("restarting V4L2\n");
1711 
1712     if (dev->stream_state == STREAM_ON) {
1713         au0828_stream_interrupt(dev);
1714         au0828_init_tuner(dev);
1715     }
1716 
1717     if (dev->vid_timeout_running)
1718         mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1719     if (dev->vbi_timeout_running)
1720         mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1721 
1722     /* If we were doing ac97 instead of i2s, it would go here...*/
1723     au0828_i2s_init(dev);
1724 
1725     au0828_analog_stream_enable(dev);
1726 
1727     if (!(dev->stream_state == STREAM_ON)) {
1728         au0828_analog_stream_reset(dev);
1729         /* submit urbs */
1730         for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1731             rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1732             if (rc) {
1733                 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1734                            i, rc);
1735                 au0828_uninit_isoc(dev);
1736             }
1737         }
1738     }
1739 }
1740 
1741 static const struct v4l2_file_operations au0828_v4l_fops = {
1742     .owner      = THIS_MODULE,
1743     .open       = au0828_v4l2_open,
1744     .release    = au0828_v4l2_close,
1745     .read       = vb2_fop_read,
1746     .poll       = vb2_fop_poll,
1747     .mmap       = vb2_fop_mmap,
1748     .unlocked_ioctl = video_ioctl2,
1749 };
1750 
1751 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1752     .vidioc_querycap            = vidioc_querycap,
1753     .vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
1754     .vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
1755     .vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
1756     .vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
1757     .vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1758     .vidioc_try_fmt_vbi_cap     = vidioc_g_fmt_vbi_cap,
1759     .vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1760     .vidioc_enumaudio           = vidioc_enumaudio,
1761     .vidioc_g_audio             = vidioc_g_audio,
1762     .vidioc_s_audio             = vidioc_s_audio,
1763     .vidioc_g_pixelaspect       = vidioc_g_pixelaspect,
1764     .vidioc_g_selection         = vidioc_g_selection,
1765 
1766     .vidioc_reqbufs             = vb2_ioctl_reqbufs,
1767     .vidioc_create_bufs         = vb2_ioctl_create_bufs,
1768     .vidioc_prepare_buf         = vb2_ioctl_prepare_buf,
1769     .vidioc_querybuf            = vb2_ioctl_querybuf,
1770     .vidioc_qbuf                = vb2_ioctl_qbuf,
1771     .vidioc_dqbuf               = vb2_ioctl_dqbuf,
1772     .vidioc_expbuf               = vb2_ioctl_expbuf,
1773 
1774     .vidioc_s_std               = vidioc_s_std,
1775     .vidioc_g_std               = vidioc_g_std,
1776     .vidioc_enum_input          = vidioc_enum_input,
1777     .vidioc_g_input             = vidioc_g_input,
1778     .vidioc_s_input             = vidioc_s_input,
1779 
1780     .vidioc_streamon            = vb2_ioctl_streamon,
1781     .vidioc_streamoff           = vb2_ioctl_streamoff,
1782 
1783     .vidioc_g_tuner             = vidioc_g_tuner,
1784     .vidioc_s_tuner             = vidioc_s_tuner,
1785     .vidioc_g_frequency         = vidioc_g_frequency,
1786     .vidioc_s_frequency         = vidioc_s_frequency,
1787 #ifdef CONFIG_VIDEO_ADV_DEBUG
1788     .vidioc_g_register          = vidioc_g_register,
1789     .vidioc_s_register          = vidioc_s_register,
1790 #endif
1791     .vidioc_log_status      = vidioc_log_status,
1792     .vidioc_subscribe_event     = v4l2_ctrl_subscribe_event,
1793     .vidioc_unsubscribe_event   = v4l2_event_unsubscribe,
1794 };
1795 
1796 static const struct video_device au0828_video_template = {
1797     .fops                       = &au0828_v4l_fops,
1798     .release                    = video_device_release_empty,
1799     .ioctl_ops          = &video_ioctl_ops,
1800     .tvnorms                    = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1801 };
1802 
1803 static int au0828_vb2_setup(struct au0828_dev *dev)
1804 {
1805     int rc;
1806     struct vb2_queue *q;
1807 
1808     /* Setup Videobuf2 for Video capture */
1809     q = &dev->vb_vidq;
1810     q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1811     q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1812     q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1813     q->drv_priv = dev;
1814     q->buf_struct_size = sizeof(struct au0828_buffer);
1815     q->ops = &au0828_video_qops;
1816     q->mem_ops = &vb2_vmalloc_memops;
1817 
1818     rc = vb2_queue_init(q);
1819     if (rc < 0)
1820         return rc;
1821 
1822     /* Setup Videobuf2 for VBI capture */
1823     q = &dev->vb_vbiq;
1824     q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1825     q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1826     q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1827     q->drv_priv = dev;
1828     q->buf_struct_size = sizeof(struct au0828_buffer);
1829     q->ops = &au0828_vbi_qops;
1830     q->mem_ops = &vb2_vmalloc_memops;
1831 
1832     rc = vb2_queue_init(q);
1833     if (rc < 0)
1834         return rc;
1835 
1836     return 0;
1837 }
1838 
1839 static void au0828_analog_create_entities(struct au0828_dev *dev)
1840 {
1841 #if defined(CONFIG_MEDIA_CONTROLLER)
1842     static const char * const inames[] = {
1843         [AU0828_VMUX_COMPOSITE] = "Composite",
1844         [AU0828_VMUX_SVIDEO] = "S-Video",
1845         [AU0828_VMUX_CABLE] = "Cable TV",
1846         [AU0828_VMUX_TELEVISION] = "Television",
1847         [AU0828_VMUX_DVB] = "DVB",
1848     };
1849     int ret, i;
1850 
1851     /* Initialize Video and VBI pads */
1852     dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1853     ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1854     if (ret < 0)
1855         pr_err("failed to initialize video media entity!\n");
1856 
1857     dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1858     ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1859     if (ret < 0)
1860         pr_err("failed to initialize vbi media entity!\n");
1861 
1862     /* Create entities for each input connector */
1863     for (i = 0; i < AU0828_MAX_INPUT; i++) {
1864         struct media_entity *ent = &dev->input_ent[i];
1865 
1866         if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1867             break;
1868 
1869         ent->name = inames[AUVI_INPUT(i).type];
1870         ent->flags = MEDIA_ENT_FL_CONNECTOR;
1871         dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1872 
1873         switch (AUVI_INPUT(i).type) {
1874         case AU0828_VMUX_COMPOSITE:
1875             ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1876             break;
1877         case AU0828_VMUX_SVIDEO:
1878             ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1879             break;
1880         case AU0828_VMUX_CABLE:
1881         case AU0828_VMUX_TELEVISION:
1882         case AU0828_VMUX_DVB:
1883         default: /* Just to shut up a warning */
1884             ent->function = MEDIA_ENT_F_CONN_RF;
1885             break;
1886         }
1887 
1888         ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1889         if (ret < 0)
1890             pr_err("failed to initialize input pad[%d]!\n", i);
1891 
1892         ret = media_device_register_entity(dev->media_dev, ent);
1893         if (ret < 0)
1894             pr_err("failed to register input entity %d!\n", i);
1895     }
1896 #endif
1897 }
1898 
1899 /**************************************************************************/
1900 
1901 int au0828_analog_register(struct au0828_dev *dev,
1902                struct usb_interface *interface)
1903 {
1904     int retval = -ENOMEM;
1905     struct usb_host_interface *iface_desc;
1906     struct usb_endpoint_descriptor *endpoint;
1907     int i, ret;
1908 
1909     dprintk(1, "au0828_analog_register called for intf#%d!\n",
1910         interface->cur_altsetting->desc.bInterfaceNumber);
1911 
1912     /* No analog TV */
1913     if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1914         return 0;
1915 
1916     /* set au0828 usb interface0 to as5 */
1917     retval = usb_set_interface(dev->usbdev,
1918             interface->cur_altsetting->desc.bInterfaceNumber, 5);
1919     if (retval != 0) {
1920         pr_info("Failure setting usb interface0 to as5\n");
1921         return retval;
1922     }
1923 
1924     /* Figure out which endpoint has the isoc interface */
1925     iface_desc = interface->cur_altsetting;
1926     for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1927         endpoint = &iface_desc->endpoint[i].desc;
1928         if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1929              == USB_DIR_IN) &&
1930             ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1931              == USB_ENDPOINT_XFER_ISOC)) {
1932 
1933             /* we find our isoc in endpoint */
1934             u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1935             dev->max_pkt_size = (tmp & 0x07ff) *
1936                 (((tmp & 0x1800) >> 11) + 1);
1937             dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1938             dprintk(1,
1939                 "Found isoc endpoint 0x%02x, max size = %d\n",
1940                 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1941         }
1942     }
1943     if (!(dev->isoc_in_endpointaddr)) {
1944         pr_info("Could not locate isoc endpoint\n");
1945         return -ENODEV;
1946     }
1947 
1948     init_waitqueue_head(&dev->open);
1949     spin_lock_init(&dev->slock);
1950 
1951     /* init video dma queues */
1952     INIT_LIST_HEAD(&dev->vidq.active);
1953     INIT_LIST_HEAD(&dev->vbiq.active);
1954 
1955     timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
1956     timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
1957 
1958     dev->width = NTSC_STD_W;
1959     dev->height = NTSC_STD_H;
1960     dev->field_size = dev->width * dev->height;
1961     dev->frame_size = dev->field_size << 1;
1962     dev->bytesperline = dev->width << 1;
1963     dev->vbi_width = 720;
1964     dev->vbi_height = 1;
1965     dev->ctrl_ainput = 0;
1966     dev->ctrl_freq = 960;
1967     dev->std = V4L2_STD_NTSC_M;
1968     /* Default input is TV Tuner */
1969     au0828_s_input(dev, 0);
1970 
1971     mutex_init(&dev->vb_queue_lock);
1972     mutex_init(&dev->vb_vbi_queue_lock);
1973 
1974     /* Fill the video capture device struct */
1975     dev->vdev = au0828_video_template;
1976     dev->vdev.v4l2_dev = &dev->v4l2_dev;
1977     dev->vdev.lock = &dev->lock;
1978     dev->vdev.queue = &dev->vb_vidq;
1979     dev->vdev.queue->lock = &dev->vb_queue_lock;
1980     dev->vdev.device_caps =
1981         V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1982         V4L2_CAP_TUNER | V4L2_CAP_VIDEO_CAPTURE;
1983     strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name));
1984 
1985     /* Setup the VBI device */
1986     dev->vbi_dev = au0828_video_template;
1987     dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1988     dev->vbi_dev.lock = &dev->lock;
1989     dev->vbi_dev.queue = &dev->vb_vbiq;
1990     dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1991     dev->vbi_dev.device_caps =
1992         V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1993         V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE;
1994     strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name));
1995 
1996     /* Init entities at the Media Controller */
1997     au0828_analog_create_entities(dev);
1998 
1999     /* initialize videobuf2 stuff */
2000     retval = au0828_vb2_setup(dev);
2001     if (retval != 0) {
2002         dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2003             retval);
2004         return -ENODEV;
2005     }
2006 
2007     /* Register the v4l2 device */
2008     video_set_drvdata(&dev->vdev, dev);
2009     retval = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
2010     if (retval != 0) {
2011         dprintk(1, "unable to register video device (error = %d).\n",
2012             retval);
2013         return -ENODEV;
2014     }
2015 
2016     /* Register the vbi device */
2017     video_set_drvdata(&dev->vbi_dev, dev);
2018     retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2019     if (retval != 0) {
2020         dprintk(1, "unable to register vbi device (error = %d).\n",
2021             retval);
2022         ret = -ENODEV;
2023         goto err_reg_vbi_dev;
2024     }
2025 
2026 #ifdef CONFIG_MEDIA_CONTROLLER
2027     retval = v4l2_mc_create_media_graph(dev->media_dev);
2028     if (retval) {
2029         pr_err("%s() au0282_dev_register failed to create graph\n",
2030             __func__);
2031         ret = -ENODEV;
2032         goto err_reg_vbi_dev;
2033     }
2034 #endif
2035 
2036     dprintk(1, "%s completed!\n", __func__);
2037 
2038     return 0;
2039 
2040 err_reg_vbi_dev:
2041     vb2_video_unregister_device(&dev->vdev);
2042     return ret;
2043 }
2044