diff options
Diffstat (limited to 'Documentation/video4linux/v4l2-framework.txt')
-rw-r--r-- | Documentation/video4linux/v4l2-framework.txt | 267 |
1 files changed, 229 insertions, 38 deletions
diff --git a/Documentation/video4linux/v4l2-framework.txt b/Documentation/video4linux/v4l2-framework.txt index f22f35c271f3..3b15608ee070 100644 --- a/Documentation/video4linux/v4l2-framework.txt +++ b/Documentation/video4linux/v4l2-framework.txt | |||
@@ -71,6 +71,10 @@ sub-device instances, the video_device struct stores V4L2 device node data | |||
71 | and in the future a v4l2_fh struct will keep track of filehandle instances | 71 | and in the future a v4l2_fh struct will keep track of filehandle instances |
72 | (this is not yet implemented). | 72 | (this is not yet implemented). |
73 | 73 | ||
74 | The V4L2 framework also optionally integrates with the media framework. If a | ||
75 | driver sets the struct v4l2_device mdev field, sub-devices and video nodes | ||
76 | will automatically appear in the media framework as entities. | ||
77 | |||
74 | 78 | ||
75 | struct v4l2_device | 79 | struct v4l2_device |
76 | ------------------ | 80 | ------------------ |
@@ -83,11 +87,20 @@ You must register the device instance: | |||
83 | 87 | ||
84 | v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev); | 88 | v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev); |
85 | 89 | ||
86 | Registration will initialize the v4l2_device struct and link dev->driver_data | 90 | Registration will initialize the v4l2_device struct. If the dev->driver_data |
87 | to v4l2_dev. If v4l2_dev->name is empty then it will be set to a value derived | 91 | field is NULL, it will be linked to v4l2_dev. |
88 | from dev (driver name followed by the bus_id, to be precise). If you set it | 92 | |
89 | up before calling v4l2_device_register then it will be untouched. If dev is | 93 | Drivers that want integration with the media device framework need to set |
90 | NULL, then you *must* setup v4l2_dev->name before calling v4l2_device_register. | 94 | dev->driver_data manually to point to the driver-specific device structure |
95 | that embed the struct v4l2_device instance. This is achieved by a | ||
96 | dev_set_drvdata() call before registering the V4L2 device instance. They must | ||
97 | also set the struct v4l2_device mdev field to point to a properly initialized | ||
98 | and registered media_device instance. | ||
99 | |||
100 | If v4l2_dev->name is empty then it will be set to a value derived from dev | ||
101 | (driver name followed by the bus_id, to be precise). If you set it up before | ||
102 | calling v4l2_device_register then it will be untouched. If dev is NULL, then | ||
103 | you *must* setup v4l2_dev->name before calling v4l2_device_register. | ||
91 | 104 | ||
92 | You can use v4l2_device_set_name() to set the name based on a driver name and | 105 | You can use v4l2_device_set_name() to set the name based on a driver name and |
93 | a driver-global atomic_t instance. This will generate names like ivtv0, ivtv1, | 106 | a driver-global atomic_t instance. This will generate names like ivtv0, ivtv1, |
@@ -108,6 +121,7 @@ You unregister with: | |||
108 | 121 | ||
109 | v4l2_device_unregister(struct v4l2_device *v4l2_dev); | 122 | v4l2_device_unregister(struct v4l2_device *v4l2_dev); |
110 | 123 | ||
124 | If the dev->driver_data field points to v4l2_dev, it will be reset to NULL. | ||
111 | Unregistering will also automatically unregister all subdevs from the device. | 125 | Unregistering will also automatically unregister all subdevs from the device. |
112 | 126 | ||
113 | If you have a hotpluggable device (e.g. a USB device), then when a disconnect | 127 | If you have a hotpluggable device (e.g. a USB device), then when a disconnect |
@@ -167,6 +181,21 @@ static int __devinit drv_probe(struct pci_dev *pdev, | |||
167 | state->instance = atomic_inc_return(&drv_instance) - 1; | 181 | state->instance = atomic_inc_return(&drv_instance) - 1; |
168 | } | 182 | } |
169 | 183 | ||
184 | If you have multiple device nodes then it can be difficult to know when it is | ||
185 | safe to unregister v4l2_device. For this purpose v4l2_device has refcounting | ||
186 | support. The refcount is increased whenever video_register_device is called and | ||
187 | it is decreased whenever that device node is released. When the refcount reaches | ||
188 | zero, then the v4l2_device release() callback is called. You can do your final | ||
189 | cleanup there. | ||
190 | |||
191 | If other device nodes (e.g. ALSA) are created, then you can increase and | ||
192 | decrease the refcount manually as well by calling: | ||
193 | |||
194 | void v4l2_device_get(struct v4l2_device *v4l2_dev); | ||
195 | |||
196 | or: | ||
197 | |||
198 | int v4l2_device_put(struct v4l2_device *v4l2_dev); | ||
170 | 199 | ||
171 | struct v4l2_subdev | 200 | struct v4l2_subdev |
172 | ------------------ | 201 | ------------------ |
@@ -254,6 +283,26 @@ A sub-device driver initializes the v4l2_subdev struct using: | |||
254 | Afterwards you need to initialize subdev->name with a unique name and set the | 283 | Afterwards you need to initialize subdev->name with a unique name and set the |
255 | module owner. This is done for you if you use the i2c helper functions. | 284 | module owner. This is done for you if you use the i2c helper functions. |
256 | 285 | ||
286 | If integration with the media framework is needed, you must initialize the | ||
287 | media_entity struct embedded in the v4l2_subdev struct (entity field) by | ||
288 | calling media_entity_init(): | ||
289 | |||
290 | struct media_pad *pads = &my_sd->pads; | ||
291 | int err; | ||
292 | |||
293 | err = media_entity_init(&sd->entity, npads, pads, 0); | ||
294 | |||
295 | The pads array must have been previously initialized. There is no need to | ||
296 | manually set the struct media_entity type and name fields, but the revision | ||
297 | field must be initialized if needed. | ||
298 | |||
299 | A reference to the entity will be automatically acquired/released when the | ||
300 | subdev device node (if any) is opened/closed. | ||
301 | |||
302 | Don't forget to cleanup the media entity before the sub-device is destroyed: | ||
303 | |||
304 | media_entity_cleanup(&sd->entity); | ||
305 | |||
257 | A device (bridge) driver needs to register the v4l2_subdev with the | 306 | A device (bridge) driver needs to register the v4l2_subdev with the |
258 | v4l2_device: | 307 | v4l2_device: |
259 | 308 | ||
@@ -263,6 +312,9 @@ This can fail if the subdev module disappeared before it could be registered. | |||
263 | After this function was called successfully the subdev->dev field points to | 312 | After this function was called successfully the subdev->dev field points to |
264 | the v4l2_device. | 313 | the v4l2_device. |
265 | 314 | ||
315 | If the v4l2_device parent device has a non-NULL mdev field, the sub-device | ||
316 | entity will be automatically registered with the media device. | ||
317 | |||
266 | You can unregister a sub-device using: | 318 | You can unregister a sub-device using: |
267 | 319 | ||
268 | v4l2_device_unregister_subdev(sd); | 320 | v4l2_device_unregister_subdev(sd); |
@@ -319,6 +371,61 @@ controlled through GPIO pins. This distinction is only relevant when setting | |||
319 | up the device, but once the subdev is registered it is completely transparent. | 371 | up the device, but once the subdev is registered it is completely transparent. |
320 | 372 | ||
321 | 373 | ||
374 | V4L2 sub-device userspace API | ||
375 | ----------------------------- | ||
376 | |||
377 | Beside exposing a kernel API through the v4l2_subdev_ops structure, V4L2 | ||
378 | sub-devices can also be controlled directly by userspace applications. | ||
379 | |||
380 | Device nodes named v4l-subdevX can be created in /dev to access sub-devices | ||
381 | directly. If a sub-device supports direct userspace configuration it must set | ||
382 | the V4L2_SUBDEV_FL_HAS_DEVNODE flag before being registered. | ||
383 | |||
384 | After registering sub-devices, the v4l2_device driver can create device nodes | ||
385 | for all registered sub-devices marked with V4L2_SUBDEV_FL_HAS_DEVNODE by calling | ||
386 | v4l2_device_register_subdev_nodes(). Those device nodes will be automatically | ||
387 | removed when sub-devices are unregistered. | ||
388 | |||
389 | The device node handles a subset of the V4L2 API. | ||
390 | |||
391 | VIDIOC_QUERYCTRL | ||
392 | VIDIOC_QUERYMENU | ||
393 | VIDIOC_G_CTRL | ||
394 | VIDIOC_S_CTRL | ||
395 | VIDIOC_G_EXT_CTRLS | ||
396 | VIDIOC_S_EXT_CTRLS | ||
397 | VIDIOC_TRY_EXT_CTRLS | ||
398 | |||
399 | The controls ioctls are identical to the ones defined in V4L2. They | ||
400 | behave identically, with the only exception that they deal only with | ||
401 | controls implemented in the sub-device. Depending on the driver, those | ||
402 | controls can be also be accessed through one (or several) V4L2 device | ||
403 | nodes. | ||
404 | |||
405 | VIDIOC_DQEVENT | ||
406 | VIDIOC_SUBSCRIBE_EVENT | ||
407 | VIDIOC_UNSUBSCRIBE_EVENT | ||
408 | |||
409 | The events ioctls are identical to the ones defined in V4L2. They | ||
410 | behave identically, with the only exception that they deal only with | ||
411 | events generated by the sub-device. Depending on the driver, those | ||
412 | events can also be reported by one (or several) V4L2 device nodes. | ||
413 | |||
414 | Sub-device drivers that want to use events need to set the | ||
415 | V4L2_SUBDEV_USES_EVENTS v4l2_subdev::flags and initialize | ||
416 | v4l2_subdev::nevents to events queue depth before registering the | ||
417 | sub-device. After registration events can be queued as usual on the | ||
418 | v4l2_subdev::devnode device node. | ||
419 | |||
420 | To properly support events, the poll() file operation is also | ||
421 | implemented. | ||
422 | |||
423 | Private ioctls | ||
424 | |||
425 | All ioctls not in the above list are passed directly to the sub-device | ||
426 | driver through the core::ioctl operation. | ||
427 | |||
428 | |||
322 | I2C sub-device drivers | 429 | I2C sub-device drivers |
323 | ---------------------- | 430 | ---------------------- |
324 | 431 | ||
@@ -457,6 +564,10 @@ You should also set these fields: | |||
457 | Otherwise you give it a pointer to a struct mutex_lock and before any | 564 | Otherwise you give it a pointer to a struct mutex_lock and before any |
458 | of the v4l2_file_operations is called this lock will be taken by the | 565 | of the v4l2_file_operations is called this lock will be taken by the |
459 | core and released afterwards. | 566 | core and released afterwards. |
567 | - prio: keeps track of the priorities. Used to implement VIDIOC_G/S_PRIORITY. | ||
568 | If left to NULL, then it will use the struct v4l2_prio_state in v4l2_device. | ||
569 | If you want to have a separate priority state per (group of) device node(s), | ||
570 | then you can point it to your own struct v4l2_prio_state. | ||
460 | - parent: you only set this if v4l2_device was registered with NULL as | 571 | - parent: you only set this if v4l2_device was registered with NULL as |
461 | the parent device struct. This only happens in cases where one hardware | 572 | the parent device struct. This only happens in cases where one hardware |
462 | device has multiple PCI devices that all share the same v4l2_device core. | 573 | device has multiple PCI devices that all share the same v4l2_device core. |
@@ -466,13 +577,34 @@ You should also set these fields: | |||
466 | (cx8802). Since the v4l2_device cannot be associated with a particular | 577 | (cx8802). Since the v4l2_device cannot be associated with a particular |
467 | PCI device it is setup without a parent device. But when the struct | 578 | PCI device it is setup without a parent device. But when the struct |
468 | video_device is setup you do know which parent PCI device to use. | 579 | video_device is setup you do know which parent PCI device to use. |
580 | - flags: optional. Set to V4L2_FL_USE_FH_PRIO if you want to let the framework | ||
581 | handle the VIDIOC_G/S_PRIORITY ioctls. This requires that you use struct | ||
582 | v4l2_fh. Eventually this flag will disappear once all drivers use the core | ||
583 | priority handling. But for now it has to be set explicitly. | ||
469 | 584 | ||
470 | If you use v4l2_ioctl_ops, then you should set either .unlocked_ioctl or | 585 | If you use v4l2_ioctl_ops, then you should set .unlocked_ioctl to video_ioctl2 |
471 | .ioctl to video_ioctl2 in your v4l2_file_operations struct. | 586 | in your v4l2_file_operations struct. |
587 | |||
588 | Do not use .ioctl! This is deprecated and will go away in the future. | ||
472 | 589 | ||
473 | The v4l2_file_operations struct is a subset of file_operations. The main | 590 | The v4l2_file_operations struct is a subset of file_operations. The main |
474 | difference is that the inode argument is omitted since it is never used. | 591 | difference is that the inode argument is omitted since it is never used. |
475 | 592 | ||
593 | If integration with the media framework is needed, you must initialize the | ||
594 | media_entity struct embedded in the video_device struct (entity field) by | ||
595 | calling media_entity_init(): | ||
596 | |||
597 | struct media_pad *pad = &my_vdev->pad; | ||
598 | int err; | ||
599 | |||
600 | err = media_entity_init(&vdev->entity, 1, pad, 0); | ||
601 | |||
602 | The pads array must have been previously initialized. There is no need to | ||
603 | manually set the struct media_entity type and name fields. | ||
604 | |||
605 | A reference to the entity will be automatically acquired/released when the | ||
606 | video device is opened/closed. | ||
607 | |||
476 | v4l2_file_operations and locking | 608 | v4l2_file_operations and locking |
477 | -------------------------------- | 609 | -------------------------------- |
478 | 610 | ||
@@ -502,6 +634,9 @@ for you. | |||
502 | return err; | 634 | return err; |
503 | } | 635 | } |
504 | 636 | ||
637 | If the v4l2_device parent device has a non-NULL mdev field, the video device | ||
638 | entity will be automatically registered with the media device. | ||
639 | |||
505 | Which device is registered depends on the type argument. The following | 640 | Which device is registered depends on the type argument. The following |
506 | types exist: | 641 | types exist: |
507 | 642 | ||
@@ -577,6 +712,13 @@ release, of course) will return an error as well. | |||
577 | When the last user of the video device node exits, then the vdev->release() | 712 | When the last user of the video device node exits, then the vdev->release() |
578 | callback is called and you can do the final cleanup there. | 713 | callback is called and you can do the final cleanup there. |
579 | 714 | ||
715 | Don't forget to cleanup the media entity associated with the video device if | ||
716 | it has been initialized: | ||
717 | |||
718 | media_entity_cleanup(&vdev->entity); | ||
719 | |||
720 | This can be done from the release callback. | ||
721 | |||
580 | 722 | ||
581 | video_device helper functions | 723 | video_device helper functions |
582 | ----------------------------- | 724 | ----------------------------- |
@@ -636,39 +778,25 @@ struct v4l2_fh | |||
636 | -------------- | 778 | -------------- |
637 | 779 | ||
638 | struct v4l2_fh provides a way to easily keep file handle specific data | 780 | struct v4l2_fh provides a way to easily keep file handle specific data |
639 | that is used by the V4L2 framework. Using v4l2_fh is optional for | 781 | that is used by the V4L2 framework. New drivers must use struct v4l2_fh |
640 | drivers. | 782 | since it is also used to implement priority handling (VIDIOC_G/S_PRIORITY) |
783 | if the video_device flag V4L2_FL_USE_FH_PRIO is also set. | ||
641 | 784 | ||
642 | The users of v4l2_fh (in the V4L2 framework, not the driver) know | 785 | The users of v4l2_fh (in the V4L2 framework, not the driver) know |
643 | whether a driver uses v4l2_fh as its file->private_data pointer by | 786 | whether a driver uses v4l2_fh as its file->private_data pointer by |
644 | testing the V4L2_FL_USES_V4L2_FH bit in video_device->flags. | 787 | testing the V4L2_FL_USES_V4L2_FH bit in video_device->flags. This bit is |
645 | 788 | set whenever v4l2_fh_init() is called. | |
646 | Useful functions: | ||
647 | |||
648 | - v4l2_fh_init() | ||
649 | |||
650 | Initialise the file handle. This *MUST* be performed in the driver's | ||
651 | v4l2_file_operations->open() handler. | ||
652 | |||
653 | - v4l2_fh_add() | ||
654 | 789 | ||
655 | Add a v4l2_fh to video_device file handle list. May be called after | 790 | struct v4l2_fh is allocated as a part of the driver's own file handle |
656 | initialising the file handle. | 791 | structure and file->private_data is set to it in the driver's open |
657 | 792 | function by the driver. | |
658 | - v4l2_fh_del() | ||
659 | |||
660 | Unassociate the file handle from video_device(). The file handle | ||
661 | exit function may now be called. | ||
662 | 793 | ||
663 | - v4l2_fh_exit() | 794 | In many cases the struct v4l2_fh will be embedded in a larger structure. |
795 | In that case you should call v4l2_fh_init+v4l2_fh_add in open() and | ||
796 | v4l2_fh_del+v4l2_fh_exit in release(). | ||
664 | 797 | ||
665 | Uninitialise the file handle. After uninitialisation the v4l2_fh | 798 | Drivers can extract their own file handle structure by using the container_of |
666 | memory can be freed. | 799 | macro. Example: |
667 | |||
668 | struct v4l2_fh is allocated as a part of the driver's own file handle | ||
669 | structure and is set to file->private_data in the driver's open | ||
670 | function by the driver. Drivers can extract their own file handle | ||
671 | structure by using the container_of macro. Example: | ||
672 | 800 | ||
673 | struct my_fh { | 801 | struct my_fh { |
674 | int blah; | 802 | int blah; |
@@ -685,15 +813,21 @@ int my_open(struct file *file) | |||
685 | 813 | ||
686 | ... | 814 | ... |
687 | 815 | ||
816 | my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL); | ||
817 | |||
818 | ... | ||
819 | |||
688 | ret = v4l2_fh_init(&my_fh->fh, vfd); | 820 | ret = v4l2_fh_init(&my_fh->fh, vfd); |
689 | if (ret) | 821 | if (ret) { |
822 | kfree(my_fh); | ||
690 | return ret; | 823 | return ret; |
824 | } | ||
691 | 825 | ||
692 | v4l2_fh_add(&my_fh->fh); | 826 | ... |
693 | 827 | ||
694 | file->private_data = &my_fh->fh; | 828 | file->private_data = &my_fh->fh; |
695 | 829 | v4l2_fh_add(&my_fh->fh); | |
696 | ... | 830 | return 0; |
697 | } | 831 | } |
698 | 832 | ||
699 | int my_release(struct file *file) | 833 | int my_release(struct file *file) |
@@ -702,8 +836,65 @@ int my_release(struct file *file) | |||
702 | struct my_fh *my_fh = container_of(fh, struct my_fh, fh); | 836 | struct my_fh *my_fh = container_of(fh, struct my_fh, fh); |
703 | 837 | ||
704 | ... | 838 | ... |
839 | v4l2_fh_del(&my_fh->fh); | ||
840 | v4l2_fh_exit(&my_fh->fh); | ||
841 | kfree(my_fh); | ||
842 | return 0; | ||
705 | } | 843 | } |
706 | 844 | ||
845 | Below is a short description of the v4l2_fh functions used: | ||
846 | |||
847 | int v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev) | ||
848 | |||
849 | Initialise the file handle. This *MUST* be performed in the driver's | ||
850 | v4l2_file_operations->open() handler. | ||
851 | |||
852 | void v4l2_fh_add(struct v4l2_fh *fh) | ||
853 | |||
854 | Add a v4l2_fh to video_device file handle list. Must be called once the | ||
855 | file handle is completely initialized. | ||
856 | |||
857 | void v4l2_fh_del(struct v4l2_fh *fh) | ||
858 | |||
859 | Unassociate the file handle from video_device(). The file handle | ||
860 | exit function may now be called. | ||
861 | |||
862 | void v4l2_fh_exit(struct v4l2_fh *fh) | ||
863 | |||
864 | Uninitialise the file handle. After uninitialisation the v4l2_fh | ||
865 | memory can be freed. | ||
866 | |||
867 | |||
868 | If struct v4l2_fh is not embedded, then you can use these helper functions: | ||
869 | |||
870 | int v4l2_fh_open(struct file *filp) | ||
871 | |||
872 | This allocates a struct v4l2_fh, initializes it and adds it to the struct | ||
873 | video_device associated with the file struct. | ||
874 | |||
875 | int v4l2_fh_release(struct file *filp) | ||
876 | |||
877 | This deletes it from the struct video_device associated with the file | ||
878 | struct, uninitialised the v4l2_fh and frees it. | ||
879 | |||
880 | These two functions can be plugged into the v4l2_file_operation's open() and | ||
881 | release() ops. | ||
882 | |||
883 | |||
884 | Several drivers need to do something when the first file handle is opened and | ||
885 | when the last file handle closes. Two helper functions were added to check | ||
886 | whether the v4l2_fh struct is the only open filehandle of the associated | ||
887 | device node: | ||
888 | |||
889 | int v4l2_fh_is_singular(struct v4l2_fh *fh) | ||
890 | |||
891 | Returns 1 if the file handle is the only open file handle, else 0. | ||
892 | |||
893 | int v4l2_fh_is_singular_file(struct file *filp) | ||
894 | |||
895 | Same, but it calls v4l2_fh_is_singular with filp->private_data. | ||
896 | |||
897 | |||
707 | V4L2 events | 898 | V4L2 events |
708 | ----------- | 899 | ----------- |
709 | 900 | ||