blob: fda48a2efda69d62fc3a343c0ae0f9a8cfe49208 [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
Hans Verkuilf9e86b52008-08-23 05:47:41 -030076void video_device_release_empty(struct video_device *vfd)
77{
78 /* Do nothing */
79 /* Only valid when the video_device struct is a static. */
80}
81EXPORT_SYMBOL(video_device_release_empty);
82
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030083static void video_release(struct device *cd)
84{
Hans Verkuil22a04f12008-07-20 06:35:02 -030085 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030086
87#if 1
88 /* needed until all drivers are fixed */
89 if (!vfd->release)
90 return;
91#endif
92 vfd->release(vfd);
93}
94
95static struct class video_class = {
96 .name = VIDEO_NAME,
97 .dev_attrs = video_device_attrs,
98 .dev_release = video_release,
99};
100
101/*
102 * Active devices
103 */
104
105static struct video_device *video_device[VIDEO_NUM_DEVICES];
106static DEFINE_MUTEX(videodev_lock);
107
108struct video_device *video_devdata(struct file *file)
109{
110 return video_device[iminor(file->f_path.dentry->d_inode)];
111}
112EXPORT_SYMBOL(video_devdata);
113
114/*
115 * Open a video device - FIXME: Obsoleted
116 */
117static int video_open(struct inode *inode, struct file *file)
118{
119 unsigned int minor = iminor(inode);
120 int err = 0;
121 struct video_device *vfl;
122 const struct file_operations *old_fops;
123
124 if (minor >= VIDEO_NUM_DEVICES)
125 return -ENODEV;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300126 mutex_lock(&videodev_lock);
127 vfl = video_device[minor];
128 if (vfl == NULL) {
129 mutex_unlock(&videodev_lock);
130 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
131 mutex_lock(&videodev_lock);
132 vfl = video_device[minor];
133 if (vfl == NULL) {
134 mutex_unlock(&videodev_lock);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300135 return -ENODEV;
136 }
137 }
138 old_fops = file->f_op;
139 file->f_op = fops_get(vfl->fops);
140 if (file->f_op->open)
141 err = file->f_op->open(inode, file);
142 if (err) {
143 fops_put(file->f_op);
144 file->f_op = fops_get(old_fops);
145 }
146 fops_put(old_fops);
147 mutex_unlock(&videodev_lock);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300148 return err;
149}
150
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300151/**
152 * get_index - assign stream number based on parent device
153 * @vdev: video_device to assign index number to, vdev->dev should be assigned
154 * @num: -1 if auto assign, requested number otherwise
155 *
156 *
157 * returns -ENFILE if num is already in use, a free index number if
158 * successful.
159 */
160static int get_index(struct video_device *vdev, int num)
161{
162 u32 used = 0;
163 const int max_index = sizeof(used) * 8 - 1;
164 int i;
165
166 /* Currently a single v4l driver instance cannot create more than
167 32 devices.
168 Increase to u64 or an array of u32 if more are needed. */
169 if (num > max_index) {
170 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
171 return -EINVAL;
172 }
173
174 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
175 if (video_device[i] != NULL &&
176 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300177 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300178 used |= 1 << video_device[i]->index;
179 }
180 }
181
182 if (num >= 0) {
183 if (used & (1 << num))
184 return -ENFILE;
185 return num;
186 }
187
188 i = ffz(used);
189 return i > max_index ? -ENFILE : i;
190}
191
192static const struct file_operations video_fops;
193
194int video_register_device(struct video_device *vfd, int type, int nr)
195{
196 return video_register_device_index(vfd, type, nr, -1);
197}
198EXPORT_SYMBOL(video_register_device);
199
200/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300201 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300202 * @vfd: video device structure we want to register
203 * @type: type of device to register
204 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
205 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300206 * @index: stream number based on parent device;
207 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300208 *
209 * The registration code assigns minor numbers based on the type
210 * requested. -ENFILE is returned in all the device slots for this
211 * category are full. If not then the minor field is set and the
212 * driver initialize function is called (if non %NULL).
213 *
214 * Zero is returned on success.
215 *
216 * Valid types are
217 *
218 * %VFL_TYPE_GRABBER - A frame grabber
219 *
220 * %VFL_TYPE_VTX - A teletext device
221 *
222 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
223 *
224 * %VFL_TYPE_RADIO - A radio card
225 */
226
227int video_register_device_index(struct video_device *vfd, int type, int nr,
228 int index)
229{
230 int i = 0;
231 int base;
232 int end;
233 int ret;
234 char *name_base;
235
Henrik Kretzschmarf3b9f502008-09-03 17:11:53 -0300236 if (vfd == NULL)
237 return -EINVAL;
238
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300239 if (vfd == NULL)
240 return -EINVAL;
241
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300242 switch (type) {
243 case VFL_TYPE_GRABBER:
244 base = MINOR_VFL_TYPE_GRABBER_MIN;
245 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
246 name_base = "video";
247 break;
248 case VFL_TYPE_VTX:
249 base = MINOR_VFL_TYPE_VTX_MIN;
250 end = MINOR_VFL_TYPE_VTX_MAX+1;
251 name_base = "vtx";
252 break;
253 case VFL_TYPE_VBI:
254 base = MINOR_VFL_TYPE_VBI_MIN;
255 end = MINOR_VFL_TYPE_VBI_MAX+1;
256 name_base = "vbi";
257 break;
258 case VFL_TYPE_RADIO:
259 base = MINOR_VFL_TYPE_RADIO_MIN;
260 end = MINOR_VFL_TYPE_RADIO_MAX+1;
261 name_base = "radio";
262 break;
263 default:
264 printk(KERN_ERR "%s called with unknown type: %d\n",
265 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300266 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300267 }
268
269 /* pick a minor number */
270 mutex_lock(&videodev_lock);
271 if (nr >= 0 && nr < end-base) {
272 /* use the one the driver asked for */
273 i = base + nr;
274 if (NULL != video_device[i]) {
275 mutex_unlock(&videodev_lock);
276 return -ENFILE;
277 }
278 } else {
279 /* use first free */
280 for (i = base; i < end; i++)
281 if (NULL == video_device[i])
282 break;
283 if (i == end) {
284 mutex_unlock(&videodev_lock);
285 return -ENFILE;
286 }
287 }
288 video_device[i] = vfd;
Hans Verkuil0ea6bc82008-07-26 08:26:43 -0300289 vfd->vfl_type = type;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300290 vfd->minor = i;
291
292 ret = get_index(vfd, index);
293 vfd->index = ret;
294
295 mutex_unlock(&videodev_lock);
296
297 if (ret < 0) {
298 printk(KERN_ERR "%s: get_index failed\n", __func__);
299 goto fail_minor;
300 }
301
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300302 /* sysfs class */
Hans Verkuil22a04f12008-07-20 06:35:02 -0300303 memset(&vfd->dev, 0x00, sizeof(vfd->dev));
304 vfd->dev.class = &video_class;
305 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300306 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300307 vfd->dev.parent = vfd->parent;
308 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
309 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300310 if (ret < 0) {
311 printk(KERN_ERR "%s: device_register failed\n", __func__);
312 goto fail_minor;
313 }
314
315#if 1
316 /* needed until all drivers are fixed */
317 if (!vfd->release)
318 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
319 "Please fix your driver for proper sysfs support, see "
320 "http://lwn.net/Articles/36850/\n", vfd->name);
321#endif
322 return 0;
323
324fail_minor:
325 mutex_lock(&videodev_lock);
326 video_device[vfd->minor] = NULL;
327 vfd->minor = -1;
328 mutex_unlock(&videodev_lock);
329 return ret;
330}
331EXPORT_SYMBOL(video_register_device_index);
332
333/**
334 * video_unregister_device - unregister a video4linux device
335 * @vfd: the device to unregister
336 *
337 * This unregisters the passed device and deassigns the minor
338 * number. Future open calls will be met with errors.
339 */
340
341void video_unregister_device(struct video_device *vfd)
342{
343 mutex_lock(&videodev_lock);
344 if (video_device[vfd->minor] != vfd)
345 panic("videodev: bad unregister");
346
347 video_device[vfd->minor] = NULL;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300348 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300349 mutex_unlock(&videodev_lock);
350}
351EXPORT_SYMBOL(video_unregister_device);
352
353/*
354 * Video fs operations
355 */
356static const struct file_operations video_fops = {
357 .owner = THIS_MODULE,
358 .llseek = no_llseek,
359 .open = video_open,
360};
361
362/*
363 * Initialise video for linux
364 */
365
366static int __init videodev_init(void)
367{
368 int ret;
369
370 printk(KERN_INFO "Linux video capture interface: v2.00\n");
371 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
372 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
373 return -EIO;
374 }
375
376 ret = class_register(&video_class);
377 if (ret < 0) {
378 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
379 printk(KERN_WARNING "video_dev: class_register failed\n");
380 return -EIO;
381 }
382
383 return 0;
384}
385
386static void __exit videodev_exit(void)
387{
388 class_unregister(&video_class);
389 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
390}
391
392module_init(videodev_init)
393module_exit(videodev_exit)
394
395MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
396MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
397MODULE_LICENSE("GPL");
398
399
400/*
401 * Local variables:
402 * c-basic-offset: 8
403 * End:
404 */