blob: ff219df4b72596b081cd040baa93375301f834e5 [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
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030087 vfd->release(vfd);
88}
89
90static struct class video_class = {
91 .name = VIDEO_NAME,
92 .dev_attrs = video_device_attrs,
93 .dev_release = video_release,
94};
95
96/*
97 * Active devices
98 */
99
100static struct video_device *video_device[VIDEO_NUM_DEVICES];
101static DEFINE_MUTEX(videodev_lock);
102
103struct video_device *video_devdata(struct file *file)
104{
105 return video_device[iminor(file->f_path.dentry->d_inode)];
106}
107EXPORT_SYMBOL(video_devdata);
108
109/*
110 * Open a video device - FIXME: Obsoleted
111 */
112static int video_open(struct inode *inode, struct file *file)
113{
114 unsigned int minor = iminor(inode);
115 int err = 0;
116 struct video_device *vfl;
117 const struct file_operations *old_fops;
118
119 if (minor >= VIDEO_NUM_DEVICES)
120 return -ENODEV;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300121 mutex_lock(&videodev_lock);
122 vfl = video_device[minor];
123 if (vfl == NULL) {
124 mutex_unlock(&videodev_lock);
125 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
126 mutex_lock(&videodev_lock);
127 vfl = video_device[minor];
128 if (vfl == NULL) {
129 mutex_unlock(&videodev_lock);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300130 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);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300143 return err;
144}
145
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300146/**
147 * get_index - assign stream number based on parent device
148 * @vdev: video_device to assign index number to, vdev->dev should be assigned
149 * @num: -1 if auto assign, requested number otherwise
150 *
151 *
152 * returns -ENFILE if num is already in use, a free index number if
153 * successful.
154 */
155static int get_index(struct video_device *vdev, int num)
156{
157 u32 used = 0;
158 const int max_index = sizeof(used) * 8 - 1;
159 int i;
160
161 /* Currently a single v4l driver instance cannot create more than
162 32 devices.
163 Increase to u64 or an array of u32 if more are needed. */
164 if (num > max_index) {
165 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
166 return -EINVAL;
167 }
168
169 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
170 if (video_device[i] != NULL &&
171 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300172 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300173 used |= 1 << video_device[i]->index;
174 }
175 }
176
177 if (num >= 0) {
178 if (used & (1 << num))
179 return -ENFILE;
180 return num;
181 }
182
183 i = ffz(used);
184 return i > max_index ? -ENFILE : i;
185}
186
187static const struct file_operations video_fops;
188
189int video_register_device(struct video_device *vfd, int type, int nr)
190{
191 return video_register_device_index(vfd, type, nr, -1);
192}
193EXPORT_SYMBOL(video_register_device);
194
195/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300196 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300197 * @vfd: video device structure we want to register
198 * @type: type of device to register
199 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
200 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300201 * @index: stream number based on parent device;
202 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300203 *
204 * The registration code assigns minor numbers based on the type
205 * requested. -ENFILE is returned in all the device slots for this
206 * category are full. If not then the minor field is set and the
207 * driver initialize function is called (if non %NULL).
208 *
209 * Zero is returned on success.
210 *
211 * Valid types are
212 *
213 * %VFL_TYPE_GRABBER - A frame grabber
214 *
215 * %VFL_TYPE_VTX - A teletext device
216 *
217 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
218 *
219 * %VFL_TYPE_RADIO - A radio card
220 */
221
222int video_register_device_index(struct video_device *vfd, int type, int nr,
223 int index)
224{
225 int i = 0;
226 int base;
227 int end;
228 int ret;
229 char *name_base;
230
Hans Verkuild6e74972008-08-23 06:27:59 -0300231 /* the release callback MUST be present */
232 BUG_ON(!vfd->release);
Henrik Kretzschmarf3b9f502008-09-03 17:11:53 -0300233
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300234 if (vfd == NULL)
235 return -EINVAL;
236
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300237 switch (type) {
238 case VFL_TYPE_GRABBER:
239 base = MINOR_VFL_TYPE_GRABBER_MIN;
240 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
241 name_base = "video";
242 break;
243 case VFL_TYPE_VTX:
244 base = MINOR_VFL_TYPE_VTX_MIN;
245 end = MINOR_VFL_TYPE_VTX_MAX+1;
246 name_base = "vtx";
247 break;
248 case VFL_TYPE_VBI:
249 base = MINOR_VFL_TYPE_VBI_MIN;
250 end = MINOR_VFL_TYPE_VBI_MAX+1;
251 name_base = "vbi";
252 break;
253 case VFL_TYPE_RADIO:
254 base = MINOR_VFL_TYPE_RADIO_MIN;
255 end = MINOR_VFL_TYPE_RADIO_MAX+1;
256 name_base = "radio";
257 break;
258 default:
259 printk(KERN_ERR "%s called with unknown type: %d\n",
260 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300261 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300262 }
263
264 /* pick a minor number */
265 mutex_lock(&videodev_lock);
266 if (nr >= 0 && nr < end-base) {
267 /* use the one the driver asked for */
268 i = base + nr;
269 if (NULL != video_device[i]) {
270 mutex_unlock(&videodev_lock);
271 return -ENFILE;
272 }
273 } else {
274 /* use first free */
275 for (i = base; i < end; i++)
276 if (NULL == video_device[i])
277 break;
278 if (i == end) {
279 mutex_unlock(&videodev_lock);
280 return -ENFILE;
281 }
282 }
283 video_device[i] = vfd;
Hans Verkuil0ea6bc82008-07-26 08:26:43 -0300284 vfd->vfl_type = type;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300285 vfd->minor = i;
286
287 ret = get_index(vfd, index);
288 vfd->index = ret;
289
290 mutex_unlock(&videodev_lock);
291
292 if (ret < 0) {
293 printk(KERN_ERR "%s: get_index failed\n", __func__);
294 goto fail_minor;
295 }
296
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300297 /* sysfs class */
Hans Verkuil22a04f12008-07-20 06:35:02 -0300298 memset(&vfd->dev, 0x00, sizeof(vfd->dev));
299 vfd->dev.class = &video_class;
300 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300301 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300302 vfd->dev.parent = vfd->parent;
303 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
304 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300305 if (ret < 0) {
306 printk(KERN_ERR "%s: device_register failed\n", __func__);
307 goto fail_minor;
308 }
309
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300310 return 0;
311
312fail_minor:
313 mutex_lock(&videodev_lock);
314 video_device[vfd->minor] = NULL;
315 vfd->minor = -1;
316 mutex_unlock(&videodev_lock);
317 return ret;
318}
319EXPORT_SYMBOL(video_register_device_index);
320
321/**
322 * video_unregister_device - unregister a video4linux device
323 * @vfd: the device to unregister
324 *
325 * This unregisters the passed device and deassigns the minor
326 * number. Future open calls will be met with errors.
327 */
328
329void video_unregister_device(struct video_device *vfd)
330{
331 mutex_lock(&videodev_lock);
332 if (video_device[vfd->minor] != vfd)
333 panic("videodev: bad unregister");
334
335 video_device[vfd->minor] = NULL;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300336 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300337 mutex_unlock(&videodev_lock);
338}
339EXPORT_SYMBOL(video_unregister_device);
340
341/*
342 * Video fs operations
343 */
344static const struct file_operations video_fops = {
345 .owner = THIS_MODULE,
346 .llseek = no_llseek,
347 .open = video_open,
348};
349
350/*
351 * Initialise video for linux
352 */
353
354static int __init videodev_init(void)
355{
356 int ret;
357
358 printk(KERN_INFO "Linux video capture interface: v2.00\n");
359 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
360 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
361 return -EIO;
362 }
363
364 ret = class_register(&video_class);
365 if (ret < 0) {
366 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
367 printk(KERN_WARNING "video_dev: class_register failed\n");
368 return -EIO;
369 }
370
371 return 0;
372}
373
374static void __exit videodev_exit(void)
375{
376 class_unregister(&video_class);
377 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
378}
379
380module_init(videodev_init)
381module_exit(videodev_exit)
382
383MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
384MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
385MODULE_LICENSE("GPL");
386
387
388/*
389 * Local variables:
390 * c-basic-offset: 8
391 * End:
392 */