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