Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / mc / mc-devnode.c
blobf11382afe23bf280736244026890924d4f4729ba
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Media device node
5 * Copyright (C) 2010 Nokia Corporation
7 * Based on drivers/media/video/v4l2_dev.c code authored by
8 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
9 * Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
11 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 * Sakari Ailus <sakari.ailus@iki.fi>
14 * --
16 * Generic media device node infrastructure to register and unregister
17 * character devices using a dynamic major number and proper reference
18 * counting.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/uaccess.h>
34 #include <media/media-devnode.h>
35 #include <media/media-device.h>
37 #define MEDIA_NUM_DEVICES 256
38 #define MEDIA_NAME "media"
40 static dev_t media_dev_t;
43 * Active devices
45 static DEFINE_MUTEX(media_devnode_lock);
46 static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
48 /* Called when the last user of the media device exits. */
49 static void media_devnode_release(struct device *cd)
51 struct media_devnode *devnode = to_media_devnode(cd);
53 mutex_lock(&media_devnode_lock);
54 /* Mark device node number as free */
55 clear_bit(devnode->minor, media_devnode_nums);
56 mutex_unlock(&media_devnode_lock);
58 /* Release media_devnode and perform other cleanups as needed. */
59 if (devnode->release)
60 devnode->release(devnode);
62 kfree(devnode);
63 pr_debug("%s: Media Devnode Deallocated\n", __func__);
66 static struct bus_type media_bus_type = {
67 .name = MEDIA_NAME,
70 static ssize_t media_read(struct file *filp, char __user *buf,
71 size_t sz, loff_t *off)
73 struct media_devnode *devnode = media_devnode_data(filp);
75 if (!devnode->fops->read)
76 return -EINVAL;
77 if (!media_devnode_is_registered(devnode))
78 return -EIO;
79 return devnode->fops->read(filp, buf, sz, off);
82 static ssize_t media_write(struct file *filp, const char __user *buf,
83 size_t sz, loff_t *off)
85 struct media_devnode *devnode = media_devnode_data(filp);
87 if (!devnode->fops->write)
88 return -EINVAL;
89 if (!media_devnode_is_registered(devnode))
90 return -EIO;
91 return devnode->fops->write(filp, buf, sz, off);
94 static __poll_t media_poll(struct file *filp,
95 struct poll_table_struct *poll)
97 struct media_devnode *devnode = media_devnode_data(filp);
99 if (!media_devnode_is_registered(devnode))
100 return EPOLLERR | EPOLLHUP;
101 if (!devnode->fops->poll)
102 return DEFAULT_POLLMASK;
103 return devnode->fops->poll(filp, poll);
106 static long
107 __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
108 long (*ioctl_func)(struct file *filp, unsigned int cmd,
109 unsigned long arg))
111 struct media_devnode *devnode = media_devnode_data(filp);
113 if (!ioctl_func)
114 return -ENOTTY;
116 if (!media_devnode_is_registered(devnode))
117 return -EIO;
119 return ioctl_func(filp, cmd, arg);
122 static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
124 struct media_devnode *devnode = media_devnode_data(filp);
126 return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
129 #ifdef CONFIG_COMPAT
131 static long media_compat_ioctl(struct file *filp, unsigned int cmd,
132 unsigned long arg)
134 struct media_devnode *devnode = media_devnode_data(filp);
136 return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
139 #endif /* CONFIG_COMPAT */
141 /* Override for the open function */
142 static int media_open(struct inode *inode, struct file *filp)
144 struct media_devnode *devnode;
145 int ret;
147 /* Check if the media device is available. This needs to be done with
148 * the media_devnode_lock held to prevent an open/unregister race:
149 * without the lock, the device could be unregistered and freed between
150 * the media_devnode_is_registered() and get_device() calls, leading to
151 * a crash.
153 mutex_lock(&media_devnode_lock);
154 devnode = container_of(inode->i_cdev, struct media_devnode, cdev);
155 /* return ENXIO if the media device has been removed
156 already or if it is not registered anymore. */
157 if (!media_devnode_is_registered(devnode)) {
158 mutex_unlock(&media_devnode_lock);
159 return -ENXIO;
161 /* and increase the device refcount */
162 get_device(&devnode->dev);
163 mutex_unlock(&media_devnode_lock);
165 filp->private_data = devnode;
167 if (devnode->fops->open) {
168 ret = devnode->fops->open(filp);
169 if (ret) {
170 put_device(&devnode->dev);
171 filp->private_data = NULL;
172 return ret;
176 return 0;
179 /* Override for the release function */
180 static int media_release(struct inode *inode, struct file *filp)
182 struct media_devnode *devnode = media_devnode_data(filp);
184 if (devnode->fops->release)
185 devnode->fops->release(filp);
187 filp->private_data = NULL;
189 /* decrease the refcount unconditionally since the release()
190 return value is ignored. */
191 put_device(&devnode->dev);
193 pr_debug("%s: Media Release\n", __func__);
194 return 0;
197 static const struct file_operations media_devnode_fops = {
198 .owner = THIS_MODULE,
199 .read = media_read,
200 .write = media_write,
201 .open = media_open,
202 .unlocked_ioctl = media_ioctl,
203 #ifdef CONFIG_COMPAT
204 .compat_ioctl = media_compat_ioctl,
205 #endif /* CONFIG_COMPAT */
206 .release = media_release,
207 .poll = media_poll,
208 .llseek = no_llseek,
211 int __must_check media_devnode_register(struct media_device *mdev,
212 struct media_devnode *devnode,
213 struct module *owner)
215 int minor;
216 int ret;
218 /* Part 1: Find a free minor number */
219 mutex_lock(&media_devnode_lock);
220 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
221 if (minor == MEDIA_NUM_DEVICES) {
222 mutex_unlock(&media_devnode_lock);
223 pr_err("could not get a free minor\n");
224 kfree(devnode);
225 return -ENFILE;
228 set_bit(minor, media_devnode_nums);
229 mutex_unlock(&media_devnode_lock);
231 devnode->minor = minor;
232 devnode->media_dev = mdev;
234 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
235 devnode->dev.bus = &media_bus_type;
236 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
237 devnode->dev.release = media_devnode_release;
238 if (devnode->parent)
239 devnode->dev.parent = devnode->parent;
240 dev_set_name(&devnode->dev, "media%d", devnode->minor);
241 device_initialize(&devnode->dev);
243 /* Part 2: Initialize the character device */
244 cdev_init(&devnode->cdev, &media_devnode_fops);
245 devnode->cdev.owner = owner;
246 kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor);
248 /* Part 3: Add the media and char device */
249 ret = cdev_device_add(&devnode->cdev, &devnode->dev);
250 if (ret < 0) {
251 pr_err("%s: cdev_device_add failed\n", __func__);
252 goto cdev_add_error;
255 /* Part 4: Activate this minor. The char device can now be used. */
256 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
258 return 0;
260 cdev_add_error:
261 mutex_lock(&media_devnode_lock);
262 clear_bit(devnode->minor, media_devnode_nums);
263 devnode->media_dev = NULL;
264 mutex_unlock(&media_devnode_lock);
266 put_device(&devnode->dev);
267 return ret;
270 void media_devnode_unregister_prepare(struct media_devnode *devnode)
272 /* Check if devnode was ever registered at all */
273 if (!media_devnode_is_registered(devnode))
274 return;
276 mutex_lock(&media_devnode_lock);
277 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
278 mutex_unlock(&media_devnode_lock);
281 void media_devnode_unregister(struct media_devnode *devnode)
283 mutex_lock(&media_devnode_lock);
284 /* Delete the cdev on this minor as well */
285 cdev_device_del(&devnode->cdev, &devnode->dev);
286 devnode->media_dev = NULL;
287 mutex_unlock(&media_devnode_lock);
289 put_device(&devnode->dev);
293 * Initialise media for linux
295 static int __init media_devnode_init(void)
297 int ret;
299 pr_info("Linux media interface: v0.10\n");
300 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
301 MEDIA_NAME);
302 if (ret < 0) {
303 pr_warn("unable to allocate major\n");
304 return ret;
307 ret = bus_register(&media_bus_type);
308 if (ret < 0) {
309 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
310 pr_warn("bus_register failed\n");
311 return -EIO;
314 return 0;
317 static void __exit media_devnode_exit(void)
319 bus_unregister(&media_bus_type);
320 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
323 subsys_initcall(media_devnode_init);
324 module_exit(media_devnode_exit)
326 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
327 MODULE_DESCRIPTION("Device node registration for media drivers");
328 MODULE_LICENSE("GPL");