include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / cafe_ccic.c
index 600dde3..be35e69 100644 (file)
  *
  * Written by Jonathan Corbet, corbet@lwn.net.
  *
+ * v4l2_device/v4l2_subdev conversion by:
+ * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
+ *
+ * Note: this conversion is untested! Please contact the linux-media
+ * mailinglist if you can test this, together with the test results.
+ *
  * This file may be distributed under the terms of the GNU General
  * Public License, version 2.
  */
@@ -25,6 +31,7 @@
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
 #include <linux/videodev2.h>
+#include <linux/slab.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-chip-ident.h>
@@ -33,7 +40,6 @@
 #include <linux/list.h>
 #include <linux/dma-mapping.h>
 #include <linux/delay.h>
-#include <linux/debugfs.h>
 #include <linux/jiffies.h>
 #include <linux/vmalloc.h>
 
@@ -148,7 +154,8 @@ struct cafe_camera
        struct pci_dev *pdev;
        struct video_device vdev;
        struct i2c_adapter i2c_adapter;
-       struct i2c_client *sensor;
+       struct v4l2_subdev *sensor;
+       unsigned short sensor_addr;
 
        unsigned char __iomem *regs;
        struct list_head dev_list;      /* link to other devices */
@@ -181,10 +188,6 @@ struct cafe_camera
        /* Misc */
        wait_queue_head_t smbus_wait;   /* Waiting on i2c events */
        wait_queue_head_t iowait;       /* Waiting on frame data */
-#ifdef CONFIG_VIDEO_ADV_DEBUG
-       struct dentry *dfs_regs;
-       struct dentry *dfs_cam_regs;
-#endif
 };
 
 /*
@@ -196,6 +199,8 @@ struct cafe_camera
 #define CF_DMA_ACTIVE   3      /* A frame is incoming */
 #define CF_CONFIG_NEEDED 4     /* Must configure hardware */
 
