xref: /linux/drivers/media/mc/mc-devnode.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Media device node
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  *
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)
10  *
11  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12  *	     Sakari Ailus <sakari.ailus@iki.fi>
13  *
14  * --
15  *
16  * Generic media device node infrastructure to register and unregister
17  * character devices using a dynamic major number and proper reference
18  * counting.
19  */
20 
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
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>
33 
34 #include <media/media-devnode.h>
35 #include <media/media-device.h>
36 
37 #define MEDIA_NUM_DEVICES	256
38 #define MEDIA_NAME		"media"
39 
40 static dev_t media_dev_t;
41 
42 /*
43  *	Active devices
44  */
45 static DEFINE_MUTEX(media_devnode_lock);
46 static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
47 
48 /* Called when the last user of the media device exits. */
49 static void media_devnode_release(struct device *cd)
50 {
51 	struct media_devnode *devnode = to_media_devnode(cd);
52 
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);
57 
58 	/* Release media_devnode and perform other cleanups as needed. */
59 	if (devnode->release)
60 		devnode->release(devnode);
61 
62 	kfree(devnode);
63 	pr_debug("%s: Media Devnode Deallocated\n", __func__);
64 }
65 
66 static const struct bus_type media_bus_type = {
67 	.name = MEDIA_NAME,
68 };
69 
70 static ssize_t media_read(struct file *filp, char __user *buf,
71 		size_t sz, loff_t *off)
72 {
73 	struct media_devnode *devnode = media_devnode_data(filp);
74 
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);
80 }
81 
82 static ssize_t media_write(struct file *filp, const char __user *buf,
83 		size_t sz, loff_t *off)
84 {
85 	struct media_devnode *devnode = media_devnode_data(filp);
86 
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);
92 }
93 
94 static __poll_t media_poll(struct file *filp,
95 			       struct poll_table_struct *poll)
96 {
97 	struct media_devnode *devnode = media_devnode_data(filp);
98 
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);
104 }
105 
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))
110 {
111 	struct media_devnode *devnode = media_devnode_data(filp);
112 
113 	if (!ioctl_func)
114 		return -ENOTTY;
115 
116 	if (!media_devnode_is_registered(devnode))
117 		return -EIO;
118 
119 	return ioctl_func(filp, cmd, arg);
120 }
121 
122 static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
123 {
124 	struct media_devnode *devnode = media_devnode_data(filp);
125 
126 	return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
127 }
128 
129 #ifdef CONFIG_COMPAT
130 
131 static long media_compat_ioctl(struct file *filp, unsigned int cmd,
132 			       unsigned long arg)
133 {
134 	struct media_devnode *devnode = media_devnode_data(filp);
135 
136 	return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
137 }
138 
139 #endif /* CONFIG_COMPAT */
140 
141 /* Override for the open function */
142 static int media_open(struct inode *inode, struct file *filp)
143 {
144 	struct media_devnode *devnode;
145 	int ret;
146 
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.
152 	 */
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;
160 	}
161 	/* and increase the device refcount */
162 	get_device(&devnode->dev);
163 	mutex_unlock(&media_devnode_lock);
164 
165 	filp->private_data = devnode;
166 
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;
173 		}
174 	}
175 
176 	return 0;
177 }
178 
179 /* Override for the release function */
180 static int media_release(struct inode *inode, struct file *filp)
181 {
182 	struct media_devnode *devnode = media_devnode_data(filp);
183 
184 	if (devnode->fops->release)
185 		devnode->fops->release(filp);
186 
187 	filp->private_data = NULL;
188 
189 	/* decrease the refcount unconditionally since the release()
190 	   return value is ignored. */
191 	put_device(&devnode->dev);
192 
193 	return 0;
194 }
195 
196 static const struct file_operations media_devnode_fops = {
197 	.owner = THIS_MODULE,
198 	.read = media_read,
199 	.write = media_write,
200 	.open = media_open,
201 	.unlocked_ioctl = media_ioctl,
202 #ifdef CONFIG_COMPAT
203 	.compat_ioctl = media_compat_ioctl,
204 #endif /* CONFIG_COMPAT */
205 	.release = media_release,
206 	.poll = media_poll,
207 	.llseek = no_llseek,
208 };
209 
210 int __must_check media_devnode_register(struct media_device *mdev,
211 					struct media_devnode *devnode,
212 					struct module *owner)
213 {
214 	int minor;
215 	int ret;
216 
217 	/* Part 1: Find a free minor number */
218 	mutex_lock(&media_devnode_lock);
219 	minor = find_first_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES);
220 	if (minor == MEDIA_NUM_DEVICES) {
221 		mutex_unlock(&media_devnode_lock);
222 		pr_err("could not get a free minor\n");
223 		kfree(devnode);
224 		return -ENFILE;
225 	}
226 
227 	set_bit(minor, media_devnode_nums);
228 	mutex_unlock(&media_devnode_lock);
229 
230 	devnode->minor = minor;
231 	devnode->media_dev = mdev;
232 
233 	/* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
234 	devnode->dev.bus = &media_bus_type;
235 	devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
236 	devnode->dev.release = media_devnode_release;
237 	if (devnode->parent)
238 		devnode->dev.parent = devnode->parent;
239 	dev_set_name(&devnode->dev, "media%d", devnode->minor);
240 	device_initialize(&devnode->dev);
241 
242 	/* Part 2: Initialize the character device */
243 	cdev_init(&devnode->cdev, &media_devnode_fops);
244 	devnode->cdev.owner = owner;
245 	kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor);
246 
247 	/* Part 3: Add the media and char device */
248 	set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
249 	ret = cdev_device_add(&devnode->cdev, &devnode->dev);
250 	if (ret < 0) {
251 		clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
252 		pr_err("%s: cdev_device_add failed\n", __func__);
253 		goto cdev_add_error;
254 	}
255 
256 	return 0;
257 
258 cdev_add_error:
259 	mutex_lock(&media_devnode_lock);
260 	clear_bit(devnode->minor, media_devnode_nums);
261 	devnode->media_dev = NULL;
262 	mutex_unlock(&media_devnode_lock);
263 
264 	put_device(&devnode->dev);
265 	return ret;
266 }
267 
268 void media_devnode_unregister_prepare(struct media_devnode *devnode)
269 {
270 	/* Check if devnode was ever registered at all */
271 	if (!media_devnode_is_registered(devnode))
272 		return;
273 
274 	mutex_lock(&media_devnode_lock);
275 	clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
276 	mutex_unlock(&media_devnode_lock);
277 }
278 
279 void media_devnode_unregister(struct media_devnode *devnode)
280 {
281 	mutex_lock(&media_devnode_lock);
282 	/* Delete the cdev on this minor as well */
283 	cdev_device_del(&devnode->cdev, &devnode->dev);
284 	devnode->media_dev = NULL;
285 	mutex_unlock(&media_devnode_lock);
286 
287 	put_device(&devnode->dev);
288 }
289 
290 /*
291  *	Initialise media for linux
292  */
293 static int __init media_devnode_init(void)
294 {
295 	int ret;
296 
297 	pr_info("Linux media interface: v0.10\n");
298 	ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
299 				  MEDIA_NAME);
300 	if (ret < 0) {
301 		pr_warn("unable to allocate major\n");
302 		return ret;
303 	}
304 
305 	ret = bus_register(&media_bus_type);
306 	if (ret < 0) {
307 		unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
308 		pr_warn("bus_register failed\n");
309 		return -EIO;
310 	}
311 
312 	return 0;
313 }
314 
315 static void __exit media_devnode_exit(void)
316 {
317 	bus_unregister(&media_bus_type);
318 	unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
319 }
320 
321 subsys_initcall(media_devnode_init);
322 module_exit(media_devnode_exit)
323 
324 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
325 MODULE_DESCRIPTION("Device node registration for media drivers");
326 MODULE_LICENSE("GPL");
327