V4L/DVB (8427): videodev: split off the ioctl handling into v4l2-ioctl.c
[linux-2.6/linux-2.6-openrd.git] / drivers / media / video / v4l2-dev.c
blob2dd82b16bc3545fe53c42cf5f2d21499f445bf45
1 /*
2 * Video capture interface for Linux version 2
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Authors: Alan Cox, <alan@redhat.com> (version 1)
13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kmod.h>
27 #include <linux/slab.h>
28 #include <linux/smp_lock.h>
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
32 #include <media/v4l2-common.h>
34 #define VIDEO_NUM_DEVICES 256
35 #define VIDEO_NAME "video4linux"
38 * sysfs stuff
41 static ssize_t show_index(struct device *cd,
42 struct device_attribute *attr, char *buf)
44 struct video_device *vfd = container_of(cd, struct video_device,
45 class_dev);
46 return sprintf(buf, "%i\n", vfd->index);
49 static ssize_t show_name(struct device *cd,
50 struct device_attribute *attr, char *buf)
52 struct video_device *vfd = container_of(cd, struct video_device,
53 class_dev);
54 return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
57 static struct device_attribute video_device_attrs[] = {
58 __ATTR(name, S_IRUGO, show_name, NULL),
59 __ATTR(index, S_IRUGO, show_index, NULL),
60 __ATTR_NULL
63 struct video_device *video_device_alloc(void)
65 struct video_device *vfd;
67 vfd = kzalloc(sizeof(*vfd), GFP_KERNEL);
68 return vfd;
70 EXPORT_SYMBOL(video_device_alloc);
72 void video_device_release(struct video_device *vfd)
74 kfree(vfd);
76 EXPORT_SYMBOL(video_device_release);
78 static void video_release(struct device *cd)
80 struct video_device *vfd = container_of(cd, struct video_device,
81 class_dev);
83 #if 1
84 /* needed until all drivers are fixed */
85 if (!vfd->release)
86 return;
87 #endif
88 vfd->release(vfd);
91 static struct class video_class = {
92 .name = VIDEO_NAME,
93 .dev_attrs = video_device_attrs,
94 .dev_release = video_release,
98 * Active devices
101 static struct video_device *video_device[VIDEO_NUM_DEVICES];
102 static DEFINE_MUTEX(videodev_lock);
104 struct video_device *video_devdata(struct file *file)
106 return video_device[iminor(file->f_path.dentry->d_inode)];
108 EXPORT_SYMBOL(video_devdata);
111 * Open a video device - FIXME: Obsoleted
113 static int video_open(struct inode *inode, struct file *file)
115 unsigned int minor = iminor(inode);
116 int err = 0;
117 struct video_device *vfl;
118 const struct file_operations *old_fops;
120 if (minor >= VIDEO_NUM_DEVICES)
121 return -ENODEV;
122 lock_kernel();
123 mutex_lock(&videodev_lock);
124 vfl = video_device[minor];
125 if (vfl == NULL) {
126 mutex_unlock(&videodev_lock);
127 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
128 mutex_lock(&videodev_lock);
129 vfl = video_device[minor];
130 if (vfl == NULL) {
131 mutex_unlock(&videodev_lock);
132 unlock_kernel();
133 return -ENODEV;
136 old_fops = file->f_op;
137 file->f_op = fops_get(vfl->fops);
138 if (file->f_op->open)
139 err = file->f_op->open(inode, file);
140 if (err) {
141 fops_put(file->f_op);
142 file->f_op = fops_get(old_fops);
144 fops_put(old_fops);
145 mutex_unlock(&videodev_lock);
146 unlock_kernel();
147 return err;
151 * open/release helper functions -- handle exclusive opens
152 * Should be removed soon
154 int video_exclusive_open(struct inode *inode, struct file *file)
156 struct video_device *vfl = video_devdata(file);
157 int retval = 0;
159 mutex_lock(&vfl->lock);
160 if (vfl->users)
161 retval = -EBUSY;
162 else
163 vfl->users++;
164 mutex_unlock(&vfl->lock);
165 return retval;
167 EXPORT_SYMBOL(video_exclusive_open);
169 int video_exclusive_release(struct inode *inode, struct file *file)
171 struct video_device *vfl = video_devdata(file);
173 vfl->users--;
174 return 0;
176 EXPORT_SYMBOL(video_exclusive_release);
179 * get_index - assign stream number based on parent device
180 * @vdev: video_device to assign index number to, vdev->dev should be assigned
181 * @num: -1 if auto assign, requested number otherwise
184 * returns -ENFILE if num is already in use, a free index number if
185 * successful.
187 static int get_index(struct video_device *vdev, int num)
189 u32 used = 0;
190 const int max_index = sizeof(used) * 8 - 1;
191 int i;
193 /* Currently a single v4l driver instance cannot create more than
194 32 devices.
195 Increase to u64 or an array of u32 if more are needed. */
196 if (num > max_index) {
197 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
198 return -EINVAL;
201 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
202 if (video_device[i] != NULL &&
203 video_device[i] != vdev &&
204 video_device[i]->dev == vdev->dev) {
205 used |= 1 << video_device[i]->index;
209 if (num >= 0) {
210 if (used & (1 << num))
211 return -ENFILE;
212 return num;
215 i = ffz(used);
216 return i > max_index ? -ENFILE : i;
219 static const struct file_operations video_fops;
221 int video_register_device(struct video_device *vfd, int type, int nr)
223 return video_register_device_index(vfd, type, nr, -1);
225 EXPORT_SYMBOL(video_register_device);
228 * video_register_device - register video4linux devices
229 * @vfd: video device structure we want to register
230 * @type: type of device to register
231 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
232 * -1 == first free)
234 * The registration code assigns minor numbers based on the type
235 * requested. -ENFILE is returned in all the device slots for this
236 * category are full. If not then the minor field is set and the
237 * driver initialize function is called (if non %NULL).
239 * Zero is returned on success.
241 * Valid types are
243 * %VFL_TYPE_GRABBER - A frame grabber
245 * %VFL_TYPE_VTX - A teletext device
247 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
249 * %VFL_TYPE_RADIO - A radio card
252 int video_register_device_index(struct video_device *vfd, int type, int nr,
253 int index)
255 int i = 0;
256 int base;
257 int end;
258 int ret;
259 char *name_base;
261 switch (type) {
262 case VFL_TYPE_GRABBER:
263 base = MINOR_VFL_TYPE_GRABBER_MIN;
264 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
265 name_base = "video";
266 break;
267 case VFL_TYPE_VTX:
268 base = MINOR_VFL_TYPE_VTX_MIN;
269 end = MINOR_VFL_TYPE_VTX_MAX+1;
270 name_base = "vtx";
271 break;
272 case VFL_TYPE_VBI:
273 base = MINOR_VFL_TYPE_VBI_MIN;
274 end = MINOR_VFL_TYPE_VBI_MAX+1;
275 name_base = "vbi";
276 break;
277 case VFL_TYPE_RADIO:
278 base = MINOR_VFL_TYPE_RADIO_MIN;
279 end = MINOR_VFL_TYPE_RADIO_MAX+1;
280 name_base = "radio";
281 break;
282 default:
283 printk(KERN_ERR "%s called with unknown type: %d\n",
284 __func__, type);
285 return -1;
288 /* pick a minor number */
289 mutex_lock(&videodev_lock);
290 if (nr >= 0 && nr < end-base) {
291 /* use the one the driver asked for */
292 i = base + nr;
293 if (NULL != video_device[i]) {
294 mutex_unlock(&videodev_lock);
295 return -ENFILE;
297 } else {
298 /* use first free */
299 for (i = base; i < end; i++)
300 if (NULL == video_device[i])
301 break;
302 if (i == end) {
303 mutex_unlock(&videodev_lock);
304 return -ENFILE;
307 video_device[i] = vfd;
308 vfd->minor = i;
310 ret = get_index(vfd, index);
311 vfd->index = ret;
313 mutex_unlock(&videodev_lock);
315 if (ret < 0) {
316 printk(KERN_ERR "%s: get_index failed\n", __func__);
317 goto fail_minor;
320 mutex_init(&vfd->lock);
322 /* sysfs class */
323 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
324 vfd->class_dev.class = &video_class;
325 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
326 if (vfd->dev)
327 vfd->class_dev.parent = vfd->dev;
328 sprintf(vfd->class_dev.bus_id, "%s%d", name_base, i - base);
329 ret = device_register(&vfd->class_dev);
330 if (ret < 0) {
331 printk(KERN_ERR "%s: device_register failed\n", __func__);
332 goto fail_minor;
335 #if 1
336 /* needed until all drivers are fixed */
337 if (!vfd->release)
338 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
339 "Please fix your driver for proper sysfs support, see "
340 "http://lwn.net/Articles/36850/\n", vfd->name);
341 #endif
342 return 0;
344 fail_minor:
345 mutex_lock(&videodev_lock);
346 video_device[vfd->minor] = NULL;
347 vfd->minor = -1;
348 mutex_unlock(&videodev_lock);
349 return ret;
351 EXPORT_SYMBOL(video_register_device_index);
354 * video_unregister_device - unregister a video4linux device
355 * @vfd: the device to unregister
357 * This unregisters the passed device and deassigns the minor
358 * number. Future open calls will be met with errors.
361 void video_unregister_device(struct video_device *vfd)
363 mutex_lock(&videodev_lock);
364 if (video_device[vfd->minor] != vfd)
365 panic("videodev: bad unregister");
367 video_device[vfd->minor] = NULL;
368 device_unregister(&vfd->class_dev);
369 mutex_unlock(&videodev_lock);
371 EXPORT_SYMBOL(video_unregister_device);
374 * Video fs operations
376 static const struct file_operations video_fops = {
377 .owner = THIS_MODULE,
378 .llseek = no_llseek,
379 .open = video_open,
383 * Initialise video for linux
386 static int __init videodev_init(void)
388 int ret;
390 printk(KERN_INFO "Linux video capture interface: v2.00\n");
391 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
392 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
393 return -EIO;
396 ret = class_register(&video_class);
397 if (ret < 0) {
398 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
399 printk(KERN_WARNING "video_dev: class_register failed\n");
400 return -EIO;
403 return 0;
406 static void __exit videodev_exit(void)
408 class_unregister(&video_class);
409 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
412 module_init(videodev_init)
413 module_exit(videodev_exit)
415 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
416 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
417 MODULE_LICENSE("GPL");
421 * Local variables:
422 * c-basic-offset: 8
423 * End: