blob: 83525ac293284af633763bff6adc9b24f204c6a1 [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
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -030023#include <linux/compat.h>
24#include <linux/export.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030025#include <linux/ioctl.h>
Laurent Pinchart140d8812010-08-18 11:41:22 -030026#include <linux/media.h>
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -030027#include <linux/types.h>
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -030028#include <linux/slab.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030029
30#include <media/media-device.h>
31#include <media/media-devnode.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030032#include <media/media-entity.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030033
Shuah Khane576d60b2015-06-05 17:11:54 -030034#ifdef CONFIG_MEDIA_CONTROLLER
35
Laurent Pinchart140d8812010-08-18 11:41:22 -030036/* -----------------------------------------------------------------------------
37 * Userspace API
38 */
39
40static int media_device_open(struct file *filp)
41{
42 return 0;
43}
44
45static int media_device_close(struct file *filp)
46{
47 return 0;
48}
49
50static int media_device_get_info(struct media_device *dev,
51 struct media_device_info __user *__info)
52{
53 struct media_device_info info;
54
55 memset(&info, 0, sizeof(info));
56
57 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
58 strlcpy(info.model, dev->model, sizeof(info.model));
59 strlcpy(info.serial, dev->serial, sizeof(info.serial));
60 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
61
62 info.media_version = MEDIA_API_VERSION;
63 info.hw_revision = dev->hw_revision;
64 info.driver_version = dev->driver_version;
65
Nicolas THERYb17d3942012-08-07 05:24:59 -030066 if (copy_to_user(__info, &info, sizeof(*__info)))
67 return -EFAULT;
68 return 0;
Laurent Pinchart140d8812010-08-18 11:41:22 -030069}
70
Laurent Pinchart16513332009-12-09 08:40:01 -030071static struct media_entity *find_entity(struct media_device *mdev, u32 id)
72{
73 struct media_entity *entity;
74 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
75
76 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
77
78 spin_lock(&mdev->lock);
79
80 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -030081 if (((media_entity_id(entity) == id) && !next) ||
82 ((media_entity_id(entity) > id) && next)) {
Laurent Pinchart16513332009-12-09 08:40:01 -030083 spin_unlock(&mdev->lock);
84 return entity;
85 }
86 }
87
88 spin_unlock(&mdev->lock);
89
90 return NULL;
91}
92
93static long media_device_enum_entities(struct media_device *mdev,
94 struct media_entity_desc __user *uent)
95{
96 struct media_entity *ent;
97 struct media_entity_desc u_ent;
98
Salva Peiróe6a62342014-04-30 19:48:02 +020099 memset(&u_ent, 0, sizeof(u_ent));
Laurent Pinchart16513332009-12-09 08:40:01 -0300100 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
101 return -EFAULT;
102
103 ent = find_entity(mdev, u_ent.id);
104
105 if (ent == NULL)
106 return -EINVAL;
107
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300108 u_ent.id = media_entity_id(ent);
Laurent Pinchartde8eae32014-07-17 08:52:08 -0300109 if (ent->name)
110 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300111 u_ent.type = ent->function;
Laurent Pinchart16513332009-12-09 08:40:01 -0300112 u_ent.revision = ent->revision;
113 u_ent.flags = ent->flags;
114 u_ent.group_id = ent->group_id;
115 u_ent.pads = ent->num_pads;
116 u_ent.links = ent->num_links - ent->num_backlinks;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300117 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300118 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
119 return -EFAULT;
120 return 0;
121}
122
123static void media_device_kpad_to_upad(const struct media_pad *kpad,
124 struct media_pad_desc *upad)
125{
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300126 upad->entity = media_entity_id(kpad->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300127 upad->index = kpad->index;
128 upad->flags = kpad->flags;
129}
130
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300131static long __media_device_enum_links(struct media_device *mdev,
132 struct media_links_enum *links)
Laurent Pinchart16513332009-12-09 08:40:01 -0300133{
134 struct media_entity *entity;
Laurent Pinchart16513332009-12-09 08:40:01 -0300135
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300136 entity = find_entity(mdev, links->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300137 if (entity == NULL)
138 return -EINVAL;
139
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300140 if (links->pads) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300141 unsigned int p;
142
143 for (p = 0; p < entity->num_pads; p++) {
144 struct media_pad_desc pad;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300145
146 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300147 media_device_kpad_to_upad(&entity->pads[p], &pad);
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300148 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300149 return -EFAULT;
150 }
151 }
152
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300153 if (links->links) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300154 struct media_link *ent_link;
155 struct media_link_desc __user *ulink = links->links;
Laurent Pinchart16513332009-12-09 08:40:01 -0300156
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300157 list_for_each_entry(ent_link, &entity->links, list) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300158 struct media_link_desc link;
159
160 /* Ignore backlinks. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300161 if (ent_link->source->entity != entity)
Laurent Pinchart16513332009-12-09 08:40:01 -0300162 continue;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300163 memset(&link, 0, sizeof(link));
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300164 media_device_kpad_to_upad(ent_link->source,
Laurent Pinchart16513332009-12-09 08:40:01 -0300165 &link.source);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300166 media_device_kpad_to_upad(ent_link->sink,
Laurent Pinchart16513332009-12-09 08:40:01 -0300167 &link.sink);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300168 link.flags = ent_link->flags;
Laurent Pinchart16513332009-12-09 08:40:01 -0300169 if (copy_to_user(ulink, &link, sizeof(*ulink)))
170 return -EFAULT;
171 ulink++;
172 }
173 }
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300174
175 return 0;
176}
177
178static long media_device_enum_links(struct media_device *mdev,
179 struct media_links_enum __user *ulinks)
180{
181 struct media_links_enum links;
182 int rval;
183
184 if (copy_from_user(&links, ulinks, sizeof(links)))
185 return -EFAULT;
186
187 rval = __media_device_enum_links(mdev, &links);
188 if (rval < 0)
189 return rval;
190
Laurent Pinchart16513332009-12-09 08:40:01 -0300191 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
192 return -EFAULT;
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300193
Laurent Pinchart16513332009-12-09 08:40:01 -0300194 return 0;
195}
196
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300197static long media_device_setup_link(struct media_device *mdev,
198 struct media_link_desc __user *_ulink)
199{
200 struct media_link *link = NULL;
201 struct media_link_desc ulink;
202 struct media_entity *source;
203 struct media_entity *sink;
204 int ret;
205
206 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
207 return -EFAULT;
208
209 /* Find the source and sink entities and link.
210 */
211 source = find_entity(mdev, ulink.source.entity);
212 sink = find_entity(mdev, ulink.sink.entity);
213
214 if (source == NULL || sink == NULL)
215 return -EINVAL;
216
217 if (ulink.source.index >= source->num_pads ||
218 ulink.sink.index >= sink->num_pads)
219 return -EINVAL;
220
221 link = media_entity_find_link(&source->pads[ulink.source.index],
222 &sink->pads[ulink.sink.index]);
223 if (link == NULL)
224 return -EINVAL;
225
226 /* Setup the link on both entities. */
227 ret = __media_entity_setup_link(link, ulink.flags);
228
229 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
230 return -EFAULT;
231
232 return ret;
233}
234
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300235static long __media_device_get_topology(struct media_device *mdev,
236 struct media_v2_topology *topo)
237{
238 struct media_entity *entity;
239 struct media_interface *intf;
240 struct media_pad *pad;
241 struct media_link *link;
242 struct media_v2_entity uentity;
243 struct media_v2_interface uintf;
244 struct media_v2_pad upad;
245 struct media_v2_link ulink;
246 int ret = 0, i;
247
248 topo->topology_version = mdev->topology_version;
249
250 /* Get entities and number of entities */
251 i = 0;
252 media_device_for_each_entity(entity, mdev) {
253 i++;
254
255 if (ret || !topo->entities)
256 continue;
257
258 if (i > topo->num_entities) {
259 ret = -ENOSPC;
260 continue;
261 }
262
263 /* Copy fields to userspace struct if not error */
264 memset(&uentity, 0, sizeof(uentity));
265 uentity.id = entity->graph_obj.id;
Mauro Carvalho Chehabd87cdb82015-09-06 10:59:08 -0300266 uentity.function = entity->function;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300267 strncpy(uentity.name, entity->name,
268 sizeof(uentity.name));
269
270 if (copy_to_user(&topo->entities[i - 1], &uentity, sizeof(uentity)))
271 ret = -EFAULT;
272 }
273 topo->num_entities = i;
274
275 /* Get interfaces and number of interfaces */
276 i = 0;
277 media_device_for_each_intf(intf, mdev) {
278 i++;
279
280 if (ret || !topo->interfaces)
281 continue;
282
283 if (i > topo->num_interfaces) {
284 ret = -ENOSPC;
285 continue;
286 }
287
288 memset(&uintf, 0, sizeof(uintf));
289
290 /* Copy intf fields to userspace struct */
291 uintf.id = intf->graph_obj.id;
292 uintf.intf_type = intf->type;
293 uintf.flags = intf->flags;
294
295 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
296 struct media_intf_devnode *devnode;
297
298 devnode = intf_to_devnode(intf);
299
300 uintf.devnode.major = devnode->major;
301 uintf.devnode.minor = devnode->minor;
302 }
303
304 if (copy_to_user(&topo->interfaces[i - 1], &uintf, sizeof(uintf)))
305 ret = -EFAULT;
306 }
307 topo->num_interfaces = i;
308
309 /* Get pads and number of pads */
310 i = 0;
311 media_device_for_each_pad(pad, mdev) {
312 i++;
313
314 if (ret || !topo->pads)
315 continue;
316
317 if (i > topo->num_pads) {
318 ret = -ENOSPC;
319 continue;
320 }
321
322 memset(&upad, 0, sizeof(upad));
323
324 /* Copy pad fields to userspace struct */
325 upad.id = pad->graph_obj.id;
326 upad.entity_id = pad->entity->graph_obj.id;
327 upad.flags = pad->flags;
328
329 if (copy_to_user(&topo->pads[i - 1], &upad, sizeof(upad)))
330 ret = -EFAULT;
331 }
332 topo->num_pads = i;
333
334 /* Get links and number of links */
335 i = 0;
336 media_device_for_each_link(link, mdev) {
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300337 if (link->is_backlink)
338 continue;
339
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300340 i++;
341
342 if (ret || !topo->links)
343 continue;
344
345 if (i > topo->num_links) {
346 ret = -ENOSPC;
347 continue;
348 }
349
350 memset(&ulink, 0, sizeof(ulink));
351
352 /* Copy link fields to userspace struct */
353 ulink.id = link->graph_obj.id;
354 ulink.source_id = link->gobj0->id;
355 ulink.sink_id = link->gobj1->id;
356 ulink.flags = link->flags;
357
358 if (media_type(link->gobj0) != MEDIA_GRAPH_PAD)
359 ulink.flags |= MEDIA_LNK_FL_INTERFACE_LINK;
360
361 if (copy_to_user(&topo->links[i - 1], &ulink, sizeof(ulink)))
362 ret = -EFAULT;
363 }
364 topo->num_links = i;
365
366 return ret;
367}
368
369static long media_device_get_topology(struct media_device *mdev,
370 struct media_v2_topology __user *utopo)
371{
372 struct media_v2_topology ktopo;
373 int ret;
374
375 ret = copy_from_user(&ktopo, utopo, sizeof(ktopo));
376
377 if (ret < 0)
378 return ret;
379
380 ret = __media_device_get_topology(mdev, &ktopo);
381 if (ret < 0)
382 return ret;
383
384 ret = copy_to_user(utopo, &ktopo, sizeof(*utopo));
385
386 return ret;
387}
388
Laurent Pinchart140d8812010-08-18 11:41:22 -0300389static long media_device_ioctl(struct file *filp, unsigned int cmd,
390 unsigned long arg)
391{
392 struct media_devnode *devnode = media_devnode_data(filp);
393 struct media_device *dev = to_media_device(devnode);
394 long ret;
395
396 switch (cmd) {
397 case MEDIA_IOC_DEVICE_INFO:
398 ret = media_device_get_info(dev,
399 (struct media_device_info __user *)arg);
400 break;
401
Laurent Pinchart16513332009-12-09 08:40:01 -0300402 case MEDIA_IOC_ENUM_ENTITIES:
403 ret = media_device_enum_entities(dev,
404 (struct media_entity_desc __user *)arg);
405 break;
406
407 case MEDIA_IOC_ENUM_LINKS:
408 mutex_lock(&dev->graph_mutex);
409 ret = media_device_enum_links(dev,
410 (struct media_links_enum __user *)arg);
411 mutex_unlock(&dev->graph_mutex);
412 break;
413
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300414 case MEDIA_IOC_SETUP_LINK:
415 mutex_lock(&dev->graph_mutex);
416 ret = media_device_setup_link(dev,
417 (struct media_link_desc __user *)arg);
418 mutex_unlock(&dev->graph_mutex);
419 break;
420
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300421 case MEDIA_IOC_G_TOPOLOGY:
422 mutex_lock(&dev->graph_mutex);
423 ret = media_device_get_topology(dev,
424 (struct media_v2_topology __user *)arg);
425 mutex_unlock(&dev->graph_mutex);
426 break;
427
Laurent Pinchart140d8812010-08-18 11:41:22 -0300428 default:
429 ret = -ENOIOCTLCMD;
430 }
431
432 return ret;
433}
434
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300435#ifdef CONFIG_COMPAT
436
437struct media_links_enum32 {
438 __u32 entity;
439 compat_uptr_t pads; /* struct media_pad_desc * */
440 compat_uptr_t links; /* struct media_link_desc * */
441 __u32 reserved[4];
442};
443
444static long media_device_enum_links32(struct media_device *mdev,
445 struct media_links_enum32 __user *ulinks)
446{
447 struct media_links_enum links;
448 compat_uptr_t pads_ptr, links_ptr;
449
450 memset(&links, 0, sizeof(links));
451
452 if (get_user(links.entity, &ulinks->entity)
453 || get_user(pads_ptr, &ulinks->pads)
454 || get_user(links_ptr, &ulinks->links))
455 return -EFAULT;
456
457 links.pads = compat_ptr(pads_ptr);
458 links.links = compat_ptr(links_ptr);
459
460 return __media_device_enum_links(mdev, &links);
461}
462
463#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
464
465static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
466 unsigned long arg)
467{
468 struct media_devnode *devnode = media_devnode_data(filp);
469 struct media_device *dev = to_media_device(devnode);
470 long ret;
471
472 switch (cmd) {
473 case MEDIA_IOC_DEVICE_INFO:
474 case MEDIA_IOC_ENUM_ENTITIES:
475 case MEDIA_IOC_SETUP_LINK:
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300476 case MEDIA_IOC_G_TOPOLOGY:
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300477 return media_device_ioctl(filp, cmd, arg);
478
479 case MEDIA_IOC_ENUM_LINKS32:
480 mutex_lock(&dev->graph_mutex);
481 ret = media_device_enum_links32(dev,
482 (struct media_links_enum32 __user *)arg);
483 mutex_unlock(&dev->graph_mutex);
484 break;
485
486 default:
487 ret = -ENOIOCTLCMD;
488 }
489
490 return ret;
491}
492#endif /* CONFIG_COMPAT */
493
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300494static const struct media_file_operations media_device_fops = {
495 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300496 .open = media_device_open,
497 .ioctl = media_device_ioctl,
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300498#ifdef CONFIG_COMPAT
499 .compat_ioctl = media_device_compat_ioctl,
500#endif /* CONFIG_COMPAT */
Laurent Pinchart140d8812010-08-18 11:41:22 -0300501 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300502};
503
504/* -----------------------------------------------------------------------------
505 * sysfs
506 */
507
508static ssize_t show_model(struct device *cd,
509 struct device_attribute *attr, char *buf)
510{
511 struct media_device *mdev = to_media_device(to_media_devnode(cd));
512
513 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
514}
515
516static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
517
518/* -----------------------------------------------------------------------------
519 * Registration/unregistration
520 */
521
522static void media_device_release(struct media_devnode *mdev)
523{
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300524 dev_dbg(mdev->parent, "Media device released\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300525}
526
527/**
528 * media_device_register - register a media device
529 * @mdev: The media device
530 *
531 * The caller is responsible for initializing the media device before
532 * registration. The following fields must be set:
533 *
534 * - dev must point to the parent device
535 * - model must be filled with the device model name
536 */
Sakari Ailus85de7212013-12-12 12:38:17 -0300537int __must_check __media_device_register(struct media_device *mdev,
538 struct module *owner)
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300539{
540 int ret;
541
542 if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
543 return -EINVAL;
544
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300545 INIT_LIST_HEAD(&mdev->entities);
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300546 INIT_LIST_HEAD(&mdev->interfaces);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300547 INIT_LIST_HEAD(&mdev->pads);
548 INIT_LIST_HEAD(&mdev->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300549 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300550 mutex_init(&mdev->graph_mutex);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300551
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300552 /* Register the device node. */
553 mdev->devnode.fops = &media_device_fops;
554 mdev->devnode.parent = mdev->dev;
555 mdev->devnode.release = media_device_release;
Sakari Ailus85de7212013-12-12 12:38:17 -0300556 ret = media_devnode_register(&mdev->devnode, owner);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300557 if (ret < 0)
558 return ret;
559
560 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
561 if (ret < 0) {
562 media_devnode_unregister(&mdev->devnode);
563 return ret;
564 }
565
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300566 dev_dbg(mdev->dev, "Media device registered\n");
567
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300568 return 0;
569}
Sakari Ailus85de7212013-12-12 12:38:17 -0300570EXPORT_SYMBOL_GPL(__media_device_register);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300571
572/**
573 * media_device_unregister - unregister a media device
574 * @mdev: The media device
575 *
576 */
577void media_device_unregister(struct media_device *mdev)
578{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300579 struct media_entity *entity;
580 struct media_entity *next;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300581 struct media_interface *intf, *tmp_intf;
582
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300583 /* Remove all entities from the media device */
584 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
585 media_device_unregister_entity(entity);
586
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300587 /* Remove all interfaces from the media device */
588 spin_lock(&mdev->lock);
589 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
590 graph_obj.list) {
591 __media_remove_intf_links(intf);
592 media_gobj_remove(&intf->graph_obj);
593 kfree(intf);
594 }
595 spin_unlock(&mdev->lock);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300596
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300597 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
598 media_devnode_unregister(&mdev->devnode);
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300599
600 dev_dbg(mdev->dev, "Media device unregistered\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300601}
602EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300603
604/**
605 * media_device_register_entity - Register an entity with a media device
606 * @mdev: The media device
607 * @entity: The entity
608 */
609int __must_check media_device_register_entity(struct media_device *mdev,
610 struct media_entity *entity)
611{
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300612 int i;
613
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300614 if (entity->function == MEDIA_ENT_T_V4L2_SUBDEV_UNKNOWN ||
615 entity->function == MEDIA_ENT_T_UNKNOWN)
Mauro Carvalho Chehabb50bde42015-05-07 22:12:38 -0300616 dev_warn(mdev->dev,
617 "Entity type for entity %s was not initialized!\n",
618 entity->name);
619
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300620 /* Warn if we apparently re-register an entity */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300621 WARN_ON(entity->graph_obj.mdev != NULL);
622 entity->graph_obj.mdev = mdev;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300623 INIT_LIST_HEAD(&entity->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300624
625 spin_lock(&mdev->lock);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300626 /* Initialize media_gobj embedded at the entity */
627 media_gobj_init(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300628
629 /* Initialize objects at the pads */
630 for (i = 0; i < entity->num_pads; i++)
631 media_gobj_init(mdev, MEDIA_GRAPH_PAD,
632 &entity->pads[i].graph_obj);
633
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300634 spin_unlock(&mdev->lock);
635
636 return 0;
637}
638EXPORT_SYMBOL_GPL(media_device_register_entity);
639
640/**
641 * media_device_unregister_entity - Unregister an entity
642 * @entity: The entity
643 *
644 * If the entity has never been registered this function will return
645 * immediately.
646 */
647void media_device_unregister_entity(struct media_entity *entity)
648{
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300649 int i;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300650 struct media_device *mdev = entity->graph_obj.mdev;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300651 struct media_link *link, *tmp;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300652 struct media_interface *intf;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300653
654 if (mdev == NULL)
655 return;
656
657 spin_lock(&mdev->lock);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300658
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300659 /* Remove all interface links pointing to this entity */
660 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
661 list_for_each_entry_safe(link, tmp, &intf->links, list) {
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300662 if (link->entity == entity)
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300663 __media_remove_intf_link(link);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300664 }
665 }
666
667 /* Remove all data links that belong to this entity */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300668 __media_entity_remove_links(entity);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300669
670 /* Remove all pads that belong to this entity */
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300671 for (i = 0; i < entity->num_pads; i++)
672 media_gobj_remove(&entity->pads[i].graph_obj);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300673
674 /* Remove the entity */
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300675 media_gobj_remove(&entity->graph_obj);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300676
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300677 spin_unlock(&mdev->lock);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300678 entity->graph_obj.mdev = NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300679}
680EXPORT_SYMBOL_GPL(media_device_unregister_entity);
Shuah Khand062f912015-06-03 12:12:53 -0300681
682static void media_device_release_devres(struct device *dev, void *res)
683{
684}
685
686/*
687 * media_device_get_devres() - get media device as device resource
688 * creates if one doesn't exist
689*/
690struct media_device *media_device_get_devres(struct device *dev)
691{
692 struct media_device *mdev;
693
694 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
695 if (mdev)
696 return mdev;
697
698 mdev = devres_alloc(media_device_release_devres,
699 sizeof(struct media_device), GFP_KERNEL);
700 if (!mdev)
701 return NULL;
702 return devres_get(dev, mdev, NULL, NULL);
703}
704EXPORT_SYMBOL_GPL(media_device_get_devres);
705
706/*
707 * media_device_find_devres() - find media device as device resource
708*/
709struct media_device *media_device_find_devres(struct device *dev)
710{
711 return devres_find(dev, media_device_release_devres, NULL, NULL);
712}
713EXPORT_SYMBOL_GPL(media_device_find_devres);
Shuah Khane576d60b2015-06-05 17:11:54 -0300714
715#endif /* CONFIG_MEDIA_CONTROLLER */