1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Video capture interface for Linux version 2
5 * A generic video device interface for the LINUX operating system
6 * using a set of device structures/vectors for low level operations.
8 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
9 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
11 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
12 * - Added procfs support
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
32 #define VIDEO_NUM_DEVICES 256
33 #define VIDEO_NAME "video4linux"
35 #define dprintk(fmt, arg...) do { \
36 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
45 static ssize_t
index_show(struct device
*cd
,
46 struct device_attribute
*attr
, char *buf
)
48 struct video_device
*vdev
= to_video_device(cd
);
50 return sprintf(buf
, "%i\n", vdev
->index
);
52 static DEVICE_ATTR_RO(index
);
54 static ssize_t
dev_debug_show(struct device
*cd
,
55 struct device_attribute
*attr
, char *buf
)
57 struct video_device
*vdev
= to_video_device(cd
);
59 return sprintf(buf
, "%i\n", vdev
->dev_debug
);
62 static ssize_t
dev_debug_store(struct device
*cd
, struct device_attribute
*attr
,
63 const char *buf
, size_t len
)
65 struct video_device
*vdev
= to_video_device(cd
);
69 res
= kstrtou16(buf
, 0, &value
);
73 vdev
->dev_debug
= value
;
76 static DEVICE_ATTR_RW(dev_debug
);
78 static ssize_t
name_show(struct device
*cd
,
79 struct device_attribute
*attr
, char *buf
)
81 struct video_device
*vdev
= to_video_device(cd
);
83 return sprintf(buf
, "%.*s\n", (int)sizeof(vdev
->name
), vdev
->name
);
85 static DEVICE_ATTR_RO(name
);
87 static struct attribute
*video_device_attrs
[] = {
89 &dev_attr_dev_debug
.attr
,
93 ATTRIBUTE_GROUPS(video_device
);
98 static struct video_device
*video_devices
[VIDEO_NUM_DEVICES
];
99 static DEFINE_MUTEX(videodev_lock
);
100 static DECLARE_BITMAP(devnode_nums
[VFL_TYPE_MAX
], VIDEO_NUM_DEVICES
);
102 /* Device node utility functions */
104 /* Note: these utility functions all assume that vfl_type is in the range
105 [0, VFL_TYPE_MAX-1]. */
107 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
108 /* Return the bitmap corresponding to vfl_type. */
109 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type
)
111 /* Any types not assigned to fixed minor ranges must be mapped to
112 one single bitmap for the purposes of finding a free node number
113 since all those unassigned types use the same minor range. */
114 int idx
= (vfl_type
> VFL_TYPE_RADIO
) ? VFL_TYPE_MAX
- 1 : vfl_type
;
116 return devnode_nums
[idx
];
119 /* Return the bitmap corresponding to vfl_type. */
120 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type
)
122 return devnode_nums
[vfl_type
];
126 /* Mark device node number vdev->num as used */
127 static inline void devnode_set(struct video_device
*vdev
)
129 set_bit(vdev
->num
, devnode_bits(vdev
->vfl_type
));
132 /* Mark device node number vdev->num as unused */
133 static inline void devnode_clear(struct video_device
*vdev
)
135 clear_bit(vdev
->num
, devnode_bits(vdev
->vfl_type
));
138 /* Try to find a free device node number in the range [from, to> */
139 static inline int devnode_find(struct video_device
*vdev
, int from
, int to
)
141 return find_next_zero_bit(devnode_bits(vdev
->vfl_type
), to
, from
);
144 struct video_device
*video_device_alloc(void)
146 return kzalloc(sizeof(struct video_device
), GFP_KERNEL
);
148 EXPORT_SYMBOL(video_device_alloc
);
150 void video_device_release(struct video_device
*vdev
)
154 EXPORT_SYMBOL(video_device_release
);
156 void video_device_release_empty(struct video_device
*vdev
)
159 /* Only valid when the video_device struct is a static. */
161 EXPORT_SYMBOL(video_device_release_empty
);
163 static inline void video_get(struct video_device
*vdev
)
165 get_device(&vdev
->dev
);
168 static inline void video_put(struct video_device
*vdev
)
170 put_device(&vdev
->dev
);
173 /* Called when the last user of the video device exits. */
174 static void v4l2_device_release(struct device
*cd
)
176 struct video_device
*vdev
= to_video_device(cd
);
177 struct v4l2_device
*v4l2_dev
= vdev
->v4l2_dev
;
179 mutex_lock(&videodev_lock
);
180 if (WARN_ON(video_devices
[vdev
->minor
] != vdev
)) {
181 /* should not happen */
182 mutex_unlock(&videodev_lock
);
186 /* Free up this device for reuse */
187 video_devices
[vdev
->minor
] = NULL
;
189 /* Delete the cdev on this minor as well */
190 cdev_del(vdev
->cdev
);
191 /* Just in case some driver tries to access this from
192 the release() callback. */
195 /* Mark device node number as free */
198 mutex_unlock(&videodev_lock
);
200 #if defined(CONFIG_MEDIA_CONTROLLER)
201 if (v4l2_dev
->mdev
&& vdev
->vfl_dir
!= VFL_DIR_M2M
) {
202 /* Remove interfaces and interface links */
203 media_devnode_remove(vdev
->intf_devnode
);
204 if (vdev
->entity
.function
!= MEDIA_ENT_F_UNKNOWN
)
205 media_device_unregister_entity(&vdev
->entity
);
209 /* Do not call v4l2_device_put if there is no release callback set.
210 * Drivers that have no v4l2_device release callback might free the
211 * v4l2_dev instance in the video_device release callback below, so we
212 * must perform this check here.
214 * TODO: In the long run all drivers that use v4l2_device should use the
215 * v4l2_device release callback. This check will then be unnecessary.
217 if (v4l2_dev
->release
== NULL
)
220 /* Release video_device and perform other
221 cleanups as needed. */
224 /* Decrease v4l2_device refcount */
226 v4l2_device_put(v4l2_dev
);
229 static struct class video_class
= {
231 .dev_groups
= video_device_groups
,
234 struct video_device
*video_devdata(struct file
*file
)
236 return video_devices
[iminor(file_inode(file
))];
238 EXPORT_SYMBOL(video_devdata
);
241 /* Priority handling */
243 static inline bool prio_is_valid(enum v4l2_priority prio
)
245 return prio
== V4L2_PRIORITY_BACKGROUND
||
246 prio
== V4L2_PRIORITY_INTERACTIVE
||
247 prio
== V4L2_PRIORITY_RECORD
;
250 void v4l2_prio_init(struct v4l2_prio_state
*global
)
252 memset(global
, 0, sizeof(*global
));
254 EXPORT_SYMBOL(v4l2_prio_init
);
256 int v4l2_prio_change(struct v4l2_prio_state
*global
, enum v4l2_priority
*local
,
257 enum v4l2_priority
new)
259 if (!prio_is_valid(new))
264 atomic_inc(&global
->prios
[new]);
265 if (prio_is_valid(*local
))
266 atomic_dec(&global
->prios
[*local
]);
270 EXPORT_SYMBOL(v4l2_prio_change
);
272 void v4l2_prio_open(struct v4l2_prio_state
*global
, enum v4l2_priority
*local
)
274 v4l2_prio_change(global
, local
, V4L2_PRIORITY_DEFAULT
);
276 EXPORT_SYMBOL(v4l2_prio_open
);
278 void v4l2_prio_close(struct v4l2_prio_state
*global
, enum v4l2_priority local
)
280 if (prio_is_valid(local
))
281 atomic_dec(&global
->prios
[local
]);
283 EXPORT_SYMBOL(v4l2_prio_close
);
285 enum v4l2_priority
v4l2_prio_max(struct v4l2_prio_state
*global
)
287 if (atomic_read(&global
->prios
[V4L2_PRIORITY_RECORD
]) > 0)
288 return V4L2_PRIORITY_RECORD
;
289 if (atomic_read(&global
->prios
[V4L2_PRIORITY_INTERACTIVE
]) > 0)
290 return V4L2_PRIORITY_INTERACTIVE
;
291 if (atomic_read(&global
->prios
[V4L2_PRIORITY_BACKGROUND
]) > 0)
292 return V4L2_PRIORITY_BACKGROUND
;
293 return V4L2_PRIORITY_UNSET
;
295 EXPORT_SYMBOL(v4l2_prio_max
);
297 int v4l2_prio_check(struct v4l2_prio_state
*global
, enum v4l2_priority local
)
299 return (local
< v4l2_prio_max(global
)) ? -EBUSY
: 0;
301 EXPORT_SYMBOL(v4l2_prio_check
);
304 static ssize_t
v4l2_read(struct file
*filp
, char __user
*buf
,
305 size_t sz
, loff_t
*off
)
307 struct video_device
*vdev
= video_devdata(filp
);
310 if (!vdev
->fops
->read
)
312 if (video_is_registered(vdev
))
313 ret
= vdev
->fops
->read(filp
, buf
, sz
, off
);
314 if ((vdev
->dev_debug
& V4L2_DEV_DEBUG_FOP
) &&
315 (vdev
->dev_debug
& V4L2_DEV_DEBUG_STREAMING
))
316 dprintk("%s: read: %zd (%d)\n",
317 video_device_node_name(vdev
), sz
, ret
);
321 static ssize_t
v4l2_write(struct file
*filp
, const char __user
*buf
,
322 size_t sz
, loff_t
*off
)
324 struct video_device
*vdev
= video_devdata(filp
);
327 if (!vdev
->fops
->write
)
329 if (video_is_registered(vdev
))
330 ret
= vdev
->fops
->write(filp
, buf
, sz
, off
);
331 if ((vdev
->dev_debug
& V4L2_DEV_DEBUG_FOP
) &&
332 (vdev
->dev_debug
& V4L2_DEV_DEBUG_STREAMING
))
333 dprintk("%s: write: %zd (%d)\n",
334 video_device_node_name(vdev
), sz
, ret
);
338 static __poll_t
v4l2_poll(struct file
*filp
, struct poll_table_struct
*poll
)
340 struct video_device
*vdev
= video_devdata(filp
);
341 __poll_t res
= EPOLLERR
| EPOLLHUP
;
343 if (!vdev
->fops
->poll
)
344 return DEFAULT_POLLMASK
;
345 if (video_is_registered(vdev
))
346 res
= vdev
->fops
->poll(filp
, poll
);
347 if (vdev
->dev_debug
& V4L2_DEV_DEBUG_POLL
)
348 dprintk("%s: poll: %08x\n",
349 video_device_node_name(vdev
), res
);
353 static long v4l2_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
355 struct video_device
*vdev
= video_devdata(filp
);
358 if (vdev
->fops
->unlocked_ioctl
) {
359 if (video_is_registered(vdev
))
360 ret
= vdev
->fops
->unlocked_ioctl(filp
, cmd
, arg
);
368 #define v4l2_get_unmapped_area NULL
370 static unsigned long v4l2_get_unmapped_area(struct file
*filp
,
371 unsigned long addr
, unsigned long len
, unsigned long pgoff
,
374 struct video_device
*vdev
= video_devdata(filp
);
377 if (!vdev
->fops
->get_unmapped_area
)
379 if (!video_is_registered(vdev
))
381 ret
= vdev
->fops
->get_unmapped_area(filp
, addr
, len
, pgoff
, flags
);
382 if (vdev
->dev_debug
& V4L2_DEV_DEBUG_FOP
)
383 dprintk("%s: get_unmapped_area (%d)\n",
384 video_device_node_name(vdev
), ret
);
389 static int v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vm
)
391 struct video_device
*vdev
= video_devdata(filp
);
394 if (!vdev
->fops
->mmap
)
396 if (video_is_registered(vdev
))
397 ret
= vdev
->fops
->mmap(filp
, vm
);
398 if (vdev
->dev_debug
& V4L2_DEV_DEBUG_FOP
)
399 dprintk("%s: mmap (%d)\n",
400 video_device_node_name(vdev
), ret
);
404 /* Override for the open function */
405 static int v4l2_open(struct inode
*inode
, struct file
*filp
)
407 struct video_device
*vdev
;
410 /* Check if the video device is available */
411 mutex_lock(&videodev_lock
);
412 vdev
= video_devdata(filp
);
413 /* return ENODEV if the video device has already been removed. */
414 if (vdev
== NULL
|| !video_is_registered(vdev
)) {
415 mutex_unlock(&videodev_lock
);
418 /* and increase the device refcount */
420 mutex_unlock(&videodev_lock
);
421 if (vdev
->fops
->open
) {
422 if (video_is_registered(vdev
))
423 ret
= vdev
->fops
->open(filp
);
428 if (vdev
->dev_debug
& V4L2_DEV_DEBUG_FOP
)
429 dprintk("%s: open (%d)\n",
430 video_device_node_name(vdev
), ret
);
431 /* decrease the refcount in case of an error */
437 /* Override for the release function */
438 static int v4l2_release(struct inode
*inode
, struct file
*filp
)
440 struct video_device
*vdev
= video_devdata(filp
);
444 * We need to serialize the release() with queueing new requests.
445 * The release() may trigger the cancellation of a streaming
446 * operation, and that should not be mixed with queueing a new
447 * request at the same time.
449 if (vdev
->fops
->release
) {
450 if (v4l2_device_supports_requests(vdev
->v4l2_dev
)) {
451 mutex_lock(&vdev
->v4l2_dev
->mdev
->req_queue_mutex
);
452 ret
= vdev
->fops
->release(filp
);
453 mutex_unlock(&vdev
->v4l2_dev
->mdev
->req_queue_mutex
);
455 ret
= vdev
->fops
->release(filp
);
459 if (vdev
->dev_debug
& V4L2_DEV_DEBUG_FOP
)
460 dprintk("%s: release\n",
461 video_device_node_name(vdev
));
463 /* decrease the refcount unconditionally since the release()
464 return value is ignored. */
469 static const struct file_operations v4l2_fops
= {
470 .owner
= THIS_MODULE
,
474 .get_unmapped_area
= v4l2_get_unmapped_area
,
476 .unlocked_ioctl
= v4l2_ioctl
,
478 .compat_ioctl
= v4l2_compat_ioctl32
,
480 .release
= v4l2_release
,
486 * get_index - assign stream index number based on v4l2_dev
487 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
489 * Note that when this is called the new device has not yet been registered
490 * in the video_device array, but it was able to obtain a minor number.
492 * This means that we can always obtain a free stream index number since
493 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
494 * use of the video_device array.
496 * Returns a free index number.
498 static int get_index(struct video_device
*vdev
)
500 /* This can be static since this function is called with the global
501 videodev_lock held. */
502 static DECLARE_BITMAP(used
, VIDEO_NUM_DEVICES
);
505 bitmap_zero(used
, VIDEO_NUM_DEVICES
);
507 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++) {
508 if (video_devices
[i
] != NULL
&&
509 video_devices
[i
]->v4l2_dev
== vdev
->v4l2_dev
) {
510 set_bit(video_devices
[i
]->index
, used
);
514 return find_first_zero_bit(used
, VIDEO_NUM_DEVICES
);
517 #define SET_VALID_IOCTL(ops, cmd, op) \
519 set_bit(_IOC_NR(cmd), valid_ioctls)
521 /* This determines which ioctls are actually implemented in the driver.
522 It's a one-time thing which simplifies video_ioctl2 as it can just do
525 Note that drivers can override this by setting bits to 1 in
526 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
527 called, then that ioctl will actually be marked as unimplemented.
529 It does that by first setting up the local valid_ioctls bitmap, and
532 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
534 static void determine_valid_ioctls(struct video_device
*vdev
)
536 const u32 vid_caps
= V4L2_CAP_VIDEO_CAPTURE
|
537 V4L2_CAP_VIDEO_CAPTURE_MPLANE
|
538 V4L2_CAP_VIDEO_OUTPUT
|
539 V4L2_CAP_VIDEO_OUTPUT_MPLANE
|
540 V4L2_CAP_VIDEO_M2M
| V4L2_CAP_VIDEO_M2M_MPLANE
;
541 const u32 meta_caps
= V4L2_CAP_META_CAPTURE
|
542 V4L2_CAP_META_OUTPUT
;
543 DECLARE_BITMAP(valid_ioctls
, BASE_VIDIOC_PRIVATE
);
544 const struct v4l2_ioctl_ops
*ops
= vdev
->ioctl_ops
;
545 bool is_vid
= vdev
->vfl_type
== VFL_TYPE_VIDEO
&&
546 (vdev
->device_caps
& vid_caps
);
547 bool is_vbi
= vdev
->vfl_type
== VFL_TYPE_VBI
;
548 bool is_radio
= vdev
->vfl_type
== VFL_TYPE_RADIO
;
549 bool is_sdr
= vdev
->vfl_type
== VFL_TYPE_SDR
;
550 bool is_tch
= vdev
->vfl_type
== VFL_TYPE_TOUCH
;
551 bool is_meta
= vdev
->vfl_type
== VFL_TYPE_VIDEO
&&
552 (vdev
->device_caps
& meta_caps
);
553 bool is_rx
= vdev
->vfl_dir
!= VFL_DIR_TX
;
554 bool is_tx
= vdev
->vfl_dir
!= VFL_DIR_RX
;
555 bool is_io_mc
= vdev
->device_caps
& V4L2_CAP_IO_MC
;
557 bitmap_zero(valid_ioctls
, BASE_VIDIOC_PRIVATE
);
559 /* vfl_type and vfl_dir independent ioctls */
561 SET_VALID_IOCTL(ops
, VIDIOC_QUERYCAP
, vidioc_querycap
);
562 set_bit(_IOC_NR(VIDIOC_G_PRIORITY
), valid_ioctls
);
563 set_bit(_IOC_NR(VIDIOC_S_PRIORITY
), valid_ioctls
);
565 /* Note: the control handler can also be passed through the filehandle,
566 and that can't be tested here. If the bit for these control ioctls
567 is set, then the ioctl is valid. But if it is 0, then it can still
568 be valid if the filehandle passed the control handler. */
569 if (vdev
->ctrl_handler
|| ops
->vidioc_queryctrl
)
570 set_bit(_IOC_NR(VIDIOC_QUERYCTRL
), valid_ioctls
);
571 if (vdev
->ctrl_handler
|| ops
->vidioc_query_ext_ctrl
)
572 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL
), valid_ioctls
);
573 if (vdev
->ctrl_handler
|| ops
->vidioc_g_ctrl
|| ops
->vidioc_g_ext_ctrls
)
574 set_bit(_IOC_NR(VIDIOC_G_CTRL
), valid_ioctls
);
575 if (vdev
->ctrl_handler
|| ops
->vidioc_s_ctrl
|| ops
->vidioc_s_ext_ctrls
)
576 set_bit(_IOC_NR(VIDIOC_S_CTRL
), valid_ioctls
);
577 if (vdev
->ctrl_handler
|| ops
->vidioc_g_ext_ctrls
)
578 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS
), valid_ioctls
);
579 if (vdev
->ctrl_handler
|| ops
->vidioc_s_ext_ctrls
)
580 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS
), valid_ioctls
);
581 if (vdev
->ctrl_handler
|| ops
->vidioc_try_ext_ctrls
)
582 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS
), valid_ioctls
);
583 if (vdev
->ctrl_handler
|| ops
->vidioc_querymenu
)
584 set_bit(_IOC_NR(VIDIOC_QUERYMENU
), valid_ioctls
);
586 SET_VALID_IOCTL(ops
, VIDIOC_G_FREQUENCY
, vidioc_g_frequency
);
587 SET_VALID_IOCTL(ops
, VIDIOC_S_FREQUENCY
, vidioc_s_frequency
);
589 SET_VALID_IOCTL(ops
, VIDIOC_LOG_STATUS
, vidioc_log_status
);
590 #ifdef CONFIG_VIDEO_ADV_DEBUG
591 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO
), valid_ioctls
);
592 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER
), valid_ioctls
);
593 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER
), valid_ioctls
);
595 /* yes, really vidioc_subscribe_event */
596 SET_VALID_IOCTL(ops
, VIDIOC_DQEVENT
, vidioc_subscribe_event
);
597 SET_VALID_IOCTL(ops
, VIDIOC_SUBSCRIBE_EVENT
, vidioc_subscribe_event
);
598 SET_VALID_IOCTL(ops
, VIDIOC_UNSUBSCRIBE_EVENT
, vidioc_unsubscribe_event
);
599 if (ops
->vidioc_enum_freq_bands
|| ops
->vidioc_g_tuner
|| ops
->vidioc_g_modulator
)
600 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS
), valid_ioctls
);
603 /* video specific ioctls */
604 if ((is_rx
&& (ops
->vidioc_enum_fmt_vid_cap
||
605 ops
->vidioc_enum_fmt_vid_overlay
)) ||
606 (is_tx
&& ops
->vidioc_enum_fmt_vid_out
))
607 set_bit(_IOC_NR(VIDIOC_ENUM_FMT
), valid_ioctls
);
608 if ((is_rx
&& (ops
->vidioc_g_fmt_vid_cap
||
609 ops
->vidioc_g_fmt_vid_cap_mplane
||
610 ops
->vidioc_g_fmt_vid_overlay
)) ||
611 (is_tx
&& (ops
->vidioc_g_fmt_vid_out
||
612 ops
->vidioc_g_fmt_vid_out_mplane
||
613 ops
->vidioc_g_fmt_vid_out_overlay
)))
614 set_bit(_IOC_NR(VIDIOC_G_FMT
), valid_ioctls
);
615 if ((is_rx
&& (ops
->vidioc_s_fmt_vid_cap
||
616 ops
->vidioc_s_fmt_vid_cap_mplane
||
617 ops
->vidioc_s_fmt_vid_overlay
)) ||
618 (is_tx
&& (ops
->vidioc_s_fmt_vid_out
||
619 ops
->vidioc_s_fmt_vid_out_mplane
||
620 ops
->vidioc_s_fmt_vid_out_overlay
)))
621 set_bit(_IOC_NR(VIDIOC_S_FMT
), valid_ioctls
);
622 if ((is_rx
&& (ops
->vidioc_try_fmt_vid_cap
||
623 ops
->vidioc_try_fmt_vid_cap_mplane
||
624 ops
->vidioc_try_fmt_vid_overlay
)) ||
625 (is_tx
&& (ops
->vidioc_try_fmt_vid_out
||
626 ops
->vidioc_try_fmt_vid_out_mplane
||
627 ops
->vidioc_try_fmt_vid_out_overlay
)))
628 set_bit(_IOC_NR(VIDIOC_TRY_FMT
), valid_ioctls
);
629 SET_VALID_IOCTL(ops
, VIDIOC_OVERLAY
, vidioc_overlay
);
630 SET_VALID_IOCTL(ops
, VIDIOC_G_FBUF
, vidioc_g_fbuf
);
631 SET_VALID_IOCTL(ops
, VIDIOC_S_FBUF
, vidioc_s_fbuf
);
632 SET_VALID_IOCTL(ops
, VIDIOC_G_JPEGCOMP
, vidioc_g_jpegcomp
);
633 SET_VALID_IOCTL(ops
, VIDIOC_S_JPEGCOMP
, vidioc_s_jpegcomp
);
634 SET_VALID_IOCTL(ops
, VIDIOC_G_ENC_INDEX
, vidioc_g_enc_index
);
635 SET_VALID_IOCTL(ops
, VIDIOC_ENCODER_CMD
, vidioc_encoder_cmd
);
636 SET_VALID_IOCTL(ops
, VIDIOC_TRY_ENCODER_CMD
, vidioc_try_encoder_cmd
);
637 SET_VALID_IOCTL(ops
, VIDIOC_DECODER_CMD
, vidioc_decoder_cmd
);
638 SET_VALID_IOCTL(ops
, VIDIOC_TRY_DECODER_CMD
, vidioc_try_decoder_cmd
);
639 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FRAMESIZES
, vidioc_enum_framesizes
);
640 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FRAMEINTERVALS
, vidioc_enum_frameintervals
);
641 if (ops
->vidioc_g_selection
) {
642 set_bit(_IOC_NR(VIDIOC_G_CROP
), valid_ioctls
);
643 set_bit(_IOC_NR(VIDIOC_CROPCAP
), valid_ioctls
);
645 if (ops
->vidioc_s_selection
)
646 set_bit(_IOC_NR(VIDIOC_S_CROP
), valid_ioctls
);
647 SET_VALID_IOCTL(ops
, VIDIOC_G_SELECTION
, vidioc_g_selection
);
648 SET_VALID_IOCTL(ops
, VIDIOC_S_SELECTION
, vidioc_s_selection
);
650 if (is_meta
&& is_rx
) {
651 /* metadata capture specific ioctls */
652 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FMT
, vidioc_enum_fmt_meta_cap
);
653 SET_VALID_IOCTL(ops
, VIDIOC_G_FMT
, vidioc_g_fmt_meta_cap
);
654 SET_VALID_IOCTL(ops
, VIDIOC_S_FMT
, vidioc_s_fmt_meta_cap
);
655 SET_VALID_IOCTL(ops
, VIDIOC_TRY_FMT
, vidioc_try_fmt_meta_cap
);
656 } else if (is_meta
&& is_tx
) {
657 /* metadata output specific ioctls */
658 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FMT
, vidioc_enum_fmt_meta_out
);
659 SET_VALID_IOCTL(ops
, VIDIOC_G_FMT
, vidioc_g_fmt_meta_out
);
660 SET_VALID_IOCTL(ops
, VIDIOC_S_FMT
, vidioc_s_fmt_meta_out
);
661 SET_VALID_IOCTL(ops
, VIDIOC_TRY_FMT
, vidioc_try_fmt_meta_out
);
664 /* vbi specific ioctls */
665 if ((is_rx
&& (ops
->vidioc_g_fmt_vbi_cap
||
666 ops
->vidioc_g_fmt_sliced_vbi_cap
)) ||
667 (is_tx
&& (ops
->vidioc_g_fmt_vbi_out
||
668 ops
->vidioc_g_fmt_sliced_vbi_out
)))
669 set_bit(_IOC_NR(VIDIOC_G_FMT
), valid_ioctls
);
670 if ((is_rx
&& (ops
->vidioc_s_fmt_vbi_cap
||
671 ops
->vidioc_s_fmt_sliced_vbi_cap
)) ||
672 (is_tx
&& (ops
->vidioc_s_fmt_vbi_out
||
673 ops
->vidioc_s_fmt_sliced_vbi_out
)))
674 set_bit(_IOC_NR(VIDIOC_S_FMT
), valid_ioctls
);
675 if ((is_rx
&& (ops
->vidioc_try_fmt_vbi_cap
||
676 ops
->vidioc_try_fmt_sliced_vbi_cap
)) ||
677 (is_tx
&& (ops
->vidioc_try_fmt_vbi_out
||
678 ops
->vidioc_try_fmt_sliced_vbi_out
)))
679 set_bit(_IOC_NR(VIDIOC_TRY_FMT
), valid_ioctls
);
680 SET_VALID_IOCTL(ops
, VIDIOC_G_SLICED_VBI_CAP
, vidioc_g_sliced_vbi_cap
);
682 /* touch specific ioctls */
683 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FMT
, vidioc_enum_fmt_vid_cap
);
684 SET_VALID_IOCTL(ops
, VIDIOC_G_FMT
, vidioc_g_fmt_vid_cap
);
685 SET_VALID_IOCTL(ops
, VIDIOC_S_FMT
, vidioc_s_fmt_vid_cap
);
686 SET_VALID_IOCTL(ops
, VIDIOC_TRY_FMT
, vidioc_try_fmt_vid_cap
);
687 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FRAMESIZES
, vidioc_enum_framesizes
);
688 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FRAMEINTERVALS
, vidioc_enum_frameintervals
);
689 SET_VALID_IOCTL(ops
, VIDIOC_ENUMINPUT
, vidioc_enum_input
);
690 SET_VALID_IOCTL(ops
, VIDIOC_G_INPUT
, vidioc_g_input
);
691 SET_VALID_IOCTL(ops
, VIDIOC_S_INPUT
, vidioc_s_input
);
692 SET_VALID_IOCTL(ops
, VIDIOC_G_PARM
, vidioc_g_parm
);
693 SET_VALID_IOCTL(ops
, VIDIOC_S_PARM
, vidioc_s_parm
);
694 } else if (is_sdr
&& is_rx
) {
695 /* SDR receiver specific ioctls */
696 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FMT
, vidioc_enum_fmt_sdr_cap
);
697 SET_VALID_IOCTL(ops
, VIDIOC_G_FMT
, vidioc_g_fmt_sdr_cap
);
698 SET_VALID_IOCTL(ops
, VIDIOC_S_FMT
, vidioc_s_fmt_sdr_cap
);
699 SET_VALID_IOCTL(ops
, VIDIOC_TRY_FMT
, vidioc_try_fmt_sdr_cap
);
700 } else if (is_sdr
&& is_tx
) {
701 /* SDR transmitter specific ioctls */
702 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_FMT
, vidioc_enum_fmt_sdr_out
);
703 SET_VALID_IOCTL(ops
, VIDIOC_G_FMT
, vidioc_g_fmt_sdr_out
);
704 SET_VALID_IOCTL(ops
, VIDIOC_S_FMT
, vidioc_s_fmt_sdr_out
);
705 SET_VALID_IOCTL(ops
, VIDIOC_TRY_FMT
, vidioc_try_fmt_sdr_out
);
708 if (is_vid
|| is_vbi
|| is_sdr
|| is_tch
|| is_meta
) {
709 /* ioctls valid for video, vbi, sdr, touch and metadata */
710 SET_VALID_IOCTL(ops
, VIDIOC_REQBUFS
, vidioc_reqbufs
);
711 SET_VALID_IOCTL(ops
, VIDIOC_QUERYBUF
, vidioc_querybuf
);
712 SET_VALID_IOCTL(ops
, VIDIOC_QBUF
, vidioc_qbuf
);
713 SET_VALID_IOCTL(ops
, VIDIOC_EXPBUF
, vidioc_expbuf
);
714 SET_VALID_IOCTL(ops
, VIDIOC_DQBUF
, vidioc_dqbuf
);
715 SET_VALID_IOCTL(ops
, VIDIOC_CREATE_BUFS
, vidioc_create_bufs
);
716 SET_VALID_IOCTL(ops
, VIDIOC_PREPARE_BUF
, vidioc_prepare_buf
);
717 SET_VALID_IOCTL(ops
, VIDIOC_STREAMON
, vidioc_streamon
);
718 SET_VALID_IOCTL(ops
, VIDIOC_STREAMOFF
, vidioc_streamoff
);
721 if (is_vid
|| is_vbi
|| is_meta
) {
722 /* ioctls valid for video, vbi and metadata */
723 if (ops
->vidioc_s_std
)
724 set_bit(_IOC_NR(VIDIOC_ENUMSTD
), valid_ioctls
);
725 SET_VALID_IOCTL(ops
, VIDIOC_S_STD
, vidioc_s_std
);
726 SET_VALID_IOCTL(ops
, VIDIOC_G_STD
, vidioc_g_std
);
728 SET_VALID_IOCTL(ops
, VIDIOC_QUERYSTD
, vidioc_querystd
);
730 set_bit(_IOC_NR(VIDIOC_ENUMINPUT
), valid_ioctls
);
731 set_bit(_IOC_NR(VIDIOC_G_INPUT
), valid_ioctls
);
732 set_bit(_IOC_NR(VIDIOC_S_INPUT
), valid_ioctls
);
734 SET_VALID_IOCTL(ops
, VIDIOC_ENUMINPUT
, vidioc_enum_input
);
735 SET_VALID_IOCTL(ops
, VIDIOC_G_INPUT
, vidioc_g_input
);
736 SET_VALID_IOCTL(ops
, VIDIOC_S_INPUT
, vidioc_s_input
);
738 SET_VALID_IOCTL(ops
, VIDIOC_ENUMAUDIO
, vidioc_enumaudio
);
739 SET_VALID_IOCTL(ops
, VIDIOC_G_AUDIO
, vidioc_g_audio
);
740 SET_VALID_IOCTL(ops
, VIDIOC_S_AUDIO
, vidioc_s_audio
);
741 SET_VALID_IOCTL(ops
, VIDIOC_QUERY_DV_TIMINGS
, vidioc_query_dv_timings
);
742 SET_VALID_IOCTL(ops
, VIDIOC_S_EDID
, vidioc_s_edid
);
746 set_bit(_IOC_NR(VIDIOC_ENUMOUTPUT
), valid_ioctls
);
747 set_bit(_IOC_NR(VIDIOC_G_OUTPUT
), valid_ioctls
);
748 set_bit(_IOC_NR(VIDIOC_S_OUTPUT
), valid_ioctls
);
750 SET_VALID_IOCTL(ops
, VIDIOC_ENUMOUTPUT
, vidioc_enum_output
);
751 SET_VALID_IOCTL(ops
, VIDIOC_G_OUTPUT
, vidioc_g_output
);
752 SET_VALID_IOCTL(ops
, VIDIOC_S_OUTPUT
, vidioc_s_output
);
754 SET_VALID_IOCTL(ops
, VIDIOC_ENUMAUDOUT
, vidioc_enumaudout
);
755 SET_VALID_IOCTL(ops
, VIDIOC_G_AUDOUT
, vidioc_g_audout
);
756 SET_VALID_IOCTL(ops
, VIDIOC_S_AUDOUT
, vidioc_s_audout
);
758 if (ops
->vidioc_g_parm
|| ops
->vidioc_g_std
)
759 set_bit(_IOC_NR(VIDIOC_G_PARM
), valid_ioctls
);
760 SET_VALID_IOCTL(ops
, VIDIOC_S_PARM
, vidioc_s_parm
);
761 SET_VALID_IOCTL(ops
, VIDIOC_S_DV_TIMINGS
, vidioc_s_dv_timings
);
762 SET_VALID_IOCTL(ops
, VIDIOC_G_DV_TIMINGS
, vidioc_g_dv_timings
);
763 SET_VALID_IOCTL(ops
, VIDIOC_ENUM_DV_TIMINGS
, vidioc_enum_dv_timings
);
764 SET_VALID_IOCTL(ops
, VIDIOC_DV_TIMINGS_CAP
, vidioc_dv_timings_cap
);
765 SET_VALID_IOCTL(ops
, VIDIOC_G_EDID
, vidioc_g_edid
);
767 if (is_tx
&& (is_radio
|| is_sdr
)) {
768 /* radio transmitter only ioctls */
769 SET_VALID_IOCTL(ops
, VIDIOC_G_MODULATOR
, vidioc_g_modulator
);
770 SET_VALID_IOCTL(ops
, VIDIOC_S_MODULATOR
, vidioc_s_modulator
);
772 if (is_rx
&& !is_tch
) {
773 /* receiver only ioctls */
774 SET_VALID_IOCTL(ops
, VIDIOC_G_TUNER
, vidioc_g_tuner
);
775 SET_VALID_IOCTL(ops
, VIDIOC_S_TUNER
, vidioc_s_tuner
);
776 SET_VALID_IOCTL(ops
, VIDIOC_S_HW_FREQ_SEEK
, vidioc_s_hw_freq_seek
);
779 bitmap_andnot(vdev
->valid_ioctls
, valid_ioctls
, vdev
->valid_ioctls
,
780 BASE_VIDIOC_PRIVATE
);
783 static int video_register_media_controller(struct video_device
*vdev
)
785 #if defined(CONFIG_MEDIA_CONTROLLER)
789 /* Memory-to-memory devices are more complex and use
790 * their own function to register its mc entities.
792 if (!vdev
->v4l2_dev
->mdev
|| vdev
->vfl_dir
== VFL_DIR_M2M
)
795 vdev
->entity
.obj_type
= MEDIA_ENTITY_TYPE_VIDEO_DEVICE
;
796 vdev
->entity
.function
= MEDIA_ENT_F_UNKNOWN
;
798 switch (vdev
->vfl_type
) {
800 intf_type
= MEDIA_INTF_T_V4L_VIDEO
;
801 vdev
->entity
.function
= MEDIA_ENT_F_IO_V4L
;
804 intf_type
= MEDIA_INTF_T_V4L_VBI
;
805 vdev
->entity
.function
= MEDIA_ENT_F_IO_VBI
;
808 intf_type
= MEDIA_INTF_T_V4L_SWRADIO
;
809 vdev
->entity
.function
= MEDIA_ENT_F_IO_SWRADIO
;
812 intf_type
= MEDIA_INTF_T_V4L_TOUCH
;
813 vdev
->entity
.function
= MEDIA_ENT_F_IO_V4L
;
816 intf_type
= MEDIA_INTF_T_V4L_RADIO
;
818 * Radio doesn't have an entity at the V4L2 side to represent
819 * radio input or output. Instead, the audio input/output goes
820 * via either physical wires or ALSA.
823 case VFL_TYPE_SUBDEV
:
824 intf_type
= MEDIA_INTF_T_V4L_SUBDEV
;
825 /* Entity will be created via v4l2_device_register_subdev() */
831 if (vdev
->entity
.function
!= MEDIA_ENT_F_UNKNOWN
) {
832 vdev
->entity
.name
= vdev
->name
;
834 /* Needed just for backward compatibility with legacy MC API */
835 vdev
->entity
.info
.dev
.major
= VIDEO_MAJOR
;
836 vdev
->entity
.info
.dev
.minor
= vdev
->minor
;
838 ret
= media_device_register_entity(vdev
->v4l2_dev
->mdev
,
841 pr_warn("%s: media_device_register_entity failed\n",
847 vdev
->intf_devnode
= media_devnode_create(vdev
->v4l2_dev
->mdev
,
851 if (!vdev
->intf_devnode
) {
852 media_device_unregister_entity(&vdev
->entity
);
856 if (vdev
->entity
.function
!= MEDIA_ENT_F_UNKNOWN
) {
857 struct media_link
*link
;
859 link
= media_create_intf_link(&vdev
->entity
,
860 &vdev
->intf_devnode
->intf
,
861 MEDIA_LNK_FL_ENABLED
|
862 MEDIA_LNK_FL_IMMUTABLE
);
864 media_devnode_remove(vdev
->intf_devnode
);
865 media_device_unregister_entity(&vdev
->entity
);
870 /* FIXME: how to create the other interface links? */
876 int __video_register_device(struct video_device
*vdev
,
877 enum vfl_devnode_type type
,
878 int nr
, int warn_if_nr_in_use
,
879 struct module
*owner
)
883 int minor_offset
= 0;
884 int minor_cnt
= VIDEO_NUM_DEVICES
;
885 const char *name_base
;
887 /* A minor value of -1 marks this video device as never
888 having been registered */
891 /* the release callback MUST be present */
892 if (WARN_ON(!vdev
->release
))
894 /* the v4l2_dev pointer MUST be present */
895 if (WARN_ON(!vdev
->v4l2_dev
))
897 /* the device_caps field MUST be set for all but subdevs */
898 if (WARN_ON(type
!= VFL_TYPE_SUBDEV
&& !vdev
->device_caps
))
901 /* v4l2_fh support */
902 spin_lock_init(&vdev
->fh_lock
);
903 INIT_LIST_HEAD(&vdev
->fh_list
);
905 /* Part 1: check device type */
916 case VFL_TYPE_SUBDEV
:
917 name_base
= "v4l-subdev";
920 /* Use device name 'swradio' because 'sdr' was already taken. */
921 name_base
= "swradio";
924 name_base
= "v4l-touch";
927 pr_err("%s called with unknown type: %d\n",
932 vdev
->vfl_type
= type
;
934 if (vdev
->dev_parent
== NULL
)
935 vdev
->dev_parent
= vdev
->v4l2_dev
->dev
;
936 if (vdev
->ctrl_handler
== NULL
)
937 vdev
->ctrl_handler
= vdev
->v4l2_dev
->ctrl_handler
;
938 /* If the prio state pointer is NULL, then use the v4l2_device
940 if (vdev
->prio
== NULL
)
941 vdev
->prio
= &vdev
->v4l2_dev
->prio
;
943 /* Part 2: find a free minor, device node number and device index. */
944 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
945 /* Keep the ranges for the first four types for historical
947 * Newer devices (not yet in place) should use the range
948 * of 128-191 and just pick the first free minor there
970 /* Pick a device node number */
971 mutex_lock(&videodev_lock
);
972 nr
= devnode_find(vdev
, nr
== -1 ? 0 : nr
, minor_cnt
);
974 nr
= devnode_find(vdev
, 0, minor_cnt
);
975 if (nr
== minor_cnt
) {
976 pr_err("could not get a free device node number\n");
977 mutex_unlock(&videodev_lock
);
980 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
981 /* 1-on-1 mapping of device node number to minor number */
984 /* The device node number and minor numbers are independent, so
985 we just find the first free minor number. */
986 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++)
987 if (video_devices
[i
] == NULL
)
989 if (i
== VIDEO_NUM_DEVICES
) {
990 mutex_unlock(&videodev_lock
);
991 pr_err("could not get a free minor\n");
995 vdev
->minor
= i
+ minor_offset
;
998 /* Should not happen since we thought this minor was free */
999 if (WARN_ON(video_devices
[vdev
->minor
])) {
1000 mutex_unlock(&videodev_lock
);
1001 pr_err("video_device not empty!\n");
1005 vdev
->index
= get_index(vdev
);
1006 video_devices
[vdev
->minor
] = vdev
;
1007 mutex_unlock(&videodev_lock
);
1009 if (vdev
->ioctl_ops
)
1010 determine_valid_ioctls(vdev
);
1012 /* Part 3: Initialize the character device */
1013 vdev
->cdev
= cdev_alloc();
1014 if (vdev
->cdev
== NULL
) {
1018 vdev
->cdev
->ops
= &v4l2_fops
;
1019 vdev
->cdev
->owner
= owner
;
1020 ret
= cdev_add(vdev
->cdev
, MKDEV(VIDEO_MAJOR
, vdev
->minor
), 1);
1022 pr_err("%s: cdev_add failed\n", __func__
);
1028 /* Part 4: register the device with sysfs */
1029 vdev
->dev
.class = &video_class
;
1030 vdev
->dev
.devt
= MKDEV(VIDEO_MAJOR
, vdev
->minor
);
1031 vdev
->dev
.parent
= vdev
->dev_parent
;
1032 dev_set_name(&vdev
->dev
, "%s%d", name_base
, vdev
->num
);
1033 ret
= device_register(&vdev
->dev
);
1035 pr_err("%s: device_register failed\n", __func__
);
1038 /* Register the release callback that will be called when the last
1039 reference to the device goes away. */
1040 vdev
->dev
.release
= v4l2_device_release
;
1042 if (nr
!= -1 && nr
!= vdev
->num
&& warn_if_nr_in_use
)
1043 pr_warn("%s: requested %s%d, got %s\n", __func__
,
1044 name_base
, nr
, video_device_node_name(vdev
));
1046 /* Increase v4l2_device refcount */
1047 v4l2_device_get(vdev
->v4l2_dev
);
1049 /* Part 5: Register the entity. */
1050 ret
= video_register_media_controller(vdev
);
1052 /* Part 6: Activate this minor. The char device can now be used. */
1053 set_bit(V4L2_FL_REGISTERED
, &vdev
->flags
);
1058 mutex_lock(&videodev_lock
);
1060 cdev_del(vdev
->cdev
);
1061 video_devices
[vdev
->minor
] = NULL
;
1062 devnode_clear(vdev
);
1063 mutex_unlock(&videodev_lock
);
1064 /* Mark this video device as never having been registered. */
1068 EXPORT_SYMBOL(__video_register_device
);
1071 * video_unregister_device - unregister a video4linux device
1072 * @vdev: the device to unregister
1074 * This unregisters the passed device. Future open calls will
1075 * be met with errors.
1077 void video_unregister_device(struct video_device
*vdev
)
1079 /* Check if vdev was ever registered at all */
1080 if (!vdev
|| !video_is_registered(vdev
))
1083 mutex_lock(&videodev_lock
);
1084 /* This must be in a critical section to prevent a race with v4l2_open.
1085 * Once this bit has been cleared video_get may never be called again.
1087 clear_bit(V4L2_FL_REGISTERED
, &vdev
->flags
);
1088 mutex_unlock(&videodev_lock
);
1089 device_unregister(&vdev
->dev
);
1091 EXPORT_SYMBOL(video_unregister_device
);
1094 * Initialise video for linux
1096 static int __init
videodev_init(void)
1098 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
1101 pr_info("Linux video capture interface: v2.00\n");
1102 ret
= register_chrdev_region(dev
, VIDEO_NUM_DEVICES
, VIDEO_NAME
);
1104 pr_warn("videodev: unable to get major %d\n",
1109 ret
= class_register(&video_class
);
1111 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
1112 pr_warn("video_dev: class_register failed\n");
1119 static void __exit
videodev_exit(void)
1121 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
1123 class_unregister(&video_class
);
1124 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
1127 subsys_initcall(videodev_init
);
1128 module_exit(videodev_exit
)
1130 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@kernel.org>, Bill Dirks, Justin Schoeman, Gerd Knorr");
1131 MODULE_DESCRIPTION("Video4Linux2 core driver");
1132 MODULE_LICENSE("GPL");
1133 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR
);