[media] pvrusb2: use struct v4l2_fh
[deliverable/linux.git] / drivers / media / v4l2-core / 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 *
d9b01449 12 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
27a5e6d3
HV
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>
27a5e6d3 28#include <asm/uaccess.h>
27a5e6d3
HV
29
30#include <media/v4l2-common.h>
9bea3514 31#include <media/v4l2-device.h>
bec43661 32#include <media/v4l2-ioctl.h>
27a5e6d3
HV
33
34#define VIDEO_NUM_DEVICES 256
35#define VIDEO_NAME "video4linux"
36
37/*
38 * sysfs stuff
39 */
40
13e2237f
GKH
41static ssize_t index_show(struct device *cd,
42 struct device_attribute *attr, char *buf)
27a5e6d3 43{
dc93a70c 44 struct video_device *vdev = to_video_device(cd);
bfa8a273 45
dc93a70c 46 return sprintf(buf, "%i\n", vdev->index);
27a5e6d3 47}
13e2237f 48static DEVICE_ATTR_RO(index);
27a5e6d3 49
17028cdb 50static ssize_t dev_debug_show(struct device *cd,
13e2237f 51 struct device_attribute *attr, char *buf)
80131fe0
HV
52{
53 struct video_device *vdev = to_video_device(cd);
54
17028cdb 55 return sprintf(buf, "%i\n", vdev->dev_debug);
80131fe0
HV
56}
57
17028cdb 58static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
13e2237f 59 const char *buf, size_t len)
80131fe0
HV
60{
61 struct video_device *vdev = to_video_device(cd);
62 int res = 0;
63 u16 value;
64
65 res = kstrtou16(buf, 0, &value);
66 if (res)
67 return res;
68
17028cdb 69 vdev->dev_debug = value;
80131fe0
HV
70 return len;
71}
17028cdb 72static DEVICE_ATTR_RW(dev_debug);
80131fe0 73
13e2237f 74static ssize_t name_show(struct device *cd,
27a5e6d3
HV
75 struct device_attribute *attr, char *buf)
76{
dc93a70c 77 struct video_device *vdev = to_video_device(cd);
bfa8a273 78
dc93a70c 79 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
27a5e6d3 80}
13e2237f 81static DEVICE_ATTR_RO(name);
27a5e6d3 82
13e2237f
GKH
83static struct attribute *video_device_attrs[] = {
84 &dev_attr_name.attr,
17028cdb 85 &dev_attr_dev_debug.attr,
13e2237f
GKH
86 &dev_attr_index.attr,
87 NULL,
27a5e6d3 88};
13e2237f 89ATTRIBUTE_GROUPS(video_device);
27a5e6d3 90
7f8ecfab
HV
91/*
92 * Active devices
93 */
94static struct video_device *video_device[VIDEO_NUM_DEVICES];
95static DEFINE_MUTEX(videodev_lock);
22e22125 96static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
7f8ecfab 97
5062cb70
HV
98/* Device node utility functions */
99
100/* Note: these utility functions all assume that vfl_type is in the range
101 [0, VFL_TYPE_MAX-1]. */
102
103#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
104/* Return the bitmap corresponding to vfl_type. */
105static inline unsigned long *devnode_bits(int vfl_type)
106{
107 /* Any types not assigned to fixed minor ranges must be mapped to
108 one single bitmap for the purposes of finding a free node number
109 since all those unassigned types use the same minor range. */
226c0eea 110 int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
5062cb70
HV
111
112 return devnode_nums[idx];
113}
114#else
115/* Return the bitmap corresponding to vfl_type. */
116static inline unsigned long *devnode_bits(int vfl_type)
117{
118 return devnode_nums[vfl_type];
119}
120#endif
121
122/* Mark device node number vdev->num as used */
123static inline void devnode_set(struct video_device *vdev)
124{
125 set_bit(vdev->num, devnode_bits(vdev->vfl_type));
126}
127
128/* Mark device node number vdev->num as unused */
129static inline void devnode_clear(struct video_device *vdev)
130{
131 clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
132}
133
134/* Try to find a free device node number in the range [from, to> */
135static inline int devnode_find(struct video_device *vdev, int from, int to)
136{
137 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
138}
139
27a5e6d3
HV
140struct video_device *video_device_alloc(void)
141{
bfa8a273 142 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
27a5e6d3
HV
143}
144EXPORT_SYMBOL(video_device_alloc);
145
dc93a70c 146void video_device_release(struct video_device *vdev)
27a5e6d3 147{
dc93a70c 148 kfree(vdev);
27a5e6d3
HV
149}
150EXPORT_SYMBOL(video_device_release);
151
dc93a70c 152void video_device_release_empty(struct video_device *vdev)
f9e86b5e
HV
153{
154 /* Do nothing */
155 /* Only valid when the video_device struct is a static. */
156}
157EXPORT_SYMBOL(video_device_release_empty);
158
dc93a70c 159static inline void video_get(struct video_device *vdev)
7f8ecfab 160{
dc93a70c
HV
161 get_device(&vdev->dev);
162}
163
164static inline void video_put(struct video_device *vdev)
165{
166 put_device(&vdev->dev);
167}
168
169/* Called when the last user of the video device exits. */
170static void v4l2_device_release(struct device *cd)
171{
172 struct video_device *vdev = to_video_device(cd);
bedf8bcf 173 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
7f8ecfab
HV
174
175 mutex_lock(&videodev_lock);
1fc2b5f7 176 if (WARN_ON(video_device[vdev->minor] != vdev)) {
dc93a70c 177 /* should not happen */
1fc2b5f7 178 mutex_unlock(&videodev_lock);
6ea9a182
HV
179 return;
180 }
7f8ecfab
HV
181
182 /* Free up this device for reuse */
dc93a70c 183 video_device[vdev->minor] = NULL;
7f8ecfab 184
dc93a70c
HV
185 /* Delete the cdev on this minor as well */
186 cdev_del(vdev->cdev);
187 /* Just in case some driver tries to access this from
188 the release() callback. */
189 vdev->cdev = NULL;
7f8ecfab 190
22e22125 191 /* Mark device node number as free */
5062cb70 192 devnode_clear(vdev);
7f8ecfab 193
dc93a70c 194 mutex_unlock(&videodev_lock);
27a5e6d3 195
c064b8ea 196#if defined(CONFIG_MEDIA_CONTROLLER)
d9bfbcc0 197 if (v4l2_dev->mdev &&
c064b8ea
LP
198 vdev->vfl_type != VFL_TYPE_SUBDEV)
199 media_device_unregister_entity(&vdev->entity);
200#endif
201
8280b662
HV
202 /* Do not call v4l2_device_put if there is no release callback set.
203 * Drivers that have no v4l2_device release callback might free the
204 * v4l2_dev instance in the video_device release callback below, so we
205 * must perform this check here.
206 *
207 * TODO: In the long run all drivers that use v4l2_device should use the
208 * v4l2_device release callback. This check will then be unnecessary.
209 */
d9bfbcc0 210 if (v4l2_dev->release == NULL)
8280b662
HV
211 v4l2_dev = NULL;
212
dc93a70c
HV
213 /* Release video_device and perform other
214 cleanups as needed. */
215 vdev->release(vdev);
bedf8bcf
HV
216
217 /* Decrease v4l2_device refcount */
218 if (v4l2_dev)
219 v4l2_device_put(v4l2_dev);
27a5e6d3
HV
220}
221
222static struct class video_class = {
223 .name = VIDEO_NAME,
13e2237f 224 .dev_groups = video_device_groups,
27a5e6d3
HV
225};
226
27a5e6d3
HV
227struct video_device *video_devdata(struct file *file)
228{
496ad9aa 229 return video_device[iminor(file_inode(file))];
27a5e6d3
HV
230}
231EXPORT_SYMBOL(video_devdata);
232
02265493
HV
233
234/* Priority handling */
235
236static inline bool prio_is_valid(enum v4l2_priority prio)
237{
238 return prio == V4L2_PRIORITY_BACKGROUND ||
239 prio == V4L2_PRIORITY_INTERACTIVE ||
240 prio == V4L2_PRIORITY_RECORD;
241}
242
243void v4l2_prio_init(struct v4l2_prio_state *global)
244{
245 memset(global, 0, sizeof(*global));
246}
247EXPORT_SYMBOL(v4l2_prio_init);
248
249int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
250 enum v4l2_priority new)
251{
252 if (!prio_is_valid(new))
253 return -EINVAL;
254 if (*local == new)
255 return 0;
256
257 atomic_inc(&global->prios[new]);
258 if (prio_is_valid(*local))
259 atomic_dec(&global->prios[*local]);
260 *local = new;
261 return 0;
262}
263EXPORT_SYMBOL(v4l2_prio_change);
264
265void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
266{
267 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
268}
269EXPORT_SYMBOL(v4l2_prio_open);
270
271void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
272{
273 if (prio_is_valid(local))
274 atomic_dec(&global->prios[local]);
275}
276EXPORT_SYMBOL(v4l2_prio_close);
277
278enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
279{
280 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
281 return V4L2_PRIORITY_RECORD;
282 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
283 return V4L2_PRIORITY_INTERACTIVE;
284 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
285 return V4L2_PRIORITY_BACKGROUND;
286 return V4L2_PRIORITY_UNSET;
287}
288EXPORT_SYMBOL(v4l2_prio_max);
289
290int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
291{
292 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
293}
294EXPORT_SYMBOL(v4l2_prio_check);
295
296
dc93a70c
HV
297static ssize_t v4l2_read(struct file *filp, char __user *buf,
298 size_t sz, loff_t *off)
299{
300 struct video_device *vdev = video_devdata(filp);
2877842d 301 int ret = -ENODEV;
dc93a70c
HV
302
303 if (!vdev->fops->read)
304 return -EINVAL;
ee6869af
HV
305 if (video_is_registered(vdev))
306 ret = vdev->fops->read(filp, buf, sz, off);
17028cdb
HV
307 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
308 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
cc4b7e7f
HV
309 printk(KERN_DEBUG "%s: read: %zd (%d)\n",
310 video_device_node_name(vdev), sz, ret);
ee6869af 311 return ret;
dc93a70c
HV
312}
313
314static ssize_t v4l2_write(struct file *filp, const char __user *buf,
315 size_t sz, loff_t *off)
316{
317 struct video_device *vdev = video_devdata(filp);
2877842d 318 int ret = -ENODEV;
dc93a70c
HV
319
320 if (!vdev->fops->write)
321 return -EINVAL;
ee6869af
HV
322 if (video_is_registered(vdev))
323 ret = vdev->fops->write(filp, buf, sz, off);
17028cdb
HV
324 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
325 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
cc4b7e7f
HV
326 printk(KERN_DEBUG "%s: write: %zd (%d)\n",
327 video_device_node_name(vdev), sz, ret);
ee6869af 328 return ret;
dc93a70c
HV
329}
330
331static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
332{
333 struct video_device *vdev = video_devdata(filp);
cf533735 334 unsigned int res = POLLERR | POLLHUP;
ee6869af
HV
335
336 if (!vdev->fops->poll)
2877842d 337 return DEFAULT_POLLMASK;
ee6869af 338 if (video_is_registered(vdev))
cf533735 339 res = vdev->fops->poll(filp, poll);
17028cdb 340 if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
cc4b7e7f 341 printk(KERN_DEBUG "%s: poll: %08x\n",
cf533735
HV
342 video_device_node_name(vdev), res);
343 return res;
dc93a70c
HV
344}
345
86a5ef7d 346static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
dc93a70c
HV
347{
348 struct video_device *vdev = video_devdata(filp);
72420630 349 int ret = -ENODEV;
dc93a70c 350
86a5ef7d 351 if (vdev->fops->unlocked_ioctl) {
5a5adf6b 352 struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
8ab75e3e 353
5a5adf6b
HV
354 if (lock && mutex_lock_interruptible(lock))
355 return -ERESTARTSYS;
72420630
MCC
356 if (video_is_registered(vdev))
357 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
5a5adf6b
HV
358 if (lock)
359 mutex_unlock(lock);
86a5ef7d
AB
360 } else
361 ret = -ENOTTY;
dc93a70c 362
86a5ef7d 363 return ret;
dc93a70c
HV
364}
365
ecc6517d
BL
366#ifdef CONFIG_MMU
367#define v4l2_get_unmapped_area NULL
368#else
369static unsigned long v4l2_get_unmapped_area(struct file *filp,
370 unsigned long addr, unsigned long len, unsigned long pgoff,
371 unsigned long flags)
372{
373 struct video_device *vdev = video_devdata(filp);
cc4b7e7f 374 int ret;
ecc6517d
BL
375
376 if (!vdev->fops->get_unmapped_area)
377 return -ENOSYS;
378 if (!video_is_registered(vdev))
379 return -ENODEV;
cc4b7e7f 380 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
17028cdb 381 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
cc4b7e7f
HV
382 printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
383 video_device_node_name(vdev), ret);
384 return ret;
ecc6517d
BL
385}
386#endif
387
dc93a70c
HV
388static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
389{
390 struct video_device *vdev = video_devdata(filp);
ee6869af
HV
391 int ret = -ENODEV;
392
393 if (!vdev->fops->mmap)
cf533735 394 return -ENODEV;
ee6869af
HV
395 if (video_is_registered(vdev))
396 ret = vdev->fops->mmap(filp, vm);
17028cdb 397 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
cc4b7e7f
HV
398 printk(KERN_DEBUG "%s: mmap (%d)\n",
399 video_device_node_name(vdev), ret);
ee6869af 400 return ret;
dc93a70c
HV
401}
402
403/* Override for the open function */
404static int v4l2_open(struct inode *inode, struct file *filp)
405{
406 struct video_device *vdev;
65d9ff9c 407 int ret = 0;
dc93a70c
HV
408
409 /* Check if the video device is available */
410 mutex_lock(&videodev_lock);
411 vdev = video_devdata(filp);
ee6869af 412 /* return ENODEV if the video device has already been removed. */
ca9afe6f 413 if (vdev == NULL || !video_is_registered(vdev)) {
dc93a70c
HV
414 mutex_unlock(&videodev_lock);
415 return -ENODEV;
416 }
417 /* and increase the device refcount */
418 video_get(vdev);
419 mutex_unlock(&videodev_lock);
ee6869af 420 if (vdev->fops->open) {
ee6869af
HV
421 if (video_is_registered(vdev))
422 ret = vdev->fops->open(filp);
423 else
424 ret = -ENODEV;
ee6869af 425 }
65d9ff9c 426
17028cdb 427 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
cc4b7e7f
HV
428 printk(KERN_DEBUG "%s: open (%d)\n",
429 video_device_node_name(vdev), ret);
8f695d3f
EG
430 /* decrease the refcount in case of an error */
431 if (ret)
432 video_put(vdev);
dc93a70c
HV
433 return ret;
434}
435
436/* Override for the release function */
437static int v4l2_release(struct inode *inode, struct file *filp)
438{
439 struct video_device *vdev = video_devdata(filp);
65d9ff9c
HV
440 int ret = 0;
441
cf533735
HV
442 if (vdev->fops->release)
443 ret = vdev->fops->release(filp);
17028cdb 444 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
cc4b7e7f
HV
445 printk(KERN_DEBUG "%s: release\n",
446 video_device_node_name(vdev));
cf533735 447
8f695d3f
EG
448 /* decrease the refcount unconditionally since the release()
449 return value is ignored. */
450 video_put(vdev);
dc93a70c
HV
451 return ret;
452}
453
dc93a70c
HV
454static const struct file_operations v4l2_fops = {
455 .owner = THIS_MODULE,
456 .read = v4l2_read,
457 .write = v4l2_write,
458 .open = v4l2_open,
ecc6517d 459 .get_unmapped_area = v4l2_get_unmapped_area,
dc93a70c 460 .mmap = v4l2_mmap,
86a5ef7d 461 .unlocked_ioctl = v4l2_ioctl,
dc93a70c 462#ifdef CONFIG_COMPAT
9bb7cde7 463 .compat_ioctl = v4l2_compat_ioctl32,
dc93a70c
HV
464#endif
465 .release = v4l2_release,
466 .poll = v4l2_poll,
467 .llseek = no_llseek,
468};
469
27a5e6d3 470/**
1c1d86a1
HV
471 * get_index - assign stream index number based on v4l2_dev
472 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
27a5e6d3 473 *
dc93a70c 474 * Note that when this is called the new device has not yet been registered
7ae0cd9b 475 * in the video_device array, but it was able to obtain a minor number.
27a5e6d3 476 *
7ae0cd9b
HV
477 * This means that we can always obtain a free stream index number since
478 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
479 * use of the video_device array.
480 *
481 * Returns a free index number.
27a5e6d3 482 */
7ae0cd9b 483static int get_index(struct video_device *vdev)
27a5e6d3 484{
775a05dd
HV
485 /* This can be static since this function is called with the global
486 videodev_lock held. */
487 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
27a5e6d3
HV
488 int i;
489
775a05dd 490 bitmap_zero(used, VIDEO_NUM_DEVICES);
806e5b7c 491
27a5e6d3
HV
492 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
493 if (video_device[i] != NULL &&
1c1d86a1 494 video_device[i]->v4l2_dev == vdev->v4l2_dev) {
775a05dd 495 set_bit(video_device[i]->index, used);
27a5e6d3
HV
496 }
497 }
498
7ae0cd9b 499 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
27a5e6d3
HV
500}
501
48ea0be0
HV
502#define SET_VALID_IOCTL(ops, cmd, op) \
503 if (ops->op) \
504 set_bit(_IOC_NR(cmd), valid_ioctls)
505
506/* This determines which ioctls are actually implemented in the driver.
507 It's a one-time thing which simplifies video_ioctl2 as it can just do
508 a bit test.
509
510 Note that drivers can override this by setting bits to 1 in
511 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
512 called, then that ioctl will actually be marked as unimplemented.
513
514 It does that by first setting up the local valid_ioctls bitmap, and
515 at the end do a:
516
517 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
518 */
519static void determine_valid_ioctls(struct video_device *vdev)
520{
521 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
522 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
4b20259f
HV
523 bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
524 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
bfffd743 525 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
582c52cb 526 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
527 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
528 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
48ea0be0
HV
529
530 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
531
90d0fc49
HV
532 /* vfl_type and vfl_dir independent ioctls */
533
48ea0be0 534 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
b7284bb0 535 if (ops->vidioc_g_priority)
48ea0be0 536 set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
b7284bb0 537 if (ops->vidioc_s_priority)
48ea0be0 538 set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
90d0fc49
HV
539 /* Note: the control handler can also be passed through the filehandle,
540 and that can't be tested here. If the bit for these control ioctls
541 is set, then the ioctl is valid. But if it is 0, then it can still
542 be valid if the filehandle passed the control handler. */
543 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
544 set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
e6bee368
HV
545 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
546 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
90d0fc49
HV
547 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
548 set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
549 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
550 set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
551 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
552 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
553 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
554 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
555 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
556 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
557 if (vdev->ctrl_handler || ops->vidioc_querymenu)
558 set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
559 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
560 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
561 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
562#ifdef CONFIG_VIDEO_ADV_DEBUG
96b03d2a 563 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
79b0c640
HV
564 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
565 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
90d0fc49 566#endif
90d0fc49
HV
567 /* yes, really vidioc_subscribe_event */
568 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
569 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
570 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
90d0fc49
HV
571 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
572 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
573
4b20259f 574 if (is_vid) {
90d0fc49 575 /* video specific ioctls */
4b20259f
HV
576 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
577 ops->vidioc_enum_fmt_vid_cap_mplane ||
578 ops->vidioc_enum_fmt_vid_overlay)) ||
579 (is_tx && (ops->vidioc_enum_fmt_vid_out ||
580 ops->vidioc_enum_fmt_vid_out_mplane)))
581 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
582 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
583 ops->vidioc_g_fmt_vid_cap_mplane ||
584 ops->vidioc_g_fmt_vid_overlay)) ||
585 (is_tx && (ops->vidioc_g_fmt_vid_out ||
586 ops->vidioc_g_fmt_vid_out_mplane ||
587 ops->vidioc_g_fmt_vid_out_overlay)))
588 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
589 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
590 ops->vidioc_s_fmt_vid_cap_mplane ||
591 ops->vidioc_s_fmt_vid_overlay)) ||
592 (is_tx && (ops->vidioc_s_fmt_vid_out ||
593 ops->vidioc_s_fmt_vid_out_mplane ||
594 ops->vidioc_s_fmt_vid_out_overlay)))
595 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
596 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
597 ops->vidioc_try_fmt_vid_cap_mplane ||
598 ops->vidioc_try_fmt_vid_overlay)) ||
599 (is_tx && (ops->vidioc_try_fmt_vid_out ||
600 ops->vidioc_try_fmt_vid_out_mplane ||
601 ops->vidioc_try_fmt_vid_out_overlay)))
602 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
90d0fc49
HV
603 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
604 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
605 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
606 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
607 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
608 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
609 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
610 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
611 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
612 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
613 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
614 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
4b20259f 615 } else if (is_vbi) {
90d0fc49 616 /* vbi specific ioctls */
4b20259f
HV
617 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
618 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
619 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
620 ops->vidioc_g_fmt_sliced_vbi_out)))
621 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
622 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
623 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
624 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
625 ops->vidioc_s_fmt_sliced_vbi_out)))
626 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
627 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
628 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
629 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
630 ops->vidioc_try_fmt_sliced_vbi_out)))
631 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
90d0fc49 632 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
582c52cb
AP
633 } else if (is_sdr) {
634 /* SDR specific ioctls */
635 if (ops->vidioc_enum_fmt_sdr_cap)
636 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
637 if (ops->vidioc_g_fmt_sdr_cap)
638 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
639 if (ops->vidioc_s_fmt_sdr_cap)
640 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
641 if (ops->vidioc_try_fmt_sdr_cap)
642 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
4b20259f 643 }
582c52cb
AP
644
645 if (is_vid || is_vbi || is_sdr) {
646 /* ioctls valid for video, vbi or sdr */
5815d0c4
FS
647 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
648 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
649 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
650 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
651 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
652 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
653 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
72be89c8
HV
654 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
655 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
582c52cb
AP
656 }
657
658 if (is_vid || is_vbi) {
659 /* ioctls valid for video or vbi */
4b20259f
HV
660 if (ops->vidioc_s_std)
661 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
4b20259f 662 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
ca371575 663 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
4b20259f 664 if (is_rx) {
90d0fc49 665 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
4b20259f
HV
666 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
667 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
668 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
669 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
670 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
671 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
90d0fc49 672 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
dd519bb3 673 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
4b20259f
HV
674 }
675 if (is_tx) {
676 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
677 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
678 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
679 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
680 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
681 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
682 }
4b20259f
HV
683 if (ops->vidioc_g_crop || ops->vidioc_g_selection)
684 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
685 if (ops->vidioc_s_crop || ops->vidioc_s_selection)
686 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
687 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
688 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
689 if (ops->vidioc_cropcap || ops->vidioc_g_selection)
690 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
4b20259f 691 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
ca371575 692 ops->vidioc_g_std))
4b20259f
HV
693 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
694 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
4b20259f
HV
695 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
696 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
697 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
4b20259f 698 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
dd519bb3 699 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
4b20259f 700 }
bfffd743
HV
701 if (is_tx && (is_radio || is_sdr)) {
702 /* radio transmitter only ioctls */
90d0fc49
HV
703 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
704 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
705 }
706 if (is_rx) {
707 /* receiver only ioctls */
708 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
709 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
710 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
711 }
712
48ea0be0
HV
713 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
714 BASE_VIDIOC_PRIVATE);
715}
716
27a5e6d3 717/**
2096a5dc 718 * __video_register_device - register video4linux devices
dc93a70c 719 * @vdev: video device structure we want to register
27a5e6d3 720 * @type: type of device to register
22e22125 721 * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
27a5e6d3 722 * -1 == first free)
6b5270d2
HV
723 * @warn_if_nr_in_use: warn if the desired device node number
724 * was already in use and another number was chosen instead.
2096a5dc 725 * @owner: module that owns the video device node
27a5e6d3 726 *
22e22125
HV
727 * The registration code assigns minor numbers and device node numbers
728 * based on the requested type and registers the new device node with
729 * the kernel.
46b63377
HV
730 *
731 * This function assumes that struct video_device was zeroed when it
732 * was allocated and does not contain any stale date.
733 *
22e22125
HV
734 * An error is returned if no free minor or device node number could be
735 * found, or if the registration of the device node failed.
27a5e6d3
HV
736 *
737 * Zero is returned on success.
738 *
739 * Valid types are
740 *
741 * %VFL_TYPE_GRABBER - A frame grabber
742 *
27a5e6d3
HV
743 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
744 *
745 * %VFL_TYPE_RADIO - A radio card
2096a5dc
LP
746 *
747 * %VFL_TYPE_SUBDEV - A subdevice
d42626bd
AP
748 *
749 * %VFL_TYPE_SDR - Software Defined Radio
27a5e6d3 750 */
2096a5dc
LP
751int __video_register_device(struct video_device *vdev, int type, int nr,
752 int warn_if_nr_in_use, struct module *owner)
27a5e6d3
HV
753{
754 int i = 0;
27a5e6d3 755 int ret;
dd89601d
HV
756 int minor_offset = 0;
757 int minor_cnt = VIDEO_NUM_DEVICES;
758 const char *name_base;
27a5e6d3 759
dc93a70c
HV
760 /* A minor value of -1 marks this video device as never
761 having been registered */
428c8d19 762 vdev->minor = -1;
ee7aa9f8 763
dc93a70c 764 /* the release callback MUST be present */
1fc2b5f7 765 if (WARN_ON(!vdev->release))
f3b9f50e 766 return -EINVAL;
1c1d86a1
HV
767 /* the v4l2_dev pointer MUST be present */
768 if (WARN_ON(!vdev->v4l2_dev))
769 return -EINVAL;
f3b9f50e 770
1babcb46
SA
771 /* v4l2_fh support */
772 spin_lock_init(&vdev->fh_lock);
773 INIT_LIST_HEAD(&vdev->fh_list);
774
dc93a70c 775 /* Part 1: check device type */
27a5e6d3
HV
776 switch (type) {
777 case VFL_TYPE_GRABBER:
27a5e6d3
HV
778 name_base = "video";
779 break;
27a5e6d3 780 case VFL_TYPE_VBI:
27a5e6d3
HV
781 name_base = "vbi";
782 break;
783 case VFL_TYPE_RADIO:
27a5e6d3
HV
784 name_base = "radio";
785 break;
2096a5dc
LP
786 case VFL_TYPE_SUBDEV:
787 name_base = "v4l-subdev";
788 break;
d42626bd
AP
789 case VFL_TYPE_SDR:
790 /* Use device name 'swradio' because 'sdr' was already taken. */
791 name_base = "swradio";
792 break;
27a5e6d3
HV
793 default:
794 printk(KERN_ERR "%s called with unknown type: %d\n",
795 __func__, type);
46f2c21c 796 return -EINVAL;
27a5e6d3
HV
797 }
798
dc93a70c
HV
799 vdev->vfl_type = type;
800 vdev->cdev = NULL;
1c1d86a1
HV
801 if (vdev->dev_parent == NULL)
802 vdev->dev_parent = vdev->v4l2_dev->dev;
803 if (vdev->ctrl_handler == NULL)
804 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
805 /* If the prio state pointer is NULL, then use the v4l2_device
806 prio state. */
807 if (vdev->prio == NULL)
808 vdev->prio = &vdev->v4l2_dev->prio;
dd89601d 809
22e22125 810 /* Part 2: find a free minor, device node number and device index. */
dd89601d
HV
811#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
812 /* Keep the ranges for the first four types for historical
813 * reasons.
814 * Newer devices (not yet in place) should use the range
815 * of 128-191 and just pick the first free minor there
816 * (new style). */
817 switch (type) {
818 case VFL_TYPE_GRABBER:
819 minor_offset = 0;
820 minor_cnt = 64;
821 break;
822 case VFL_TYPE_RADIO:
823 minor_offset = 64;
824 minor_cnt = 64;
825 break;
dd89601d
HV
826 case VFL_TYPE_VBI:
827 minor_offset = 224;
828 minor_cnt = 32;
829 break;
830 default:
831 minor_offset = 128;
832 minor_cnt = 64;
833 break;
834 }
835#endif
836
22e22125 837 /* Pick a device node number */
27a5e6d3 838 mutex_lock(&videodev_lock);
5062cb70 839 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
dd89601d 840 if (nr == minor_cnt)
5062cb70 841 nr = devnode_find(vdev, 0, minor_cnt);
dd89601d 842 if (nr == minor_cnt) {
22e22125 843 printk(KERN_ERR "could not get a free device node number\n");
dd89601d
HV
844 mutex_unlock(&videodev_lock);
845 return -ENFILE;
27a5e6d3 846 }
dd89601d 847#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
22e22125 848 /* 1-on-1 mapping of device node number to minor number */
dd89601d
HV
849 i = nr;
850#else
22e22125
HV
851 /* The device node number and minor numbers are independent, so
852 we just find the first free minor number. */
dd89601d
HV
853 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
854 if (video_device[i] == NULL)
855 break;
856 if (i == VIDEO_NUM_DEVICES) {
857 mutex_unlock(&videodev_lock);
858 printk(KERN_ERR "could not get a free minor\n");
859 return -ENFILE;
860 }
861#endif
dc93a70c
HV
862 vdev->minor = i + minor_offset;
863 vdev->num = nr;
5062cb70
HV
864 devnode_set(vdev);
865
dc93a70c
HV
866 /* Should not happen since we thought this minor was free */
867 WARN_ON(video_device[vdev->minor] != NULL);
7ae0cd9b 868 vdev->index = get_index(vdev);
6c3df5da 869 video_device[vdev->minor] = vdev;
27a5e6d3
HV
870 mutex_unlock(&videodev_lock);
871
48ea0be0
HV
872 if (vdev->ioctl_ops)
873 determine_valid_ioctls(vdev);
874
dc93a70c
HV
875 /* Part 3: Initialize the character device */
876 vdev->cdev = cdev_alloc();
877 if (vdev->cdev == NULL) {
878 ret = -ENOMEM;
879 goto cleanup;
880 }
86a5ef7d 881 vdev->cdev->ops = &v4l2_fops;
2096a5dc 882 vdev->cdev->owner = owner;
dc93a70c 883 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
7f8ecfab
HV
884 if (ret < 0) {
885 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
dc93a70c
HV
886 kfree(vdev->cdev);
887 vdev->cdev = NULL;
888 goto cleanup;
7f8ecfab 889 }
dc93a70c
HV
890
891 /* Part 4: register the device with sysfs */
dc93a70c
HV
892 vdev->dev.class = &video_class;
893 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
1c1d86a1 894 vdev->dev.parent = vdev->dev_parent;
22e22125 895 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
dc93a70c 896 ret = device_register(&vdev->dev);
27a5e6d3
HV
897 if (ret < 0) {
898 printk(KERN_ERR "%s: device_register failed\n", __func__);
dc93a70c 899 goto cleanup;
27a5e6d3 900 }
dc93a70c
HV
901 /* Register the release callback that will be called when the last
902 reference to the device goes away. */
903 vdev->dev.release = v4l2_device_release;
27a5e6d3 904
6b5270d2 905 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
eac8ea53
LP
906 printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
907 name_base, nr, video_device_node_name(vdev));
bedf8bcf
HV
908
909 /* Increase v4l2_device refcount */
d9bfbcc0 910 v4l2_device_get(vdev->v4l2_dev);
bedf8bcf 911
2c0ab67b
LP
912#if defined(CONFIG_MEDIA_CONTROLLER)
913 /* Part 5: Register the entity. */
d9bfbcc0 914 if (vdev->v4l2_dev->mdev &&
c4f0b78a 915 vdev->vfl_type != VFL_TYPE_SUBDEV) {
2c0ab67b
LP
916 vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
917 vdev->entity.name = vdev->name;
e31a0ba7
MCC
918 vdev->entity.info.dev.major = VIDEO_MAJOR;
919 vdev->entity.info.dev.minor = vdev->minor;
2c0ab67b
LP
920 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
921 &vdev->entity);
922 if (ret < 0)
923 printk(KERN_WARNING
924 "%s: media_device_register_entity failed\n",
925 __func__);
926 }
927#endif
928 /* Part 6: Activate this minor. The char device can now be used. */
957b4aa9 929 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
2c0ab67b 930
dc93a70c 931 return 0;
7f8ecfab 932
dc93a70c 933cleanup:
27a5e6d3 934 mutex_lock(&videodev_lock);
dc93a70c
HV
935 if (vdev->cdev)
936 cdev_del(vdev->cdev);
1056e438 937 video_device[vdev->minor] = NULL;
5062cb70 938 devnode_clear(vdev);
27a5e6d3 939 mutex_unlock(&videodev_lock);
dc93a70c
HV
940 /* Mark this video device as never having been registered. */
941 vdev->minor = -1;
27a5e6d3
HV
942 return ret;
943}
2096a5dc 944EXPORT_SYMBOL(__video_register_device);
6b5270d2 945
27a5e6d3
HV
946/**
947 * video_unregister_device - unregister a video4linux device
dc93a70c 948 * @vdev: the device to unregister
27a5e6d3 949 *
dc93a70c
HV
950 * This unregisters the passed device. Future open calls will
951 * be met with errors.
27a5e6d3 952 */
dc93a70c 953void video_unregister_device(struct video_device *vdev)
27a5e6d3 954{
dc93a70c 955 /* Check if vdev was ever registered at all */
957b4aa9 956 if (!vdev || !video_is_registered(vdev))
dc93a70c
HV
957 return;
958
ca9afe6f
HV
959 mutex_lock(&videodev_lock);
960 /* This must be in a critical section to prevent a race with v4l2_open.
961 * Once this bit has been cleared video_get may never be called again.
962 */
957b4aa9 963 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
ca9afe6f 964 mutex_unlock(&videodev_lock);
dc93a70c 965 device_unregister(&vdev->dev);
27a5e6d3
HV
966}
967EXPORT_SYMBOL(video_unregister_device);
968
27a5e6d3
HV
969/*
970 * Initialise video for linux
971 */
27a5e6d3
HV
972static int __init videodev_init(void)
973{
7f8ecfab 974 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
27a5e6d3
HV
975 int ret;
976
977 printk(KERN_INFO "Linux video capture interface: v2.00\n");
7f8ecfab
HV
978 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
979 if (ret < 0) {
980 printk(KERN_WARNING "videodev: unable to get major %d\n",
981 VIDEO_MAJOR);
982 return ret;
27a5e6d3
HV
983 }
984
985 ret = class_register(&video_class);
986 if (ret < 0) {
7f8ecfab 987 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
27a5e6d3
HV
988 printk(KERN_WARNING "video_dev: class_register failed\n");
989 return -EIO;
990 }
991
992 return 0;
993}
994
995static void __exit videodev_exit(void)
996{
7f8ecfab
HV
997 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
998
27a5e6d3 999 class_unregister(&video_class);
7f8ecfab 1000 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
27a5e6d3
HV
1001}
1002
ee981c6f 1003subsys_initcall(videodev_init);
27a5e6d3
HV
1004module_exit(videodev_exit)
1005
1006MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1007MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1008MODULE_LICENSE("GPL");
cbb72b0f 1009MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
This page took 0.558537 seconds and 5 git commands to generate.