sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / media-devnode.c
blobf2772ba6f61163f59d25c31b9b1b4cf16d824856
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>
47 #include <media/media-device.h>
49 #define MEDIA_NUM_DEVICES 256
50 #define MEDIA_NAME "media"
52 static dev_t media_dev_t;
55 * Active devices
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. */
71 if (devnode->release)
72 devnode->release(devnode);
74 kfree(devnode);
75 pr_debug("%s: Media Devnode Deallocated\n", __func__);
78 static struct bus_type media_bus_type = {
79 .name = MEDIA_NAME,
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)
88 return -EINVAL;
89 if (!media_devnode_is_registered(devnode))
90 return -EIO;
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)
100 return -EINVAL;
101 if (!media_devnode_is_registered(devnode))
102 return -EIO;
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);
118 static long
119 __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
120 long (*ioctl_func)(struct file *filp, unsigned int cmd,
121 unsigned long arg))
123 struct media_devnode *devnode = media_devnode_data(filp);
125 if (!ioctl_func)
126 return -ENOTTY;
128 if (!media_devnode_is_registered(devnode))
129 return -EIO;
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);
141 #ifdef CONFIG_COMPAT
143 static long media_compat_ioctl(struct file *filp, unsigned int cmd,
144 unsigned long arg)
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;
157 int ret;
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
163 * a crash.
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);
171 return -ENXIO;
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);
181 if (ret) {
182 put_device(&devnode->dev);
183 filp->private_data = NULL;
184 return ret;
188 return 0;
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__);
206 return 0;
209 static const struct file_operations media_devnode_fops = {
210 .owner = THIS_MODULE,
211 .read = media_read,
212 .write = media_write,
213 .open = media_open,
214 .unlocked_ioctl = media_ioctl,
215 #ifdef CONFIG_COMPAT
216 .compat_ioctl = media_compat_ioctl,
217 #endif /* CONFIG_COMPAT */
218 .release = media_release,
219 .poll = media_poll,
220 .llseek = no_llseek,
223 int __must_check media_devnode_register(struct media_device *mdev,
224 struct media_devnode *devnode,
225 struct module *owner)
227 int minor;
228 int ret;
230 /* Part 1: Find a free minor number */
231 mutex_lock(&media_devnode_lock);
232 minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
233 if (minor == MEDIA_NUM_DEVICES) {
234 mutex_unlock(&media_devnode_lock);
235 pr_err("could not get a free minor\n");
236 kfree(devnode);
237 return -ENFILE;
240 set_bit(minor, media_devnode_nums);
241 mutex_unlock(&media_devnode_lock);
243 devnode->minor = minor;
244 devnode->media_dev = mdev;
246 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
247 devnode->dev.bus = &media_bus_type;
248 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
249 devnode->dev.release = media_devnode_release;
250 if (devnode->parent)
251 devnode->dev.parent = devnode->parent;
252 dev_set_name(&devnode->dev, "media%d", devnode->minor);
253 device_initialize(&devnode->dev);
255 /* Part 2: Initialize and register the character device */
256 cdev_init(&devnode->cdev, &media_devnode_fops);
257 devnode->cdev.owner = owner;
258 devnode->cdev.kobj.parent = &devnode->dev.kobj;
260 ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1);
261 if (ret < 0) {
262 pr_err("%s: cdev_add failed\n", __func__);
263 goto cdev_add_error;
266 /* Part 3: Add the media device */
267 ret = device_add(&devnode->dev);
268 if (ret < 0) {
269 pr_err("%s: device_add failed\n", __func__);
270 goto device_add_error;
273 /* Part 4: Activate this minor. The char device can now be used. */
274 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
276 return 0;
278 device_add_error:
279 cdev_del(&devnode->cdev);
280 cdev_add_error:
281 mutex_lock(&media_devnode_lock);
282 clear_bit(devnode->minor, media_devnode_nums);
283 devnode->media_dev = NULL;
284 mutex_unlock(&media_devnode_lock);
286 put_device(&devnode->dev);
287 return ret;
290 void media_devnode_unregister_prepare(struct media_devnode *devnode)
292 /* Check if devnode was ever registered at all */
293 if (!media_devnode_is_registered(devnode))
294 return;
296 mutex_lock(&media_devnode_lock);
297 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
298 mutex_unlock(&media_devnode_lock);
301 void media_devnode_unregister(struct media_devnode *devnode)
303 mutex_lock(&media_devnode_lock);
304 /* Delete the cdev on this minor as well */
305 cdev_del(&devnode->cdev);
306 mutex_unlock(&media_devnode_lock);
307 device_del(&devnode->dev);
308 devnode->media_dev = NULL;
309 put_device(&devnode->dev);
313 * Initialise media for linux
315 static int __init media_devnode_init(void)
317 int ret;
319 pr_info("Linux media interface: v0.10\n");
320 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
321 MEDIA_NAME);
322 if (ret < 0) {
323 pr_warn("unable to allocate major\n");
324 return ret;
327 ret = bus_register(&media_bus_type);
328 if (ret < 0) {
329 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
330 pr_warn("bus_register failed\n");
331 return -EIO;
334 return 0;
337 static void __exit media_devnode_exit(void)
339 bus_unregister(&media_bus_type);
340 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
343 subsys_initcall(media_devnode_init);
344 module_exit(media_devnode_exit)
346 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
347 MODULE_DESCRIPTION("Device node registration for media drivers");
348 MODULE_LICENSE("GPL");