4 * Copyright (C) 2010 Nokia Corporation
6 * Based on drivers/media/video/v4l2_dev.c code authored by
7 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
8 * Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
10 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Generic media device node infrastructure to register and unregister
29 * character devices using a dynamic major number and proper reference
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/kmod.h>
40 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/types.h>
44 #include <linux/uaccess.h>
46 #include <media/media-devnode.h>
47 #include <media/media-device.h>
49 #define MEDIA_NUM_DEVICES 256
50 #define MEDIA_NAME "media"
52 static dev_t media_dev_t;
57 static DEFINE_MUTEX(media_devnode_lock);
58 static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
60 /* Called when the last user of the media device exits. */
61 static void media_devnode_release(struct device *cd)
63 struct media_devnode *devnode = to_media_devnode(cd);
65 mutex_lock(&media_devnode_lock);
66 /* Mark device node number as free */
67 clear_bit(devnode->minor, media_devnode_nums);
68 mutex_unlock(&media_devnode_lock);
70 /* Release media_devnode and perform other cleanups as needed. */
72 devnode->release(devnode);
75 pr_debug("%s: Media Devnode Deallocated\n", __func__);
78 static struct bus_type media_bus_type = {
82 static ssize_t media_read(struct file *filp, char __user *buf,
83 size_t sz, loff_t *off)
85 struct media_devnode *devnode = media_devnode_data(filp);
87 if (!devnode->fops->read)
89 if (!media_devnode_is_registered(devnode))
91 return devnode->fops->read(filp, buf, sz, off);
94 static ssize_t media_write(struct file *filp, const char __user *buf,
95 size_t sz, loff_t *off)
97 struct media_devnode *devnode = media_devnode_data(filp);
99 if (!devnode->fops->write)
101 if (!media_devnode_is_registered(devnode))
103 return devnode->fops->write(filp, buf, sz, off);
106 static unsigned int media_poll(struct file *filp,
107 struct poll_table_struct *poll)
109 struct media_devnode *devnode = media_devnode_data(filp);
111 if (!media_devnode_is_registered(devnode))
112 return POLLERR | POLLHUP;
113 if (!devnode->fops->poll)
114 return DEFAULT_POLLMASK;
115 return devnode->fops->poll(filp, poll);
119 __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
120 long (*ioctl_func)(struct file *filp, unsigned int cmd,
123 struct media_devnode *devnode = media_devnode_data(filp);
128 if (!media_devnode_is_registered(devnode))
131 return ioctl_func(filp, cmd, arg);
134 static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
136 struct media_devnode *devnode = media_devnode_data(filp);
138 return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
143 static long media_compat_ioctl(struct file *filp, unsigned int cmd,
146 struct media_devnode *devnode = media_devnode_data(filp);
148 return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
151 #endif /* CONFIG_COMPAT */
153 /* Override for the open function */
154 static int media_open(struct inode *inode, struct file *filp)
156 struct media_devnode *devnode;
159 /* Check if the media device is available. This needs to be done with
160 * the media_devnode_lock held to prevent an open/unregister race:
161 * without the lock, the device could be unregistered and freed between
162 * the media_devnode_is_registered() and get_device() calls, leading to
165 mutex_lock(&media_devnode_lock);
166 devnode = container_of(inode->i_cdev, struct media_devnode, cdev);
167 /* return ENXIO if the media device has been removed
168 already or if it is not registered anymore. */
169 if (!media_devnode_is_registered(devnode)) {
170 mutex_unlock(&media_devnode_lock);
173 /* and increase the device refcount */
174 get_device(&devnode->dev);
175 mutex_unlock(&media_devnode_lock);
177 filp->private_data = devnode;
179 if (devnode->fops->open) {
180 ret = devnode->fops->open(filp);
182 put_device(&devnode->dev);
183 filp->private_data = NULL;
191 /* Override for the release function */
192 static int media_release(struct inode *inode, struct file *filp)
194 struct media_devnode *devnode = media_devnode_data(filp);
196 if (devnode->fops->release)
197 devnode->fops->release(filp);
199 filp->private_data = NULL;
201 /* decrease the refcount unconditionally since the release()
202 return value is ignored. */
203 put_device(&devnode->dev);
205 pr_debug("%s: Media Release\n", __func__);
209 static const struct file_operations media_devnode_fops = {
210 .owner = THIS_MODULE,
212 .write = media_write,
214 .unlocked_ioctl = media_ioctl,
216 .compat_ioctl = media_compat_ioctl,
217 #endif /* CONFIG_COMPAT */
218 .release = media_release,
224 * media_devnode_register - register a media device node
225 * @media_dev: struct media_device we want to register a device node
226 * @devnode: media device node structure we want to register
228 * The registration code assigns minor numbers and registers the new device node
229 * with the kernel. An error is returned if no free minor number can be found,
230 * or if the registration of the device node fails.
232 * Zero is returned on success.
234 * Note that if the media_devnode_register call fails, the release() callback of
235 * the media_devnode structure is *not* called, so the caller is responsible for
238 int __must_check media_devnode_register(struct media_device *mdev,
239 struct media_devnode *devnode,
240 struct module *owner)
245 /* Part 1: Find a free minor number */
246 mutex_lock(&media_devnode_lock);
247 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
248 if (minor == MEDIA_NUM_DEVICES) {
249 mutex_unlock(&media_devnode_lock);
250 pr_err("could not get a free minor\n");
255 set_bit(minor, media_devnode_nums);
256 mutex_unlock(&media_devnode_lock);
258 devnode->minor = minor;
259 devnode->media_dev = mdev;
261 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
262 devnode->dev.bus = &media_bus_type;
263 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
264 devnode->dev.release = media_devnode_release;
266 devnode->dev.parent = devnode->parent;
267 dev_set_name(&devnode->dev, "media%d", devnode->minor);
268 device_initialize(&devnode->dev);
270 /* Part 2: Initialize and register the character device */
271 cdev_init(&devnode->cdev, &media_devnode_fops);
272 devnode->cdev.owner = owner;
273 devnode->cdev.kobj.parent = &devnode->dev.kobj;
275 ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1);
277 pr_err("%s: cdev_add failed\n", __func__);
281 /* Part 3: Add the media device */
282 ret = device_add(&devnode->dev);
284 pr_err("%s: device_add failed\n", __func__);
285 goto device_add_error;
288 /* Part 4: Activate this minor. The char device can now be used. */
289 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
294 cdev_del(&devnode->cdev);
296 mutex_lock(&media_devnode_lock);
297 clear_bit(devnode->minor, media_devnode_nums);
298 devnode->media_dev = NULL;
299 mutex_unlock(&media_devnode_lock);
301 put_device(&devnode->dev);
305 void media_devnode_unregister_prepare(struct media_devnode *devnode)
307 /* Check if devnode was ever registered at all */
308 if (!media_devnode_is_registered(devnode))
311 mutex_lock(&media_devnode_lock);
312 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
313 mutex_unlock(&media_devnode_lock);
317 * media_devnode_unregister - unregister a media device node
318 * @devnode: the device node to unregister
320 * This unregisters the passed device. Future open calls will be met with
323 * Should be called after media_devnode_unregister_prepare()
325 void media_devnode_unregister(struct media_devnode *devnode)
327 mutex_lock(&media_devnode_lock);
328 /* Delete the cdev on this minor as well */
329 cdev_del(&devnode->cdev);
330 mutex_unlock(&media_devnode_lock);
331 device_del(&devnode->dev);
332 devnode->media_dev = NULL;
333 put_device(&devnode->dev);
337 * Initialise media for linux
339 static int __init media_devnode_init(void)
343 pr_info("Linux media interface: v0.10\n");
344 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
347 pr_warn("unable to allocate major\n");
351 ret = bus_register(&media_bus_type);
353 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
354 pr_warn("bus_register failed\n");
361 static void __exit media_devnode_exit(void)
363 bus_unregister(&media_bus_type);
364 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
367 subsys_initcall(media_devnode_init);
368 module_exit(media_devnode_exit)
370 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
371 MODULE_DESCRIPTION("Device node registration for media drivers");
372 MODULE_LICENSE("GPL");