Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / media-devnode.c
blob29409f440f1caef38f82694b382d3b66a3895ffb
1 /*
2 * Media device node
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
26 * --
28 * Generic media device node infrastructure to register and unregister
29 * character devices using a dynamic major number and proper reference
30 * counting.
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>
41 #include <linux/mm.h>
42 #include <linux/string.h>
43 #include <linux/types.h>
44 #include <linux/uaccess.h>
46 #include <media/media-devnode.h>
48 #define MEDIA_NUM_DEVICES 256
49 #define MEDIA_NAME "media"
51 static dev_t media_dev_t;
54 * Active devices
56 static DEFINE_MUTEX(media_devnode_lock);
57 static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
59 /* Called when the last user of the media device exits. */
60 static void media_devnode_release(struct device *cd)
62 struct media_devnode *mdev = to_media_devnode(cd);
64 mutex_lock(&media_devnode_lock);
66 /* Delete the cdev on this minor as well */
67 cdev_del(&mdev->cdev);
69 /* Mark device node number as free */
70 clear_bit(mdev->minor, media_devnode_nums);
72 mutex_unlock(&media_devnode_lock);
74 /* Release media_devnode and perform other cleanups as needed. */
75 if (mdev->release)
76 mdev->release(mdev);
79 static struct bus_type media_bus_type = {
80 .name = MEDIA_NAME,
83 static ssize_t media_read(struct file *filp, char __user *buf,
84 size_t sz, loff_t *off)
86 struct media_devnode *mdev = media_devnode_data(filp);
88 if (!mdev->fops->read)
89 return -EINVAL;
90 if (!media_devnode_is_registered(mdev))
91 return -EIO;
92 return mdev->fops->read(filp, buf, sz, off);
95 static ssize_t media_write(struct file *filp, const char __user *buf,
96 size_t sz, loff_t *off)
98 struct media_devnode *mdev = media_devnode_data(filp);
100 if (!mdev->fops->write)
101 return -EINVAL;
102 if (!media_devnode_is_registered(mdev))
103 return -EIO;
104 return mdev->fops->write(filp, buf, sz, off);
107 static unsigned int media_poll(struct file *filp,
108 struct poll_table_struct *poll)
110 struct media_devnode *mdev = media_devnode_data(filp);
112 if (!media_devnode_is_registered(mdev))
113 return POLLERR | POLLHUP;
114 if (!mdev->fops->poll)
115 return DEFAULT_POLLMASK;
116 return mdev->fops->poll(filp, poll);
119 static long
120 __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
121 long (*ioctl_func)(struct file *filp, unsigned int cmd,
122 unsigned long arg))
124 struct media_devnode *mdev = media_devnode_data(filp);
126 if (!ioctl_func)
127 return -ENOTTY;
129 if (!media_devnode_is_registered(mdev))
130 return -EIO;
132 return ioctl_func(filp, cmd, arg);
135 static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
137 struct media_devnode *mdev = media_devnode_data(filp);
139 return __media_ioctl(filp, cmd, arg, mdev->fops->ioctl);
142 #ifdef CONFIG_COMPAT
144 static long media_compat_ioctl(struct file *filp, unsigned int cmd,
145 unsigned long arg)
147 struct media_devnode *mdev = media_devnode_data(filp);
149 return __media_ioctl(filp, cmd, arg, mdev->fops->compat_ioctl);
152 #endif /* CONFIG_COMPAT */
154 /* Override for the open function */
155 static int media_open(struct inode *inode, struct file *filp)
157 struct media_devnode *mdev;
158 int ret;
160 /* Check if the media device is available. This needs to be done with
161 * the media_devnode_lock held to prevent an open/unregister race:
162 * without the lock, the device could be unregistered and freed between
163 * the media_devnode_is_registered() and get_device() calls, leading to
164 * a crash.
166 mutex_lock(&media_devnode_lock);
167 mdev = container_of(inode->i_cdev, struct media_devnode, cdev);
168 /* return ENXIO if the media device has been removed
169 already or if it is not registered anymore. */
170 if (!media_devnode_is_registered(mdev)) {
171 mutex_unlock(&media_devnode_lock);
172 return -ENXIO;
174 /* and increase the device refcount */
175 get_device(&mdev->dev);
176 mutex_unlock(&media_devnode_lock);
178 filp->private_data = mdev;
180 if (mdev->fops->open) {
181 ret = mdev->fops->open(filp);
182 if (ret) {
183 put_device(&mdev->dev);
184 filp->private_data = NULL;
185 return ret;
189 return 0;
192 /* Override for the release function */
193 static int media_release(struct inode *inode, struct file *filp)
195 struct media_devnode *mdev = media_devnode_data(filp);
197 if (mdev->fops->release)
198 mdev->fops->release(filp);
200 /* decrease the refcount unconditionally since the release()
201 return value is ignored. */
202 put_device(&mdev->dev);
203 filp->private_data = NULL;
204 return 0;
207 static const struct file_operations media_devnode_fops = {
208 .owner = THIS_MODULE,
209 .read = media_read,
210 .write = media_write,
211 .open = media_open,
212 .unlocked_ioctl = media_ioctl,
213 #ifdef CONFIG_COMPAT
214 .compat_ioctl = media_compat_ioctl,
215 #endif /* CONFIG_COMPAT */
216 .release = media_release,
217 .poll = media_poll,
218 .llseek = no_llseek,
221 int __must_check media_devnode_register(struct media_devnode *mdev,
222 struct module *owner)
224 int minor;
225 int ret;
227 /* Part 1: Find a free minor number */
228 mutex_lock(&media_devnode_lock);
229 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
230 if (minor == MEDIA_NUM_DEVICES) {
231 mutex_unlock(&media_devnode_lock);
232 pr_err("could not get a free minor\n");
233 return -ENFILE;
236 set_bit(minor, media_devnode_nums);
237 mutex_unlock(&media_devnode_lock);
239 mdev->minor = minor;
241 /* Part 2: Initialize and register the character device */
242 cdev_init(&mdev->cdev, &media_devnode_fops);
243 mdev->cdev.owner = owner;
245 ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1);
246 if (ret < 0) {
247 pr_err("%s: cdev_add failed\n", __func__);
248 goto error;
251 /* Part 3: Register the media device */
252 mdev->dev.bus = &media_bus_type;
253 mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
254 mdev->dev.release = media_devnode_release;
255 if (mdev->parent)
256 mdev->dev.parent = mdev->parent;
257 dev_set_name(&mdev->dev, "media%d", mdev->minor);
258 ret = device_register(&mdev->dev);
259 if (ret < 0) {
260 pr_err("%s: device_register failed\n", __func__);
261 goto error;
264 /* Part 4: Activate this minor. The char device can now be used. */
265 set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
267 return 0;
269 error:
270 cdev_del(&mdev->cdev);
271 clear_bit(mdev->minor, media_devnode_nums);
272 return ret;
275 void media_devnode_unregister(struct media_devnode *mdev)
277 /* Check if mdev was ever registered at all */
278 if (!media_devnode_is_registered(mdev))
279 return;
281 mutex_lock(&media_devnode_lock);
282 clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
283 mutex_unlock(&media_devnode_lock);
284 device_unregister(&mdev->dev);
288 * Initialise media for linux
290 static int __init media_devnode_init(void)
292 int ret;
294 pr_info("Linux media interface: v0.10\n");
295 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
296 MEDIA_NAME);
297 if (ret < 0) {
298 pr_warn("unable to allocate major\n");
299 return ret;
302 ret = bus_register(&media_bus_type);
303 if (ret < 0) {
304 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
305 pr_warn("bus_register failed\n");
306 return -EIO;
309 return 0;
312 static void __exit media_devnode_exit(void)
314 bus_unregister(&media_bus_type);
315 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
318 subsys_initcall(media_devnode_init);
319 module_exit(media_devnode_exit)
321 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
322 MODULE_DESCRIPTION("Device node registration for media drivers");
323 MODULE_LICENSE("GPL");