0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/types.h>
0010 #include <linux/ioctl.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/videodev2.h>
0014 #include <media/v4l2-device.h>
0015 #include <media/v4l2-ctrls.h>
0016
0017 int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
0018 {
0019 if (v4l2_dev == NULL)
0020 return -EINVAL;
0021
0022 INIT_LIST_HEAD(&v4l2_dev->subdevs);
0023 spin_lock_init(&v4l2_dev->lock);
0024 v4l2_prio_init(&v4l2_dev->prio);
0025 kref_init(&v4l2_dev->ref);
0026 get_device(dev);
0027 v4l2_dev->dev = dev;
0028 if (dev == NULL) {
0029
0030 if (WARN_ON(!v4l2_dev->name[0]))
0031 return -EINVAL;
0032 return 0;
0033 }
0034
0035
0036 if (!v4l2_dev->name[0])
0037 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
0038 dev->driver->name, dev_name(dev));
0039 if (!dev_get_drvdata(dev))
0040 dev_set_drvdata(dev, v4l2_dev);
0041 return 0;
0042 }
0043 EXPORT_SYMBOL_GPL(v4l2_device_register);
0044
0045 static void v4l2_device_release(struct kref *ref)
0046 {
0047 struct v4l2_device *v4l2_dev =
0048 container_of(ref, struct v4l2_device, ref);
0049
0050 if (v4l2_dev->release)
0051 v4l2_dev->release(v4l2_dev);
0052 }
0053
0054 int v4l2_device_put(struct v4l2_device *v4l2_dev)
0055 {
0056 return kref_put(&v4l2_dev->ref, v4l2_device_release);
0057 }
0058 EXPORT_SYMBOL_GPL(v4l2_device_put);
0059
0060 int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
0061 atomic_t *instance)
0062 {
0063 int num = atomic_inc_return(instance) - 1;
0064 int len = strlen(basename);
0065
0066 if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
0067 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
0068 "%s-%d", basename, num);
0069 else
0070 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
0071 "%s%d", basename, num);
0072 return num;
0073 }
0074 EXPORT_SYMBOL_GPL(v4l2_device_set_name);
0075
0076 void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
0077 {
0078 if (v4l2_dev->dev == NULL)
0079 return;
0080
0081 if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
0082 dev_set_drvdata(v4l2_dev->dev, NULL);
0083 put_device(v4l2_dev->dev);
0084 v4l2_dev->dev = NULL;
0085 }
0086 EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
0087
0088 void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
0089 {
0090 struct v4l2_subdev *sd, *next;
0091
0092
0093
0094 if (v4l2_dev == NULL || !v4l2_dev->name[0])
0095 return;
0096 v4l2_device_disconnect(v4l2_dev);
0097
0098
0099 list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
0100 v4l2_device_unregister_subdev(sd);
0101 if (sd->flags & V4L2_SUBDEV_FL_IS_I2C)
0102 v4l2_i2c_subdev_unregister(sd);
0103 else if (sd->flags & V4L2_SUBDEV_FL_IS_SPI)
0104 v4l2_spi_subdev_unregister(sd);
0105 }
0106
0107 v4l2_dev->name[0] = '\0';
0108 }
0109 EXPORT_SYMBOL_GPL(v4l2_device_unregister);
0110
0111 int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
0112 struct v4l2_subdev *sd)
0113 {
0114 int err;
0115
0116
0117 if (!v4l2_dev || !sd || sd->v4l2_dev || !sd->name[0])
0118 return -EINVAL;
0119
0120
0121
0122
0123
0124
0125
0126
0127 sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
0128 sd->owner == v4l2_dev->dev->driver->owner;
0129
0130 if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
0131 return -ENODEV;
0132
0133 sd->v4l2_dev = v4l2_dev;
0134
0135 err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler,
0136 NULL, true);
0137 if (err)
0138 goto error_module;
0139
0140 #if defined(CONFIG_MEDIA_CONTROLLER)
0141
0142 if (v4l2_dev->mdev) {
0143 err = media_device_register_entity(v4l2_dev->mdev, &sd->entity);
0144 if (err < 0)
0145 goto error_module;
0146 }
0147 #endif
0148
0149 if (sd->internal_ops && sd->internal_ops->registered) {
0150 err = sd->internal_ops->registered(sd);
0151 if (err)
0152 goto error_unregister;
0153 }
0154
0155 spin_lock(&v4l2_dev->lock);
0156 list_add_tail(&sd->list, &v4l2_dev->subdevs);
0157 spin_unlock(&v4l2_dev->lock);
0158
0159 return 0;
0160
0161 error_unregister:
0162 #if defined(CONFIG_MEDIA_CONTROLLER)
0163 media_device_unregister_entity(&sd->entity);
0164 #endif
0165 error_module:
0166 if (!sd->owner_v4l2_dev)
0167 module_put(sd->owner);
0168 sd->v4l2_dev = NULL;
0169 return err;
0170 }
0171 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
0172
0173 static void v4l2_subdev_release(struct v4l2_subdev *sd)
0174 {
0175 struct module *owner = !sd->owner_v4l2_dev ? sd->owner : NULL;
0176
0177 if (sd->internal_ops && sd->internal_ops->release)
0178 sd->internal_ops->release(sd);
0179 sd->devnode = NULL;
0180 module_put(owner);
0181 }
0182
0183 static void v4l2_device_release_subdev_node(struct video_device *vdev)
0184 {
0185 v4l2_subdev_release(video_get_drvdata(vdev));
0186 kfree(vdev);
0187 }
0188
0189 int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
0190 bool read_only)
0191 {
0192 struct video_device *vdev;
0193 struct v4l2_subdev *sd;
0194 int err;
0195
0196
0197
0198
0199 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
0200 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
0201 continue;
0202
0203 if (sd->devnode)
0204 continue;
0205
0206 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
0207 if (!vdev) {
0208 err = -ENOMEM;
0209 goto clean_up;
0210 }
0211
0212 video_set_drvdata(vdev, sd);
0213 strscpy(vdev->name, sd->name, sizeof(vdev->name));
0214 vdev->dev_parent = sd->dev;
0215 vdev->v4l2_dev = v4l2_dev;
0216 vdev->fops = &v4l2_subdev_fops;
0217 vdev->release = v4l2_device_release_subdev_node;
0218 vdev->ctrl_handler = sd->ctrl_handler;
0219 if (read_only)
0220 set_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
0221 sd->devnode = vdev;
0222 err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
0223 sd->owner);
0224 if (err < 0) {
0225 sd->devnode = NULL;
0226 kfree(vdev);
0227 goto clean_up;
0228 }
0229 #if defined(CONFIG_MEDIA_CONTROLLER)
0230 sd->entity.info.dev.major = VIDEO_MAJOR;
0231 sd->entity.info.dev.minor = vdev->minor;
0232
0233
0234 if (vdev->v4l2_dev->mdev) {
0235 struct media_link *link;
0236
0237 link = media_create_intf_link(&sd->entity,
0238 &vdev->intf_devnode->intf,
0239 MEDIA_LNK_FL_ENABLED |
0240 MEDIA_LNK_FL_IMMUTABLE);
0241 if (!link) {
0242 err = -ENOMEM;
0243 goto clean_up;
0244 }
0245 }
0246 #endif
0247 }
0248 return 0;
0249
0250 clean_up:
0251 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
0252 if (!sd->devnode)
0253 break;
0254 video_unregister_device(sd->devnode);
0255 }
0256
0257 return err;
0258 }
0259 EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
0260
0261 void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
0262 {
0263 struct v4l2_device *v4l2_dev;
0264
0265
0266 if (sd == NULL || sd->v4l2_dev == NULL)
0267 return;
0268
0269 v4l2_dev = sd->v4l2_dev;
0270
0271 spin_lock(&v4l2_dev->lock);
0272 list_del(&sd->list);
0273 spin_unlock(&v4l2_dev->lock);
0274
0275 if (sd->internal_ops && sd->internal_ops->unregistered)
0276 sd->internal_ops->unregistered(sd);
0277 sd->v4l2_dev = NULL;
0278
0279 #if defined(CONFIG_MEDIA_CONTROLLER)
0280 if (v4l2_dev->mdev) {
0281
0282
0283
0284
0285 media_device_unregister_entity(&sd->entity);
0286 }
0287 #endif
0288 if (sd->devnode)
0289 video_unregister_device(sd->devnode);
0290 else
0291 v4l2_subdev_release(sd);
0292 }
0293 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);