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