aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/v4l2-dev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/v4l2-dev.c')
-rw-r--r--drivers/media/video/v4l2-dev.c427
1 files changed, 427 insertions, 0 deletions
diff --git a/drivers/media/video/v4l2-dev.c b/drivers/media/video/v4l2-dev.c
new file mode 100644
index 000000000000..ccd6566a515e
--- /dev/null
+++ b/drivers/media/video/v4l2-dev.c
@@ -0,0 +1,427 @@
1/*
2 * Video capture interface for Linux version 2
3 *
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
6 *
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.
11 *
12 * Authors: Alan Cox, <alan@redhat.com> (version 1)
13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
14 *
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
17 */
18
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>
31
32#include <media/v4l2-common.h>
33
34#define VIDEO_NUM_DEVICES 256
35#define VIDEO_NAME "video4linux"
36
37/*
38 * sysfs stuff
39 */
40
41static ssize_t show_index(struct device *cd,
42 struct device_attribute *attr, char *buf)
43{
44 struct video_device *vfd = container_of(cd, struct video_device, dev);
45
46 return sprintf(buf, "%i\n", vfd->index);
47}
48
49static ssize_t show_name(struct device *cd,
50 struct device_attribute *attr, char *buf)
51{
52 struct video_device *vfd = container_of(cd, struct video_device, dev);
53
54 return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
55}
56
57static 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
61};
62
63/*
64 * Active devices
65 */
66static struct video_device *video_device[VIDEO_NUM_DEVICES];
67static DEFINE_MUTEX(videodev_lock);
68static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
69
70struct video_device *video_device_alloc(void)
71{
72 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
73}
74EXPORT_SYMBOL(video_device_alloc);
75
76void video_device_release(struct video_device *vfd)
77{
78 kfree(vfd);
79}
80EXPORT_SYMBOL(video_device_release);
81
82void video_device_release_empty(struct video_device *vfd)
83{
84 /* Do nothing */
85 /* Only valid when the video_device struct is a static. */
86}
87EXPORT_SYMBOL(video_device_release_empty);
88
89/* Called when the last user of the character device is gone. */
90static void v4l2_chardev_release(struct kobject *kobj)
91{
92 struct video_device *vfd = container_of(kobj, struct video_device, cdev.kobj);
93
94 mutex_lock(&videodev_lock);
95 if (video_device[vfd->minor] != vfd) {
96 mutex_unlock(&videodev_lock);
97 BUG();
98 return;
99 }
100
101 /* Free up this device for reuse */
102 video_device[vfd->minor] = NULL;
103 clear_bit(vfd->num, video_nums[vfd->vfl_type]);
104 mutex_unlock(&videodev_lock);
105
106 /* Release the character device */
107 vfd->cdev_release(kobj);
108 /* Release video_device and perform other
109 cleanups as needed. */
110 if (vfd->release)
111 vfd->release(vfd);
112}
113
114/* The new kobj_type for the character device */
115static struct kobj_type v4l2_ktype_cdev_default = {
116 .release = v4l2_chardev_release,
117};
118
119static void video_release(struct device *cd)
120{
121 struct video_device *vfd = container_of(cd, struct video_device, dev);
122
123 /* It's now safe to delete the char device.
124 This will either trigger the v4l2_chardev_release immediately (if
125 the refcount goes to 0) or later when the last user of the
126 character device closes it. */
127 cdev_del(&vfd->cdev);
128}
129
130static struct class video_class = {
131 .name = VIDEO_NAME,
132 .dev_attrs = video_device_attrs,
133 .dev_release = video_release,
134};
135
136struct video_device *video_devdata(struct file *file)
137{
138 return video_device[iminor(file->f_path.dentry->d_inode)];
139}
140EXPORT_SYMBOL(video_devdata);
141
142/**
143 * get_index - assign stream number based on parent device
144 * @vdev: video_device to assign index number to, vdev->dev should be assigned
145 * @num: -1 if auto assign, requested number otherwise
146 *
147 *
148 * returns -ENFILE if num is already in use, a free index number if
149 * successful.
150 */
151static int get_index(struct video_device *vdev, int num)
152{
153 u32 used = 0;
154 const int max_index = sizeof(used) * 8 - 1;
155 int i;
156
157 /* Currently a single v4l driver instance cannot create more than
158 32 devices.
159 Increase to u64 or an array of u32 if more are needed. */
160 if (num > max_index) {
161 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
162 return -EINVAL;
163 }
164
165 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
166 if (video_device[i] != NULL &&
167 video_device[i] != vdev &&
168 video_device[i]->parent == vdev->parent) {
169 used |= 1 << video_device[i]->index;
170 }
171 }
172
173 if (num >= 0) {
174 if (used & (1 << num))
175 return -ENFILE;
176 return num;
177 }
178
179 i = ffz(used);
180 return i > max_index ? -ENFILE : i;
181}
182
183static const struct file_operations video_fops;
184
185int video_register_device(struct video_device *vfd, int type, int nr)
186{
187 return video_register_device_index(vfd, type, nr, -1);
188}
189EXPORT_SYMBOL(video_register_device);
190
191/**
192 * video_register_device_index - register video4linux devices
193 * @vfd: video device structure we want to register
194 * @type: type of device to register
195 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
196 * -1 == first free)
197 * @index: stream number based on parent device;
198 * -1 if auto assign, requested number otherwise
199 *
200 * The registration code assigns minor numbers based on the type
201 * requested. -ENFILE is returned in all the device slots for this
202 * category are full. If not then the minor field is set and the
203 * driver initialize function is called (if non %NULL).
204 *
205 * Zero is returned on success.
206 *
207 * Valid types are
208 *
209 * %VFL_TYPE_GRABBER - A frame grabber
210 *
211 * %VFL_TYPE_VTX - A teletext device
212 *
213 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
214 *
215 * %VFL_TYPE_RADIO - A radio card
216 */
217
218int video_register_device_index(struct video_device *vfd, int type, int nr,
219 int index)
220{
221 int i = 0;
222 int ret;
223 int minor_offset = 0;
224 int minor_cnt = VIDEO_NUM_DEVICES;
225 const char *name_base;
226 void *priv = video_get_drvdata(vfd);
227
228 /* the release callback MUST be present */
229 BUG_ON(!vfd->release);
230
231 if (vfd == NULL)
232 return -EINVAL;
233
234 switch (type) {
235 case VFL_TYPE_GRABBER:
236 name_base = "video";
237 break;
238 case VFL_TYPE_VTX:
239 name_base = "vtx";
240 break;
241 case VFL_TYPE_VBI:
242 name_base = "vbi";
243 break;
244 case VFL_TYPE_RADIO:
245 name_base = "radio";
246 break;
247 default:
248 printk(KERN_ERR "%s called with unknown type: %d\n",
249 __func__, type);
250 return -EINVAL;
251 }
252
253 vfd->vfl_type = type;
254
255#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
256 /* Keep the ranges for the first four types for historical
257 * reasons.
258 * Newer devices (not yet in place) should use the range
259 * of 128-191 and just pick the first free minor there
260 * (new style). */
261 switch (type) {
262 case VFL_TYPE_GRABBER:
263 minor_offset = 0;
264 minor_cnt = 64;
265 break;
266 case VFL_TYPE_RADIO:
267 minor_offset = 64;
268 minor_cnt = 64;
269 break;
270 case VFL_TYPE_VTX:
271 minor_offset = 192;
272 minor_cnt = 32;
273 break;
274 case VFL_TYPE_VBI:
275 minor_offset = 224;
276 minor_cnt = 32;
277 break;
278 default:
279 minor_offset = 128;
280 minor_cnt = 64;
281 break;
282 }
283#endif
284
285 /* Initialize the character device */
286 cdev_init(&vfd->cdev, vfd->fops);
287 vfd->cdev.owner = vfd->fops->owner;
288 /* pick a minor number */
289 mutex_lock(&videodev_lock);
290 nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
291 if (nr == minor_cnt)
292 nr = find_first_zero_bit(video_nums[type], minor_cnt);
293 if (nr == minor_cnt) {
294 printk(KERN_ERR "could not get a free kernel number\n");
295 mutex_unlock(&videodev_lock);
296 return -ENFILE;
297 }
298#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
299 /* 1-on-1 mapping of kernel number to minor number */
300 i = nr;
301#else
302 /* The kernel number and minor numbers are independent */
303 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
304 if (video_device[i] == NULL)
305 break;
306 if (i == VIDEO_NUM_DEVICES) {
307 mutex_unlock(&videodev_lock);
308 printk(KERN_ERR "could not get a free minor\n");
309 return -ENFILE;
310 }
311#endif
312 vfd->minor = i + minor_offset;
313 vfd->num = nr;
314 set_bit(nr, video_nums[type]);
315 BUG_ON(video_device[vfd->minor]);
316 video_device[vfd->minor] = vfd;
317
318 ret = get_index(vfd, index);
319 vfd->index = ret;
320
321 mutex_unlock(&videodev_lock);
322
323 if (ret < 0) {
324 printk(KERN_ERR "%s: get_index failed\n", __func__);
325 goto fail_minor;
326 }
327
328 ret = cdev_add(&vfd->cdev, MKDEV(VIDEO_MAJOR, vfd->minor), 1);
329 if (ret < 0) {
330 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
331 goto fail_minor;
332 }
333 /* sysfs class */
334 memset(&vfd->dev, 0, sizeof(vfd->dev));
335 /* The memset above cleared the device's drvdata, so
336 put back the copy we made earlier. */
337 video_set_drvdata(vfd, priv);
338 vfd->dev.class = &video_class;
339 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
340 if (vfd->parent)
341 vfd->dev.parent = vfd->parent;
342 sprintf(vfd->dev.bus_id, "%s%d", name_base, nr);
343 ret = device_register(&vfd->dev);
344 if (ret < 0) {
345 printk(KERN_ERR "%s: device_register failed\n", __func__);
346 goto del_cdev;
347 }
348 /* Remember the cdev's release function */
349 vfd->cdev_release = vfd->cdev.kobj.ktype->release;
350 /* Install our own */
351 vfd->cdev.kobj.ktype = &v4l2_ktype_cdev_default;
352 return 0;
353
354del_cdev:
355 cdev_del(&vfd->cdev);
356
357fail_minor:
358 mutex_lock(&videodev_lock);
359 video_device[vfd->minor] = NULL;
360 clear_bit(vfd->num, video_nums[type]);
361 mutex_unlock(&videodev_lock);
362 vfd->minor = -1;
363 return ret;
364}
365EXPORT_SYMBOL(video_register_device_index);
366
367/**
368 * video_unregister_device - unregister a video4linux device
369 * @vfd: the device to unregister
370 *
371 * This unregisters the passed device and deassigns the minor
372 * number. Future open calls will be met with errors.
373 */
374
375void video_unregister_device(struct video_device *vfd)
376{
377 device_unregister(&vfd->dev);
378}
379EXPORT_SYMBOL(video_unregister_device);
380
381/*
382 * Initialise video for linux
383 */
384static int __init videodev_init(void)
385{
386 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
387 int ret;
388
389 printk(KERN_INFO "Linux video capture interface: v2.00\n");
390 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
391 if (ret < 0) {
392 printk(KERN_WARNING "videodev: unable to get major %d\n",
393 VIDEO_MAJOR);
394 return ret;
395 }
396
397 ret = class_register(&video_class);
398 if (ret < 0) {
399 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
400 printk(KERN_WARNING "video_dev: class_register failed\n");
401 return -EIO;
402 }
403
404 return 0;
405}
406
407static void __exit videodev_exit(void)
408{
409 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
410
411 class_unregister(&video_class);
412 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
413}
414
415module_init(videodev_init)
416module_exit(videodev_exit)
417
418MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
419MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
420MODULE_LICENSE("GPL");
421
422
423/*
424 * Local variables:
425 * c-basic-offset: 8
426 * End:
427 */