[media] v4l: Make v4l2_subdev inherit from media_entity
[linux-2.6-block.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 #if defined(CONFIG_MEDIA_CONTROLLER)
124         struct media_entity *entity = &sd->entity;
125 #endif
126         int err;
127
128         /* Check for valid input */
129         if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
130                 return -EINVAL;
131
132         /* Warn if we apparently re-register a subdev */
133         WARN_ON(sd->v4l2_dev != NULL);
134
135         if (!try_module_get(sd->owner))
136                 return -ENODEV;
137
138         sd->v4l2_dev = v4l2_dev;
139         if (sd->internal_ops && sd->internal_ops->registered) {
140                 err = sd->internal_ops->registered(sd);
141                 if (err)
142                         return err;
143         }
144
145         /* This just returns 0 if either of the two args is NULL */
146         err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
147         if (err) {
148                 if (sd->internal_ops && sd->internal_ops->unregistered)
149                         sd->internal_ops->unregistered(sd);
150                 return err;
151         }
152
153 #if defined(CONFIG_MEDIA_CONTROLLER)
154         /* Register the entity. */
155         if (v4l2_dev->mdev) {
156                 err = media_device_register_entity(v4l2_dev->mdev, entity);
157                 if (err < 0) {
158                         if (sd->internal_ops && sd->internal_ops->unregistered)
159                                 sd->internal_ops->unregistered(sd);
160                         module_put(sd->owner);
161                         return err;
162                 }
163         }
164 #endif
165
166         spin_lock(&v4l2_dev->lock);
167         list_add_tail(&sd->list, &v4l2_dev->subdevs);
168         spin_unlock(&v4l2_dev->lock);
169
170         return 0;
171 }
172 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
173
174 int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
175 {
176         struct video_device *vdev;
177         struct v4l2_subdev *sd;
178         int err;
179
180         /* Register a device node for every subdev marked with the
181          * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
182          */
183         list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
184                 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
185                         continue;
186
187                 vdev = &sd->devnode;
188                 strlcpy(vdev->name, sd->name, sizeof(vdev->name));
189                 vdev->v4l2_dev = v4l2_dev;
190                 vdev->fops = &v4l2_subdev_fops;
191                 vdev->release = video_device_release_empty;
192                 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
193                                               sd->owner);
194                 if (err < 0)
195                         return err;
196 #if defined(CONFIG_MEDIA_CONTROLLER)
197                 sd->entity.v4l.major = VIDEO_MAJOR;
198                 sd->entity.v4l.minor = vdev->minor;
199 #endif
200         }
201         return 0;
202 }
203 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
204
205 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
206 {
207         struct v4l2_device *v4l2_dev;
208
209         /* return if it isn't registered */
210         if (sd == NULL || sd->v4l2_dev == NULL)
211                 return;
212
213         v4l2_dev = sd->v4l2_dev;
214
215         spin_lock(&v4l2_dev->lock);
216         list_del(&sd->list);
217         spin_unlock(&v4l2_dev->lock);
218
219         if (sd->internal_ops && sd->internal_ops->unregistered)
220                 sd->internal_ops->unregistered(sd);
221         sd->v4l2_dev = NULL;
222
223 #if defined(CONFIG_MEDIA_CONTROLLER)
224         if (v4l2_dev->mdev)
225                 media_device_unregister_entity(&sd->entity);
226 #endif
227         video_unregister_device(&sd->devnode);
228         module_put(sd->owner);
229 }
230 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);