+#define sensor_call(cam, o, f, args...) \
+       v4l2_subdev_call(cam->sensor, o, f, ##args)
 
 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
 {
@@ -440,14 +445,6 @@ static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
        int ret = -EINVAL;
 
        /*
-        * Refuse to talk to anything but OV cam chips.  We should
-        * never even see an attempt to do so, but one never knows.
-        */
-       if (cam->sensor && addr != cam->sensor->addr) {
-               cam_err(cam, "funky smbus addr %d\n", addr);
-               return -EINVAL;
-       }
-       /*
         * This interface would appear to only do byte data ops.  OK
         * it can do word too, but the cam chip has no use for that.
         */
@@ -485,50 +482,16 @@ static struct i2c_algorithm cafe_smbus_algo = {
 };
 
 /* Somebody is on the bus */
-static int cafe_cam_init(struct cafe_camera *cam);
 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
 static void cafe_ctlr_power_down(struct cafe_camera *cam);
 
-static int cafe_smbus_attach(struct i2c_client *client)
-{
-       struct v4l2_device *v4l2_dev = i2c_get_adapdata(client->adapter);
-       struct cafe_camera *cam = to_cam(v4l2_dev);
-
-       /*
-        * Don't talk to chips we don't recognize.
-        */
-       if (client->driver->id == I2C_DRIVERID_OV7670) {
-               cam->sensor = client;
-               return cafe_cam_init(cam);
-       }
-       return -EINVAL;
-}
-
-static int cafe_smbus_detach(struct i2c_client *client)
-{
-       struct v4l2_device *v4l2_dev = i2c_get_adapdata(client->adapter);
-       struct cafe_camera *cam = to_cam(v4l2_dev);
-
-       if (cam->sensor == client) {
-               cafe_ctlr_stop_dma(cam);
-               cafe_ctlr_power_down(cam);
-               cam_err(cam, "lost the sensor!\n");
-               cam->sensor = NULL;  /* Bummer, no camera */
-               cam->state = S_NOTREADY;
-       }
-       return 0;
-}
-
 static int cafe_smbus_setup(struct cafe_camera *cam)
 {
        struct i2c_adapter *adap = &cam->i2c_adapter;
        int ret;
 
        cafe_smbus_enable_irq(cam);
-       adap->id = I2C_HW_SMBUS_CAFE;
        adap->owner = THIS_MODULE;
-       adap->client_register = cafe_smbus_attach;
-       adap->client_unregister = cafe_smbus_detach;
        adap->algo = &cafe_smbus_algo;
        strcpy(adap->name, "cafe_ccic");
        adap->dev.parent = &cam->pdev->dev;
@@ -790,23 +753,9 @@ static void cafe_ctlr_power_down(struct cafe_camera *cam)
  * Communications with the sensor.
  */
 
-static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
-{
-       struct i2c_client *sc = cam->sensor;
-       int ret;
-
-       if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
-               return -EINVAL;
-       ret = sc->driver->command(sc, cmd, arg);
-       if (ret == -EPERM) /* Unsupported command */
-               return 0;
-       return ret;
-}
-
 static int __cafe_cam_reset(struct cafe_camera *cam)
 {
-       int zero = 0;
-       return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
+       return sensor_call(cam, core, reset, 0);
 }
 
 /*
@@ -825,15 +774,15 @@ static int cafe_cam_init(struct cafe_camera *cam)
        ret = __cafe_cam_reset(cam);
        if (ret)
                goto out;
+       chip.ident = V4L2_IDENT_NONE;
        chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
-       chip.match.addr = cam->sensor->addr;
-       ret = __cafe_cam_cmd(cam, VIDIOC_DBG_G_CHIP_IDENT, &chip);
+       chip.match.addr = cam->sensor_addr;
+       ret = sensor_call(cam, core, g_chip_ident, &chip);
        if (ret)
                goto out;
        cam->sensor_type = chip.ident;
-/*     if (cam->sensor->addr != OV7xx0_SID) { */
        if (cam->sensor_type != V4L2_IDENT_OV7670) {
-               cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
+               cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
                ret = -EINVAL;
                goto out;
        }
@@ -857,21 +806,21 @@ static int cafe_cam_set_flip(struct cafe_camera *cam)
        memset(&ctrl, 0, sizeof(ctrl));
        ctrl.id = V4L2_CID_VFLIP;
        ctrl.value = flip;
-       return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
+       return sensor_call(cam, core, s_ctrl, &ctrl);
 }
 
 
 static int cafe_cam_configure(struct cafe_camera *cam)
 {
        struct v4l2_format fmt;
-       int ret, zero = 0;
+       int ret;
 
        if (cam->state != S_IDLE)
                return -EINVAL;
        fmt.fmt.pix = cam->pix_format;
-       ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
+       ret = sensor_call(cam, core, init, 0);
        if (ret == 0)
-               ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
+               ret = sensor_call(cam, video, s_fmt, &fmt);
        /*
         * OV7670 does weird things if flip is set *before* format...
         */
@@ -1203,8 +1152,6 @@ static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
         * Make sure it's something we can do.  User pointers could be
         * implemented without great pain, but that's not been done yet.
         */
-       if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-               return -EINVAL;
        if (req->memory != V4L2_MEMORY_MMAP)
                return -EINVAL;
        /*
@@ -1268,9 +1215,7 @@ static int cafe_vidioc_querybuf(struct file *filp, void *priv,
        int ret = -EINVAL;
 
        mutex_lock(&cam->s_mutex);
-       if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-               goto out;
-       if (buf->index < 0 || buf->index >= cam->n_sbufs)
+       if (buf->index >= cam->n_sbufs)
                goto out;
        *buf = cam->sb_bufs[buf->index].v4lbuf;
        ret = 0;
@@ -1288,9 +1233,7 @@ static int cafe_vidioc_qbuf(struct file *filp, void *priv,
        unsigned long flags;
 
        mutex_lock(&cam->s_mutex);
-       if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-               goto out;
-       if (buf->index < 0 || buf->index >= cam->n_sbufs)
+       if (buf->index >= cam->n_sbufs)
                goto out;
        sbuf = cam->sb_bufs + buf->index;
        if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
@@ -1321,8 +1264,6 @@ static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
        unsigned long flags;
 
        mutex_lock(&cam->s_mutex);
-       if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-               goto out_unlock;
        if (cam->state != S_STREAMING)
                goto out_unlock;
        if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
@@ -1385,7 +1326,7 @@ static void cafe_v4l_vm_close(struct vm_area_struct *vma)
        mutex_unlock(&sbuf->cam->s_mutex);
 }
 
-static struct vm_operations_struct cafe_v4l_vm_ops = {
+static const struct vm_operations_struct cafe_v4l_vm_ops = {
        .open = cafe_v4l_vm_open,
        .close = cafe_v4l_vm_close
 };
@@ -1490,7 +1431,7 @@ static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
        int ret;
 
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
+       ret = sensor_call(cam, core, queryctrl, qc);
        mutex_unlock(&cam->s_mutex);
        return ret;
 }
@@ -1503,7 +1444,7 @@ static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
        int ret;
 
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
+       ret = sensor_call(cam, core, g_ctrl, ctrl);
        mutex_unlock(&cam->s_mutex);
        return ret;
 }
@@ -1516,7 +1457,7 @@ static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
        int ret;
 
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
+       ret = sensor_call(cam, core, s_ctrl, ctrl);
        mutex_unlock(&cam->s_mutex);
        return ret;
 }
@@ -1555,10 +1496,8 @@ static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
        struct cafe_camera *cam = priv;
        int ret;
 
-       if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-               return -EINVAL;
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
+       ret = sensor_call(cam, video, enum_fmt, fmt);
        mutex_unlock(&cam->s_mutex);
        return ret;
 }
@@ -1571,7 +1510,7 @@ static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
        int ret;
 
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
+       ret = sensor_call(cam, video, try_fmt, fmt);
        mutex_unlock(&cam->s_mutex);
        return ret;
 }
@@ -1680,7 +1619,7 @@ static int cafe_vidioc_g_parm(struct file *filp, void *priv,
        int ret;
 
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
+       ret = sensor_call(cam, video, g_parm, parms);
        mutex_unlock(&cam->s_mutex);
        parms->parm.capture.readbuffers = n_dma_bufs;
        return ret;
@@ -1693,12 +1632,53 @@ static int cafe_vidioc_s_parm(struct file *filp, void *priv,
        int ret;
 
        mutex_lock(&cam->s_mutex);
-       ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
+       ret = sensor_call(cam, video, s_parm, parms);
        mutex_unlock(&cam->s_mutex);
        parms->parm.capture.readbuffers = n_dma_bufs;
        return ret;
 }
 
+static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
+               struct v4l2_dbg_chip_ident *chip)
+{
+       struct cafe_camera *cam = priv;
+
+       chip->ident = V4L2_IDENT_NONE;
+       chip->revision = 0;
+       if (v4l2_chip_match_host(&chip->match)) {
+               chip->ident = V4L2_IDENT_CAFE;
+               return 0;
+       }
+       return sensor_call(cam, core, g_chip_ident, chip);
+}
+
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+static int cafe_vidioc_g_register(struct file *file, void *priv,
+               struct v4l2_dbg_register *reg)
+{
+       struct cafe_camera *cam = priv;
+
+       if (v4l2_chip_match_host(&reg->match)) {
+               reg->val = cafe_reg_read(cam, reg->reg);
+               reg->size = 4;
+               return 0;
+       }
+       return sensor_call(cam, core, g_register, reg);
+}
+
+static int cafe_vidioc_s_register(struct file *file, void *priv,
+               struct v4l2_dbg_register *reg)
+{
+       struct cafe_camera *cam = priv;
+
+       if (v4l2_chip_match_host(&reg->match)) {
+               cafe_reg_write(cam, reg->reg, reg->val);
+               return 0;
+       }
+       return sensor_call(cam, core, s_register, reg);
+}
+#endif
+
 /*
  * This template device holds all of those v4l2 methods; we
  * clone it for specific real devices.
@@ -1735,11 +1715,15 @@ static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
        .vidioc_s_ctrl          = cafe_vidioc_s_ctrl,
        .vidioc_g_parm          = cafe_vidioc_g_parm,
        .vidioc_s_parm          = cafe_vidioc_s_parm,
+       .vidioc_g_chip_ident    = cafe_vidioc_g_chip_ident,
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+       .vidioc_g_register      = cafe_vidioc_g_register,
+       .vidioc_s_register      = cafe_vidioc_s_register,
+#endif
 };
 
 static struct video_device cafe_v4l_template = {
        .name = "cafe",
-       .minor = -1, /* Get one dynamically */
        .tvnorms = V4L2_STD_NTSC_M,
        .current_norm = V4L2_STD_NTSC_M,  /* make mplayer happy */
 
@@ -1902,127 +1886,6 @@ static irqreturn_t cafe_irq(int irq, void *data)
 
 
 /* -------------------------------------------------------------------------- */
-#ifdef CONFIG_VIDEO_ADV_DEBUG
-/*
- * Debugfs stuff.
- */
-
-static char cafe_debug_buf[1024];
-static struct dentry *cafe_dfs_root;
-
-static void cafe_dfs_setup(void)
-{
-       cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
-       if (IS_ERR(cafe_dfs_root)) {
-               cafe_dfs_root = NULL;  /* Never mind */
-               printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
-       }
-}
-
-static void cafe_dfs_shutdown(void)
-{
-       if (cafe_dfs_root)
-               debugfs_remove(cafe_dfs_root);
-}
-
-static int cafe_dfs_open(struct inode *inode, struct file *file)
-{
-       file->private_data = inode->i_private;
-       return 0;
-}
-
-static ssize_t cafe_dfs_read_regs(struct file *file,
-               char __user *buf, size_t count, loff_t *ppos)
-{
-       struct cafe_camera *cam = file->private_data;
-       char *s = cafe_debug_buf;
-       int offset;
-
-       for (offset = 0; offset < 0x44; offset += 4)
-               s += sprintf(s, "%02x: %08x\n", offset,
-                               cafe_reg_read(cam, offset));
-       for (offset = 0x88; offset <= 0x90; offset += 4)
-               s += sprintf(s, "%02x: %08x\n", offset,
-                               cafe_reg_read(cam, offset));
-       for (offset = 0xb4; offset <= 0xbc; offset += 4)
-               s += sprintf(s, "%02x: %08x\n", offset,
-                               cafe_reg_read(cam, offset));
-       for (offset = 0x3000; offset <= 0x300c; offset += 4)
-               s += sprintf(s, "%04x: %08x\n", offset,
-                               cafe_reg_read(cam, offset));
-       return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
-                       s - cafe_debug_buf);
-}
-
-static const struct file_operations cafe_dfs_reg_ops = {
-       .owner = THIS_MODULE,
-       .read = cafe_dfs_read_regs,
-       .open = cafe_dfs_open
-};
-
-static ssize_t cafe_dfs_read_cam(struct file *file,
-               char __user *buf, size_t count, loff_t *ppos)
-{
-       struct cafe_camera *cam = file->private_data;
-       char *s = cafe_debug_buf;
-       int offset;
-
-       if (! cam->sensor)
-               return -EINVAL;
-       for (offset = 0x0; offset < 0x8a; offset++)
-       {
-               u8 v;
-
-               cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
-               s += sprintf(s, "%02x: %02x\n", offset, v);
-       }
-       return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
-                       s - cafe_debug_buf);
-}
-
-static const struct file_operations cafe_dfs_cam_ops = {
-       .owner = THIS_MODULE,
-       .read = cafe_dfs_read_cam,
-       .open = cafe_dfs_open
-};
-
-
-
-static void cafe_dfs_cam_setup(struct cafe_camera *cam)
-{
-       char fname[40];
-
-       if (!cafe_dfs_root)
-               return;
-       sprintf(fname, "regs-%d", cam->vdev.num);
-       cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
-                       cam, &cafe_dfs_reg_ops);
-       sprintf(fname, "cam-%d", cam->vdev.num);
-       cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
-                       cam, &cafe_dfs_cam_ops);
-}
-
-
-static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
-{
-       if (! IS_ERR(cam->dfs_regs))
-               debugfs_remove(cam->dfs_regs);
-       if (! IS_ERR(cam->dfs_cam_regs))
-               debugfs_remove(cam->dfs_cam_regs);
-}
-
-#else
-
-#define cafe_dfs_setup()
-#define cafe_dfs_shutdown()
-#define cafe_dfs_cam_setup(cam)
-#define cafe_dfs_cam_shutdown(cam)
-#endif    /* CONFIG_VIDEO_ADV_DEBUG */
-
-
-
-
-/* ------------------------------------------------------------------------*/
 /*
  * PCI interface stuff.
  */
