V4L/DVB (13554a): v4l: Use the video_drvdata function in drivers
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-blackbird.c
index 6c0c94c..f0ef941 100644 (file)
@@ -3,7 +3,7 @@
  *  Support for a cx23416 mpeg encoder via cx2388x host port.
  *  "blackbird" reference design.
  *
- *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
+ *    (c) 2004 Jelle Foks <jelle@foks.us>
  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  *
  *    (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/firmware.h>
+#include <linux/smp_lock.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 #include <media/cx2341x.h>
 
 #include "cx88.h"
 
 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
-MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
+MODULE_AUTHOR("Jelle Foks <jelle@foks.us>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
 MODULE_LICENSE("GPL");
 
 static unsigned int mpegbufs = 32;
@@ -715,7 +717,8 @@ static int vidioc_querymenu (struct file *file, void *priv,
 
        qctrl.id = qmenu->id;
        blackbird_queryctrl(dev, &qctrl);
-       return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
+       return v4l2_ctrl_query_menu(qmenu, &qctrl,
+                       cx2341x_ctrl_get_menu(&dev->params, qmenu->id));
 }
 
 static int vidioc_querycap (struct file *file, void  *priv,
@@ -737,25 +740,23 @@ static int vidioc_querycap (struct file *file, void  *priv,
        return 0;
 }
 
-static int vidioc_enum_fmt_cap (struct file *file, void  *priv,
+static int vidioc_enum_fmt_vid_cap (struct file *file, void  *priv,
                                        struct v4l2_fmtdesc *f)
 {
        if (f->index != 0)
                return -EINVAL;
 
        strlcpy(f->description, "MPEG", sizeof(f->description));
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->pixelformat = V4L2_PIX_FMT_MPEG;
        return 0;
 }
 
-static int vidioc_g_fmt_cap (struct file *file, void *priv,
+static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
                                        struct v4l2_format *f)
 {
        struct cx8802_fh  *fh   = priv;
        struct cx8802_dev *dev  = fh->dev;
 
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
        f->fmt.pix.bytesperline = 0;
        f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
@@ -768,13 +769,12 @@ static int vidioc_g_fmt_cap (struct file *file, void *priv,
        return 0;
 }
 
-static int vidioc_try_fmt_cap (struct file *file, void *priv,
+static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
                        struct v4l2_format *f)
 {
        struct cx8802_fh  *fh   = priv;
        struct cx8802_dev *dev  = fh->dev;
 
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
        f->fmt.pix.bytesperline = 0;
        f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
@@ -784,14 +784,13 @@ static int vidioc_try_fmt_cap (struct file *file, void *priv,
        return 0;
 }
 
-static int vidioc_s_fmt_cap (struct file *file, void *priv,
+static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
                                        struct v4l2_format *f)
 {
        struct cx8802_fh  *fh   = priv;
        struct cx8802_dev *dev  = fh->dev;
        struct cx88_core  *core = dev->core;
 
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
        f->fmt.pix.bytesperline = 0;
        f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
@@ -917,7 +916,7 @@ static int vidioc_log_status (struct file *file, void *priv)
        snprintf(name, sizeof(name), "%s/2", core->name);
        printk("%s/2: ============  START LOG STATUS  ============\n",
                core->name);
-       cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
+       call_all(core, core, log_status);
        cx2341x_log_status(&dev->params, name);
        printk("%s/2: =============  END LOG STATUS  =============\n",
                core->name);
@@ -972,7 +971,7 @@ static int vidioc_g_frequency (struct file *file, void *priv,
 
        f->type = V4L2_TUNER_ANALOG_TV;
        f->frequency = core->freq;
-       cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
+       call_all(core, tuner, g_frequency, f);
 
        return 0;
 }
@@ -1047,20 +1046,17 @@ static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
 
 /* FIXME: cx88_ioctl_hook not implemented */
 
-static int mpeg_open(struct inode *inode, struct file *file)
+static int mpeg_open(struct file *file)
 {
-       int minor = iminor(inode);
-       struct cx8802_dev *dev = NULL;
+       int minor = video_devdata(file)->minor;
+       struct cx8802_dev *dev = video_drvdata(file);
        struct cx8802_fh *fh;
        struct cx8802_driver *drv = NULL;
        int err;
 
-       dev = cx8802_get_device(inode);
-
        dprintk( 1, "%s\n", __func__);
 
-       if (dev == NULL)
-               return -ENODEV;
+       lock_kernel();
 
        /* Make sure we can acquire the hardware */
        drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
@@ -1068,13 +1064,15 @@ static int mpeg_open(struct inode *inode, struct file *file)
                err = drv->request_acquire(drv);
                if(err != 0) {
                        dprintk(1,"%s: Unable to acquire hardware, %d\n", __func__, err);
+                       unlock_kernel();
                        return err;
                }
        }
 
-       if (blackbird_initialize_codec(dev) < 0) {
+       if (!atomic_read(&dev->core->mpeg_users) && blackbird_initialize_codec(dev) < 0) {
                if (drv)
                        drv->request_release(drv);
+               unlock_kernel();
                return -EINVAL;
        }
        dprintk(1,"open minor=%d\n",minor);
@@ -1084,6 +1082,7 @@ static int mpeg_open(struct inode *inode, struct file *file)
        if (NULL == fh) {
                if (drv)
                        drv->request_release(drv);
+               unlock_kernel();
                return -ENOMEM;
        }
        file->private_data = fh;
@@ -1099,17 +1098,20 @@ static int mpeg_open(struct inode *inode, struct file *file)
        /* FIXME: locking against other video device */
        cx88_set_scale(dev->core, dev->width, dev->height,
                        fh->mpegq.field);
+       unlock_kernel();
+
+       atomic_inc(&dev->core->mpeg_users);
 
        return 0;
 }
 
-static int mpeg_release(struct inode *inode, struct file *file)
+static int mpeg_release(struct file *file)
 {
        struct cx8802_fh  *fh  = file->private_data;
        struct cx8802_dev *dev = fh->dev;
        struct cx8802_driver *drv = NULL;
 
-       if (dev->mpeg_active)
+       if (dev->mpeg_active && atomic_read(&dev->core->mpeg_users) == 1)
                blackbird_stop_codec(dev);
 
        cx8802_cancel_buffers(fh->dev);
@@ -1121,14 +1123,12 @@ static int mpeg_release(struct inode *inode, struct file *file)
        kfree(fh);
 
        /* Make sure we release the hardware */
-       dev = cx8802_get_device(inode);
-       if (dev == NULL)
-               return -ENODEV;
-
        drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
        if (drv)
                drv->request_release(drv);
 
+       atomic_dec(&dev->core->mpeg_users);
+
        return 0;
 }
 
@@ -1149,6 +1149,10 @@ static unsigned int
 mpeg_poll(struct file *file, struct poll_table_struct *wait)
 {
        struct cx8802_fh *fh = file->private_data;
+       struct cx8802_dev *dev = fh->dev;
+
+       if (!dev->mpeg_active)
+               blackbird_start_codec(file, fh);
 
        return videobuf_poll_stream(file, &fh->mpegq, wait);
 }
@@ -1161,7 +1165,7 @@ mpeg_mmap(struct file *file, struct vm_area_struct * vma)
        return videobuf_mmap_mapper(&fh->mpegq, vma);
 }
 
-static const struct file_operations mpeg_fops =
+static const struct v4l2_file_operations mpeg_fops =
 {
        .owner         = THIS_MODULE,
        .open          = mpeg_open,
@@ -1170,21 +1174,15 @@ static const struct file_operations mpeg_fops =
        .poll          = mpeg_poll,
        .mmap          = mpeg_mmap,
        .ioctl         = video_ioctl2,
-       .llseek        = no_llseek,
 };
 
-static struct video_device cx8802_mpeg_template =
-{
-       .name                 = "cx8802",
-       .type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
-       .fops                 = &mpeg_fops,
-       .minor                = -1,
+static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
        .vidioc_querymenu     = vidioc_querymenu,
        .vidioc_querycap      = vidioc_querycap,
-       .vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
-       .vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
-       .vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
-       .vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
+       .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
+       .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
+       .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
+       .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
        .vidioc_reqbufs       = vidioc_reqbufs,
        .vidioc_querybuf      = vidioc_querybuf,
        .vidioc_qbuf          = vidioc_qbuf,
@@ -1206,6 +1204,13 @@ static struct video_device cx8802_mpeg_template =
        .vidioc_g_tuner       = vidioc_g_tuner,
        .vidioc_s_tuner       = vidioc_s_tuner,
        .vidioc_s_std         = vidioc_s_std,
+};
+
+static struct video_device cx8802_mpeg_template = {
+       .name                 = "cx8802",
+       .fops                 = &mpeg_fops,
+       .ioctl_ops            = &mpeg_ioctl_ops,
+       .minor                = -1,
        .tvnorms              = CX88_NORMS,
        .current_norm         = V4L2_STD_NTSC_M,
 };
@@ -1225,8 +1230,16 @@ static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
                 * We're being given access to re-arrange the GPIOs.
                 * Take the bus off the cx22702 and put the cx23416 on it.
                 */
-               cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
-               cx_set(MO_GP0_IO,   0x00000004); /* Disable the cx22702 */
+               /* Toggle reset on cx22702 leaving i2c active */
+               cx_set(MO_GP0_IO, 0x00000080);
+               udelay(1000);
+               cx_clear(MO_GP0_IO, 0x00000080);
+               udelay(50);
+               cx_set(MO_GP0_IO, 0x00000080);
+               udelay(1000);
+               /* tri-state the cx22702 pins */
+               cx_set(MO_GP0_IO, 0x00000004);
+               udelay(1000);
                break;
        default:
                err = -ENODEV;
@@ -1253,7 +1266,7 @@ static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
 static void blackbird_unregister_video(struct cx8802_dev *dev)
 {
        if (dev->mpeg_dev) {
-               if (-1 != dev->mpeg_dev->minor)
+               if (video_is_registered(dev->mpeg_dev))
                        video_unregister_device(dev->mpeg_dev);
                else
                        video_device_release(dev->mpeg_dev);
@@ -1267,14 +1280,15 @@ static int blackbird_register_video(struct cx8802_dev *dev)
 
        dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
                                       &cx8802_mpeg_template,"mpeg");
+       video_set_drvdata(dev->mpeg_dev, dev);
        err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
        if (err < 0) {
                printk(KERN_INFO "%s/2: can't register mpeg device\n",
                       dev->core->name);
                return err;
        }
-       printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
-              dev->core->name,dev->mpeg_dev->minor & 0x1f);
+       printk(KERN_INFO "%s/2: registered device %s [mpeg]\n",
+              dev->core->name, video_device_node_name(dev->mpeg_dev));
        return 0;
 }
 
@@ -1348,7 +1362,7 @@ static struct cx8802_driver cx8802_blackbird_driver = {
        .advise_release = cx8802_blackbird_advise_release,
 };
 
-static int blackbird_init(void)
+static int __init blackbird_init(void)
 {
        printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
               (CX88_VERSION_CODE >> 16) & 0xff,
@@ -1361,7 +1375,7 @@ static int blackbird_init(void)
        return cx8802_register_driver(&cx8802_blackbird_driver);
 }
 
-static void blackbird_fini(void)
+static void __exit blackbird_fini(void)
 {
        cx8802_unregister_driver(&cx8802_blackbird_driver);
 }