]>
Commit | Line | Data |
---|---|---|
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 | #include <linux/media.h> | |
26 | ||
27 | #include <media/media-device.h> | |
28 | #include <media/media-devnode.h> | |
29 | #include <media/media-entity.h> | |
30 | ||
31 | /* ----------------------------------------------------------------------------- | |
32 | * Userspace API | |
33 | */ | |
34 | ||
35 | static int media_device_open(struct file *filp) | |
36 | { | |
37 | return 0; | |
38 | } | |
39 | ||
40 | static int media_device_close(struct file *filp) | |
41 | { | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static int media_device_get_info(struct media_device *dev, | |
46 | struct media_device_info __user *__info) | |
47 | { | |
48 | struct media_device_info info; | |
49 | ||
50 | memset(&info, 0, sizeof(info)); | |
51 | ||
52 | strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver)); | |
53 | strlcpy(info.model, dev->model, sizeof(info.model)); | |
54 | strlcpy(info.serial, dev->serial, sizeof(info.serial)); | |
55 | strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info)); | |
56 | ||
57 | info.media_version = MEDIA_API_VERSION; | |
58 | info.hw_revision = dev->hw_revision; | |
59 | info.driver_version = dev->driver_version; | |
60 | ||
61 | return copy_to_user(__info, &info, sizeof(*__info)); | |
62 | } | |
63 | ||
64 | static struct media_entity *find_entity(struct media_device *mdev, u32 id) | |
65 | { | |
66 | struct media_entity *entity; | |
67 | int next = id & MEDIA_ENT_ID_FLAG_NEXT; | |
68 | ||
69 | id &= ~MEDIA_ENT_ID_FLAG_NEXT; | |
70 | ||
71 | spin_lock(&mdev->lock); | |
72 | ||
73 | media_device_for_each_entity(entity, mdev) { | |
74 | if ((entity->id == id && !next) || | |
75 | (entity->id > id && next)) { | |
76 | spin_unlock(&mdev->lock); | |
77 | return entity; | |
78 | } | |
79 | } | |
80 | ||
81 | spin_unlock(&mdev->lock); | |
82 | ||
83 | return NULL; | |
84 | } | |
85 | ||
86 | static long media_device_enum_entities(struct media_device *mdev, | |
87 | struct media_entity_desc __user *uent) | |
88 | { | |
89 | struct media_entity *ent; | |
90 | struct media_entity_desc u_ent; | |
91 | ||
92 | if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id))) | |
93 | return -EFAULT; | |
94 | ||
95 | ent = find_entity(mdev, u_ent.id); | |
96 | ||
97 | if (ent == NULL) | |
98 | return -EINVAL; | |
99 | ||
100 | u_ent.id = ent->id; | |
101 | u_ent.name[0] = '\0'; | |
102 | if (ent->name) | |
103 | strlcpy(u_ent.name, ent->name, sizeof(u_ent.name)); | |
104 | u_ent.type = ent->type; | |
105 | u_ent.revision = ent->revision; | |
106 | u_ent.flags = ent->flags; | |
107 | u_ent.group_id = ent->group_id; | |
108 | u_ent.pads = ent->num_pads; | |
109 | u_ent.links = ent->num_links - ent->num_backlinks; | |
110 | u_ent.v4l.major = ent->v4l.major; | |
111 | u_ent.v4l.minor = ent->v4l.minor; | |
112 | if (copy_to_user(uent, &u_ent, sizeof(u_ent))) | |
113 | return -EFAULT; | |
114 | return 0; | |
115 | } | |
116 | ||
117 | static void media_device_kpad_to_upad(const struct media_pad *kpad, | |
118 | struct media_pad_desc *upad) | |
119 | { | |
120 | upad->entity = kpad->entity->id; | |
121 | upad->index = kpad->index; | |
122 | upad->flags = kpad->flags; | |
123 | } | |
124 | ||
125 | static long media_device_enum_links(struct media_device *mdev, | |
126 | struct media_links_enum __user *ulinks) | |
127 | { | |
128 | struct media_entity *entity; | |
129 | struct media_links_enum links; | |
130 | ||
131 | if (copy_from_user(&links, ulinks, sizeof(links))) | |
132 | return -EFAULT; | |
133 | ||
134 | entity = find_entity(mdev, links.entity); | |
135 | if (entity == NULL) | |
136 | return -EINVAL; | |
137 | ||
138 | if (links.pads) { | |
139 | unsigned int p; | |
140 | ||
141 | for (p = 0; p < entity->num_pads; p++) { | |
142 | struct media_pad_desc pad; | |
143 | media_device_kpad_to_upad(&entity->pads[p], &pad); | |
144 | if (copy_to_user(&links.pads[p], &pad, sizeof(pad))) | |
145 | return -EFAULT; | |
146 | } | |
147 | } | |
148 | ||
149 | if (links.links) { | |
150 | struct media_link_desc __user *ulink; | |
151 | unsigned int l; | |
152 | ||
153 | for (l = 0, ulink = links.links; l < entity->num_links; l++) { | |
154 | struct media_link_desc link; | |
155 | ||
156 | /* Ignore backlinks. */ | |
157 | if (entity->links[l].source->entity != entity) | |
158 | continue; | |
159 | ||
160 | media_device_kpad_to_upad(entity->links[l].source, | |
161 | &link.source); | |
162 | media_device_kpad_to_upad(entity->links[l].sink, | |
163 | &link.sink); | |
164 | link.flags = entity->links[l].flags; | |
165 | if (copy_to_user(ulink, &link, sizeof(*ulink))) | |
166 | return -EFAULT; | |
167 | ulink++; | |
168 | } | |
169 | } | |
170 | if (copy_to_user(ulinks, &links, sizeof(*ulinks))) | |
171 | return -EFAULT; | |
172 | return 0; | |
173 | } | |
174 | ||
175 | static long media_device_ioctl(struct file *filp, unsigned int cmd, | |
176 | unsigned long arg) | |
177 | { | |
178 | struct media_devnode *devnode = media_devnode_data(filp); | |
179 | struct media_device *dev = to_media_device(devnode); | |
180 | long ret; | |
181 | ||
182 | switch (cmd) { | |
183 | case MEDIA_IOC_DEVICE_INFO: | |
184 | ret = media_device_get_info(dev, | |
185 | (struct media_device_info __user *)arg); | |
186 | break; | |
187 | ||
188 | case MEDIA_IOC_ENUM_ENTITIES: | |
189 | ret = media_device_enum_entities(dev, | |
190 | (struct media_entity_desc __user *)arg); | |
191 | break; | |
192 | ||
193 | case MEDIA_IOC_ENUM_LINKS: | |
194 | mutex_lock(&dev->graph_mutex); | |
195 | ret = media_device_enum_links(dev, | |
196 | (struct media_links_enum __user *)arg); | |
197 | mutex_unlock(&dev->graph_mutex); | |
198 | break; | |
199 | ||
200 | default: | |
201 | ret = -ENOIOCTLCMD; | |
202 | } | |
203 | ||
204 | return ret; | |
205 | } | |
206 | ||
207 | static const struct media_file_operations media_device_fops = { | |
208 | .owner = THIS_MODULE, | |
209 | .open = media_device_open, | |
210 | .ioctl = media_device_ioctl, | |
211 | .release = media_device_close, | |
212 | }; | |
213 | ||
214 | /* ----------------------------------------------------------------------------- | |
215 | * sysfs | |
216 | */ | |
217 | ||
218 | static ssize_t show_model(struct device *cd, | |
219 | struct device_attribute *attr, char *buf) | |
220 | { | |
221 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); | |
222 | ||
223 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); | |
224 | } | |
225 | ||
226 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); | |
227 | ||
228 | /* ----------------------------------------------------------------------------- | |
229 | * Registration/unregistration | |
230 | */ | |
231 | ||
232 | static void media_device_release(struct media_devnode *mdev) | |
233 | { | |
234 | } | |
235 | ||
236 | /** | |
237 | * media_device_register - register a media device | |
238 | * @mdev: The media device | |
239 | * | |
240 | * The caller is responsible for initializing the media device before | |
241 | * registration. The following fields must be set: | |
242 | * | |
243 | * - dev must point to the parent device | |
244 | * - model must be filled with the device model name | |
245 | */ | |
246 | int __must_check media_device_register(struct media_device *mdev) | |
247 | { | |
248 | int ret; | |
249 | ||
250 | if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0)) | |
251 | return -EINVAL; | |
252 | ||
253 | mdev->entity_id = 1; | |
254 | INIT_LIST_HEAD(&mdev->entities); | |
255 | spin_lock_init(&mdev->lock); | |
256 | mutex_init(&mdev->graph_mutex); | |
257 | ||
258 | /* Register the device node. */ | |
259 | mdev->devnode.fops = &media_device_fops; | |
260 | mdev->devnode.parent = mdev->dev; | |
261 | mdev->devnode.release = media_device_release; | |
262 | ret = media_devnode_register(&mdev->devnode); | |
263 | if (ret < 0) | |
264 | return ret; | |
265 | ||
266 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); | |
267 | if (ret < 0) { | |
268 | media_devnode_unregister(&mdev->devnode); | |
269 | return ret; | |
270 | } | |
271 | ||
272 | return 0; | |
273 | } | |
274 | EXPORT_SYMBOL_GPL(media_device_register); | |
275 | ||
276 | /** | |
277 | * media_device_unregister - unregister a media device | |
278 | * @mdev: The media device | |
279 | * | |
280 | */ | |
281 | void media_device_unregister(struct media_device *mdev) | |
282 | { | |
283 | struct media_entity *entity; | |
284 | struct media_entity *next; | |
285 | ||
286 | list_for_each_entry_safe(entity, next, &mdev->entities, list) | |
287 | media_device_unregister_entity(entity); | |
288 | ||
289 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); | |
290 | media_devnode_unregister(&mdev->devnode); | |
291 | } | |
292 | EXPORT_SYMBOL_GPL(media_device_unregister); | |
293 | ||
294 | /** | |
295 | * media_device_register_entity - Register an entity with a media device | |
296 | * @mdev: The media device | |
297 | * @entity: The entity | |
298 | */ | |
299 | int __must_check media_device_register_entity(struct media_device *mdev, | |
300 | struct media_entity *entity) | |
301 | { | |
302 | /* Warn if we apparently re-register an entity */ | |
303 | WARN_ON(entity->parent != NULL); | |
304 | entity->parent = mdev; | |
305 | ||
306 | spin_lock(&mdev->lock); | |
307 | if (entity->id == 0) | |
308 | entity->id = mdev->entity_id++; | |
309 | else | |
310 | mdev->entity_id = max(entity->id + 1, mdev->entity_id); | |
311 | list_add_tail(&entity->list, &mdev->entities); | |
312 | spin_unlock(&mdev->lock); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | EXPORT_SYMBOL_GPL(media_device_register_entity); | |
317 | ||
318 | /** | |
319 | * media_device_unregister_entity - Unregister an entity | |
320 | * @entity: The entity | |
321 | * | |
322 | * If the entity has never been registered this function will return | |
323 | * immediately. | |
324 | */ | |
325 | void media_device_unregister_entity(struct media_entity *entity) | |
326 | { | |
327 | struct media_device *mdev = entity->parent; | |
328 | ||
329 | if (mdev == NULL) | |
330 | return; | |
331 | ||
332 | spin_lock(&mdev->lock); | |
333 | list_del(&entity->list); | |
334 | spin_unlock(&mdev->lock); | |
335 | entity->parent = NULL; | |
336 | } | |
337 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |