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