blob: 1ec0a1a8fb73f2a962358d557c838b087254e7ab [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 Verkuilbfa8a272008-08-23 07:48:38 -030045
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030046 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{
Hans Verkuil22a04f12008-07-20 06:35:02 -030052 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030053
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030054 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
63struct video_device *video_device_alloc(void)
64{
Hans Verkuilbfa8a272008-08-23 07:48:38 -030065 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030066}
67EXPORT_SYMBOL(video_device_alloc);
68
69void video_device_release(struct video_device *vfd)
70{
71 kfree(vfd);
72}
73EXPORT_SYMBOL(video_device_release);
74
Hans Verkuilf9e86b52008-08-23 05:47:41 -030075void video_device_release_empty(struct video_device *vfd)
76{
77 /* Do nothing */
78 /* Only valid when the video_device struct is a static. */
79}
80EXPORT_SYMBOL(video_device_release_empty);
81
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030082static void video_release(struct device *cd)
83{
Hans Verkuil22a04f12008-07-20 06:35:02 -030084 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030085
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030086 vfd->release(vfd);
87}
88
89static struct class video_class = {
90 .name = VIDEO_NAME,
91 .dev_attrs = video_device_attrs,
92 .dev_release = video_release,
93};
94
95/*
96 * Active devices
97 */
98
99static struct video_device *video_device[VIDEO_NUM_DEVICES];
100static DEFINE_MUTEX(videodev_lock);
101
102struct video_device *video_devdata(struct file *file)
103{
104 return video_device[iminor(file->f_path.dentry->d_inode)];
105}
106EXPORT_SYMBOL(video_devdata);
107
108/*
109 * Open a video device - FIXME: Obsoleted
110 */
111static int video_open(struct inode *inode, struct file *file)
112{
113 unsigned int minor = iminor(inode);
114 int err = 0;
115 struct video_device *vfl;
116 const struct file_operations *old_fops;
117
118 if (minor >= VIDEO_NUM_DEVICES)
119 return -ENODEV;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300120 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);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300129 return -ENODEV;
130 }
131 }
132 old_fops = file->f_op;
133 file->f_op = fops_get(vfl->fops);
134 if (file->f_op->open)
135 err = file->f_op->open(inode, file);
136 if (err) {
137 fops_put(file->f_op);
138 file->f_op = fops_get(old_fops);
139 }
140 fops_put(old_fops);
141 mutex_unlock(&videodev_lock);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300142 return err;
143}
144
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300145/**
146 * get_index - assign stream number based on parent device
147 * @vdev: video_device to assign index number to, vdev->dev should be assigned
148 * @num: -1 if auto assign, requested number otherwise
149 *
150 *
151 * returns -ENFILE if num is already in use, a free index number if
152 * successful.
153 */
154static int get_index(struct video_device *vdev, int num)
155{
156 u32 used = 0;
157 const int max_index = sizeof(used) * 8 - 1;
158 int i;
159
160 /* Currently a single v4l driver instance cannot create more than
161 32 devices.
162 Increase to u64 or an array of u32 if more are needed. */
163 if (num > max_index) {
164 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
165 return -EINVAL;
166 }
167
168 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
169 if (video_device[i] != NULL &&
170 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300171 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300172 used |= 1 << video_device[i]->index;
173 }
174 }
175
176 if (num >= 0) {
177 if (used & (1 << num))
178 return -ENFILE;
179 return num;
180 }
181
182 i = ffz(used);
183 return i > max_index ? -ENFILE : i;
184}
185
186static const struct file_operations video_fops;
187
188int video_register_device(struct video_device *vfd, int type, int nr)
189{
190 return video_register_device_index(vfd, type, nr, -1);
191}
192EXPORT_SYMBOL(video_register_device);
193
194/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300195 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300196 * @vfd: video device structure we want to register
197 * @type: type of device to register
198 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
199 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300200 * @index: stream number based on parent device;
201 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300202 *
203 * The registration code assigns minor numbers based on the type
204 * requested. -ENFILE is returned in all the device slots for this
205 * category are full. If not then the minor field is set and the
206 * driver initialize function is called (if non %NULL).
207 *
208 * Zero is returned on success.
209 *
210 * Valid types are
211 *
212 * %VFL_TYPE_GRABBER - A frame grabber
213 *
214 * %VFL_TYPE_VTX - A teletext device
215 *
216 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
217 *
218 * %VFL_TYPE_RADIO - A radio card
219 */
220
221int video_register_device_index(struct video_device *vfd, int type, int nr,
222 int index)
223{
224 int i = 0;
225 int base;
226 int end;
227 int ret;
228 char *name_base;
229
Hans Verkuild6e74972008-08-23 06:27:59 -0300230 /* the release callback MUST be present */
231 BUG_ON(!vfd->release);
Henrik Kretzschmarf3b9f502008-09-03 17:11:53 -0300232
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300233 if (vfd == NULL)
234 return -EINVAL;
235
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300236 switch (type) {
237 case VFL_TYPE_GRABBER:
238 base = MINOR_VFL_TYPE_GRABBER_MIN;
239 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
240 name_base = "video";
241 break;
242 case VFL_TYPE_VTX:
243 base = MINOR_VFL_TYPE_VTX_MIN;
244 end = MINOR_VFL_TYPE_VTX_MAX+1;
245 name_base = "vtx";
246 break;
247 case VFL_TYPE_VBI:
248 base = MINOR_VFL_TYPE_VBI_MIN;
249 end = MINOR_VFL_TYPE_VBI_MAX+1;
250 name_base = "vbi";
251 break;
252 case VFL_TYPE_RADIO:
253 base = MINOR_VFL_TYPE_RADIO_MIN;
254 end = MINOR_VFL_TYPE_RADIO_MAX+1;
255 name_base = "radio";
256 break;
257 default:
258 printk(KERN_ERR "%s called with unknown type: %d\n",
259 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300260 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300261 }
262
263 /* pick a minor number */
264 mutex_lock(&videodev_lock);
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300265 if (nr >= 0 && nr < end-base) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300266 /* use the one the driver asked for */
267 i = base + nr;
268 if (NULL != video_device[i]) {
269 mutex_unlock(&videodev_lock);
270 return -ENFILE;
271 }
272 } else {
273 /* use first free */
274 for (i = base; i < end; i++)
275 if (NULL == video_device[i])
276 break;
277 if (i == end) {
278 mutex_unlock(&videodev_lock);
279 return -ENFILE;
280 }
281 }
282 video_device[i] = vfd;
Hans Verkuil0ea6bc82008-07-26 08:26:43 -0300283 vfd->vfl_type = type;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300284 vfd->minor = i;
285
286 ret = get_index(vfd, index);
287 vfd->index = ret;
288
289 mutex_unlock(&videodev_lock);
290
291 if (ret < 0) {
292 printk(KERN_ERR "%s: get_index failed\n", __func__);
293 goto fail_minor;
294 }
295
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300296 /* sysfs class */
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300297 memset(&vfd->dev, 0, sizeof(vfd->dev));
Hans Verkuil22a04f12008-07-20 06:35:02 -0300298 vfd->dev.class = &video_class;
299 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300300 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300301 vfd->dev.parent = vfd->parent;
302 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
303 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300304 if (ret < 0) {
305 printk(KERN_ERR "%s: device_register failed\n", __func__);
306 goto fail_minor;
307 }
308
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300309 return 0;
310
311fail_minor:
312 mutex_lock(&videodev_lock);
313 video_device[vfd->minor] = NULL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300314 mutex_unlock(&videodev_lock);
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300315 vfd->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300316 return ret;
317}
318EXPORT_SYMBOL(video_register_device_index);
319
320/**
321 * video_unregister_device - unregister a video4linux device
322 * @vfd: the device to unregister
323 *
324 * This unregisters the passed device and deassigns the minor
325 * number. Future open calls will be met with errors.
326 */
327
328void video_unregister_device(struct video_device *vfd)
329{
330 mutex_lock(&videodev_lock);
331 if (video_device[vfd->minor] != vfd)
332 panic("videodev: bad unregister");
333
334 video_device[vfd->minor] = NULL;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300335 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300336 mutex_unlock(&videodev_lock);
337}
338EXPORT_SYMBOL(video_unregister_device);
339
340/*
341 * Video fs operations
342 */
343static const struct file_operations video_fops = {
344 .owner = THIS_MODULE,
345 .llseek = no_llseek,
346 .open = video_open,
347};
348
349/*
350 * Initialise video for linux
351 */
352
353static int __init videodev_init(void)
354{
355 int ret;
356
357 printk(KERN_INFO "Linux video capture interface: v2.00\n");
358 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
359 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
360 return -EIO;
361 }
362
363 ret = class_register(&video_class);
364 if (ret < 0) {
365 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
366 printk(KERN_WARNING "video_dev: class_register failed\n");
367 return -EIO;
368 }
369
370 return 0;
371}
372
373static void __exit videodev_exit(void)
374{
375 class_unregister(&video_class);
376 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
377}
378
379module_init(videodev_init)
380module_exit(videodev_exit)
381
382MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
383MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
384MODULE_LICENSE("GPL");
385
386
387/*
388 * Local variables:
389 * c-basic-offset: 8
390 * End:
391 */