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