diff options
| author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2009-12-09 06:40:10 -0500 |
|---|---|---|
| committer | Mauro Carvalho Chehab <mchehab@redhat.com> | 2011-03-22 03:53:19 -0400 |
| commit | 2c0ab67be1b4197a4effac89bb0604832e38be8d (patch) | |
| tree | 033b1d2c6ae25e05f1f46f3c2e7bdb504c359ad3 | |
| parent | 95db3a60e0652a52df145aacade1a88c5acef659 (diff) | |
[media] v4l: Make video_device inherit from media_entity
V4L2 devices are media entities. As such they need to inherit from
(include) the media_entity structure.
When registering/unregistering the device, the media entity is
automatically registered/unregistered. The entity is acquired on device
open and released on device close.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Sakari Ailus <sakari.ailus@iki.fi>
Acked-by: Hans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
| -rw-r--r-- | Documentation/video4linux/v4l2-framework.txt | 38 | ||||
| -rw-r--r-- | drivers/media/video/v4l2-dev.c | 49 | ||||
| -rw-r--r-- | include/media/v4l2-dev.h | 7 |
3 files changed, 87 insertions, 7 deletions
diff --git a/Documentation/video4linux/v4l2-framework.txt b/Documentation/video4linux/v4l2-framework.txt index 7de55cfae04e..062708169def 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 | ------------------ |
| @@ -84,11 +88,14 @@ You must register the device instance: | |||
| 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. If the dev->driver_data | 90 | Registration will initialize the v4l2_device struct. If the dev->driver_data |
| 87 | field is NULL, it will be linked to v4l2_dev. Drivers that use the media | 91 | field is NULL, it will be linked to v4l2_dev. |
| 88 | device framework in addition to the V4L2 framework need to set | 92 | |
| 93 | Drivers that want integration with the media device framework need to set | ||
| 89 | dev->driver_data manually to point to the driver-specific device structure | 94 | dev->driver_data manually to point to the driver-specific device structure |
| 90 | that embed the struct v4l2_device instance. This is achieved by a | 95 | that embed the struct v4l2_device instance. This is achieved by a |
| 91 | dev_set_drvdata() call before registering the V4L2 device instance. | 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. | ||
| 92 | 99 | ||
| 93 | If v4l2_dev->name is empty then it will be set to a value derived from dev | 100 | If v4l2_dev->name is empty then it will be set to a value derived from dev |
| 94 | (driver name followed by the bus_id, to be precise). If you set it up before | 101 | (driver name followed by the bus_id, to be precise). If you set it up before |
| @@ -530,6 +537,21 @@ If you use v4l2_ioctl_ops, then you should set either .unlocked_ioctl or | |||
| 530 | The v4l2_file_operations struct is a subset of file_operations. The main | 537 | The v4l2_file_operations struct is a subset of file_operations. The main |
| 531 | difference is that the inode argument is omitted since it is never used. | 538 | difference is that the inode argument is omitted since it is never used. |
| 532 | 539 | ||
| 540 | If integration with the media framework is needed, you must initialize the | ||
| 541 | media_entity struct embedded in the video_device struct (entity field) by | ||
| 542 | calling media_entity_init(): | ||
| 543 | |||
| 544 | struct media_pad *pad = &my_vdev->pad; | ||
| 545 | int err; | ||
| 546 | |||
| 547 | err = media_entity_init(&vdev->entity, 1, pad, 0); | ||
| 548 | |||
| 549 | The pads array must have been previously initialized. There is no need to | ||
| 550 | manually set the struct media_entity type and name fields. | ||
| 551 | |||
| 552 | A reference to the entity will be automatically acquired/released when the | ||
| 553 | video device is opened/closed. | ||
| 554 | |||
| 533 | v4l2_file_operations and locking | 555 | v4l2_file_operations and locking |
| 534 | -------------------------------- | 556 | -------------------------------- |
| 535 | 557 | ||
| @@ -559,6 +581,9 @@ for you. | |||
| 559 | return err; | 581 | return err; |
| 560 | } | 582 | } |
| 561 | 583 | ||
| 584 | If the v4l2_device parent device has a non-NULL mdev field, the video device | ||
| 585 | entity will be automatically registered with the media device. | ||
| 586 | |||
| 562 | Which device is registered depends on the type argument. The following | 587 | Which device is registered depends on the type argument. The following |
| 563 | types exist: | 588 | types exist: |
| 564 | 589 | ||
| @@ -634,6 +659,13 @@ release, of course) will return an error as well. | |||
| 634 | When the last user of the video device node exits, then the vdev->release() | 659 | When the last user of the video device node exits, then the vdev->release() |
| 635 | callback is called and you can do the final cleanup there. | 660 | callback is called and you can do the final cleanup there. |
| 636 | 661 | ||
| 662 | Don't forget to cleanup the media entity associated with the video device if | ||
| 663 | it has been initialized: | ||
| 664 | |||
| 665 | media_entity_cleanup(&vdev->entity); | ||
| 666 | |||
| 667 | This can be done from the release callback. | ||
| 668 | |||
| 637 | 669 | ||
| 638 | video_device helper functions | 670 | video_device helper functions |
| 639 | ----------------------------- | 671 | ----------------------------- |
diff --git a/drivers/media/video/v4l2-dev.c b/drivers/media/video/v4l2-dev.c index abe04ef38066..e405b8094b94 100644 --- a/drivers/media/video/v4l2-dev.c +++ b/drivers/media/video/v4l2-dev.c | |||
| @@ -303,6 +303,9 @@ static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm) | |||
| 303 | static int v4l2_open(struct inode *inode, struct file *filp) | 303 | static int v4l2_open(struct inode *inode, struct file *filp) |
| 304 | { | 304 | { |
| 305 | struct video_device *vdev; | 305 | struct video_device *vdev; |
| 306 | #if defined(CONFIG_MEDIA_CONTROLLER) | ||
| 307 | struct media_entity *entity = NULL; | ||
| 308 | #endif | ||
| 306 | int ret = 0; | 309 | int ret = 0; |
| 307 | 310 | ||
| 308 | /* Check if the video device is available */ | 311 | /* Check if the video device is available */ |
| @@ -316,6 +319,16 @@ static int v4l2_open(struct inode *inode, struct file *filp) | |||
| 316 | /* and increase the device refcount */ | 319 | /* and increase the device refcount */ |
| 317 | video_get(vdev); | 320 | video_get(vdev); |
| 318 | mutex_unlock(&videodev_lock); | 321 | mutex_unlock(&videodev_lock); |
| 322 | #if defined(CONFIG_MEDIA_CONTROLLER) | ||
| 323 | if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) { | ||
| 324 | entity = media_entity_get(&vdev->entity); | ||
| 325 | if (!entity) { | ||
| 326 | ret = -EBUSY; | ||
| 327 | video_put(vdev); | ||
| 328 | return ret; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | #endif | ||
| 319 | if (vdev->fops->open) { | 332 | if (vdev->fops->open) { |
| 320 | if (vdev->lock && mutex_lock_interruptible(vdev->lock)) { | 333 | if (vdev->lock && mutex_lock_interruptible(vdev->lock)) { |
| 321 | ret = -ERESTARTSYS; | 334 | ret = -ERESTARTSYS; |
| @@ -331,8 +344,13 @@ static int v4l2_open(struct inode *inode, struct file *filp) | |||
| 331 | 344 | ||
| 332 | err: | 345 | err: |
| 333 | /* decrease the refcount in case of an error */ | 346 | /* decrease the refcount in case of an error */ |
| 334 | if (ret) | 347 | if (ret) { |
| 348 | #if defined(CONFIG_MEDIA_CONTROLLER) | ||
| 349 | if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) | ||
| 350 | media_entity_put(entity); | ||
| 351 | #endif | ||
| 335 | video_put(vdev); | 352 | video_put(vdev); |
| 353 | } | ||
| 336 | return ret; | 354 | return ret; |
| 337 | } | 355 | } |
| 338 | 356 | ||
| @@ -349,7 +367,10 @@ static int v4l2_release(struct inode *inode, struct file *filp) | |||
| 349 | if (vdev->lock) | 367 | if (vdev->lock) |
| 350 | mutex_unlock(vdev->lock); | 368 | mutex_unlock(vdev->lock); |
| 351 | } | 369 | } |
| 352 | 370 | #if defined(CONFIG_MEDIA_CONTROLLER) | |
| 371 | if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) | ||
| 372 | media_entity_put(&vdev->entity); | ||
| 373 | #endif | ||
| 353 | /* decrease the refcount unconditionally since the release() | 374 | /* decrease the refcount unconditionally since the release() |
| 354 | return value is ignored. */ | 375 | return value is ignored. */ |
| 355 | video_put(vdev); | 376 | video_put(vdev); |
| @@ -585,12 +606,27 @@ int __video_register_device(struct video_device *vdev, int type, int nr, | |||
| 585 | if (nr != -1 && nr != vdev->num && warn_if_nr_in_use) | 606 | if (nr != -1 && nr != vdev->num && warn_if_nr_in_use) |
| 586 | printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__, | 607 | printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__, |
| 587 | name_base, nr, video_device_node_name(vdev)); | 608 | name_base, nr, video_device_node_name(vdev)); |
| 588 | 609 | #if defined(CONFIG_MEDIA_CONTROLLER) | |
| 589 | /* Part 5: Activate this minor. The char device can now be used. */ | 610 | /* Part 5: Register the entity. */ |
| 611 | if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) { | ||
| 612 | vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L; | ||
| 613 | vdev->entity.name = vdev->name; | ||
| 614 | vdev->entity.v4l.major = VIDEO_MAJOR; | ||
| 615 | vdev->entity.v4l.minor = vdev->minor; | ||
| 616 | ret = media_device_register_entity(vdev->v4l2_dev->mdev, | ||
| 617 | &vdev->entity); | ||
| 618 | if (ret < 0) | ||
| 619 | printk(KERN_WARNING | ||
| 620 | "%s: media_device_register_entity failed\n", | ||
| 621 | __func__); | ||
| 622 | } | ||
| 623 | #endif | ||
| 624 | /* Part 6: Activate this minor. The char device can now be used. */ | ||
