Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[linux-2.6] / drivers / media / video / v4l2-device.c
1 /*
2     V4L2 device support.
3
4     Copyright (C) 2008  Hans Verkuil <hverkuil@xs4all.nl>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/types.h>
22 #include <linux/ioctl.h>
23 #include <linux/i2c.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-device.h>
26
27 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
28 {
29         if (dev == NULL || v4l2_dev == NULL)
30                 return -EINVAL;
31         /* Warn if we apparently re-register a device */
32         WARN_ON(dev_get_drvdata(dev));
33         INIT_LIST_HEAD(&v4l2_dev->subdevs);
34         spin_lock_init(&v4l2_dev->lock);
35         v4l2_dev->dev = dev;
36         snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
37                         dev->driver->name, dev->bus_id);
38         dev_set_drvdata(dev, v4l2_dev);
39         return 0;
40 }
41 EXPORT_SYMBOL_GPL(v4l2_device_register);
42
43 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
44 {
45         struct v4l2_subdev *sd, *next;
46
47         if (v4l2_dev == NULL || v4l2_dev->dev == NULL)
48                 return;
49         dev_set_drvdata(v4l2_dev->dev, NULL);
50         /* unregister subdevs */
51         list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list)
52                 v4l2_device_unregister_subdev(sd);
53
54         v4l2_dev->dev = NULL;
55 }
56 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
57
58 int v4l2_device_register_subdev(struct v4l2_device *dev, struct v4l2_subdev *sd)
59 {
60         /* Check for valid input */
61         if (dev == NULL || sd == NULL || !sd->name[0])
62                 return -EINVAL;
63         /* Warn if we apparently re-register a subdev */
64         WARN_ON(sd->dev);
65         if (!try_module_get(sd->owner))
66                 return -ENODEV;
67         sd->dev = dev;
68         spin_lock(&dev->lock);
69         list_add_tail(&sd->list, &dev->subdevs);
70         spin_unlock(&dev->lock);
71         return 0;
72 }
73 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
74
75 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
76 {
77         /* return if it isn't registered */
78         if (sd == NULL || sd->dev == NULL)
79                 return;
80         spin_lock(&sd->dev->lock);
81         list_del(&sd->list);
82         spin_unlock(&sd->dev->lock);
83         sd->dev = NULL;
84         module_put(sd->owner);
85 }
86 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);