4 * Copyright (C) 2010 Nokia Corporation
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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
23 #include <linux/compat.h>
24 #include <linux/export.h>
25 #include <linux/ioctl.h>
26 #include <linux/media.h>
27 #include <linux/types.h>
29 #include <media/media-device.h>
30 #include <media/media-devnode.h>
31 #include <media/media-entity.h>
33 /* -----------------------------------------------------------------------------
37 static int media_device_open(struct file
*filp
)
42 static int media_device_close(struct file
*filp
)
47 static int media_device_get_info(struct media_device
*dev
,
48 struct media_device_info __user
*__info
)
50 struct media_device_info info
;
52 memset(&info
, 0, sizeof(info
));
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
));
59 info
.media_version
= MEDIA_API_VERSION
;
60 info
.hw_revision
= dev
->hw_revision
;
61 info
.driver_version
= dev
->driver_version
;
63 if (copy_to_user(__info
, &info
, sizeof(*__info
)))
68 static struct media_entity
*find_entity(struct media_device
*mdev
, u32 id
)
70 struct media_entity
*entity
;
71 int next
= id
& MEDIA_ENT_ID_FLAG_NEXT
;
73 id
&= ~MEDIA_ENT_ID_FLAG_NEXT
;
75 spin_lock(&mdev
->lock
);
77 media_device_for_each_entity(entity
, mdev
) {
78 if ((entity
->id
== id
&& !next
) ||
79 (entity
->id
> id
&& next
)) {
80 spin_unlock(&mdev
->lock
);
85 spin_unlock(&mdev
->lock
);
90 static long media_device_enum_entities(struct media_device
*mdev
,
91 struct media_entity_desc __user
*uent
)
93 struct media_entity
*ent
;
94 struct media_entity_desc u_ent
;
96 memset(&u_ent
, 0, sizeof(u_ent
));
97 if (copy_from_user(&u_ent
.id
, &uent
->id
, sizeof(u_ent
.id
)))
100 ent
= find_entity(mdev
, u_ent
.id
);
107 strncpy(u_ent
.name
, ent
->name
, sizeof(u_ent
.name
));
108 u_ent
.name
[sizeof(u_ent
.name
) - 1] = '\0';
110 u_ent
.type
= ent
->type
;
111 u_ent
.revision
= ent
->revision
;
112 u_ent
.flags
= ent
->flags
;
113 u_ent
.group_id
= ent
->group_id
;
114 u_ent
.pads
= ent
->num_pads
;
115 u_ent
.links
= ent
->num_links
- ent
->num_backlinks
;
116 memcpy(&u_ent
.raw
, &ent
->info
, sizeof(ent
->info
));
117 if (copy_to_user(uent
, &u_ent
, sizeof(u_ent
)))
122 static void media_device_kpad_to_upad(const struct media_pad
*kpad
,
123 struct media_pad_desc
*upad
)
125 upad
->entity
= kpad
->entity
->id
;
126 upad
->index
= kpad
->index
;
127 upad
->flags
= kpad
->flags
;
130 static long __media_device_enum_links(struct media_device
*mdev
,
131 struct media_links_enum
*links
)
133 struct media_entity
*entity
;
135 entity
= find_entity(mdev
, links
->entity
);
142 for (p
= 0; p
< entity
->num_pads
; p
++) {
143 struct media_pad_desc pad
;
145 memset(&pad
, 0, sizeof(pad
));
146 media_device_kpad_to_upad(&entity
->pads
[p
], &pad
);
147 if (copy_to_user(&links
->pads
[p
], &pad
, sizeof(pad
)))
153 struct media_link_desc __user
*ulink
;
156 for (l
= 0, ulink
= links
->links
; l
< entity
->num_links
; l
++) {
157 struct media_link_desc link
;
159 /* Ignore backlinks. */
160 if (entity
->links
[l
].source
->entity
!= entity
)
163 memset(&link
, 0, sizeof(link
));
164 media_device_kpad_to_upad(entity
->links
[l
].source
,
166 media_device_kpad_to_upad(entity
->links
[l
].sink
,
168 link
.flags
= entity
->links
[l
].flags
;
169 if (copy_to_user(ulink
, &link
, sizeof(*ulink
)))
178 static long media_device_enum_links(struct media_device
*mdev
,
179 struct media_links_enum __user
*ulinks
)
181 struct media_links_enum links
;
184 if (copy_from_user(&links
, ulinks
, sizeof(links
)))
187 rval
= __media_device_enum_links(mdev
, &links
);
191 if (copy_to_user(ulinks
, &links
, sizeof(*ulinks
)))
197 static long media_device_setup_link(struct media_device
*mdev
,
198 struct media_link_desc __user
*_ulink
)
200 struct media_link
*link
= NULL
;
201 struct media_link_desc ulink
;
202 struct media_entity
*source
;
203 struct media_entity
*sink
;
206 if (copy_from_user(&ulink
, _ulink
, sizeof(ulink
)))
209 /* Find the source and sink entities and link.
211 source
= find_entity(mdev
, ulink
.source
.entity
);
212 sink
= find_entity(mdev
, ulink
.sink
.entity
);
214 if (source
== NULL
|| sink
== NULL
)
217 if (ulink
.source
.index
>= source
->num_pads
||
218 ulink
.sink
.index
>= sink
->num_pads
)
221 link
= media_entity_find_link(&source
->pads
[ulink
.source
.index
],
222 &sink
->pads
[ulink
.sink
.index
]);
226 /* Setup the link on both entities. */
227 ret
= __media_entity_setup_link(link
, ulink
.flags
);
229 if (copy_to_user(_ulink
, &ulink
, sizeof(ulink
)))
235 static long media_device_ioctl(struct file
*filp
, unsigned int cmd
,
238 struct media_devnode
*devnode
= media_devnode_data(filp
);
239 struct media_device
*dev
= to_media_device(devnode
);
243 case MEDIA_IOC_DEVICE_INFO
:
244 ret
= media_device_get_info(dev
,
245 (struct media_device_info __user
*)arg
);
248 case MEDIA_IOC_ENUM_ENTITIES
:
249 ret
= media_device_enum_entities(dev
,
250 (struct media_entity_desc __user
*)arg
);
253 case MEDIA_IOC_ENUM_LINKS
:
254 mutex_lock(&dev
->graph_mutex
);
255 ret
= media_device_enum_links(dev
,
256 (struct media_links_enum __user
*)arg
);
257 mutex_unlock(&dev
->graph_mutex
);
260 case MEDIA_IOC_SETUP_LINK
:
261 mutex_lock(&dev
->graph_mutex
);
262 ret
= media_device_setup_link(dev
,
263 (struct media_link_desc __user
*)arg
);
264 mutex_unlock(&dev
->graph_mutex
);
276 struct media_links_enum32
{
278 compat_uptr_t pads
; /* struct media_pad_desc * */
279 compat_uptr_t links
; /* struct media_link_desc * */
283 static long media_device_enum_links32(struct media_device
*mdev
,
284 struct media_links_enum32 __user
*ulinks
)
286 struct media_links_enum links
;
287 compat_uptr_t pads_ptr
, links_ptr
;
289 memset(&links
, 0, sizeof(links
));
291 if (get_user(links
.entity
, &ulinks
->entity
)
292 || get_user(pads_ptr
, &ulinks
->pads
)
293 || get_user(links_ptr
, &ulinks
->links
))
296 links
.pads
= compat_ptr(pads_ptr
);
297 links
.links
= compat_ptr(links_ptr
);
299 return __media_device_enum_links(mdev
, &links
);
302 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
304 static long media_device_compat_ioctl(struct file
*filp
, unsigned int cmd
,
307 struct media_devnode
*devnode
= media_devnode_data(filp
);
308 struct media_device
*dev
= to_media_device(devnode
);
312 case MEDIA_IOC_DEVICE_INFO
:
313 case MEDIA_IOC_ENUM_ENTITIES
:
314 case MEDIA_IOC_SETUP_LINK
:
315 return media_device_ioctl(filp
, cmd
, arg
);
317 case MEDIA_IOC_ENUM_LINKS32
:
318 mutex_lock(&dev
->graph_mutex
);
319 ret
= media_device_enum_links32(dev
,
320 (struct media_links_enum32 __user
*)arg
);
321 mutex_unlock(&dev
->graph_mutex
);
330 #endif /* CONFIG_COMPAT */
332 static const struct media_file_operations media_device_fops
= {
333 .owner
= THIS_MODULE
,
334 .open
= media_device_open
,
335 .ioctl
= media_device_ioctl
,
337 .compat_ioctl
= media_device_compat_ioctl
,
338 #endif /* CONFIG_COMPAT */
339 .release
= media_device_close
,
342 /* -----------------------------------------------------------------------------
346 static ssize_t
show_model(struct device
*cd
,
347 struct device_attribute
*attr
, char *buf
)
349 struct media_device
*mdev
= to_media_device(to_media_devnode(cd
));
351 return sprintf(buf
, "%.*s\n", (int)sizeof(mdev
->model
), mdev
->model
);
354 static DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
356 /* -----------------------------------------------------------------------------
357 * Registration/unregistration
360 static void media_device_release(struct media_devnode
*mdev
)
365 * media_device_register - register a media device
366 * @mdev: The media device
368 * The caller is responsible for initializing the media device before
369 * registration. The following fields must be set:
371 * - dev must point to the parent device
372 * - model must be filled with the device model name
374 int __must_check
media_device_register(struct media_device
*mdev
)
378 if (WARN_ON(mdev
->dev
== NULL
|| mdev
->model
[0] == 0))
382 INIT_LIST_HEAD(&mdev
->entities
);
383 spin_lock_init(&mdev
->lock
);
384 mutex_init(&mdev
->graph_mutex
);
386 /* Register the device node. */
387 mdev
->devnode
.fops
= &media_device_fops
;
388 mdev
->devnode
.parent
= mdev
->dev
;
389 mdev
->devnode
.release
= media_device_release
;
390 ret
= media_devnode_register(&mdev
->devnode
);
394 ret
= device_create_file(&mdev
->devnode
.dev
, &dev_attr_model
);
396 media_devnode_unregister(&mdev
->devnode
);
402 EXPORT_SYMBOL_GPL(media_device_register
);
405 * media_device_unregister - unregister a media device
406 * @mdev: The media device
409 void media_device_unregister(struct media_device
*mdev
)
411 struct media_entity
*entity
;
412 struct media_entity
*next
;
414 list_for_each_entry_safe(entity
, next
, &mdev
->entities
, list
)
415 media_device_unregister_entity(entity
);
417 device_remove_file(&mdev
->devnode
.dev
, &dev_attr_model
);
418 media_devnode_unregister(&mdev
->devnode
);
420 EXPORT_SYMBOL_GPL(media_device_unregister
);
423 * media_device_register_entity - Register an entity with a media device
424 * @mdev: The media device
425 * @entity: The entity
427 int __must_check
media_device_register_entity(struct media_device
*mdev
,
428 struct media_entity
*entity
)
430 /* Warn if we apparently re-register an entity */
431 WARN_ON(entity
->parent
!= NULL
);
432 entity
->parent
= mdev
;
434 spin_lock(&mdev
->lock
);
436 entity
->id
= mdev
->entity_id
++;
438 mdev
->entity_id
= max(entity
->id
+ 1, mdev
->entity_id
);
439 list_add_tail(&entity
->list
, &mdev
->entities
);
440 spin_unlock(&mdev
->lock
);
444 EXPORT_SYMBOL_GPL(media_device_register_entity
);
447 * media_device_unregister_entity - Unregister an entity
448 * @entity: The entity
450 * If the entity has never been registered this function will return
453 void media_device_unregister_entity(struct media_entity
*entity
)
455 struct media_device
*mdev
= entity
->parent
;
460 spin_lock(&mdev
->lock
);
461 list_del(&entity
->list
);
462 spin_unlock(&mdev
->lock
);
463 entity
->parent
= NULL
;
465 EXPORT_SYMBOL_GPL(media_device_unregister_entity
);