blob: 0320fb84119cf42d25affe9db19bda3eafedc602 [file] [log] [blame]
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001/*
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{
Hans Verkuil22a04f12008-07-20 06:35:02 -030044 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030045 return sprintf(buf, "%i\n", vfd->index);
46}
47
48static ssize_t show_name(struct device *cd,
49 struct device_attribute *attr, char *buf)
50{
Hans Verkuil22a04f12008-07-20 06:35:02 -030051 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030052 return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
53}
54
55static struct device_attribute video_device_attrs[] = {
56 __ATTR(name, S_IRUGO, show_name, NULL),
57 __ATTR(index, S_IRUGO, show_index, NULL),
58 __ATTR_NULL
59};
60
61struct video_device *video_device_alloc(void)
62{
63 struct video_device *vfd;
64
65 vfd = kzalloc(sizeof(*vfd), GFP_KERNEL);
66 return vfd;
67}
68EXPORT_SYMBOL(video_device_alloc);
69
70void video_device_release(struct video_device *vfd)
71{
72 kfree(vfd);
73}
74EXPORT_SYMBOL(video_device_release);
75
76static void video_release(struct device *cd)
77{
Hans Verkuil22a04f12008-07-20 06:35:02 -030078 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030079
80#if 1
81 /* needed until all drivers are fixed */
82 if (!vfd->release)
83 return;
84#endif
85 vfd->release(vfd);
86}
87
88static struct class video_class = {
89 .name = VIDEO_NAME,
90 .dev_attrs = video_device_attrs,
91 .dev_release = video_release,
92};
93
94/*
95 * Active devices
96 */
97
98static struct video_device *video_device[VIDEO_NUM_DEVICES];
99static DEFINE_MUTEX(videodev_lock);
100
101struct video_device *video_devdata(struct file *file)
102{
103 return video_device[iminor(file->f_path.dentry->d_inode)];
104}
105EXPORT_SYMBOL(video_devdata);
106
107/*
108 * Open a video device - FIXME: Obsoleted
109 */
110static int video_open(struct inode *inode, struct file *file)
111{
112 unsigned int minor = iminor(inode);
113 int err = 0;
114 struct video_device *vfl;
115 const struct file_operations *old_fops;
116
117 if (minor >= VIDEO_NUM_DEVICES)
118 return -ENODEV;
119 lock_kernel();
120 mutex_lock(&videodev_lock);
121 vfl = video_device[minor];
122 if (vfl == NULL) {
123 mutex_unlock(&videodev_lock);
124 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
125 mutex_lock(&videodev_lock);
126 vfl = video_device[minor];
127 if (vfl == NULL) {
128 mutex_unlock(&videodev_lock);
129 unlock_kernel();
130 return -ENODEV;
131 }
132 }
133 old_fops = file->f_op;
134 file->f_op = fops_get(vfl->fops);
135 if (file->f_op->open)
136 err = file->f_op->open(inode, file);
137 if (err) {
138 fops_put(file->f_op);
139 file->f_op = fops_get(old_fops);
140 }
141 fops_put(old_fops);
142 mutex_unlock(&videodev_lock);
143 unlock_kernel();
144 return err;
145}
146
147/*
148 * open/release helper functions -- handle exclusive opens
149 * Should be removed soon
150 */
151int video_exclusive_open(struct inode *inode, struct file *file)
152{
153 struct video_device *vfl = video_devdata(file);
154 int retval = 0;
155
156 mutex_lock(&vfl->lock);
157 if (vfl->users)
158 retval = -EBUSY;
159 else
160 vfl->users++;
161 mutex_unlock(&vfl->lock);
162 return retval;
163}
164EXPORT_SYMBOL(video_exclusive_open);
165
166int video_exclusive_release(struct inode *inode, struct file *file)
167{
168 struct video_device *vfl = video_devdata(file);
169
170 vfl->users--;
171 return 0;
172}
173EXPORT_SYMBOL(video_exclusive_release);
174
175/**
176 * get_index - assign stream number based on parent device
177 * @vdev: video_device to assign index number to, vdev->dev should be assigned
178 * @num: -1 if auto assign, requested number otherwise
179 *
180 *
181 * returns -ENFILE if num is already in use, a free index number if
182 * successful.
183 */
184static int get_index(struct video_device *vdev, int num)
185{
186 u32 used = 0;
187 const int max_index = sizeof(used) * 8 - 1;
188 int i;
189
190 /* Currently a single v4l driver instance cannot create more than
191 32 devices.
192 Increase to u64 or an array of u32 if more are needed. */
193 if (num > max_index) {
194 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
195 return -EINVAL;
196 }
197
198 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
199 if (video_device[i] != NULL &&
200 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300201 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300202 used |= 1 << video_device[i]->index;
203 }
204 }
205
206 if (num >= 0) {
207 if (used & (1 << num))
208 return -ENFILE;
209 return num;
210 }
211
212 i = ffz(used);
213 return i > max_index ? -ENFILE : i;
214}
215
216static const struct file_operations video_fops;
217
218int video_register_device(struct video_device *vfd, int type, int nr)
219{
220 return video_register_device_index(vfd, type, nr, -1);
221}
222EXPORT_SYMBOL(video_register_device);
223
224/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300225 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300226 * @vfd: video device structure we want to register
227 * @type: type of device to register
228 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
229 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300230 * @index: stream number based on parent device;
231 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300232 *
233 * The registration code assigns minor numbers based on the type
234 * requested. -ENFILE is returned in all the device slots for this
235 * category are full. If not then the minor field is set and the
236 * driver initialize function is called (if non %NULL).
237 *
238 * Zero is returned on success.
239 *
240 * Valid types are
241 *
242 * %VFL_TYPE_GRABBER - A frame grabber
243 *
244 * %VFL_TYPE_VTX - A teletext device
245 *
246 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
247 *
248 * %VFL_TYPE_RADIO - A radio card
249 */
250
251int video_register_device_index(struct video_device *vfd, int type, int nr,
252 int index)
253{
254 int i = 0;
255 int base;
256 int end;
257 int ret;
258 char *name_base;
259
260 switch (type) {
261 case VFL_TYPE_GRABBER:
262 base = MINOR_VFL_TYPE_GRABBER_MIN;
263 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
264 name_base = "video";
265 break;
266 case VFL_TYPE_VTX:
267 base = MINOR_VFL_TYPE_VTX_MIN;
268 end = MINOR_VFL_TYPE_VTX_MAX+1;
269 name_base = "vtx";
270 break;
271 case VFL_TYPE_VBI:
272 base = MINOR_VFL_TYPE_VBI_MIN;
273 end = MINOR_VFL_TYPE_VBI_MAX+1;
274 name_base = "vbi";
275 break;
276 case VFL_TYPE_RADIO:
277 base = MINOR_VFL_TYPE_RADIO_MIN;
278 end = MINOR_VFL_TYPE_RADIO_MAX+1;
279 name_base = "radio";
280 break;
281 default:
282 printk(KERN_ERR "%s called with unknown type: %d\n",
283 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300284 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300285 }
286
287 /* pick a minor number */
288 mutex_lock(&videodev_lock);
289 if (nr >= 0 && nr < end-base) {
290 /* use the one the driver asked for */
291 i = base + nr;
292 if (NULL != video_device[i]) {
293 mutex_unlock(&videodev_lock);
294 return -ENFILE;
295 }
296 } else {
297 /* use first free */
298 for (i = base; i < end; i++)
299 if (NULL == video_device[i])
300 break;
301 if (i == end) {
302 mutex_unlock(&videodev_lock);
303 return -ENFILE;
304 }
305 }
306 video_device[i] = vfd;
Hans Verkuil0ea6bc82008-07-26 08:26:43 -0300307 vfd->vfl_type = type;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300308 vfd->minor = i;
309
310 ret = get_index(vfd, index);
311 vfd->index = ret;
312
313 mutex_unlock(&videodev_lock);
314
315 if (ret < 0) {
316 printk(KERN_ERR "%s: get_index failed\n", __func__);
317 goto fail_minor;
318 }
319
320 mutex_init(&vfd->lock);
321
322 /* sysfs class */
Hans Verkuil22a04f12008-07-20 06:35:02 -0300323 memset(&vfd->dev, 0x00, sizeof(vfd->dev));
324 vfd->dev.class = &video_class;
325 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300326 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300327 vfd->dev.parent = vfd->parent;
328 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
329 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300330 if (ret < 0) {
331 printk(KERN_ERR "%s: device_register failed\n", __func__);
332 goto fail_minor;
333 }
334
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;
343
344fail_minor:
345 mutex_lock(&videodev_lock);
346 video_device[vfd->minor] = NULL;
347 vfd->minor = -1;
348 mutex_unlock(&videodev_lock);
349 return ret;
350}
351EXPORT_SYMBOL(video_register_device_index);
352
353/**
354 * video_unregister_device - unregister a video4linux device
355 * @vfd: the device to unregister
356 *
357 * This unregisters the passed device and deassigns the minor
358 * number. Future open calls will be met with errors.
359 */
360
361void video_unregister_device(struct video_device *vfd)
362{
363 mutex_lock(&videodev_lock);
364 if (video_device[vfd->minor] != vfd)
365 panic("videodev: bad unregister");
366
367 video_device[vfd->minor] = NULL;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300368 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300369 mutex_unlock(&videodev_lock);
370}
371EXPORT_SYMBOL(video_unregister_device);
372
373/*
374 * Video fs operations
375 */
376static const struct file_operations video_fops = {
377 .owner = THIS_MODULE,
378 .llseek = no_llseek,
379 .open = video_open,
380};
381
382/*
383 * Initialise video for linux
384 */
385
386static int __init videodev_init(void)
387{
388 int ret;
389
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;
394 }
395
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;
401 }
402
403 return 0;
404}
405
406static void __exit videodev_exit(void)
407{
408 class_unregister(&video_class);
409 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
410}
411
412module_init(videodev_init)
413module_exit(videodev_exit)
414
415MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
416MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
417MODULE_LICENSE("GPL");
418
419
420/*
421 * Local variables:
422 * c-basic-offset: 8
423 * End:
424 */