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