[media] media-device: map new functions into old types for legacy API
[deliverable/linux.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
b2cd2744
MCC
23/* We need to access legacy defines from linux/media.h */
24#define __NEED_MEDIA_LEGACY_API
25
b0a1f2a8
SA
26#include <linux/compat.h>
27#include <linux/export.h>
665faa97 28#include <linux/idr.h>
176fb0d1 29#include <linux/ioctl.h>
140d8816 30#include <linux/media.h>
57208e5e 31#include <linux/slab.h>
497e80cd 32#include <linux/types.h>
176fb0d1
LP
33
34#include <media/media-device.h>
35#include <media/media-devnode.h>
53e269c1 36#include <media/media-entity.h>
176fb0d1 37
e576d60b
SK
38#ifdef CONFIG_MEDIA_CONTROLLER
39
140d8816
LP
40/* -----------------------------------------------------------------------------
41 * Userspace API
42 */
43
44static int media_device_open(struct file *filp)
45{
46 return 0;
47}
48
49static int media_device_close(struct file *filp)
50{
51 return 0;
52}
53
54static int media_device_get_info(struct media_device *dev,
55 struct media_device_info __user *__info)
56{
57 struct media_device_info info;
58
59 memset(&info, 0, sizeof(info));
60
61 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
62 strlcpy(info.model, dev->model, sizeof(info.model));
63 strlcpy(info.serial, dev->serial, sizeof(info.serial));
64 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
65
66 info.media_version = MEDIA_API_VERSION;
67 info.hw_revision = dev->hw_revision;
68 info.driver_version = dev->driver_version;
69
b17d3945
NT
70 if (copy_to_user(__info, &info, sizeof(*__info)))
71 return -EFAULT;
72 return 0;
140d8816
LP
73}
74
1651333b
LP
75static struct media_entity *find_entity(struct media_device *mdev, u32 id)
76{
77 struct media_entity *entity;
78 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
79
80 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
81
82 spin_lock(&mdev->lock);
83
84 media_device_for_each_entity(entity, mdev) {
fa762394
MCC
85 if (((media_entity_id(entity) == id) && !next) ||
86 ((media_entity_id(entity) > id) && next)) {
1651333b
LP
87 spin_unlock(&mdev->lock);
88 return entity;
89 }
90 }
91
92 spin_unlock(&mdev->lock);
93
94 return NULL;
95}
96
97static long media_device_enum_entities(struct media_device *mdev,
98 struct media_entity_desc __user *uent)
99{
100 struct media_entity *ent;
101 struct media_entity_desc u_ent;
102
e6a62346 103 memset(&u_ent, 0, sizeof(u_ent));
1651333b
LP
104 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
105 return -EFAULT;
106
107 ent = find_entity(mdev, u_ent.id);
108
109 if (ent == NULL)
110 return -EINVAL;
111
fa762394 112 u_ent.id = media_entity_id(ent);
de8eae36
LP
113 if (ent->name)
114 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
0e576b76 115 u_ent.type = ent->function;
8ed8c88c 116 u_ent.revision = 0; /* Unused */
1651333b 117 u_ent.flags = ent->flags;
8ed8c88c 118 u_ent.group_id = 0; /* Unused */
1651333b
LP
119 u_ent.pads = ent->num_pads;
120 u_ent.links = ent->num_links - ent->num_backlinks;
b2cd2744
MCC
121
122 /*
123 * Workaround for a bug at media-ctl <= v1.10 that makes it to
124 * do the wrong thing if the entity function doesn't belong to
125 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
126 * Ranges.
127 *
128 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
129 * or, otherwise, will be silently ignored by media-ctl when
130 * printing the graphviz diagram. So, map them into the devnode
131 * old range.
132 */
133 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
134 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
135 if (is_media_entity_v4l2_subdev(ent))
136 u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
137 else if (ent->function != MEDIA_ENT_F_IO_V4L)
138 u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
139 }
140
fa5034c6 141 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
1651333b
LP
142 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
143 return -EFAULT;
144 return 0;
145}
146
147static void media_device_kpad_to_upad(const struct media_pad *kpad,
148 struct media_pad_desc *upad)
149{
fa762394 150 upad->entity = media_entity_id(kpad->entity);
1651333b
LP
151 upad->index = kpad->index;
152 upad->flags = kpad->flags;
153}
154
b0a1f2a8
SA
155static long __media_device_enum_links(struct media_device *mdev,
156 struct media_links_enum *links)
1651333b
LP
157{
158 struct media_entity *entity;
1651333b 159
b0a1f2a8 160 entity = find_entity(mdev, links->entity);
1651333b
LP
161 if (entity == NULL)
162 return -EINVAL;
163
b0a1f2a8 164 if (links->pads) {
1651333b
LP
165 unsigned int p;
166
167 for (p = 0; p < entity->num_pads; p++) {
168 struct media_pad_desc pad;
c88e739b
DC
169
170 memset(&pad, 0, sizeof(pad));
1651333b 171 media_device_kpad_to_upad(&entity->pads[p], &pad);
b0a1f2a8 172 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
1651333b
LP
173 return -EFAULT;
174 }
175 }
176
b0a1f2a8 177 if (links->links) {
b83e2508
MCC
178 struct media_link *link;
179 struct media_link_desc __user *ulink_desc = links->links;
1651333b 180
b83e2508
MCC
181 list_for_each_entry(link, &entity->links, list) {
182 struct media_link_desc klink_desc;
1651333b
LP
183
184 /* Ignore backlinks. */
b83e2508 185 if (link->source->entity != entity)
1651333b 186 continue;
b83e2508
MCC
187 memset(&klink_desc, 0, sizeof(klink_desc));
188 media_device_kpad_to_upad(link->source,
189 &klink_desc.source);
190 media_device_kpad_to_upad(link->sink,
191 &klink_desc.sink);
192 klink_desc.flags = link->flags;
193 if (copy_to_user(ulink_desc, &klink_desc,
194 sizeof(*ulink_desc)))
1651333b 195 return -EFAULT;
b83e2508 196 ulink_desc++;
1651333b
LP
197 }
198 }
b0a1f2a8
SA
199
200 return 0;
201}
202
203static long media_device_enum_links(struct media_device *mdev,
204 struct media_links_enum __user *ulinks)
205{
206 struct media_links_enum links;
207 int rval;
208
209 if (copy_from_user(&links, ulinks, sizeof(links)))
210 return -EFAULT;
211
212 rval = __media_device_enum_links(mdev, &links);
213 if (rval < 0)
214 return rval;
215
1651333b
LP
216 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
217 return -EFAULT;
b0a1f2a8 218
1651333b
LP
219 return 0;
220}
221
97548ed4
LP
222static long media_device_setup_link(struct media_device *mdev,
223 struct media_link_desc __user *_ulink)
224{
225 struct media_link *link = NULL;
226 struct media_link_desc ulink;
227 struct media_entity *source;
228 struct media_entity *sink;
229 int ret;
230
231 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
232 return -EFAULT;
233
234 /* Find the source and sink entities and link.
235 */
236 source = find_entity(mdev, ulink.source.entity);
237 sink = find_entity(mdev, ulink.sink.entity);
238
239 if (source == NULL || sink == NULL)
240 return -EINVAL;
241
242 if (ulink.source.index >= source->num_pads ||
243 ulink.sink.index >= sink->num_pads)
244 return -EINVAL;
245
246 link = media_entity_find_link(&source->pads[ulink.source.index],
247 &sink->pads[ulink.sink.index]);
248 if (link == NULL)
249 return -EINVAL;
250
251 /* Setup the link on both entities. */
252 ret = __media_entity_setup_link(link, ulink.flags);
253
254 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
255 return -EFAULT;
256
257 return ret;
258}
259
be0270ec 260#if 0 /* Let's postpone it to Kernel 4.6 */
8309f47c
MCC
261static long __media_device_get_topology(struct media_device *mdev,
262 struct media_v2_topology *topo)
263{
264 struct media_entity *entity;
265 struct media_interface *intf;
266 struct media_pad *pad;
267 struct media_link *link;
b3b7a9f1
MCC
268 struct media_v2_entity kentity, *uentity;
269 struct media_v2_interface kintf, *uintf;
270 struct media_v2_pad kpad, *upad;
271 struct media_v2_link klink, *ulink;
9033b1a4
MCC
272 unsigned int i;
273 int ret = 0;
8309f47c
MCC
274
275 topo->topology_version = mdev->topology_version;
276
277 /* Get entities and number of entities */
278 i = 0;
b3b7a9f1 279 uentity = media_get_uptr(topo->ptr_entities);
8309f47c
MCC
280 media_device_for_each_entity(entity, mdev) {
281 i++;
b3b7a9f1 282 if (ret || !uentity)
8309f47c
MCC
283 continue;
284
285 if (i > topo->num_entities) {
286 ret = -ENOSPC;
287 continue;
288 }
289
290 /* Copy fields to userspace struct if not error */
b3b7a9f1
MCC
291 memset(&kentity, 0, sizeof(kentity));
292 kentity.id = entity->graph_obj.id;
293 kentity.function = entity->function;
294 strncpy(kentity.name, entity->name,
295 sizeof(kentity.name));
8309f47c 296
b3b7a9f1 297 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
8309f47c 298 ret = -EFAULT;
b3b7a9f1 299 uentity++;
8309f47c
MCC
300 }
301 topo->num_entities = i;
302
303 /* Get interfaces and number of interfaces */
304 i = 0;
b3b7a9f1 305 uintf = media_get_uptr(topo->ptr_interfaces);
8309f47c
MCC
306 media_device_for_each_intf(intf, mdev) {
307 i++;
b3b7a9f1 308 if (ret || !uintf)
8309f47c
MCC
309 continue;
310
311 if (i > topo->num_interfaces) {
312 ret = -ENOSPC;
313 continue;
314 }
315
b3b7a9f1 316 memset(&kintf, 0, sizeof(kintf));
8309f47c
MCC
317
318 /* Copy intf fields to userspace struct */
b3b7a9f1
MCC
319 kintf.id = intf->graph_obj.id;
320 kintf.intf_type = intf->type;
321 kintf.flags = intf->flags;
8309f47c
MCC
322
323 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
324 struct media_intf_devnode *devnode;
325
326 devnode = intf_to_devnode(intf);
327
b3b7a9f1
MCC
328 kintf.devnode.major = devnode->major;
329 kintf.devnode.minor = devnode->minor;
8309f47c
MCC
330 }
331
b3b7a9f1 332 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
8309f47c 333 ret = -EFAULT;
b3b7a9f1 334 uintf++;
8309f47c
MCC
335 }
336 topo->num_interfaces = i;
337
338 /* Get pads and number of pads */
339 i = 0;
b3b7a9f1 340 upad = media_get_uptr(topo->ptr_pads);
8309f47c
MCC
341 media_device_for_each_pad(pad, mdev) {
342 i++;
b3b7a9f1 343 if (ret || !upad)
8309f47c
MCC
344 continue;
345
346 if (i > topo->num_pads) {
347 ret = -ENOSPC;
348 continue;
349 }
350
b3b7a9f1 351 memset(&kpad, 0, sizeof(kpad));
8309f47c
MCC
352
353 /* Copy pad fields to userspace struct */
b3b7a9f1
MCC
354 kpad.id = pad->graph_obj.id;
355 kpad.entity_id = pad->entity->graph_obj.id;
356 kpad.flags = pad->flags;
8309f47c 357
b3b7a9f1 358 if (copy_to_user(upad, &kpad, sizeof(kpad)))
8309f47c 359 ret = -EFAULT;
b3b7a9f1 360 upad++;
8309f47c
MCC
361 }
362 topo->num_pads = i;
363
364 /* Get links and number of links */
365 i = 0;
b3b7a9f1 366 ulink = media_get_uptr(topo->ptr_links);
8309f47c 367 media_device_for_each_link(link, mdev) {
39d1ebc6
MCC
368 if (link->is_backlink)
369 continue;
370
8309f47c
MCC
371 i++;
372
b3b7a9f1 373 if (ret || !ulink)
8309f47c
MCC
374 continue;
375
376 if (i > topo->num_links) {
377 ret = -ENOSPC;
378 continue;
379 }
380
b3b7a9f1 381 memset(&klink, 0, sizeof(klink));
8309f47c
MCC
382
383 /* Copy link fields to userspace struct */
b3b7a9f1
MCC
384 klink.id = link->graph_obj.id;
385 klink.source_id = link->gobj0->id;
386 klink.sink_id = link->gobj1->id;
387 klink.flags = link->flags;
8309f47c 388
b3b7a9f1 389 if (copy_to_user(ulink, &klink, sizeof(klink)))
8309f47c 390 ret = -EFAULT;
b3b7a9f1 391 ulink++;
8309f47c
MCC
392 }
393 topo->num_links = i;
394
395 return ret;
396}
397
398static long media_device_get_topology(struct media_device *mdev,
399 struct media_v2_topology __user *utopo)
400{
401 struct media_v2_topology ktopo;
402 int ret;
403
a5c82e56
DC
404 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
405 return -EFAULT;
8309f47c
MCC
406
407 ret = __media_device_get_topology(mdev, &ktopo);
408 if (ret < 0)
409 return ret;
410
a5c82e56
DC
411 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
412 return -EFAULT;
8309f47c 413
a5c82e56 414 return 0;
8309f47c 415}
be0270ec 416#endif
8309f47c 417
140d8816
LP
418static long media_device_ioctl(struct file *filp, unsigned int cmd,
419 unsigned long arg)
420{
421 struct media_devnode *devnode = media_devnode_data(filp);
422 struct media_device *dev = to_media_device(devnode);
423 long ret;
424
425 switch (cmd) {
426 case MEDIA_IOC_DEVICE_INFO:
427 ret = media_device_get_info(dev,
428 (struct media_device_info __user *)arg);
429 break;
430
1651333b
LP
431 case MEDIA_IOC_ENUM_ENTITIES:
432 ret = media_device_enum_entities(dev,
433 (struct media_entity_desc __user *)arg);
434 break;
435
436 case MEDIA_IOC_ENUM_LINKS:
437 mutex_lock(&dev->graph_mutex);
438 ret = media_device_enum_links(dev,
439 (struct media_links_enum __user *)arg);
440 mutex_unlock(&dev->graph_mutex);
441 break;
442
97548ed4
LP
443 case MEDIA_IOC_SETUP_LINK:
444 mutex_lock(&dev->graph_mutex);
445 ret = media_device_setup_link(dev,
446 (struct media_link_desc __user *)arg);
447 mutex_unlock(&dev->graph_mutex);
448 break;
449
be0270ec 450#if 0 /* Let's postpone it to Kernel 4.6 */
8309f47c
MCC
451 case MEDIA_IOC_G_TOPOLOGY:
452 mutex_lock(&dev->graph_mutex);
453 ret = media_device_get_topology(dev,
454 (struct media_v2_topology __user *)arg);
455 mutex_unlock(&dev->graph_mutex);
456 break;
be0270ec 457#endif
140d8816
LP
458 default:
459 ret = -ENOIOCTLCMD;
460 }
461
462 return ret;
463}
464
b0a1f2a8
SA
465#ifdef CONFIG_COMPAT
466
467struct media_links_enum32 {
468 __u32 entity;
469 compat_uptr_t pads; /* struct media_pad_desc * */
470 compat_uptr_t links; /* struct media_link_desc * */
471 __u32 reserved[4];
472};
473
474static long media_device_enum_links32(struct media_device *mdev,
475 struct media_links_enum32 __user *ulinks)
476{
477 struct media_links_enum links;
478 compat_uptr_t pads_ptr, links_ptr;
479
480 memset(&links, 0, sizeof(links));
481
482 if (get_user(links.entity, &ulinks->entity)
483 || get_user(pads_ptr, &ulinks->pads)
484 || get_user(links_ptr, &ulinks->links))
485 return -EFAULT;
486
487 links.pads = compat_ptr(pads_ptr);
488 links.links = compat_ptr(links_ptr);
489
490 return __media_device_enum_links(mdev, &links);
491}
492
493#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
494
495static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
496 unsigned long arg)
497{
498 struct media_devnode *devnode = media_devnode_data(filp);
499 struct media_device *dev = to_media_device(devnode);
500 long ret;
501
502 switch (cmd) {
503 case MEDIA_IOC_DEVICE_INFO:
504 case MEDIA_IOC_ENUM_ENTITIES:
505 case MEDIA_IOC_SETUP_LINK:
be0270ec 506#if 0 /* Let's postpone it to Kernel 4.6 */
8309f47c 507 case MEDIA_IOC_G_TOPOLOGY:
be0270ec 508#endif
b0a1f2a8
SA
509 return media_device_ioctl(filp, cmd, arg);
510
511 case MEDIA_IOC_ENUM_LINKS32:
512 mutex_lock(&dev->graph_mutex);
513 ret = media_device_enum_links32(dev,
514 (struct media_links_enum32 __user *)arg);
515 mutex_unlock(&dev->graph_mutex);
516 break;
517
518 default:
519 ret = -ENOIOCTLCMD;
520 }
521
522 return ret;
523}
524#endif /* CONFIG_COMPAT */
525
176fb0d1
LP
526static const struct media_file_operations media_device_fops = {
527 .owner = THIS_MODULE,
140d8816
LP
528 .open = media_device_open,
529 .ioctl = media_device_ioctl,
b0a1f2a8
SA
530#ifdef CONFIG_COMPAT
531 .compat_ioctl = media_device_compat_ioctl,
532#endif /* CONFIG_COMPAT */
140d8816 533 .release = media_device_close,
176fb0d1
LP
534};
535
536/* -----------------------------------------------------------------------------
537 * sysfs
538 */
539
540static ssize_t show_model(struct device *cd,
541 struct device_attribute *attr, char *buf)
542{
543 struct media_device *mdev = to_media_device(to_media_devnode(cd));
544
545 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
546}
547
548static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
549
550/* -----------------------------------------------------------------------------
551 * Registration/unregistration
552 */
553
554static void media_device_release(struct media_devnode *mdev)
555{
8f5a3188 556 dev_dbg(mdev->parent, "Media device released\n");
176fb0d1
LP
557}
558
b2ed8af9
MCC
559/**
560 * media_device_register_entity - Register an entity with a media device
561 * @mdev: The media device
562 * @entity: The entity
563 */
564int __must_check media_device_register_entity(struct media_device *mdev,
565 struct media_entity *entity)
566{
567 unsigned int i;
ba0dd729 568 int ret;
b2ed8af9
MCC
569
570 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
571 entity->function == MEDIA_ENT_F_UNKNOWN)
572 dev_warn(mdev->dev,
573 "Entity type for entity %s was not initialized!\n",
574 entity->name);
575
576 /* Warn if we apparently re-register an entity */
577 WARN_ON(entity->graph_obj.mdev != NULL);
578 entity->graph_obj.mdev = mdev;
579 INIT_LIST_HEAD(&entity->links);
580 entity->num_links = 0;
581 entity->num_backlinks = 0;
582
ba0dd729
MCC
583 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
584 return -ENOMEM;
585
b2ed8af9 586 spin_lock(&mdev->lock);
665faa97 587
ba0dd729
MCC
588 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
589 &entity->internal_idx);
590 if (ret < 0) {
665faa97 591 spin_unlock(&mdev->lock);
ba0dd729 592 return ret;
665faa97
SA
593 }
594
595 mdev->entity_internal_idx_max =
596 max(mdev->entity_internal_idx_max, entity->internal_idx);
597
b2ed8af9
MCC
598 /* Initialize media_gobj embedded at the entity */
599 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
600
601 /* Initialize objects at the pads */
602 for (i = 0; i < entity->num_pads; i++)
603 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
604 &entity->pads[i].graph_obj);
605
606 spin_unlock(&mdev->lock);
607
608 return 0;
609}
610EXPORT_SYMBOL_GPL(media_device_register_entity);
611
821ed366 612static void __media_device_unregister_entity(struct media_entity *entity)
b2ed8af9
MCC
613{
614 struct media_device *mdev = entity->graph_obj.mdev;
615 struct media_link *link, *tmp;
616 struct media_interface *intf;
617 unsigned int i;
618
665faa97
SA
619 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
620
b2ed8af9
MCC
621 /* Remove all interface links pointing to this entity */
622 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
623 list_for_each_entry_safe(link, tmp, &intf->links, list) {
624 if (link->entity == entity)
625 __media_remove_intf_link(link);
626 }
627 }
628
629 /* Remove all data links that belong to this entity */
630 __media_entity_remove_links(entity);
631
632 /* Remove all pads that belong to this entity */
633 for (i = 0; i < entity->num_pads; i++)
634 media_gobj_destroy(&entity->pads[i].graph_obj);
635
636 /* Remove the entity */
637 media_gobj_destroy(&entity->graph_obj);
638
b2ed8af9
MCC
639 entity->graph_obj.mdev = NULL;
640}
821ed366
MCC
641
642void media_device_unregister_entity(struct media_entity *entity)
643{
644 struct media_device *mdev = entity->graph_obj.mdev;
645
646 if (mdev == NULL)
647 return;
648
649 spin_lock(&mdev->lock);
650 __media_device_unregister_entity(entity);
651 spin_unlock(&mdev->lock);
652}
b2ed8af9
MCC
653EXPORT_SYMBOL_GPL(media_device_unregister_entity);
654
176fb0d1 655/**
9832e155 656 * media_device_init() - initialize a media device
176fb0d1
LP
657 * @mdev: The media device
658 *
659 * The caller is responsible for initializing the media device before
660 * registration. The following fields must be set:
661 *
662 * - dev must point to the parent device
663 * - model must be filled with the device model name
664 */
9832e155 665void media_device_init(struct media_device *mdev)
176fb0d1 666{
53e269c1 667 INIT_LIST_HEAD(&mdev->entities);
57cf79b7 668 INIT_LIST_HEAD(&mdev->interfaces);
9155d859
MCC
669 INIT_LIST_HEAD(&mdev->pads);
670 INIT_LIST_HEAD(&mdev->links);
53e269c1 671 spin_lock_init(&mdev->lock);
503c3d82 672 mutex_init(&mdev->graph_mutex);
665faa97 673 ida_init(&mdev->entity_internal_idx);
53e269c1 674
9832e155
JMC
675 dev_dbg(mdev->dev, "Media device initialized\n");
676}
677EXPORT_SYMBOL_GPL(media_device_init);
678
9832e155
JMC
679void media_device_cleanup(struct media_device *mdev)
680{
665faa97
SA
681 ida_destroy(&mdev->entity_internal_idx);
682 mdev->entity_internal_idx_max = 0;
9832e155
JMC
683 mutex_destroy(&mdev->graph_mutex);
684}
685EXPORT_SYMBOL_GPL(media_device_cleanup);
686
9832e155
JMC
687int __must_check __media_device_register(struct media_device *mdev,
688 struct module *owner)
689{
690 int ret;
691
176fb0d1
LP
692 /* Register the device node. */
693 mdev->devnode.fops = &media_device_fops;
694 mdev->devnode.parent = mdev->dev;
695 mdev->devnode.release = media_device_release;
8a950796
JMC
696
697 /* Set version 0 to indicate user-space that the graph is static */
698 mdev->topology_version = 0;
699
85de721c 700 ret = media_devnode_register(&mdev->devnode, owner);
176fb0d1
LP
701 if (ret < 0)
702 return ret;
703
704 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
705 if (ret < 0) {
706 media_devnode_unregister(&mdev->devnode);
707 return ret;
708 }
709
8f5a3188
MCC
710 dev_dbg(mdev->dev, "Media device registered\n");
711
176fb0d1
LP
712 return 0;
713}
85de721c 714EXPORT_SYMBOL_GPL(__media_device_register);
176fb0d1 715
176fb0d1
LP
716void media_device_unregister(struct media_device *mdev)
717{
53e269c1
LP
718 struct media_entity *entity;
719 struct media_entity *next;
d47109fa
MCC
720 struct media_interface *intf, *tmp_intf;
721
821ed366
MCC
722 if (mdev == NULL)
723 return;
724
725 spin_lock(&mdev->lock);
726
223d19c5 727 /* Check if mdev was ever registered at all */
821ed366
MCC
728 if (!media_devnode_is_registered(&mdev->devnode)) {
729 spin_unlock(&mdev->lock);
223d19c5 730 return;
821ed366 731 }
223d19c5 732
f50d5166
MCC
733 /* Remove all entities from the media device */
734 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
821ed366 735 __media_device_unregister_entity(entity);
f50d5166 736
d47109fa 737 /* Remove all interfaces from the media device */
d47109fa
MCC
738 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
739 graph_obj.list) {
740 __media_remove_intf_links(intf);
c350ef83 741 media_gobj_destroy(&intf->graph_obj);
d47109fa
MCC
742 kfree(intf);
743 }
821ed366 744
d47109fa 745 spin_unlock(&mdev->lock);
53e269c1 746
176fb0d1
LP
747 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
748 media_devnode_unregister(&mdev->devnode);
8f5a3188
MCC
749
750 dev_dbg(mdev->dev, "Media device unregistered\n");
176fb0d1
LP
751}
752EXPORT_SYMBOL_GPL(media_device_unregister);
53e269c1 753
d062f911
SK
754static void media_device_release_devres(struct device *dev, void *res)
755{
756}
757
d062f911
SK
758struct media_device *media_device_get_devres(struct device *dev)
759{
760 struct media_device *mdev;
761
762 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
763 if (mdev)
764 return mdev;
765
766 mdev = devres_alloc(media_device_release_devres,
767 sizeof(struct media_device), GFP_KERNEL);
768 if (!mdev)
769 return NULL;
770 return devres_get(dev, mdev, NULL, NULL);
771}
772EXPORT_SYMBOL_GPL(media_device_get_devres);
773
d062f911
SK
774struct media_device *media_device_find_devres(struct device *dev)
775{
776 return devres_find(dev, media_device_release_devres, NULL, NULL);
777}
778EXPORT_SYMBOL_GPL(media_device_find_devres);
e576d60b
SK
779
780#endif /* CONFIG_MEDIA_CONTROLLER */
This page took 0.282707 seconds and 5 git commands to generate.