blob: 7d97c260b440e527684a93b5fcf9be2a7dd83089 [file] [log] [blame]
Laurent Pinchart176fb0d2009-12-09 08:39:58 -03001/*
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>
Laurent Pinchart140d8812010-08-18 11:41:22 -030025#include <linux/media.h>
Paul Gortmaker35a24632011-08-01 15:26:38 -040026#include <linux/export.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030027
28#include <media/media-device.h>
29#include <media/media-devnode.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030030#include <media/media-entity.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030031
Laurent Pinchart140d8812010-08-18 11:41:22 -030032/* -----------------------------------------------------------------------------
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
Laurent Pinchart16513332009-12-09 08:40:01 -030065static 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;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300111 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300112 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
113 return -EFAULT;
114 return 0;
115}
116
117static void media_device_kpad_to_upad(const struct media_pad *kpad,
118 struct media_pad_desc *upad)
119{
120 upad->entity = kpad->entity->id;
121 upad->index = kpad->index;
122 upad->flags = kpad->flags;
123}
124
125static long media_device_enum_links(struct media_device *mdev,
126 struct media_links_enum __user *ulinks)
127{
128 struct media_entity *entity;
129 struct media_links_enum links;
130
131 if (copy_from_user(&links, ulinks, sizeof(links)))
132 return -EFAULT;
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++) {
Deva Ramasubramaniane9a00402014-02-18 16:24:51 -0800142 struct media_pad_desc pad;
Dan Carpenteraa3f3342013-04-13 06:32:15 -0300143
144 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300145 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
Dan Carpenteraa3f3342013-04-13 06:32:15 -0300162 memset(&link, 0, sizeof(link));
Laurent Pinchart16513332009-12-09 08:40:01 -0300163 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 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
174 return -EFAULT;
175 return 0;
176}
177
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300178static long media_device_setup_link(struct media_device *mdev,
179 struct media_link_desc __user *_ulink)
180{
181 struct media_link *link = NULL;
182 struct media_link_desc ulink;
183 struct media_entity *source;
184 struct media_entity *sink;
185 int ret;
186
187 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
188 return -EFAULT;
189
190 /* Find the source and sink entities and link.
191 */
192 source = find_entity(mdev, ulink.source.entity);
193 sink = find_entity(mdev, ulink.sink.entity);
194
195 if (source == NULL || sink == NULL)
196 return -EINVAL;
197
198 if (ulink.source.index >= source->num_pads ||
199 ulink.sink.index >= sink->num_pads)
200 return -EINVAL;
201
202 link = media_entity_find_link(&source->pads[ulink.source.index],
203 &sink->pads[ulink.sink.index]);
204 if (link == NULL)
205 return -EINVAL;
206
207 /* Setup the link on both entities. */
208 ret = __media_entity_setup_link(link, ulink.flags);
209
210 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
211 return -EFAULT;
212
213 return ret;
214}
215
Laurent Pinchart140d8812010-08-18 11:41:22 -0300216static long media_device_ioctl(struct file *filp, unsigned int cmd,
217 unsigned long arg)
218{
219 struct media_devnode *devnode = media_devnode_data(filp);
220 struct media_device *dev = to_media_device(devnode);
221 long ret;
222
223 switch (cmd) {
224 case MEDIA_IOC_DEVICE_INFO:
225 ret = media_device_get_info(dev,
226 (struct media_device_info __user *)arg);
227 break;
228
Laurent Pinchart16513332009-12-09 08:40:01 -0300229 case MEDIA_IOC_ENUM_ENTITIES:
230 ret = media_device_enum_entities(dev,
231 (struct media_entity_desc __user *)arg);
232 break;
233
234 case MEDIA_IOC_ENUM_LINKS:
235 mutex_lock(&dev->graph_mutex);
236 ret = media_device_enum_links(dev,
237 (struct media_links_enum __user *)arg);
238 mutex_unlock(&dev->graph_mutex);
239 break;
240
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300241 case MEDIA_IOC_SETUP_LINK:
242 mutex_lock(&dev->graph_mutex);
243 ret = media_device_setup_link(dev,
244 (struct media_link_desc __user *)arg);
245 mutex_unlock(&dev->graph_mutex);
246 break;
247
Laurent Pinchart140d8812010-08-18 11:41:22 -0300248 default:
249 ret = -ENOIOCTLCMD;
250 }
251
252 return ret;
253}
254
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300255static const struct media_file_operations media_device_fops = {
256 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300257 .open = media_device_open,
258 .ioctl = media_device_ioctl,
259 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300260};
261
262/* -----------------------------------------------------------------------------
263 * sysfs
264 */
265
266static ssize_t show_model(struct device *cd,
267 struct device_attribute *attr, char *buf)
268{
269 struct media_device *mdev = to_media_device(to_media_devnode(cd));
270
271 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
272}
273
274static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
275
276/* -----------------------------------------------------------------------------
277 * Registration/unregistration
278 */
279
280static void media_device_release(struct media_devnode *mdev)
281{
282}
283
284/**
285 * media_device_register - register a media device
286 * @mdev: The media device
287 *
288 * The caller is responsible for initializing the media device before
289 * registration. The following fields must be set:
290 *
291 * - dev must point to the parent device
292 * - model must be filled with the device model name
293 */
294int __must_check media_device_register(struct media_device *mdev)
295{
296 int ret;
297
298 if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
299 return -EINVAL;
300
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300301 mdev->entity_id = 1;
302 INIT_LIST_HEAD(&mdev->entities);
303 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d82010-03-07 15:04:59 -0300304 mutex_init(&mdev->graph_mutex);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300305
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300306 /* Register the device node. */
307 mdev->devnode.fops = &media_device_fops;
308 mdev->devnode.parent = mdev->dev;
309 mdev->devnode.release = media_device_release;
310 ret = media_devnode_register(&mdev->devnode);
311 if (ret < 0)
312 return ret;
313
314 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
315 if (ret < 0) {
316 media_devnode_unregister(&mdev->devnode);
317 return ret;
318 }
319
320 return 0;
321}
322EXPORT_SYMBOL_GPL(media_device_register);
323
324/**
325 * media_device_unregister - unregister a media device
326 * @mdev: The media device
327 *
328 */
329void media_device_unregister(struct media_device *mdev)
330{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300331 struct media_entity *entity;
332 struct media_entity *next;
333
334 list_for_each_entry_safe(entity, next, &mdev->entities, list)
335 media_device_unregister_entity(entity);
336
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300337 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
338 media_devnode_unregister(&mdev->devnode);
339}
340EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300341
342/**
343 * media_device_register_entity - Register an entity with a media device
344 * @mdev: The media device
345 * @entity: The entity
346 */
347int __must_check media_device_register_entity(struct media_device *mdev,
348 struct media_entity *entity)
349{
350 /* Warn if we apparently re-register an entity */
351 WARN_ON(entity->parent != NULL);
352 entity->parent = mdev;
353
354 spin_lock(&mdev->lock);
355 if (entity->id == 0)
356 entity->id = mdev->entity_id++;
357 else
358 mdev->entity_id = max(entity->id + 1, mdev->entity_id);
359 list_add_tail(&entity->list, &mdev->entities);
360 spin_unlock(&mdev->lock);
361
362 return 0;
363}
364EXPORT_SYMBOL_GPL(media_device_register_entity);
365
366/**
367 * media_device_unregister_entity - Unregister an entity
368 * @entity: The entity
369 *
370 * If the entity has never been registered this function will return
371 * immediately.
372 */
373void media_device_unregister_entity(struct media_entity *entity)
374{
375 struct media_device *mdev = entity->parent;
376
377 if (mdev == NULL)
378 return;
379
380 spin_lock(&mdev->lock);
381 list_del(&entity->list);
382 spin_unlock(&mdev->lock);
383 entity->parent = NULL;
384}
385EXPORT_SYMBOL_GPL(media_device_unregister_entity);