[media] media: Entity use count
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / media-device.c
CommitLineData
176fb0d1
LP
1/*
2 * Media device
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/types.h>
24#include <linux/ioctl.h>
25
26#include <media/media-device.h>
27#include <media/media-devnode.h>
53e269c1 28#include <media/media-entity.h>
176fb0d1
LP
29
30static const struct media_file_operations media_device_fops = {
31 .owner = THIS_MODULE,
32};
33
34/* -----------------------------------------------------------------------------
35 * sysfs
36 */
37
38static ssize_t show_model(struct device *cd,
39 struct device_attribute *attr, char *buf)
40{
41 struct media_device *mdev = to_media_device(to_media_devnode(cd));
42
43 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
44}
45
46static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
47
48/* -----------------------------------------------------------------------------
49 * Registration/unregistration
50 */
51
52static void media_device_release(struct media_devnode *mdev)
53{
54}
55
56/**
57 * media_device_register - register a media device
58 * @mdev: The media device
59 *
60 * The caller is responsible for initializing the media device before
61 * registration. The following fields must be set:
62 *
63 * - dev must point to the parent device
64 * - model must be filled with the device model name
65 */
66int __must_check media_device_register(struct media_device *mdev)
67{
68 int ret;
69
70 if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
71 return -EINVAL;
72
53e269c1
LP
73 mdev->entity_id = 1;
74 INIT_LIST_HEAD(&mdev->entities);
75 spin_lock_init(&mdev->lock);
503c3d82 76 mutex_init(&mdev->graph_mutex);
53e269c1 77
176fb0d1
LP
78 /* Register the device node. */
79 mdev->devnode.fops = &media_device_fops;
80 mdev->devnode.parent = mdev->dev;
81 mdev->devnode.release = media_device_release;
82 ret = media_devnode_register(&mdev->devnode);
83 if (ret < 0)
84 return ret;
85
86 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
87 if (ret < 0) {
88 media_devnode_unregister(&mdev->devnode);
89 return ret;
90 }
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(media_device_register);
95
96/**
97 * media_device_unregister - unregister a media device
98 * @mdev: The media device
99 *
100 */
101void media_device_unregister(struct media_device *mdev)
102{
53e269c1
LP
103 struct media_entity *entity;
104 struct media_entity *next;
105
106 list_for_each_entry_safe(entity, next, &mdev->entities, list)
107 media_device_unregister_entity(entity);
108
176fb0d1
LP
109 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
110 media_devnode_unregister(&mdev->devnode);
111}
112EXPORT_SYMBOL_GPL(media_device_unregister);
53e269c1
LP
113
114/**
115 * media_device_register_entity - Register an entity with a media device
116 * @mdev: The media device
117 * @entity: The entity
118 */
119int __must_check media_device_register_entity(struct media_device *mdev,
120 struct media_entity *entity)
121{
122 /* Warn if we apparently re-register an entity */
123 WARN_ON(entity->parent != NULL);
124 entity->parent = mdev;
125
126 spin_lock(&mdev->lock);
127 if (entity->id == 0)
128 entity->id = mdev->entity_id++;
129 else
130 mdev->entity_id = max(entity->id + 1, mdev->entity_id);
131 list_add_tail(&entity->list, &mdev->entities);
132 spin_unlock(&mdev->lock);
133
134 return 0;
135}
136EXPORT_SYMBOL_GPL(media_device_register_entity);
137
138/**
139 * media_device_unregister_entity - Unregister an entity
140 * @entity: The entity
141 *
142 * If the entity has never been registered this function will return
143 * immediately.
144 */
145void media_device_unregister_entity(struct media_entity *entity)
146{
147 struct media_device *mdev = entity->parent;
148
149 if (mdev == NULL)
150 return;
151
152 spin_lock(&mdev->lock);
153 list_del(&entity->list);
154 spin_unlock(&mdev->lock);
155 entity->parent = NULL;
156}
157EXPORT_SYMBOL_GPL(media_device_unregister_entity);