Commit | Line | Data |
---|---|---|
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 | ||
b0a1f2a8 SA |
23 | #include <linux/compat.h> |
24 | #include <linux/export.h> | |
176fb0d1 | 25 | #include <linux/ioctl.h> |
140d8816 | 26 | #include <linux/media.h> |
b0a1f2a8 | 27 | #include <linux/types.h> |
176fb0d1 LP |
28 | |
29 | #include <media/media-device.h> | |
30 | #include <media/media-devnode.h> | |
53e269c1 | 31 | #include <media/media-entity.h> |
176fb0d1 | 32 | |
140d8816 LP |
33 | /* ----------------------------------------------------------------------------- |
34 | * Userspace API | |
35 | */ | |
36 | ||
37 | static int media_device_open(struct file *filp) | |
38 | { | |
39 | return 0; | |
40 | } | |
41 | ||
42 | static int media_device_close(struct file *filp) | |
43 | { | |
44 | return 0; | |
45 | } | |
46 | ||
47 | static int media_device_get_info(struct media_device *dev, | |
48 | struct media_device_info __user *__info) | |
49 | { | |
50 | struct media_device_info info; | |
51 | ||
52 | memset(&info, 0, sizeof(info)); | |
53 | ||
54 | strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver)); | |
55 | strlcpy(info.model, dev->model, sizeof(info.model)); | |
56 | strlcpy(info.serial, dev->serial, sizeof(info.serial)); | |
57 | strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info)); | |
58 | ||
59 | info.media_version = MEDIA_API_VERSION; | |
60 | info.hw_revision = dev->hw_revision; | |
61 | info.driver_version = dev->driver_version; | |
62 | ||
b17d3945 NT |
63 | if (copy_to_user(__info, &info, sizeof(*__info))) |
64 | return -EFAULT; | |
65 | return 0; | |
140d8816 LP |
66 | } |
67 | ||
1651333b LP |
68 | static struct media_entity *find_entity(struct media_device *mdev, u32 id) |
69 | { | |
70 | struct media_entity *entity; | |
71 | int next = id & MEDIA_ENT_ID_FLAG_NEXT; | |
72 | ||
73 | id &= ~MEDIA_ENT_ID_FLAG_NEXT; | |
74 | ||
75 | spin_lock(&mdev->lock); | |
76 | ||
77 | media_device_for_each_entity(entity, mdev) { | |
78 | if ((entity->id == id && !next) || | |
79 | (entity->id > id && next)) { | |
80 | spin_unlock(&mdev->lock); | |
81 | return entity; | |
82 | } | |
83 | } | |
84 | ||
85 | spin_unlock(&mdev->lock); | |
86 | ||
87 | return NULL; | |
88 | } | |
89 | ||
90 | static long media_device_enum_entities(struct media_device *mdev, | |
91 | struct media_entity_desc __user *uent) | |
92 | { | |
93 | struct media_entity *ent; | |
94 | struct media_entity_desc u_ent; | |
95 | ||
96 | if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id))) | |
97 | return -EFAULT; | |
98 | ||
99 | ent = find_entity(mdev, u_ent.id); | |
100 | ||
101 | if (ent == NULL) | |
102 | return -EINVAL; | |
103 | ||
104 | u_ent.id = ent->id; | |
c06ca8f9 DC |
105 | if (ent->name) { |
106 | strncpy(u_ent.name, ent->name, sizeof(u_ent.name)); | |
107 | u_ent.name[sizeof(u_ent.name) - 1] = '\0'; | |
108 | } else { | |
109 | memset(u_ent.name, 0, sizeof(u_ent.name)); | |
110 | } | |
1651333b LP |
111 | u_ent.type = ent->type; |
112 | u_ent.revision = ent->revision; | |
113 | u_ent.flags = ent->flags; | |
114 | u_ent.group_id = ent->group_id; | |
115 | u_ent.pads = ent->num_pads; | |
116 | u_ent.links = ent->num_links - ent->num_backlinks; | |
fa5034c6 | 117 | memcpy(&u_ent.raw, &ent->info, sizeof(ent->info)); |
1651333b LP |
118 | if (copy_to_user(uent, &u_ent, sizeof(u_ent))) |
119 | return -EFAULT; | |
120 | return 0; | |
121 | } | |
122 | ||
123 | static void media_device_kpad_to_upad(const struct media_pad *kpad, | |
124 | struct media_pad_desc *upad) | |
125 | { | |
126 | upad->entity = kpad->entity->id; | |
127 | upad->index = kpad->index; | |
128 | upad->flags = kpad->flags; | |
129 | } | |
130 | ||
b0a1f2a8 SA |
131 | static long __media_device_enum_links(struct media_device *mdev, |
132 | struct media_links_enum *links) | |
1651333b LP |
133 | { |
134 | struct media_entity *entity; | |
1651333b | 135 | |
b0a1f2a8 | 136 | entity = find_entity(mdev, links->entity); |
1651333b LP |
137 | if (entity == NULL) |
138 | return -EINVAL; | |
139 | ||
b0a1f2a8 | 140 | if (links->pads) { |
1651333b LP |
141 | unsigned int p; |
142 | ||
143 | for (p = 0; p < entity->num_pads; p++) { | |
144 | struct media_pad_desc pad; | |
c88e739b DC |
145 | |
146 | memset(&pad, 0, sizeof(pad)); | |
1651333b | 147 | media_device_kpad_to_upad(&entity->pads[p], &pad); |
b0a1f2a8 | 148 | if (copy_to_user(&links->pads[p], &pad, sizeof(pad))) |
1651333b LP |
149 | return -EFAULT; |
150 | } | |
151 | } | |
152 | ||
b0a1f2a8 | 153 | if (links->links) { |
1651333b LP |
154 | struct media_link_desc __user *ulink; |
155 | unsigned int l; | |
156 | ||
b0a1f2a8 | 157 | for (l = 0, ulink = links->links; l < entity->num_links; l++) { |
1651333b LP |
158 | struct media_link_desc link; |
159 | ||
160 | /* Ignore backlinks. */ | |
161 | if (entity->links[l].source->entity != entity) | |
162 | continue; | |
163 | ||
c88e739b | 164 | memset(&link, 0, sizeof(link)); |
1651333b LP |
165 | media_device_kpad_to_upad(entity->links[l].source, |
166 | &link.source); | |
167 | media_device_kpad_to_upad(entity->links[l].sink, | |
168 | &link.sink); | |
169 | link.flags = entity->links[l].flags; | |
170 | if (copy_to_user(ulink, &link, sizeof(*ulink))) | |
171 | return -EFAULT; | |
172 | ulink++; | |
173 | } | |
174 | } | |
b0a1f2a8 SA |
175 | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static long media_device_enum_links(struct media_device *mdev, | |
180 | struct media_links_enum __user *ulinks) | |
181 | { | |
182 | struct media_links_enum links; | |
183 | int rval; | |
184 | ||
185 | if (copy_from_user(&links, ulinks, sizeof(links))) | |
186 | return -EFAULT; | |
187 | ||
188 | rval = __media_device_enum_links(mdev, &links); | |
189 | if (rval < 0) | |
190 | return rval; | |
191 | ||
1651333b LP |
192 | if (copy_to_user(ulinks, &links, sizeof(*ulinks))) |
193 | return -EFAULT; | |
b0a1f2a8 | 194 | |
1651333b LP |
195 | return 0; |
196 | } | |
197 | ||
97548ed4 LP |
198 | static long media_device_setup_link(struct media_device *mdev, |
199 | struct media_link_desc __user *_ulink) | |
200 | { | |
201 | struct media_link *link = NULL; | |
202 | struct media_link_desc ulink; | |
203 | struct media_entity *source; | |
204 | struct media_entity *sink; | |
205 | int ret; | |
206 | ||
207 | if (copy_from_user(&ulink, _ulink, sizeof(ulink))) | |
208 | return -EFAULT; | |
209 | ||
210 | /* Find the source and sink entities and link. | |
211 | */ | |
212 | source = find_entity(mdev, ulink.source.entity); | |
213 | sink = find_entity(mdev, ulink.sink.entity); | |
214 | ||
215 | if (source == NULL || sink == NULL) | |
216 | return -EINVAL; | |
217 | ||
218 | if (ulink.source.index >= source->num_pads || | |
219 | ulink.sink.index >= sink->num_pads) | |
220 | return -EINVAL; | |
221 | ||
222 | link = media_entity_find_link(&source->pads[ulink.source.index], | |
223 | &sink->pads[ulink.sink.index]); | |
224 | if (link == NULL) | |
225 | return -EINVAL; | |
226 | ||
227 | /* Setup the link on both entities. */ | |
228 | ret = __media_entity_setup_link(link, ulink.flags); | |
229 | ||
230 | if (copy_to_user(_ulink, &ulink, sizeof(ulink))) | |
231 | return -EFAULT; | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
140d8816 LP |
236 | static long media_device_ioctl(struct file *filp, unsigned int cmd, |
237 | unsigned long arg) | |
238 | { | |
239 | struct media_devnode *devnode = media_devnode_data(filp); | |
240 | struct media_device *dev = to_media_device(devnode); | |
241 | long ret; | |
242 | ||
243 | switch (cmd) { | |
244 | case MEDIA_IOC_DEVICE_INFO: | |
245 | ret = media_device_get_info(dev, | |
246 | (struct media_device_info __user *)arg); | |
247 | break; | |
248 | ||
1651333b LP |
249 | case MEDIA_IOC_ENUM_ENTITIES: |
250 | ret = media_device_enum_entities(dev, | |
251 | (struct media_entity_desc __user *)arg); | |
252 | break; | |
253 | ||
254 | case MEDIA_IOC_ENUM_LINKS: | |
255 | mutex_lock(&dev->graph_mutex); | |
256 | ret = media_device_enum_links(dev, | |
257 | (struct media_links_enum __user *)arg); | |
258 | mutex_unlock(&dev->graph_mutex); | |
259 | break; | |
260 | ||
97548ed4 LP |
261 | case MEDIA_IOC_SETUP_LINK: |
262 | mutex_lock(&dev->graph_mutex); | |
263 | ret = media_device_setup_link(dev, | |
264 | (struct media_link_desc __user *)arg); | |
265 | mutex_unlock(&dev->graph_mutex); | |
266 | break; | |
267 | ||
140d8816 LP |
268 | default: |
269 | ret = -ENOIOCTLCMD; | |
270 | } | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
b0a1f2a8 SA |
275 | #ifdef CONFIG_COMPAT |
276 | ||
277 | struct media_links_enum32 { | |
278 | __u32 entity; | |
279 | compat_uptr_t pads; /* struct media_pad_desc * */ | |
280 | compat_uptr_t links; /* struct media_link_desc * */ | |
281 | __u32 reserved[4]; | |
282 | }; | |
283 | ||
284 | static long media_device_enum_links32(struct media_device *mdev, | |
285 | struct media_links_enum32 __user *ulinks) | |
286 | { | |
287 | struct media_links_enum links; | |
288 | compat_uptr_t pads_ptr, links_ptr; | |
289 | ||
290 | memset(&links, 0, sizeof(links)); | |
291 | ||
292 | if (get_user(links.entity, &ulinks->entity) | |
293 | || get_user(pads_ptr, &ulinks->pads) | |
294 | || get_user(links_ptr, &ulinks->links)) | |
295 | return -EFAULT; | |
296 | ||
297 | links.pads = compat_ptr(pads_ptr); | |
298 | links.links = compat_ptr(links_ptr); | |
299 | ||
300 | return __media_device_enum_links(mdev, &links); | |
301 | } | |
302 | ||
303 | #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32) | |
304 | ||
305 | static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, | |
306 | unsigned long arg) | |
307 | { | |
308 | struct media_devnode *devnode = media_devnode_data(filp); | |
309 | struct media_device *dev = to_media_device(devnode); | |
310 | long ret; | |
311 | ||
312 | switch (cmd) { | |
313 | case MEDIA_IOC_DEVICE_INFO: | |
314 | case MEDIA_IOC_ENUM_ENTITIES: | |
315 | case MEDIA_IOC_SETUP_LINK: | |
316 | return media_device_ioctl(filp, cmd, arg); | |
317 | ||
318 | case MEDIA_IOC_ENUM_LINKS32: | |
319 | mutex_lock(&dev->graph_mutex); | |
320 | ret = media_device_enum_links32(dev, | |
321 | (struct media_links_enum32 __user *)arg); | |
322 | mutex_unlock(&dev->graph_mutex); | |
323 | break; | |
324 | ||
325 | default: | |
326 | ret = -ENOIOCTLCMD; | |
327 | } | |
328 | ||
329 | return ret; | |
330 | } | |
331 | #endif /* CONFIG_COMPAT */ | |
332 | ||
176fb0d1 LP |
333 | static const struct media_file_operations media_device_fops = { |
334 | .owner = THIS_MODULE, | |
140d8816 LP |
335 | .open = media_device_open, |
336 | .ioctl = media_device_ioctl, | |
b0a1f2a8 SA |
337 | #ifdef CONFIG_COMPAT |
338 | .compat_ioctl = media_device_compat_ioctl, | |
339 | #endif /* CONFIG_COMPAT */ | |
140d8816 | 340 | .release = media_device_close, |
176fb0d1 LP |
341 | }; |
342 | ||
343 | /* ----------------------------------------------------------------------------- | |
344 | * sysfs | |
345 | */ | |
346 | ||
347 | static ssize_t show_model(struct device *cd, | |
348 | struct device_attribute *attr, char *buf) | |
349 | { | |
350 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); | |
351 | ||
352 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); | |
353 | } | |
354 | ||
355 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); | |
356 | ||
357 | /* ----------------------------------------------------------------------------- | |
358 | * Registration/unregistration | |
359 | */ | |
360 | ||
361 | static void media_device_release(struct media_devnode *mdev) | |
362 | { | |
363 | } | |
364 | ||
365 | /** | |
366 | * media_device_register - register a media device | |
367 | * @mdev: The media device | |
368 | * | |
369 | * The caller is responsible for initializing the media device before | |
370 | * registration. The following fields must be set: | |
371 | * | |
372 | * - dev must point to the parent device | |
373 | * - model must be filled with the device model name | |
374 | */ | |
375 | int __must_check media_device_register(struct media_device *mdev) | |
376 | { | |
377 | int ret; | |
378 | ||
379 | if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0)) | |
380 | return -EINVAL; | |
381 | ||
53e269c1 LP |
382 | mdev->entity_id = 1; |
383 | INIT_LIST_HEAD(&mdev->entities); | |
384 | spin_lock_init(&mdev->lock); | |
503c3d82 | 385 | mutex_init(&mdev->graph_mutex); |
53e269c1 | 386 | |
176fb0d1 LP |
387 | /* Register the device node. */ |
388 | mdev->devnode.fops = &media_device_fops; | |
389 | mdev->devnode.parent = mdev->dev; | |
390 | mdev->devnode.release = media_device_release; | |
391 | ret = media_devnode_register(&mdev->devnode); | |
392 | if (ret < 0) | |
393 | return ret; | |
394 | ||
395 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); | |
396 | if (ret < 0) { | |
397 | media_devnode_unregister(&mdev->devnode); | |
398 | return ret; | |
399 | } | |
400 | ||
401 | return 0; | |
402 | } | |
403 | EXPORT_SYMBOL_GPL(media_device_register); | |
404 | ||
405 | /** | |
406 | * media_device_unregister - unregister a media device | |
407 | * @mdev: The media device | |
408 | * | |
409 | */ | |
410 | void media_device_unregister(struct media_device *mdev) | |
411 | { | |
53e269c1 LP |
412 | struct media_entity *entity; |
413 | struct media_entity *next; | |
414 | ||
415 | list_for_each_entry_safe(entity, next, &mdev->entities, list) | |
416 | media_device_unregister_entity(entity); | |
417 | ||
176fb0d1 LP |
418 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); |
419 | media_devnode_unregister(&mdev->devnode); | |
420 | } | |
421 | EXPORT_SYMBOL_GPL(media_device_unregister); | |
53e269c1 LP |
422 | |
423 | /** | |
424 | * media_device_register_entity - Register an entity with a media device | |
425 | * @mdev: The media device | |
426 | * @entity: The entity | |
427 | */ | |
428 | int __must_check media_device_register_entity(struct media_device *mdev, | |
429 | struct media_entity *entity) | |
430 | { | |
431 | /* Warn if we apparently re-register an entity */ | |
432 | WARN_ON(entity->parent != NULL); | |
433 | entity->parent = mdev; | |
434 | ||
435 | spin_lock(&mdev->lock); | |
436 | if (entity->id == 0) | |
437 | entity->id = mdev->entity_id++; | |
438 | else | |
439 | mdev->entity_id = max(entity->id + 1, mdev->entity_id); | |
440 | list_add_tail(&entity->list, &mdev->entities); | |
441 | spin_unlock(&mdev->lock); | |
442 | ||
443 | return 0; | |
444 | } | |
445 | EXPORT_SYMBOL_GPL(media_device_register_entity); | |
446 | ||
447 | /** | |
448 | * media_device_unregister_entity - Unregister an entity | |
449 | * @entity: The entity | |
450 | * | |
451 | * If the entity has never been registered this function will return | |
452 | * immediately. | |
453 | */ | |
454 | void media_device_unregister_entity(struct media_entity *entity) | |
455 | { | |
456 | struct media_device *mdev = entity->parent; | |
457 | ||
458 | if (mdev == NULL) | |
459 | return; | |
460 | ||
461 | spin_lock(&mdev->lock); | |
462 | list_del(&entity->list); | |
463 | spin_unlock(&mdev->lock); | |
464 | entity->parent = NULL; | |
465 | } | |
466 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |