[media] v4l: Add a media_device pointer to the v4l2_device structure
[pandora-kernel.git] / 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 #if defined(CONFIG_SPI)
25 #include <linux/spi/spi.h>
26 #endif
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
30
31 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
32 {
33         if (v4l2_dev == NULL)
34                 return -EINVAL;
35
36         INIT_LIST_HEAD(&v4l2_dev->subdevs);
37         spin_lock_init(&v4l2_dev->lock);
38         mutex_init(&v4l2_dev->ioctl_lock);
39         v4l2_dev->dev = dev;
40         if (dev == NULL) {
41                 /* If dev == NULL, then name must be filled in by the caller */
42                 WARN_ON(!v4l2_dev->name[0]);
43                 return 0;
44         }
45
46         /* Set name to driver name + device name if it is empty. */
47         if (!v4l2_dev->name[0])
48                 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
49                         dev->driver->name, dev_name(dev));
50         if (!dev_get_drvdata(dev))
51                 dev_set_drvdata(dev, v4l2_dev);
52         return 0;
53 }
54 EXPORT_SYMBOL_GPL(v4l2_device_register);
55
56 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
57                                                 atomic_t *instance)
58 {
59         int num = atomic_inc_return(instance) - 1;
60         int len = strlen(basename);
61
62         if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
63                 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
64                                 "%s-%d", basename, num);
65         else
66                 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
67                                 "%s%d", basename, num);
68         return num;
69 }
70 EXPORT_SYMBOL_GPL(v4l2_device_set_name);
71
72 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
73 {
74         if (v4l2_dev->dev == NULL)
75                 return;
76
77         if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
78                 dev_set_drvdata(v4l2_dev->dev, NULL);
79         v4l2_dev->dev = NULL;
80 }
81 EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
82
83 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
84 {
85         struct v4l2_subdev *sd, *next;
86
87         if (v4l2_dev == NULL)
88                 return;
89         v4l2_device_disconnect(v4l2_dev);
90
91         /* Unregister subdevs */
92         list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
93                 v4l2_device_unregister_subdev(sd);
94 #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
95                 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
96                         struct i2c_client *client = v4l2_get_subdevdata(sd);
97
98                         /* We need to unregister the i2c client explicitly.
99                            We cannot rely on i2c_del_adapter to always
100                            unregister clients for us, since if the i2c bus
101                            is a platform bus, then it is never deleted. */
102                         if (client)
103                                 i2c_unregister_device(client);
104                         continue;
105                 }
106 #endif
107 #if defined(CONFIG_SPI)
108                 if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
109                         struct spi_device *spi = v4l2_get_subdevdata(sd);
110
111                         if (spi)
112                                 spi_unregister_device(spi);
113                         continue;
114                 }
115 #endif
116         }
117 }
118 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
119
120 int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
121                                                 struct v4l2_subdev *sd)
122 {
123         int err;
124
125         /* Check for valid input */
126         if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
127                 return -EINVAL;
128
129         /* Warn if we apparently re-register a subdev */
130         WARN_ON(sd->v4l2_dev != NULL);
131
132         if (!try_module_get(sd->owner))
133                 return -ENODEV;
134
135         sd->v4l2_dev = v4l2_dev;
136         if (sd->internal_ops && sd->internal_ops->registered) {
137                 err = sd->internal_ops->registered(sd);
138                 if (err)
139                         return err;
140         }
141
142         /* This just returns 0 if either of the two args is NULL */
143         err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
144         if (err) {
145                 if (sd->internal_ops && sd->internal_ops->unregistered)
146                         sd->internal_ops->unregistered(sd);
147                 return err;
148         }
149
150         spin_lock(&v4l2_dev->lock);
151         list_add_tail(&sd->list, &v4l2_dev->subdevs);
152         spin_unlock(&v4l2_dev->lock);
153
154         return 0;
155 }
156 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
157
158 int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
159 {
160         struct video_device *vdev;
161         struct v4l2_subdev *sd;
162         int err;
163
164         /* Register a device node for every subdev marked with the
165          * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
166          */
167         list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
168                 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
169                         continue;
170
171                 vdev = &sd->devnode;
172                 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
173                 vdev->v4l2_dev = v4l2_dev;
174                 vdev->fops = &v4l2_subdev_fops;
175                 vdev->release = video_device_release_empty;
176                 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
177                                               sd->owner);
178                 if (err < 0)
179                         return err;
180         }
181
182         return 0;
183 }
184 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
185
186 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
187 {
188         /* return if it isn't registered */
189         if (sd == NULL || sd->v4l2_dev == NULL)
190                 return;
191
192         spin_lock(&sd->v4l2_dev->lock);
193         list_del(&sd->list);
194         spin_unlock(&sd->v4l2_dev->lock);
195         if (sd->internal_ops && sd->internal_ops->unregistered)
196                 sd->internal_ops->unregistered(sd);
197         sd->v4l2_dev = NULL;
198
199         video_unregister_device(&sd->devnode);
200         module_put(sd->owner);
201 }
202 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);