]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/v4l2-dev.c
V4L/DVB (8781): v4l2-dev: remove obsolete video_exclusive_open/release
[net-next-2.6.git] / drivers / media / video / v4l2-dev.c
CommitLineData
27a5e6d3
HV
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{
22a04f10 44 struct video_device *vfd = container_of(cd, struct video_device, dev);
27a5e6d3
HV
45 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{
22a04f10 51 struct video_device *vfd = container_of(cd, struct video_device, dev);
27a5e6d3
HV
52 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{
22a04f10 78 struct video_device *vfd = container_of(cd, struct video_device, dev);
27a5e6d3
HV
79
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;
27a5e6d3
HV
119 mutex_lock(&videodev_lock);
120 vfl = video_device[minor];
121 if (vfl == NULL) {
122 mutex_unlock(&videodev_lock);
123 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
124 mutex_lock(&videodev_lock);
125 vfl = video_device[minor];
126 if (vfl == NULL) {
127 mutex_unlock(&videodev_lock);
27a5e6d3
HV
128 return -ENODEV;
129 }
130 }
131 old_fops = file->f_op;
132 file->f_op = fops_get(vfl->fops);
133 if (file->f_op->open)
134 err = file->f_op->open(inode, file);
135 if (err) {
136 fops_put(file->f_op);
137 file->f_op = fops_get(old_fops);
138 }
139 fops_put(old_fops);
140 mutex_unlock(&videodev_lock);
27a5e6d3
HV
141 return err;
142}
143
27a5e6d3
HV
144/**
145 * get_index - assign stream number based on parent device
146 * @vdev: video_device to assign index number to, vdev->dev should be assigned
147 * @num: -1 if auto assign, requested number otherwise
148 *
149 *
150 * returns -ENFILE if num is already in use, a free index number if
151 * successful.
152 */
153static int get_index(struct video_device *vdev, int num)
154{
155 u32 used = 0;
156 const int max_index = sizeof(used) * 8 - 1;
157 int i;
158
159 /* Currently a single v4l driver instance cannot create more than
160 32 devices.
161 Increase to u64 or an array of u32 if more are needed. */
162 if (num > max_index) {
163 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
164 return -EINVAL;
165 }
166
167 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
168 if (video_device[i] != NULL &&
169 video_device[i] != vdev &&
5e85e732 170 video_device[i]->parent == vdev->parent) {
27a5e6d3
HV
171 used |= 1 << video_device[i]->index;
172 }
173 }
174
175 if (num >= 0) {
176 if (used & (1 << num))
177 return -ENFILE;
178 return num;
179 }
180
181 i = ffz(used);
182 return i > max_index ? -ENFILE : i;
183}
184
185static const struct file_operations video_fops;
186
187int video_register_device(struct video_device *vfd, int type, int nr)
188{
189 return video_register_device_index(vfd, type, nr, -1);
190}
191EXPORT_SYMBOL(video_register_device);
192
193/**
edc9189c 194 * video_register_device_index - register video4linux devices
27a5e6d3
HV
195 * @vfd: video device structure we want to register
196 * @type: type of device to register
197 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
198 * -1 == first free)
edc9189c
RD
199 * @index: stream number based on parent device;
200 * -1 if auto assign, requested number otherwise
27a5e6d3
HV
201 *
202 * The registration code assigns minor numbers based on the type
203 * requested. -ENFILE is returned in all the device slots for this
204 * category are full. If not then the minor field is set and the
205 * driver initialize function is called (if non %NULL).
206 *
207 * Zero is returned on success.
208 *
209 * Valid types are
210 *
211 * %VFL_TYPE_GRABBER - A frame grabber
212 *
213 * %VFL_TYPE_VTX - A teletext device
214 *
215 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
216 *
217 * %VFL_TYPE_RADIO - A radio card
218 */
219
220int video_register_device_index(struct video_device *vfd, int type, int nr,
221 int index)
222{
223 int i = 0;
224 int base;
225 int end;
226 int ret;
227 char *name_base;
228
ee7aa9f8
HK
229 if (vfd == NULL)
230 return -EINVAL;
231
f3b9f50e
HK
232 if (vfd == NULL)
233 return -EINVAL;
234
27a5e6d3
HV
235 switch (type) {
236 case VFL_TYPE_GRABBER:
237 base = MINOR_VFL_TYPE_GRABBER_MIN;
238 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
239 name_base = "video";
240 break;
241 case VFL_TYPE_VTX:
242 base = MINOR_VFL_TYPE_VTX_MIN;
243 end = MINOR_VFL_TYPE_VTX_MAX+1;
244 name_base = "vtx";
245 break;
246 case VFL_TYPE_VBI:
247 base = MINOR_VFL_TYPE_VBI_MIN;
248 end = MINOR_VFL_TYPE_VBI_MAX+1;
249 name_base = "vbi";
250 break;
251 case VFL_TYPE_RADIO:
252 base = MINOR_VFL_TYPE_RADIO_MIN;
253 end = MINOR_VFL_TYPE_RADIO_MAX+1;
254 name_base = "radio";
255 break;
256 default:
257 printk(KERN_ERR "%s called with unknown type: %d\n",
258 __func__, type);
46f2c21c 259 return -EINVAL;
27a5e6d3
HV
260 }
261
262 /* pick a minor number */
263 mutex_lock(&videodev_lock);
264 if (nr >= 0 && nr < end-base) {
265 /* use the one the driver asked for */
266 i = base + nr;
267 if (NULL != video_device[i]) {
268 mutex_unlock(&videodev_lock);
269 return -ENFILE;
270 }
271 } else {
272 /* use first free */
273 for (i = base; i < end; i++)
274 if (NULL == video_device[i])
275 break;
276 if (i == end) {
277 mutex_unlock(&videodev_lock);
278 return -ENFILE;
279 }
280 }
281 video_device[i] = vfd;
0ea6bc8d 282 vfd->vfl_type = type;
27a5e6d3
HV
283 vfd->minor = i;
284
285 ret = get_index(vfd, index);
286 vfd->index = ret;
287
288 mutex_unlock(&videodev_lock);
289
290 if (ret < 0) {
291 printk(KERN_ERR "%s: get_index failed\n", __func__);
292 goto fail_minor;
293 }
294
27a5e6d3 295 /* sysfs class */
22a04f10
HV
296 memset(&vfd->dev, 0x00, sizeof(vfd->dev));
297 vfd->dev.class = &video_class;
298 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
5e85e732 299 if (vfd->parent)
22a04f10
HV
300 vfd->dev.parent = vfd->parent;
301 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
302 ret = device_register(&vfd->dev);
27a5e6d3
HV
303 if (ret < 0) {
304 printk(KERN_ERR "%s: device_register failed\n", __func__);
305 goto fail_minor;
306 }
307
308#if 1
309 /* needed until all drivers are fixed */
310 if (!vfd->release)
311 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
312 "Please fix your driver for proper sysfs support, see "
313 "http://lwn.net/Articles/36850/\n", vfd->name);
314#endif
315 return 0;
316
317fail_minor:
318 mutex_lock(&videodev_lock);
319 video_device[vfd->minor] = NULL;
320 vfd->minor = -1;
321 mutex_unlock(&videodev_lock);
322 return ret;
323}
324EXPORT_SYMBOL(video_register_device_index);
325
326/**
327 * video_unregister_device - unregister a video4linux device
328 * @vfd: the device to unregister
329 *
330 * This unregisters the passed device and deassigns the minor
331 * number. Future open calls will be met with errors.
332 */
333
334void video_unregister_device(struct video_device *vfd)
335{
336 mutex_lock(&videodev_lock);
337 if (video_device[vfd->minor] != vfd)
338 panic("videodev: bad unregister");
339
340 video_device[vfd->minor] = NULL;
22a04f10 341 device_unregister(&vfd->dev);
27a5e6d3
HV
342 mutex_unlock(&videodev_lock);
343}
344EXPORT_SYMBOL(video_unregister_device);
345
346/*
347 * Video fs operations
348 */
349static const struct file_operations video_fops = {
350 .owner = THIS_MODULE,
351 .llseek = no_llseek,
352 .open = video_open,
353};
354
355/*
356 * Initialise video for linux
357 */
358
359static int __init videodev_init(void)
360{
361 int ret;
362
363 printk(KERN_INFO "Linux video capture interface: v2.00\n");
364 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
365 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
366 return -EIO;
367 }
368
369 ret = class_register(&video_class);
370 if (ret < 0) {
371 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
372 printk(KERN_WARNING "video_dev: class_register failed\n");
373 return -EIO;
374 }
375
376 return 0;
377}
378
379static void __exit videodev_exit(void)
380{
381 class_unregister(&video_class);
382 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
383}
384
385module_init(videodev_init)
386module_exit(videodev_exit)
387
388MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
389MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
390MODULE_LICENSE("GPL");
391
392
393/*
394 * Local variables:
395 * c-basic-offset: 8
396 * End:
397 */