1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/string.h>
26#include <linux/errno.h>
27#include <linux/init.h>
28#include <linux/kmod.h>
29#include <linux/slab.h>
30#include <linux/uaccess.h>
31
32#include <media/v4l2-common.h>
33#include <media/v4l2-device.h>
34#include <media/v4l2-ioctl.h>
35
36#define VIDEO_NUM_DEVICES 256
37#define VIDEO_NAME "video4linux"
38
39#define dprintk(fmt, arg...) do { \
40 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
41 __func__, ##arg); \
42} while (0)
43
44
45
46
47
48
49static ssize_t index_show(struct device *cd,
50 struct device_attribute *attr, char *buf)
51{
52 struct video_device *vdev = to_video_device(cd);
53
54 return sprintf(buf, "%i\n", vdev->index);
55}
56static DEVICE_ATTR_RO(index);
57
58static ssize_t dev_debug_show(struct device *cd,
59 struct device_attribute *attr, char *buf)
60{
61 struct video_device *vdev = to_video_device(cd);
62
63 return sprintf(buf, "%i\n", vdev->dev_debug);
64}
65
66static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
67 const char *buf, size_t len)
68{
69 struct video_device *vdev = to_video_device(cd);
70 int res = 0;
71 u16 value;
72
73 res = kstrtou16(buf, 0, &value);
74 if (res)
75 return res;
76
77 vdev->dev_debug = value;
78 return len;
79}
80static DEVICE_ATTR_RW(dev_debug);
81
82static ssize_t name_show(struct device *cd,
83 struct device_attribute *attr, char *buf)
84{
85 struct video_device *vdev = to_video_device(cd);
86
87 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
88}
89static DEVICE_ATTR_RO(name);
90
91static struct attribute *video_device_attrs[] = {
92 &dev_attr_name.attr,
93 &dev_attr_dev_debug.attr,
94 &dev_attr_index.attr,
95 NULL,
96};
97ATTRIBUTE_GROUPS(video_device);
98
99
100
101
102static struct video_device *video_devices[VIDEO_NUM_DEVICES];
103static DEFINE_MUTEX(videodev_lock);
104static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
105
106
107
108
109
110
111#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
112
113static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
114{
115
116
117
118 int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
119
120 return devnode_nums[idx];
121}
122#else
123
124static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
125{
126 return devnode_nums[vfl_type];
127}
128#endif
129
130
131static inline void devnode_set(struct video_device *vdev)
132{
133 set_bit(vdev->num, devnode_bits(vdev->vfl_type));
134}
135
136
137static inline void devnode_clear(struct video_device *vdev)
138{
139 clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
140}
141
142
143static inline int devnode_find(struct video_device *vdev, int from, int to)
144{
145 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
146}
147
148struct video_device *video_device_alloc(void)
149{
150 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
151}
152EXPORT_SYMBOL(video_device_alloc);
153
154void video_device_release(struct video_device *vdev)
155{
156 kfree(vdev);
157}
158EXPORT_SYMBOL(video_device_release);
159
160void video_device_release_empty(struct video_device *vdev)
161{
162
163
164}
165EXPORT_SYMBOL(video_device_release_empty);
166
167static inline void video_get(struct video_device *vdev)
168{
169 get_device(&vdev->dev);
170}
171
172static inline void video_put(struct video_device *vdev)
173{
174 put_device(&vdev->dev);
175}
176
177
178static void v4l2_device_release(struct device *cd)
179{
180 struct video_device *vdev = to_video_device(cd);
181 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
182
183 mutex_lock(&videodev_lock);
184 if (WARN_ON(video_devices[vdev->minor] != vdev)) {
185
186 mutex_unlock(&videodev_lock);
187 return;
188 }
189
190
191 video_devices[vdev->minor] = NULL;
192
193
194 cdev_del(vdev->cdev);
195
196
197 vdev->cdev = NULL;
198
199
200 devnode_clear(vdev);
201
202 mutex_unlock(&videodev_lock);
203
204#if defined(CONFIG_MEDIA_CONTROLLER)
205 if (v4l2_dev->mdev) {
206
207 media_devnode_remove(vdev->intf_devnode);
208 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
209 media_device_unregister_entity(&vdev->entity);
210 }
211#endif
212
213
214
215
216
217
218
219
220
221 if (v4l2_dev->release == NULL)
222 v4l2_dev = NULL;
223
224
225
226 vdev->release(vdev);
227
228
229 if (v4l2_dev)
230 v4l2_device_put(v4l2_dev);
231}
232
233static struct class video_class = {
234 .name = VIDEO_NAME,
235 .dev_groups = video_device_groups,
236};
237
238struct video_device *video_devdata(struct file *file)
239{
240 return video_devices[iminor(file_inode(file))];
241}
242EXPORT_SYMBOL(video_devdata);
243
244
245
246
247static inline bool prio_is_valid(enum v4l2_priority prio)
248{
249 return prio == V4L2_PRIORITY_BACKGROUND ||
250 prio == V4L2_PRIORITY_INTERACTIVE ||
251 prio == V4L2_PRIORITY_RECORD;
252}
253
254void v4l2_prio_init(struct v4l2_prio_state *global)
255{
256 memset(global, 0, sizeof(*global));
257}
258EXPORT_SYMBOL(v4l2_prio_init);
259
260int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
261 enum v4l2_priority new)
262{
263 if (!prio_is_valid(new))
264 return -EINVAL;
265 if (*local == new)
266 return 0;
267
268 atomic_inc(&global->prios[new]);
269 if (prio_is_valid(*local))
270 atomic_dec(&global->prios[*local]);
271 *local = new;
272 return 0;
273}
274EXPORT_SYMBOL(v4l2_prio_change);
275
276void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
277{
278 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
279}
280EXPORT_SYMBOL(v4l2_prio_open);
281
282void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
283{
284 if (prio_is_valid(local))
285 atomic_dec(&global->prios[local]);
286}
287EXPORT_SYMBOL(v4l2_prio_close);
288
289enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
290{
291 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
292 return V4L2_PRIORITY_RECORD;
293 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
294 return V4L2_PRIORITY_INTERACTIVE;
295 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
296 return V4L2_PRIORITY_BACKGROUND;
297 return V4L2_PRIORITY_UNSET;
298}
299EXPORT_SYMBOL(v4l2_prio_max);
300
301int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
302{
303 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
304}
305EXPORT_SYMBOL(v4l2_prio_check);
306
307
308static ssize_t v4l2_read(struct file *filp, char __user *buf,
309 size_t sz, loff_t *off)
310{
311 struct video_device *vdev = video_devdata(filp);
312 int ret = -ENODEV;
313
314 if (!vdev->fops->read)
315 return -EINVAL;
316 if (video_is_registered(vdev))
317 ret = vdev->fops->read(filp, buf, sz, off);
318 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
319 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
320 dprintk("%s: read: %zd (%d)\n",
321 video_device_node_name(vdev), sz, ret);
322 return ret;
323}
324
325static ssize_t v4l2_write(struct file *filp, const char __user *buf,
326 size_t sz, loff_t *off)
327{
328 struct video_device *vdev = video_devdata(filp);
329 int ret = -ENODEV;
330
331 if (!vdev->fops->write)
332 return -EINVAL;
333 if (video_is_registered(vdev))
334 ret = vdev->fops->write(filp, buf, sz, off);
335 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
336 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
337 dprintk("%s: write: %zd (%d)\n",
338 video_device_node_name(vdev), sz, ret);
339 return ret;
340}
341
342static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll)
343{
344 struct video_device *vdev = video_devdata(filp);
345 __poll_t res = EPOLLERR | EPOLLHUP;
346
347 if (!vdev->fops->poll)
348 return DEFAULT_POLLMASK;
349 if (video_is_registered(vdev))
350 res = vdev->fops->poll(filp, poll);
351 if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
352 dprintk("%s: poll: %08x\n",
353 video_device_node_name(vdev), res);
354 return res;
355}
356
357static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
358{
359 struct video_device *vdev = video_devdata(filp);
360 int ret = -ENODEV;
361
362 if (vdev->fops->unlocked_ioctl) {
363 if (video_is_registered(vdev))
364 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
365 } else
366 ret = -ENOTTY;
367
368 return ret;
369}
370
371#ifdef CONFIG_MMU
372#define v4l2_get_unmapped_area NULL
373#else
374static unsigned long v4l2_get_unmapped_area(struct file *filp,
375 unsigned long addr, unsigned long len, unsigned long pgoff,
376 unsigned long flags)
377{
378 struct video_device *vdev = video_devdata(filp);
379 int ret;
380
381 if (!vdev->fops->get_unmapped_area)
382 return -ENOSYS;
383 if (!video_is_registered(vdev))
384 return -ENODEV;
385 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
386 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
387 dprintk("%s: get_unmapped_area (%d)\n",
388 video_device_node_name(vdev), ret);
389 return ret;
390}
391#endif
392
393static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
394{
395 struct video_device *vdev = video_devdata(filp);
396 int ret = -ENODEV;
397
398 if (!vdev->fops->mmap)
399 return -ENODEV;
400 if (video_is_registered(vdev))
401 ret = vdev->fops->mmap(filp, vm);
402 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
403 dprintk("%s: mmap (%d)\n",
404 video_device_node_name(vdev), ret);
405 return ret;
406}
407
408
409static int v4l2_open(struct inode *inode, struct file *filp)
410{
411 struct video_device *vdev;
412 int ret = 0;
413
414
415 mutex_lock(&videodev_lock);
416 vdev = video_devdata(filp);
417
418 if (vdev == NULL || !video_is_registered(vdev)) {
419 mutex_unlock(&videodev_lock);
420 return -ENODEV;
421 }
422
423 video_get(vdev);
424 mutex_unlock(&videodev_lock);
425 if (vdev->fops->open) {
426 if (video_is_registered(vdev))
427 ret = vdev->fops->open(filp);
428 else
429 ret = -ENODEV;
430 }
431
432 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
433 dprintk("%s: open (%d)\n",
434 video_device_node_name(vdev), ret);
435
436 if (ret)
437 video_put(vdev);
438 return ret;
439}
440
441
442static int v4l2_release(struct inode *inode, struct file *filp)
443{
444 struct video_device *vdev = video_devdata(filp);
445 int ret = 0;
446
447 if (vdev->fops->release)
448 ret = vdev->fops->release(filp);
449 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
450 dprintk("%s: release\n",
451 video_device_node_name(vdev));
452
453
454
455 video_put(vdev);
456 return ret;
457}
458
459static const struct file_operations v4l2_fops = {
460 .owner = THIS_MODULE,
461 .read = v4l2_read,
462 .write = v4l2_write,
463 .open = v4l2_open,
464 .get_unmapped_area = v4l2_get_unmapped_area,
465 .mmap = v4l2_mmap,
466 .unlocked_ioctl = v4l2_ioctl,
467#ifdef CONFIG_COMPAT
468 .compat_ioctl = v4l2_compat_ioctl32,
469#endif
470 .release = v4l2_release,
471 .poll = v4l2_poll,
472 .llseek = no_llseek,
473};
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488static int get_index(struct video_device *vdev)
489{
490
491
492 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
493 int i;
494
495 bitmap_zero(used, VIDEO_NUM_DEVICES);
496
497 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
498 if (video_devices[i] != NULL &&
499 video_devices[i]->v4l2_dev == vdev->v4l2_dev) {
500 set_bit(video_devices[i]->index, used);
501 }
502 }
503
504 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
505}
506
507#define SET_VALID_IOCTL(ops, cmd, op) \
508 if (ops->op) \
509 set_bit(_IOC_NR(cmd), valid_ioctls)
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524static void determine_valid_ioctls(struct video_device *vdev)
525{
526 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
527 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
528 bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
529 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
530 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
531 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
532 bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
533 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
534 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
535
536 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
537
538
539
540 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
541 set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
542 set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
543
544
545
546
547
548 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
549 set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
550 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
551 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
552 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
553 set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
554 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
555 set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
556 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
557 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
558 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
559 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
560 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
561 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
562 if (vdev->ctrl_handler || ops->vidioc_querymenu)
563 set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
564 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
565 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
566 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
567#ifdef CONFIG_VIDEO_ADV_DEBUG
568 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
569 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
570 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
571#endif
572
573 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
574 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
575 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
576 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
577 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
578
579 if (is_vid || is_tch) {
580
581 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
582 ops->vidioc_enum_fmt_vid_cap_mplane ||
583 ops->vidioc_enum_fmt_vid_overlay ||
584 ops->vidioc_enum_fmt_meta_cap)) ||
585 (is_tx && (ops->vidioc_enum_fmt_vid_out ||
586 ops->vidioc_enum_fmt_vid_out_mplane ||
587 ops->vidioc_enum_fmt_meta_out)))
588 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
589 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
590 ops->vidioc_g_fmt_vid_cap_mplane ||
591 ops->vidioc_g_fmt_vid_overlay ||
592 ops->vidioc_g_fmt_meta_cap)) ||
593 (is_tx && (ops->vidioc_g_fmt_vid_out ||
594 ops->vidioc_g_fmt_vid_out_mplane ||
595 ops->vidioc_g_fmt_vid_out_overlay ||
596 ops->vidioc_g_fmt_meta_out)))
597 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
598 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
599 ops->vidioc_s_fmt_vid_cap_mplane ||
600 ops->vidioc_s_fmt_vid_overlay ||
601 ops->vidioc_s_fmt_meta_cap)) ||
602 (is_tx && (ops->vidioc_s_fmt_vid_out ||
603 ops->vidioc_s_fmt_vid_out_mplane ||
604 ops->vidioc_s_fmt_vid_out_overlay ||
605 ops->vidioc_s_fmt_meta_out)))
606 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
607 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
608 ops->vidioc_try_fmt_vid_cap_mplane ||
609 ops->vidioc_try_fmt_vid_overlay ||
610 ops->vidioc_try_fmt_meta_cap)) ||
611 (is_tx && (ops->vidioc_try_fmt_vid_out ||
612 ops->vidioc_try_fmt_vid_out_mplane ||
613 ops->vidioc_try_fmt_vid_out_overlay ||
614 ops->vidioc_try_fmt_meta_out)))
615 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
616 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
617 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
618 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
619 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
620 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
621 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
622 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
623 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
624 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
625 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
626 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
627 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
628 if (ops->vidioc_g_crop || ops->vidioc_g_selection)
629 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
630 if (ops->vidioc_s_crop || ops->vidioc_s_selection)
631 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
632 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
633 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
634 if (ops->vidioc_cropcap || ops->vidioc_g_selection)
635 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
636 } else if (is_vbi) {
637
638 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
639 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
640 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
641 ops->vidioc_g_fmt_sliced_vbi_out)))
642 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
643 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
644 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
645 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
646 ops->vidioc_s_fmt_sliced_vbi_out)))
647 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
648 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
649 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
650 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
651 ops->vidioc_try_fmt_sliced_vbi_out)))
652 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
653 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
654 } else if (is_sdr && is_rx) {
655
656 if (ops->vidioc_enum_fmt_sdr_cap)
657 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
658 if (ops->vidioc_g_fmt_sdr_cap)
659 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
660 if (ops->vidioc_s_fmt_sdr_cap)
661 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
662 if (ops->vidioc_try_fmt_sdr_cap)
663 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
664 } else if (is_sdr && is_tx) {
665
666 if (ops->vidioc_enum_fmt_sdr_out)
667 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
668 if (ops->vidioc_g_fmt_sdr_out)
669 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
670 if (ops->vidioc_s_fmt_sdr_out)
671 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
672 if (ops->vidioc_try_fmt_sdr_out)
673 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
674 }
675
676 if (is_vid || is_vbi || is_sdr || is_tch) {
677
678 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
679 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
680 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
681 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
682 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
683 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
684 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
685 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
686 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
687 }
688
689 if (is_vid || is_vbi || is_tch) {
690
691 if (ops->vidioc_s_std)
692 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
693 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
694 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
695 if (is_rx) {
696 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
697 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
698 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
699 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
700 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
701 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
702 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
703 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
704 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
705 }
706 if (is_tx) {
707 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
708 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
709 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
710 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
711 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
712 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
713 }
714 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
715 ops->vidioc_g_std))
716 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
717 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
718 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
719 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
720 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
721 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
722 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
723 }
724 if (is_tx && (is_radio || is_sdr)) {
725
726 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
727 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
728 }
729 if (is_rx) {
730
731 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
732 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
733 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
734 }
735
736 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
737 BASE_VIDIOC_PRIVATE);
738}
739
740static int video_register_media_controller(struct video_device *vdev, int type)
741{
742#if defined(CONFIG_MEDIA_CONTROLLER)
743 u32 intf_type;
744 int ret;
745
746 if (!vdev->v4l2_dev->mdev)
747 return 0;
748
749 vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
750 vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
751
752 switch (type) {
753 case VFL_TYPE_GRABBER:
754 intf_type = MEDIA_INTF_T_V4L_VIDEO;
755 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
756 break;
757 case VFL_TYPE_VBI:
758 intf_type = MEDIA_INTF_T_V4L_VBI;
759 vdev->entity.function = MEDIA_ENT_F_IO_VBI;
760 break;
761 case VFL_TYPE_SDR:
762 intf_type = MEDIA_INTF_T_V4L_SWRADIO;
763 vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
764 break;
765 case VFL_TYPE_TOUCH:
766 intf_type = MEDIA_INTF_T_V4L_TOUCH;
767 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
768 break;
769 case VFL_TYPE_RADIO:
770 intf_type = MEDIA_INTF_T_V4L_RADIO;
771
772
773
774
775
776 break;
777 case VFL_TYPE_SUBDEV:
778 intf_type = MEDIA_INTF_T_V4L_SUBDEV;
779
780 break;
781 default:
782 return 0;
783 }
784
785 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
786 vdev->entity.name = vdev->name;
787
788
789 vdev->entity.info.dev.major = VIDEO_MAJOR;
790 vdev->entity.info.dev.minor = vdev->minor;
791
792 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
793 &vdev->entity);
794 if (ret < 0) {
795 pr_warn("%s: media_device_register_entity failed\n",
796 __func__);
797 return ret;
798 }
799 }
800
801 vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
802 intf_type,
803 0, VIDEO_MAJOR,
804 vdev->minor);
805 if (!vdev->intf_devnode) {
806 media_device_unregister_entity(&vdev->entity);
807 return -ENOMEM;
808 }
809
810 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
811 struct media_link *link;
812
813 link = media_create_intf_link(&vdev->entity,
814 &vdev->intf_devnode->intf,
815 MEDIA_LNK_FL_ENABLED);
816 if (!link) {
817 media_devnode_remove(vdev->intf_devnode);
818 media_device_unregister_entity(&vdev->entity);
819 return -ENOMEM;
820 }
821 }
822
823
824
825#endif
826 return 0;
827}
828
829int __video_register_device(struct video_device *vdev,
830 enum vfl_devnode_type type,
831 int nr, int warn_if_nr_in_use,
832 struct module *owner)
833{
834 int i = 0;
835 int ret;
836 int minor_offset = 0;
837 int minor_cnt = VIDEO_NUM_DEVICES;
838 const char *name_base;
839
840
841
842 vdev->minor = -1;
843
844
845 if (WARN_ON(!vdev->release))
846 return -EINVAL;
847
848 if (WARN_ON(!vdev->v4l2_dev))
849 return -EINVAL;
850
851
852 spin_lock_init(&vdev->fh_lock);
853 INIT_LIST_HEAD(&vdev->fh_list);
854
855
856 switch (type) {
857 case VFL_TYPE_GRABBER:
858 name_base = "video";
859 break;
860 case VFL_TYPE_VBI:
861 name_base = "vbi";
862 break;
863 case VFL_TYPE_RADIO:
864 name_base = "radio";
865 break;
866 case VFL_TYPE_SUBDEV:
867 name_base = "v4l-subdev";
868 break;
869 case VFL_TYPE_SDR:
870
871 name_base = "swradio";
872 break;
873 case VFL_TYPE_TOUCH:
874 name_base = "v4l-touch";
875 break;
876 default:
877 pr_err("%s called with unknown type: %d\n",
878 __func__, type);
879 return -EINVAL;
880 }
881
882 vdev->vfl_type = type;
883 vdev->cdev = NULL;
884 if (vdev->dev_parent == NULL)
885 vdev->dev_parent = vdev->v4l2_dev->dev;
886 if (vdev->ctrl_handler == NULL)
887 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
888
889
890 if (vdev->prio == NULL)
891 vdev->prio = &vdev->v4l2_dev->prio;
892
893
894#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
895
896
897
898
899
900 switch (type) {
901 case VFL_TYPE_GRABBER:
902 minor_offset = 0;
903 minor_cnt = 64;
904 break;
905 case VFL_TYPE_RADIO:
906 minor_offset = 64;
907 minor_cnt = 64;
908 break;
909 case VFL_TYPE_VBI:
910 minor_offset = 224;
911 minor_cnt = 32;
912 break;
913 default:
914 minor_offset = 128;
915 minor_cnt = 64;
916 break;
917 }
918#endif
919
920
921 mutex_lock(&videodev_lock);
922 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
923 if (nr == minor_cnt)
924 nr = devnode_find(vdev, 0, minor_cnt);
925 if (nr == minor_cnt) {
926 pr_err("could not get a free device node number\n");
927 mutex_unlock(&videodev_lock);
928 return -ENFILE;
929 }
930#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
931
932 i = nr;
933#else
934
935
936 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
937 if (video_devices[i] == NULL)
938 break;
939 if (i == VIDEO_NUM_DEVICES) {
940 mutex_unlock(&videodev_lock);
941 pr_err("could not get a free minor\n");
942 return -ENFILE;
943 }
944#endif
945 vdev->minor = i + minor_offset;
946 vdev->num = nr;
947
948
949 if (WARN_ON(video_devices[vdev->minor])) {
950 mutex_unlock(&videodev_lock);
951 pr_err("video_device not empty!\n");
952 return -ENFILE;
953 }
954 devnode_set(vdev);
955 vdev->index = get_index(vdev);
956 video_devices[vdev->minor] = vdev;
957 mutex_unlock(&videodev_lock);
958
959 if (vdev->ioctl_ops)
960 determine_valid_ioctls(vdev);
961
962
963 vdev->cdev = cdev_alloc();
964 if (vdev->cdev == NULL) {
965 ret = -ENOMEM;
966 goto cleanup;
967 }
968 vdev->cdev->ops = &v4l2_fops;
969 vdev->cdev->owner = owner;
970 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
971 if (ret < 0) {
972 pr_err("%s: cdev_add failed\n", __func__);
973 kfree(vdev->cdev);
974 vdev->cdev = NULL;
975 goto cleanup;
976 }
977
978
979 vdev->dev.class = &video_class;
980 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
981 vdev->dev.parent = vdev->dev_parent;
982 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
983 ret = device_register(&vdev->dev);
984 if (ret < 0) {
985 pr_err("%s: device_register failed\n", __func__);
986 goto cleanup;
987 }
988
989
990 vdev->dev.release = v4l2_device_release;
991
992 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
993 pr_warn("%s: requested %s%d, got %s\n", __func__,
994 name_base, nr, video_device_node_name(vdev));
995
996
997 v4l2_device_get(vdev->v4l2_dev);
998
999
1000 ret = video_register_media_controller(vdev, type);
1001
1002
1003 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
1004
1005 return 0;
1006
1007cleanup:
1008 mutex_lock(&videodev_lock);
1009 if (vdev->cdev)
1010 cdev_del(vdev->cdev);
1011 video_devices[vdev->minor] = NULL;
1012 devnode_clear(vdev);
1013 mutex_unlock(&videodev_lock);
1014
1015 vdev->minor = -1;
1016 return ret;
1017}
1018EXPORT_SYMBOL(__video_register_device);
1019
1020
1021
1022
1023
1024
1025
1026
1027void video_unregister_device(struct video_device *vdev)
1028{
1029
1030 if (!vdev || !video_is_registered(vdev))
1031 return;
1032
1033 mutex_lock(&videodev_lock);
1034
1035
1036
1037 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
1038 mutex_unlock(&videodev_lock);
1039 device_unregister(&vdev->dev);
1040}
1041EXPORT_SYMBOL(video_unregister_device);
1042
1043
1044
1045
1046static int __init videodev_init(void)
1047{
1048 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1049 int ret;
1050
1051 pr_info("Linux video capture interface: v2.00\n");
1052 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1053 if (ret < 0) {
1054 pr_warn("videodev: unable to get major %d\n",
1055 VIDEO_MAJOR);
1056 return ret;
1057 }
1058
1059 ret = class_register(&video_class);
1060 if (ret < 0) {
1061 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1062 pr_warn("video_dev: class_register failed\n");
1063 return -EIO;
1064 }
1065
1066 return 0;
1067}
1068
1069static void __exit videodev_exit(void)
1070{
1071 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1072
1073 class_unregister(&video_class);
1074 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1075}
1076
1077subsys_initcall(videodev_init);
1078module_exit(videodev_exit)
1079
1080MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@kernel.org>");
1081MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1082MODULE_LICENSE("GPL");
1083MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
1084