crypto: caam - ARRAY_SIZE() vs sizeof()
[linux/fpc-iii.git] / drivers / media / media-devnode.c
blobaf5263c6625adeafab44bfa5cdff167122ff52d5
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 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/kmod.h>
38 #include <linux/slab.h>
39 #include <linux/mm.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
42 #include <linux/uaccess.h>
43 #include <asm/system.h>
45 #include <media/media-devnode.h>
47 #define MEDIA_NUM_DEVICES 256
48 #define MEDIA_NAME "media"
50 static dev_t media_dev_t;
53 * Active devices
55 static DEFINE_MUTEX(media_devnode_lock);
56 static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
58 /* Called when the last user of the media device exits. */
59 static void media_devnode_release(struct device *cd)
61 struct media_devnode *mdev = to_media_devnode(cd);
63 mutex_lock(&media_devnode_lock);
65 /* Delete the cdev on this minor as well */
66 cdev_del(&mdev->cdev);
68 /* Mark device node number as free */
69 clear_bit(mdev->minor, media_devnode_nums);
71 mutex_unlock(&media_devnode_lock);
73 /* Release media_devnode and perform other cleanups as needed. */
74 if (mdev->release)
75 mdev->release(mdev);
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 *mdev = media_devnode_data(filp);
87 if (!mdev->fops->read)
88 return -EINVAL;
89 if (!media_devnode_is_registered(mdev))
90 return -EIO;
91 return mdev->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 *mdev = media_devnode_data(filp);
99 if (!mdev->fops->write)
100 return -EINVAL;
101 if (!media_devnode_is_registered(mdev))
102 return -EIO;
103 return mdev->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 *mdev = media_devnode_data(filp);
111 if (!media_devnode_is_registered(mdev))
112 return POLLERR | POLLHUP;
113 if (!mdev->fops->poll)
114 return DEFAULT_POLLMASK;
115 return mdev->fops->poll(filp, poll);
118 static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
120 struct media_devnode *mdev = media_devnode_data(filp);
122 if (!mdev->fops->ioctl)
123 return -ENOTTY;
125 if (!media_devnode_is_registered(mdev))
126 return -EIO;
128 return mdev->fops->ioctl(filp, cmd, arg);
131 /* Override for the open function */
132 static int media_open(struct inode *inode, struct file *filp)
134 struct media_devnode *mdev;
135 int ret;
137 /* Check if the media device is available. This needs to be done with
138 * the media_devnode_lock held to prevent an open/unregister race:
139 * without the lock, the device could be unregistered and freed between
140 * the media_devnode_is_registered() and get_device() calls, leading to
141 * a crash.
143 mutex_lock(&media_devnode_lock);
144 mdev = container_of(inode->i_cdev, struct media_devnode, cdev);
145 /* return ENXIO if the media device has been removed
146 already or if it is not registered anymore. */
147 if (!media_devnode_is_registered(mdev)) {
148 mutex_unlock(&media_devnode_lock);
149 return -ENXIO;
151 /* and increase the device refcount */
152 get_device(&mdev->dev);
153 mutex_unlock(&media_devnode_lock);
155 filp->private_data = mdev;
157 if (mdev->fops->open) {
158 ret = mdev->fops->open(filp);
159 if (ret) {
160 put_device(&mdev->dev);
161 return ret;
165 return 0;
168 /* Override for the release function */
169 static int media_release(struct inode *inode, struct file *filp)
171 struct media_devnode *mdev = media_devnode_data(filp);
172 int ret = 0;
174 if (mdev->fops->release)
175 mdev->fops->release(filp);
177 /* decrease the refcount unconditionally since the release()
178 return value is ignored. */
179 put_device(&mdev->dev);
180 filp->private_data = NULL;
181 return ret;
184 static const struct file_operations media_devnode_fops = {
185 .owner = THIS_MODULE,
186 .read = media_read,
187 .write = media_write,
188 .open = media_open,
189 .unlocked_ioctl = media_ioctl,
190 .release = media_release,
191 .poll = media_poll,
192 .llseek = no_llseek,
196 * media_devnode_register - register a media device node
197 * @mdev: media device node structure we want to register
199 * The registration code assigns minor numbers and registers the new device node
200 * with the kernel. An error is returned if no free minor number can be found,
201 * or if the registration of the device node fails.
203 * Zero is returned on success.
205 * Note that if the media_devnode_register call fails, the release() callback of
206 * the media_devnode structure is *not* called, so the caller is responsible for
207 * freeing any data.
209 int __must_check media_devnode_register(struct media_devnode *mdev)
211 int minor;
212 int ret;
214 /* Part 1: Find a free minor number */
215 mutex_lock(&media_devnode_lock);
216 minor = find_next_zero_bit(media_devnode_nums, 0, MEDIA_NUM_DEVICES);
217 if (minor == MEDIA_NUM_DEVICES) {
218 mutex_unlock(&media_devnode_lock);
219 printk(KERN_ERR "could not get a free minor\n");
220 return -ENFILE;
223 set_bit(mdev->minor, media_devnode_nums);
224 mutex_unlock(&media_devnode_lock);
226 mdev->minor = minor;
228 /* Part 2: Initialize and register the character device */
229 cdev_init(&mdev->cdev, &media_devnode_fops);
230 mdev->cdev.owner = mdev->fops->owner;
232 ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1);
233 if (ret < 0) {
234 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
235 goto error;
238 /* Part 3: Register the media device */
239 mdev->dev.bus = &media_bus_type;
240 mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
241 mdev->dev.release = media_devnode_release;
242 if (mdev->parent)
243 mdev->dev.parent = mdev->parent;
244 dev_set_name(&mdev->dev, "media%d", mdev->minor);
245 ret = device_register(&mdev->dev);
246 if (ret < 0) {
247 printk(KERN_ERR "%s: device_register failed\n", __func__);
248 goto error;
251 /* Part 4: Activate this minor. The char device can now be used. */
252 set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
254 return 0;
256 error:
257 cdev_del(&mdev->cdev);
258 clear_bit(mdev->minor, media_devnode_nums);
259 return ret;
263 * media_devnode_unregister - unregister a media device node
264 * @mdev: the device node to unregister
266 * This unregisters the passed device. Future open calls will be met with
267 * errors.
269 * This function can safely be called if the device node has never been
270 * registered or has already been unregistered.
272 void media_devnode_unregister(struct media_devnode *mdev)
274 /* Check if mdev was ever registered at all */
275 if (!media_devnode_is_registered(mdev))
276 return;
278 mutex_lock(&media_devnode_lock);
279 clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
280 mutex_unlock(&media_devnode_lock);
281 device_unregister(&mdev->dev);
285 * Initialise media for linux
287 static int __init media_devnode_init(void)
289 int ret;
291 printk(KERN_INFO "Linux media interface: v0.10\n");
292 ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
293 MEDIA_NAME);
294 if (ret < 0) {
295 printk(KERN_WARNING "media: unable to allocate major\n");
296 return ret;
299 ret = bus_register(&media_bus_type);
300 if (ret < 0) {
301 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
302 printk(KERN_WARNING "media: bus_register failed\n");
303 return -EIO;
306 return 0;
309 static void __exit media_devnode_exit(void)
311 bus_unregister(&media_bus_type);
312 unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
315 module_init(media_devnode_init)
316 module_exit(media_devnode_exit)
318 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
319 MODULE_DESCRIPTION("Device node registration for media drivers");
320 MODULE_LICENSE("GPL");