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