@@ -2045,7 +1908,6 @@ static int cafe_pci_probe(struct pci_dev *pdev,
                goto out_free;
 
        mutex_init(&cam->s_mutex);
-       mutex_lock(&cam->s_mutex);
        spin_lock_init(&cam->dev_lock);
        cam->state = S_NOTREADY;
        cafe_set_config_needed(cam, 1);
@@ -2085,10 +1947,21 @@ static int cafe_pci_probe(struct pci_dev *pdev,
         * because the sensor could attach in this call chain, leading to
         * unsightly deadlocks.
         */
-       mutex_unlock(&cam->s_mutex);  /* attach can deadlock */
        ret = cafe_smbus_setup(cam);
        if (ret)
                goto out_freeirq;
+
+       cam->sensor_addr = 0x42;
+       cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
+                       "ov7670", "ov7670", cam->sensor_addr, NULL);
+       if (cam->sensor == NULL) {
+               ret = -ENODEV;
+               goto out_smbus;
+       }
+       ret = cafe_cam_init(cam);
+       if (ret)
+               goto out_smbus;
+
        /*
         * Get the v4l2 setup done.
         */
@@ -2099,7 +1972,7 @@ static int cafe_pci_probe(struct pci_dev *pdev,
        cam->vdev.v4l2_dev = &cam->v4l2_dev;
        ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
        if (ret)
-               goto out_smbus;
+               goto out_unlock;
        video_set_drvdata(&cam->vdev, cam);
 
        /*
@@ -2111,10 +1984,11 @@ static int cafe_pci_probe(struct pci_dev *pdev,
                                        " will try again later.");
        }
 
-       cafe_dfs_cam_setup(cam);
        mutex_unlock(&cam->s_mutex);
        return 0;
 
+out_unlock:
+       mutex_unlock(&cam->s_mutex);
 out_smbus:
        cafe_smbus_shutdown(cam);
 out_freeirq:
@@ -2137,7 +2011,6 @@ out:
 static void cafe_shutdown(struct cafe_camera *cam)
 {
 /* FIXME: Make sure we take care of everything here */
-       cafe_dfs_cam_shutdown(cam);
        if (cam->n_sbufs > 0)
                /* What if they are still mapped?  Shouldn't be, but... */
                cafe_free_sio_buffers(cam);
@@ -2257,13 +2130,11 @@ static int __init cafe_init(void)
 
        printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
                        CAFE_VERSION);
-       cafe_dfs_setup();
        ret = pci_register_driver(&cafe_pci_driver);
        if (ret) {
                printk(KERN_ERR "Unable to register cafe_ccic driver\n");
                goto out;
        }
-       request_module("ov7670");  /* FIXME want something more general */
        ret = 0;
 
   out:
@@ -2274,7 +2145,6 @@ static int __init cafe_init(void)
 static void __exit cafe_exit(void)
 {
        pci_unregister_driver(&cafe_pci_driver);
-       cafe_dfs_shutdown();
 }
 
 module_init(cafe_init);