s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / media / v4l2-core / v4l2-dev.c
blob6b1eaeddbdb3c9d973866ecb28aab9f24f379e41
1 /*
2 * Video capture interface for Linux version 2
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
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.
12 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
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 <asm/uaccess.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
34 #define VIDEO_NUM_DEVICES 256
35 #define VIDEO_NAME "video4linux"
38 * sysfs stuff
41 static ssize_t index_show(struct device *cd,
42 struct device_attribute *attr, char *buf)
44 struct video_device *vdev = to_video_device(cd);
46 return sprintf(buf, "%i\n", vdev->index);
48 static DEVICE_ATTR_RO(index);
50 static ssize_t dev_debug_show(struct device *cd,
51 struct device_attribute *attr, char *buf)
53 struct video_device *vdev = to_video_device(cd);
55 return sprintf(buf, "%i\n", vdev->dev_debug);
58 static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
59 const char *buf, size_t len)
61 struct video_device *vdev = to_video_device(cd);
62 int res = 0;
63 u16 value;
65 res = kstrtou16(buf, 0, &value);
66 if (res)
67 return res;
69 vdev->dev_debug = value;
70 return len;
72 static DEVICE_ATTR_RW(dev_debug);
74 static ssize_t name_show(struct device *cd,
75 struct device_attribute *attr, char *buf)
77 struct video_device *vdev = to_video_device(cd);
79 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
81 static DEVICE_ATTR_RO(name);
83 static struct attribute *video_device_attrs[] = {
84 &dev_attr_name.attr,
85 &dev_attr_dev_debug.attr,
86 &dev_attr_index.attr,
87 NULL,
89 ATTRIBUTE_GROUPS(video_device);
92 * Active devices
94 static struct video_device *video_device[VIDEO_NUM_DEVICES];
95 static DEFINE_MUTEX(videodev_lock);
96 static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
98 /* Device node utility functions */
100 /* Note: these utility functions all assume that vfl_type is in the range
101 [0, VFL_TYPE_MAX-1]. */
103 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
104 /* Return the bitmap corresponding to vfl_type. */
105 static inline unsigned long *devnode_bits(int vfl_type)
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. */
110 int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
112 return devnode_nums[idx];
114 #else
115 /* Return the bitmap corresponding to vfl_type. */
116 static inline unsigned long *devnode_bits(int vfl_type)
118 return devnode_nums[vfl_type];
120 #endif
122 /* Mark device node number vdev->num as used */
123 static inline void devnode_set(struct video_device *vdev)
125 set_bit(vdev->num, devnode_bits(vdev->vfl_type));
128 /* Mark device node number vdev->num as unused */
129 static inline void devnode_clear(struct video_device *vdev)
131 clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
134 /* Try to find a free device node number in the range [from, to> */
135 static inline int devnode_find(struct video_device *vdev, int from, int to)
137 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
140 struct video_device *video_device_alloc(void)
142 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
144 EXPORT_SYMBOL(video_device_alloc);
146 void video_device_release(struct video_device *vdev)
148 kfree(vdev);
150 EXPORT_SYMBOL(video_device_release);
152 void video_device_release_empty(struct video_device *vdev)
154 /* Do nothing */
155 /* Only valid when the video_device struct is a static. */
157 EXPORT_SYMBOL(video_device_release_empty);
159 static inline void video_get(struct video_device *vdev)
161 get_device(&vdev->dev);
164 static inline void video_put(struct video_device *vdev)
166 put_device(&vdev->dev);
169 /* Called when the last user of the video device exits. */
170 static void v4l2_device_release(struct device *cd)
172 struct video_device *vdev = to_video_device(cd);
173 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
175 mutex_lock(&videodev_lock);
176 if (WARN_ON(video_device[vdev->minor] != vdev)) {
177 /* should not happen */
178 mutex_unlock(&videodev_lock);
179 return;
182 /* Free up this device for reuse */
183 video_device[vdev->minor] = NULL;
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;
191 /* Mark device node number as free */
192 devnode_clear(vdev);
194 mutex_unlock(&videodev_lock);
196 #if defined(CONFIG_MEDIA_CONTROLLER)
197 if (v4l2_dev->mdev &&
198 vdev->vfl_type != VFL_TYPE_SUBDEV)
199 media_device_unregister_entity(&vdev->entity);
200 #endif
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.
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.
210 if (v4l2_dev->release == NULL)
211 v4l2_dev = NULL;
213 /* Release video_device and perform other
214 cleanups as needed. */
215 vdev->release(vdev);
217 /* Decrease v4l2_device refcount */
218 if (v4l2_dev)
219 v4l2_device_put(v4l2_dev);
222 static struct class video_class = {
223 .name = VIDEO_NAME,
224 .dev_groups = video_device_groups,
227 struct video_device *video_devdata(struct file *file)
229 return video_device[iminor(file_inode(file))];
231 EXPORT_SYMBOL(video_devdata);
234 /* Priority handling */
236 static inline bool prio_is_valid(enum v4l2_priority prio)
238 return prio == V4L2_PRIORITY_BACKGROUND ||
239 prio == V4L2_PRIORITY_INTERACTIVE ||
240 prio == V4L2_PRIORITY_RECORD;
243 void v4l2_prio_init(struct v4l2_prio_state *global)
245 memset(global, 0, sizeof(*global));
247 EXPORT_SYMBOL(v4l2_prio_init);
249 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
250 enum v4l2_priority new)
252 if (!prio_is_valid(new))
253 return -EINVAL;
254 if (*local == new)
255 return 0;
257 atomic_inc(&global->prios[new]);
258 if (prio_is_valid(*local))
259 atomic_dec(&global->prios[*local]);
260 *local = new;
261 return 0;
263 EXPORT_SYMBOL(v4l2_prio_change);
265 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
267 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
269 EXPORT_SYMBOL(v4l2_prio_open);
271 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
273 if (prio_is_valid(local))
274 atomic_dec(&global->prios[local]);
276 EXPORT_SYMBOL(v4l2_prio_close);
278 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
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;
288 EXPORT_SYMBOL(v4l2_prio_max);
290 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
292 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
294 EXPORT_SYMBOL(v4l2_prio_check);
297 static ssize_t v4l2_read(struct file *filp, char __user *buf,
298 size_t sz, loff_t *off)
300 struct video_device *vdev = video_devdata(filp);
301 int ret = -ENODEV;
303 if (!vdev->fops->read)
304 return -EINVAL;
305 if (video_is_registered(vdev))
306 ret = vdev->fops->read(filp, buf, sz, off);
307 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
308 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
309 printk(KERN_DEBUG "%s: read: %zd (%d)\n",
310 video_device_node_name(vdev), sz, ret);
311 return ret;
314 static ssize_t v4l2_write(struct file *filp, const char __user *buf,
315 size_t sz, loff_t *off)
317 struct video_device *vdev = video_devdata(filp);
318 int ret = -ENODEV;
320 if (!vdev->fops->write)
321 return -EINVAL;
322 if (video_is_registered(vdev))
323 ret = vdev->fops->write(filp, buf, sz, off);
324 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
325 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
326 printk(KERN_DEBUG "%s: write: %zd (%d)\n",
327 video_device_node_name(vdev), sz, ret);
328 return ret;
331 static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
333 struct video_device *vdev = video_devdata(filp);
334 unsigned int res = POLLERR | POLLHUP;
336 if (!vdev->fops->poll)
337 return DEFAULT_POLLMASK;
338 if (video_is_registered(vdev))
339 res = vdev->fops->poll(filp, poll);
340 if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
341 printk(KERN_DEBUG "%s: poll: %08x\n",
342 video_device_node_name(vdev), res);
343 return res;
346 static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
348 struct video_device *vdev = video_devdata(filp);
349 int ret = -ENODEV;
351 if (vdev->fops->unlocked_ioctl) {
352 struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
354 if (lock && mutex_lock_interruptible(lock))
355 return -ERESTARTSYS;
356 if (video_is_registered(vdev))
357 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
358 if (lock)
359 mutex_unlock(lock);
360 } else
361 ret = -ENOTTY;
363 return ret;
366 #ifdef CONFIG_MMU
367 #define v4l2_get_unmapped_area NULL
368 #else
369 static unsigned long v4l2_get_unmapped_area(struct file *filp,
370 unsigned long addr, unsigned long len, unsigned long pgoff,
371 unsigned long flags)
373 struct video_device *vdev = video_devdata(filp);
374 int ret;
376 if (!vdev->fops->get_unmapped_area)
377 return -ENOSYS;
378 if (!video_is_registered(vdev))
379 return -ENODEV;
380 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
381 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
382 printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
383 video_device_node_name(vdev), ret);
384 return ret;
386 #endif
388 static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
390 struct video_device *vdev = video_devdata(filp);
391 int ret = -ENODEV;
393 if (!vdev->fops->mmap)
394 return -ENODEV;
395 if (video_is_registered(vdev))
396 ret = vdev->fops->mmap(filp, vm);
397 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
398 printk(KERN_DEBUG "%s: mmap (%d)\n",
399 video_device_node_name(vdev), ret);
400 return ret;
403 /* Override for the open function */
404 static int v4l2_open(struct inode *inode, struct file *filp)
406 struct video_device *vdev;
407 int ret = 0;
409 /* Check if the video device is available */
410 mutex_lock(&videodev_lock);
411 vdev = video_devdata(filp);
412 /* return ENODEV if the video device has already been removed. */
413 if (vdev == NULL || !video_is_registered(vdev)) {
414 mutex_unlock(&videodev_lock);
415 return -ENODEV;
417 /* and increase the device refcount */
418 video_get(vdev);
419 mutex_unlock(&videodev_lock);
420 if (vdev->fops->open) {
421 if (video_is_registered(vdev))
422 ret = vdev->fops->open(filp);
423 else
424 ret = -ENODEV;
427 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
428 printk(KERN_DEBUG "%s: open (%d)\n",
429 video_device_node_name(vdev), ret);
430 /* decrease the refcount in case of an error */
431 if (ret)
432 video_put(vdev);
433 return ret;
436 /* Override for the release function */
437 static int v4l2_release(struct inode *inode, struct file *filp)
439 struct video_device *vdev = video_devdata(filp);
440 int ret = 0;
442 if (vdev->fops->release)
443 ret = vdev->fops->release(filp);
444 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
445 printk(KERN_DEBUG "%s: release\n",
446 video_device_node_name(vdev));
448 /* decrease the refcount unconditionally since the release()
449 return value is ignored. */
450 video_put(vdev);
451 return ret;
454 static const struct file_operations v4l2_fops = {
455 .owner = THIS_MODULE,
456 .read = v4l2_read,
457 .write = v4l2_write,
458 .open = v4l2_open,
459 .get_unmapped_area = v4l2_get_unmapped_area,
460 .mmap = v4l2_mmap,
461 .unlocked_ioctl = v4l2_ioctl,
462 #ifdef CONFIG_COMPAT
463 .compat_ioctl = v4l2_compat_ioctl32,
464 #endif
465 .release = v4l2_release,
466 .poll = v4l2_poll,
467 .llseek = no_llseek,
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
474 * Note that when this is called the new device has not yet been registered
475 * in the video_device array, but it was able to obtain a minor number.
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.
481 * Returns a free index number.
483 static int get_index(struct video_device *vdev)
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);
488 int i;
490 bitmap_zero(used, VIDEO_NUM_DEVICES);
492 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
493 if (video_device[i] != NULL &&
494 video_device[i]->v4l2_dev == vdev->v4l2_dev) {
495 set_bit(video_device[i]->index, used);
499 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
502 #define SET_VALID_IOCTL(ops, cmd, op) \
503 if (ops->op) \
504 set_bit(_IOC_NR(cmd), valid_ioctls)
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.
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.
514 It does that by first setting up the local valid_ioctls bitmap, and
515 at the end do a:
517 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
519 static void determine_valid_ioctls(struct video_device *vdev)
521 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
522 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
523 bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
524 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
525 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
526 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
527 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
528 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
530 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
532 /* vfl_type and vfl_dir independent ioctls */
534 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
535 set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
536 set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
538 /* Note: the control handler can also be passed through the filehandle,
539 and that can't be tested here. If the bit for these control ioctls
540 is set, then the ioctl is valid. But if it is 0, then it can still
541 be valid if the filehandle passed the control handler. */
542 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
543 set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
544 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
545 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
546 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
547 set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
548 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
549 set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
550 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
551 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
552 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
553 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
554 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
555 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
556 if (vdev->ctrl_handler || ops->vidioc_querymenu)
557 set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
558 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
559 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
560 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
561 #ifdef CONFIG_VIDEO_ADV_DEBUG
562 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
563 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
564 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
565 #endif
566 /* yes, really vidioc_subscribe_event */
567 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
568 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
569 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
570 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
571 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
573 if (is_vid) {
574 /* video specific ioctls */
575 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
576 ops->vidioc_enum_fmt_vid_cap_mplane ||
577 ops->vidioc_enum_fmt_vid_overlay)) ||
578 (is_tx && (ops->vidioc_enum_fmt_vid_out ||
579 ops->vidioc_enum_fmt_vid_out_mplane)))
580 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
581 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
582 ops->vidioc_g_fmt_vid_cap_mplane ||
583 ops->vidioc_g_fmt_vid_overlay)) ||
584 (is_tx && (ops->vidioc_g_fmt_vid_out ||
585 ops->vidioc_g_fmt_vid_out_mplane ||
586 ops->vidioc_g_fmt_vid_out_overlay)))
587 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
588 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
589 ops->vidioc_s_fmt_vid_cap_mplane ||
590 ops->vidioc_s_fmt_vid_overlay)) ||
591 (is_tx && (ops->vidioc_s_fmt_vid_out ||
592 ops->vidioc_s_fmt_vid_out_mplane ||
593 ops->vidioc_s_fmt_vid_out_overlay)))
594 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
595 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
596 ops->vidioc_try_fmt_vid_cap_mplane ||
597 ops->vidioc_try_fmt_vid_overlay)) ||
598 (is_tx && (ops->vidioc_try_fmt_vid_out ||
599 ops->vidioc_try_fmt_vid_out_mplane ||
600 ops->vidioc_try_fmt_vid_out_overlay)))
601 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
602 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
603 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
604 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
605 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
606 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
607 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
608 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
609 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
610 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
611 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
612 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
613 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
614 if (ops->vidioc_g_crop || ops->vidioc_g_selection)
615 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
616 if (ops->vidioc_s_crop || ops->vidioc_s_selection)
617 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
618 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
619 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
620 if (ops->vidioc_cropcap || ops->vidioc_g_selection)
621 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
622 } else if (is_vbi) {
623 /* vbi specific ioctls */
624 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
625 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
626 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
627 ops->vidioc_g_fmt_sliced_vbi_out)))
628 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
629 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
630 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
631 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
632 ops->vidioc_s_fmt_sliced_vbi_out)))
633 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
634 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
635 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
636 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
637 ops->vidioc_try_fmt_sliced_vbi_out)))
638 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
639 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
640 } else if (is_sdr && is_rx) {
641 /* SDR receiver specific ioctls */
642 if (ops->vidioc_enum_fmt_sdr_cap)
643 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
644 if (ops->vidioc_g_fmt_sdr_cap)
645 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
646 if (ops->vidioc_s_fmt_sdr_cap)
647 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
648 if (ops->vidioc_try_fmt_sdr_cap)
649 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
650 } else if (is_sdr && is_tx) {
651 /* SDR transmitter specific ioctls */
652 if (ops->vidioc_enum_fmt_sdr_out)
653 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
654 if (ops->vidioc_g_fmt_sdr_out)
655 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
656 if (ops->vidioc_s_fmt_sdr_out)
657 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
658 if (ops->vidioc_try_fmt_sdr_out)
659 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
662 if (is_vid || is_vbi || is_sdr) {
663 /* ioctls valid for video, vbi or sdr */
664 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
665 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
666 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
667 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
668 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
669 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
670 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
671 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
672 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
675 if (is_vid || is_vbi) {
676 /* ioctls valid for video or vbi */
677 if (ops->vidioc_s_std)
678 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
679 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
680 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
681 if (is_rx) {
682 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
683 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
684 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
685 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
686 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
687 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
688 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
689 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
690 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
692 if (is_tx) {
693 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
694 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
695 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
696 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
697 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
698 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
700 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
701 ops->vidioc_g_std))
702 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
703 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
704 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
705 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
706 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
707 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
708 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
710 if (is_tx && (is_radio || is_sdr)) {
711 /* radio transmitter only ioctls */
712 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
713 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
715 if (is_rx) {
716 /* receiver only ioctls */
717 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
718 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
719 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
722 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
723 BASE_VIDIOC_PRIVATE);
727 * __video_register_device - register video4linux devices
728 * @vdev: video device structure we want to register
729 * @type: type of device to register
730 * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
731 * -1 == first free)
732 * @warn_if_nr_in_use: warn if the desired device node number
733 * was already in use and another number was chosen instead.
734 * @owner: module that owns the video device node
736 * The registration code assigns minor numbers and device node numbers
737 * based on the requested type and registers the new device node with
738 * the kernel.
740 * This function assumes that struct video_device was zeroed when it
741 * was allocated and does not contain any stale date.
743 * An error is returned if no free minor or device node number could be
744 * found, or if the registration of the device node failed.
746 * Zero is returned on success.
748 * Valid types are
750 * %VFL_TYPE_GRABBER - A frame grabber
752 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
754 * %VFL_TYPE_RADIO - A radio card
756 * %VFL_TYPE_SUBDEV - A subdevice
758 * %VFL_TYPE_SDR - Software Defined Radio
760 int __video_register_device(struct video_device *vdev, int type, int nr,
761 int warn_if_nr_in_use, struct module *owner)
763 int i = 0;
764 int ret;
765 int minor_offset = 0;
766 int minor_cnt = VIDEO_NUM_DEVICES;
767 const char *name_base;
769 /* A minor value of -1 marks this video device as never
770 having been registered */
771 vdev->minor = -1;
773 /* the release callback MUST be present */
774 if (WARN_ON(!vdev->release))
775 return -EINVAL;
776 /* the v4l2_dev pointer MUST be present */
777 if (WARN_ON(!vdev->v4l2_dev))
778 return -EINVAL;
780 /* v4l2_fh support */
781 spin_lock_init(&vdev->fh_lock);
782 INIT_LIST_HEAD(&vdev->fh_list);
784 /* Part 1: check device type */
785 switch (type) {
786 case VFL_TYPE_GRABBER:
787 name_base = "video";
788 break;
789 case VFL_TYPE_VBI:
790 name_base = "vbi";
791 break;
792 case VFL_TYPE_RADIO:
793 name_base = "radio";
794 break;
795 case VFL_TYPE_SUBDEV:
796 name_base = "v4l-subdev";
797 break;
798 case VFL_TYPE_SDR:
799 /* Use device name 'swradio' because 'sdr' was already taken. */
800 name_base = "swradio";
801 break;
802 default:
803 printk(KERN_ERR "%s called with unknown type: %d\n",
804 __func__, type);
805 return -EINVAL;
808 vdev->vfl_type = type;
809 vdev->cdev = NULL;
810 if (vdev->dev_parent == NULL)
811 vdev->dev_parent = vdev->v4l2_dev->dev;
812 if (vdev->ctrl_handler == NULL)
813 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
814 /* If the prio state pointer is NULL, then use the v4l2_device
815 prio state. */
816 if (vdev->prio == NULL)
817 vdev->prio = &vdev->v4l2_dev->prio;
819 /* Part 2: find a free minor, device node number and device index. */
820 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
821 /* Keep the ranges for the first four types for historical
822 * reasons.
823 * Newer devices (not yet in place) should use the range
824 * of 128-191 and just pick the first free minor there
825 * (new style). */
826 switch (type) {
827 case VFL_TYPE_GRABBER:
828 minor_offset = 0;
829 minor_cnt = 64;
830 break;
831 case VFL_TYPE_RADIO:
832 minor_offset = 64;
833 minor_cnt = 64;
834 break;
835 case VFL_TYPE_VBI:
836 minor_offset = 224;
837 minor_cnt = 32;
838 break;
839 default:
840 minor_offset = 128;
841 minor_cnt = 64;
842 break;
844 #endif
846 /* Pick a device node number */
847 mutex_lock(&videodev_lock);
848 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
849 if (nr == minor_cnt)
850 nr = devnode_find(vdev, 0, minor_cnt);
851 if (nr == minor_cnt) {
852 printk(KERN_ERR "could not get a free device node number\n");
853 mutex_unlock(&videodev_lock);
854 return -ENFILE;
856 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
857 /* 1-on-1 mapping of device node number to minor number */
858 i = nr;
859 #else
860 /* The device node number and minor numbers are independent, so
861 we just find the first free minor number. */
862 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
863 if (video_device[i] == NULL)
864 break;
865 if (i == VIDEO_NUM_DEVICES) {
866 mutex_unlock(&videodev_lock);
867 printk(KERN_ERR "could not get a free minor\n");
868 return -ENFILE;
870 #endif
871 vdev->minor = i + minor_offset;
872 vdev->num = nr;
873 devnode_set(vdev);
875 /* Should not happen since we thought this minor was free */
876 WARN_ON(video_device[vdev->minor] != NULL);
877 vdev->index = get_index(vdev);
878 video_device[vdev->minor] = vdev;
879 mutex_unlock(&videodev_lock);
881 if (vdev->ioctl_ops)
882 determine_valid_ioctls(vdev);
884 /* Part 3: Initialize the character device */
885 vdev->cdev = cdev_alloc();
886 if (vdev->cdev == NULL) {
887 ret = -ENOMEM;
888 goto cleanup;
890 vdev->cdev->ops = &v4l2_fops;
891 vdev->cdev->owner = owner;
892 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
893 if (ret < 0) {
894 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
895 kfree(vdev->cdev);
896 vdev->cdev = NULL;
897 goto cleanup;
900 /* Part 4: register the device with sysfs */
901 vdev->dev.class = &video_class;
902 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
903 vdev->dev.parent = vdev->dev_parent;
904 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
905 ret = device_register(&vdev->dev);
906 if (ret < 0) {
907 printk(KERN_ERR "%s: device_register failed\n", __func__);
908 goto cleanup;
910 /* Register the release callback that will be called when the last
911 reference to the device goes away. */
912 vdev->dev.release = v4l2_device_release;
914 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
915 printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
916 name_base, nr, video_device_node_name(vdev));
918 /* Increase v4l2_device refcount */
919 v4l2_device_get(vdev->v4l2_dev);
921 #if defined(CONFIG_MEDIA_CONTROLLER)
922 /* Part 5: Register the entity. */
923 if (vdev->v4l2_dev->mdev &&
924 vdev->vfl_type != VFL_TYPE_SUBDEV) {
925 vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
926 vdev->entity.name = vdev->name;
927 vdev->entity.info.dev.major = VIDEO_MAJOR;
928 vdev->entity.info.dev.minor = vdev->minor;
929 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
930 &vdev->entity);
931 if (ret < 0)
932 printk(KERN_WARNING
933 "%s: media_device_register_entity failed\n",
934 __func__);
936 #endif
937 /* Part 6: Activate this minor. The char device can now be used. */
938 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
940 return 0;
942 cleanup:
943 mutex_lock(&videodev_lock);
944 if (vdev->cdev)
945 cdev_del(vdev->cdev);
946 video_device[vdev->minor] = NULL;
947 devnode_clear(vdev);
948 mutex_unlock(&videodev_lock);
949 /* Mark this video device as never having been registered. */
950 vdev->minor = -1;
951 return ret;
953 EXPORT_SYMBOL(__video_register_device);
956 * video_unregister_device - unregister a video4linux device
957 * @vdev: the device to unregister
959 * This unregisters the passed device. Future open calls will
960 * be met with errors.
962 void video_unregister_device(struct video_device *vdev)
964 /* Check if vdev was ever registered at all */
965 if (!vdev || !video_is_registered(vdev))
966 return;
968 mutex_lock(&videodev_lock);
969 /* This must be in a critical section to prevent a race with v4l2_open.
970 * Once this bit has been cleared video_get may never be called again.
972 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
973 mutex_unlock(&videodev_lock);
974 device_unregister(&vdev->dev);
976 EXPORT_SYMBOL(video_unregister_device);
979 * Initialise video for linux
981 static int __init videodev_init(void)
983 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
984 int ret;
986 printk(KERN_INFO "Linux video capture interface: v2.00\n");
987 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
988 if (ret < 0) {
989 printk(KERN_WARNING "videodev: unable to get major %d\n",
990 VIDEO_MAJOR);
991 return ret;
994 ret = class_register(&video_class);
995 if (ret < 0) {
996 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
997 printk(KERN_WARNING "video_dev: class_register failed\n");
998 return -EIO;
1001 return 0;
1004 static void __exit videodev_exit(void)
1006 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1008 class_unregister(&video_class);
1009 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1012 subsys_initcall(videodev_init);
1013 module_exit(videodev_exit)
1015 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1016 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1017 MODULE_LICENSE("GPL");
1018 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);