]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/media-device.c
[media] media: Refactor copying IOCTL arguments from and to user space
[mirror_ubuntu-artful-kernel.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>
41b44e35
MCC
33#include <linux/pci.h>
34#include <linux/usb.h>
176fb0d1
LP
35
36#include <media/media-device.h>
37#include <media/media-devnode.h>
53e269c1 38#include <media/media-entity.h>
176fb0d1 39
e576d60b
SK
40#ifdef CONFIG_MEDIA_CONTROLLER
41
140d8816
LP
42/* -----------------------------------------------------------------------------
43 * Userspace API
44 */
45
0629e991
SA
46static inline void __user *media_get_uptr(__u64 arg)
47{
48 return (void __user *)(uintptr_t)arg;
49}
50
140d8816
LP
51static int media_device_open(struct file *filp)
52{
53 return 0;
54}
55
56static int media_device_close(struct file *filp)
57{
58 return 0;
59}
60
61static int media_device_get_info(struct media_device *dev,
bcd5081b 62 struct media_device_info *info)
140d8816 63{
bcd5081b 64 memset(info, 0, sizeof(*info));
140d8816 65
bb07bd6b 66 if (dev->driver_name[0])
bcd5081b 67 strlcpy(info->driver, dev->driver_name, sizeof(info->driver));
bb07bd6b 68 else
bcd5081b
SA
69 strlcpy(info->driver, dev->dev->driver->name,
70 sizeof(info->driver));
bb07bd6b 71
bcd5081b
SA
72 strlcpy(info->model, dev->model, sizeof(info->model));
73 strlcpy(info->serial, dev->serial, sizeof(info->serial));
74 strlcpy(info->bus_info, dev->bus_info, sizeof(info->bus_info));
140d8816 75
bcd5081b
SA
76 info->media_version = MEDIA_API_VERSION;
77 info->hw_revision = dev->hw_revision;
78 info->driver_version = dev->driver_version;
140d8816 79
b17d3945 80 return 0;
140d8816
LP
81}
82
1651333b
LP
83static struct media_entity *find_entity(struct media_device *mdev, u32 id)
84{
85 struct media_entity *entity;
86 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
87
88 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
89
1651333b 90 media_device_for_each_entity(entity, mdev) {
fa762394
MCC
91 if (((media_entity_id(entity) == id) && !next) ||
92 ((media_entity_id(entity) > id) && next)) {
1651333b
LP
93 return entity;
94 }
95 }
96
1651333b
LP
97 return NULL;
98}
99
100static long media_device_enum_entities(struct media_device *mdev,
bcd5081b 101 struct media_entity_desc *entd)
1651333b
LP
102{
103 struct media_entity *ent;
1651333b 104
bcd5081b 105 ent = find_entity(mdev, entd->id);
1651333b
LP
106 if (ent == NULL)
107 return -EINVAL;
108
bcd5081b
SA
109 memset(entd, 0, sizeof(*entd));
110
111 entd->id = media_entity_id(ent);
de8eae36 112 if (ent->name)
bcd5081b
SA
113 strlcpy(entd->name, ent->name, sizeof(entd->name));
114 entd->type = ent->function;
115 entd->revision = 0; /* Unused */
116 entd->flags = ent->flags;
117 entd->group_id = 0; /* Unused */
118 entd->pads = ent->num_pads;
119 entd->links = ent->num_links - ent->num_backlinks;
b2cd2744
MCC
120
121 /*
122 * Workaround for a bug at media-ctl <= v1.10 that makes it to
123 * do the wrong thing if the entity function doesn't belong to
124 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
125 * Ranges.
126 *
127 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
128 * or, otherwise, will be silently ignored by media-ctl when
129 * printing the graphviz diagram. So, map them into the devnode
130 * old range.
131 */
132 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
133 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
134 if (is_media_entity_v4l2_subdev(ent))
bcd5081b 135 entd->type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
b2cd2744 136 else if (ent->function != MEDIA_ENT_F_IO_V4L)
bcd5081b 137 entd->type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
b2cd2744
MCC
138 }
139
bcd5081b
SA
140 memcpy(&entd->raw, &ent->info, sizeof(ent->info));
141
1651333b
LP
142 return 0;
143}
144
145static void media_device_kpad_to_upad(const struct media_pad *kpad,
146 struct media_pad_desc *upad)
147{
fa762394 148 upad->entity = media_entity_id(kpad->entity);
1651333b
LP
149 upad->index = kpad->index;
150 upad->flags = kpad->flags;
151}
152
bcd5081b
SA
153static long media_device_enum_links(struct media_device *mdev,
154 struct media_links_enum *links)
1651333b
LP
155{
156 struct media_entity *entity;
1651333b 157
b0a1f2a8 158 entity = find_entity(mdev, links->entity);
1651333b
LP
159 if (entity == NULL)
160 return -EINVAL;
161
b0a1f2a8 162 if (links->pads) {
1651333b
LP
163 unsigned int p;
164
165 for (p = 0; p < entity->num_pads; p++) {
166 struct media_pad_desc pad;
c88e739b
DC
167
168 memset(&pad, 0, sizeof(pad));
1651333b 169 media_device_kpad_to_upad(&entity->pads[p], &pad);
b0a1f2a8 170 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
1651333b
LP
171 return -EFAULT;
172 }
173 }
174
b0a1f2a8 175 if (links->links) {
b83e2508
MCC
176 struct media_link *link;
177 struct media_link_desc __user *ulink_desc = links->links;
1651333b 178
b83e2508
MCC
179 list_for_each_entry(link, &entity->links, list) {
180 struct media_link_desc klink_desc;
1651333b
LP
181
182 /* Ignore backlinks. */
b83e2508 183 if (link->source->entity != entity)
1651333b 184 continue;
b83e2508
MCC
185 memset(&klink_desc, 0, sizeof(klink_desc));
186 media_device_kpad_to_upad(link->source,
187 &klink_desc.source);
188 media_device_kpad_to_upad(link->sink,
189 &klink_desc.sink);
190 klink_desc.flags = link->flags;
191 if (copy_to_user(ulink_desc, &klink_desc,
192 sizeof(*ulink_desc)))
1651333b 193 return -EFAULT;
b83e2508 194 ulink_desc++;
1651333b
LP
195 }
196 }
b0a1f2a8
SA
197
198 return 0;
199}
200
97548ed4 201static long media_device_setup_link(struct media_device *mdev,
bcd5081b 202 struct media_link_desc *linkd)
97548ed4
LP
203{
204 struct media_link *link = NULL;
97548ed4
LP
205 struct media_entity *source;
206 struct media_entity *sink;
97548ed4
LP
207
208 /* Find the source and sink entities and link.
209 */
bcd5081b
SA
210 source = find_entity(mdev, linkd->source.entity);
211 sink = find_entity(mdev, linkd->sink.entity);
97548ed4
LP
212
213 if (source == NULL || sink == NULL)
214 return -EINVAL;
215
bcd5081b
SA
216 if (linkd->source.index >= source->num_pads ||
217 linkd->sink.index >= sink->num_pads)
97548ed4
LP
218 return -EINVAL;
219
bcd5081b
SA
220 link = media_entity_find_link(&source->pads[linkd->source.index],
221 &sink->pads[linkd->sink.index]);
97548ed4
LP
222 if (link == NULL)
223 return -EINVAL;
224
225 /* Setup the link on both entities. */
bcd5081b 226 return __media_entity_setup_link(link, linkd->flags);
97548ed4
LP
227}
228
bcd5081b 229static long media_device_get_topology(struct media_device *mdev,
8309f47c
MCC
230 struct media_v2_topology *topo)
231{
232 struct media_entity *entity;
233 struct media_interface *intf;
234 struct media_pad *pad;
235 struct media_link *link;
d37410c2
SA
236 struct media_v2_entity kentity, __user *uentity;
237 struct media_v2_interface kintf, __user *uintf;
238 struct media_v2_pad kpad, __user *upad;
239 struct media_v2_link klink, __user *ulink;
9033b1a4
MCC
240 unsigned int i;
241 int ret = 0;
8309f47c
MCC
242
243 topo->topology_version = mdev->topology_version;
244
245 /* Get entities and number of entities */
246 i = 0;
b3b7a9f1 247 uentity = media_get_uptr(topo->ptr_entities);
8309f47c
MCC
248 media_device_for_each_entity(entity, mdev) {
249 i++;
b3b7a9f1 250 if (ret || !uentity)
8309f47c
MCC
251 continue;
252
253 if (i > topo->num_entities) {
254 ret = -ENOSPC;
255 continue;
256 }
257
258 /* Copy fields to userspace struct if not error */
b3b7a9f1
MCC
259 memset(&kentity, 0, sizeof(kentity));
260 kentity.id = entity->graph_obj.id;
261 kentity.function = entity->function;
262 strncpy(kentity.name, entity->name,
263 sizeof(kentity.name));
8309f47c 264
b3b7a9f1 265 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
8309f47c 266 ret = -EFAULT;
b3b7a9f1 267 uentity++;
8309f47c
MCC
268 }
269 topo->num_entities = i;
270
271 /* Get interfaces and number of interfaces */
272 i = 0;
b3b7a9f1 273 uintf = media_get_uptr(topo->ptr_interfaces);
8309f47c
MCC
274 media_device_for_each_intf(intf, mdev) {
275 i++;
b3b7a9f1 276 if (ret || !uintf)
8309f47c
MCC
277 continue;
278
279 if (i > topo->num_interfaces) {
280 ret = -ENOSPC;
281 continue;
282 }
283
b3b7a9f1 284 memset(&kintf, 0, sizeof(kintf));
8309f47c
MCC
285
286 /* Copy intf fields to userspace struct */
b3b7a9f1
MCC
287 kintf.id = intf->graph_obj.id;
288 kintf.intf_type = intf->type;
289 kintf.flags = intf->flags;
8309f47c
MCC
290
291 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
292 struct media_intf_devnode *devnode;
293
294 devnode = intf_to_devnode(intf);
295
b3b7a9f1
MCC
296 kintf.devnode.major = devnode->major;
297 kintf.devnode.minor = devnode->minor;
8309f47c
MCC
298 }
299
b3b7a9f1 300 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
8309f47c 301 ret = -EFAULT;
b3b7a9f1 302 uintf++;
8309f47c
MCC
303 }
304 topo->num_interfaces = i;
305
306 /* Get pads and number of pads */
307 i = 0;
b3b7a9f1 308 upad = media_get_uptr(topo->ptr_pads);
8309f47c
MCC
309 media_device_for_each_pad(pad, mdev) {
310 i++;
b3b7a9f1 311 if (ret || !upad)
8309f47c
MCC
312 continue;
313
314 if (i > topo->num_pads) {
315 ret = -ENOSPC;
316 continue;
317 }
318
b3b7a9f1 319 memset(&kpad, 0, sizeof(kpad));
8309f47c
MCC
320
321 /* Copy pad fields to userspace struct */
b3b7a9f1
MCC
322 kpad.id = pad->graph_obj.id;
323 kpad.entity_id = pad->entity->graph_obj.id;
324 kpad.flags = pad->flags;
8309f47c 325
b3b7a9f1 326 if (copy_to_user(upad, &kpad, sizeof(kpad)))
8309f47c 327 ret = -EFAULT;
b3b7a9f1 328 upad++;
8309f47c
MCC
329 }
330 topo->num_pads = i;
331
332 /* Get links and number of links */
333 i = 0;
b3b7a9f1 334 ulink = media_get_uptr(topo->ptr_links);
8309f47c 335 media_device_for_each_link(link, mdev) {
39d1ebc6
MCC
336 if (link->is_backlink)
337 continue;
338
8309f47c
MCC
339 i++;
340
b3b7a9f1 341 if (ret || !ulink)
8309f47c
MCC
342 continue;
343
344 if (i > topo->num_links) {
345 ret = -ENOSPC;
346 continue;
347 }
348
b3b7a9f1 349 memset(&klink, 0, sizeof(klink));
8309f47c
MCC
350
351 /* Copy link fields to userspace struct */
b3b7a9f1
MCC
352 klink.id = link->graph_obj.id;
353 klink.source_id = link->gobj0->id;
354 klink.sink_id = link->gobj1->id;
355 klink.flags = link->flags;
8309f47c 356
b3b7a9f1 357 if (copy_to_user(ulink, &klink, sizeof(klink)))
8309f47c 358 ret = -EFAULT;
b3b7a9f1 359 ulink++;
8309f47c
MCC
360 }
361 topo->num_links = i;
362
363 return ret;
364}
365
bcd5081b 366static long copy_arg_from_user(void *karg, void __user *uarg, unsigned int cmd)
8309f47c 367{
bcd5081b
SA
368 /* All media IOCTLs are _IOWR() */
369 if (copy_from_user(karg, uarg, _IOC_SIZE(cmd)))
a5c82e56 370 return -EFAULT;
8309f47c 371
bcd5081b
SA
372 return 0;
373}
8309f47c 374
bcd5081b
SA
375static long copy_arg_to_user(void __user *uarg, void *karg, unsigned int cmd)
376{
377 /* All media IOCTLs are _IOWR() */
378 if (copy_to_user(uarg, karg, _IOC_SIZE(cmd)))
a5c82e56 379 return -EFAULT;
8309f47c 380
a5c82e56 381 return 0;
8309f47c
MCC
382}
383
bcd5081b
SA
384#define MEDIA_IOC_ARG(__cmd, func, from_user, to_user) \
385 [_IOC_NR(MEDIA_IOC_##__cmd)] = { \
386 .cmd = MEDIA_IOC_##__cmd, \
387 .fn = (long (*)(struct media_device *, void *))func, \
388 .arg_from_user = from_user, \
389 .arg_to_user = to_user, \
6975264c 390 }
cf439d5f 391
bcd5081b
SA
392#define MEDIA_IOC(__cmd, func) \
393 MEDIA_IOC_ARG(__cmd, func, copy_arg_from_user, copy_arg_to_user)
394
cf439d5f
SA
395/* the table is indexed by _IOC_NR(cmd) */
396struct media_ioctl_info {
397 unsigned int cmd;
bcd5081b
SA
398 long (*fn)(struct media_device *dev, void *arg);
399 long (*arg_from_user)(void *karg, void __user *uarg, unsigned int cmd);
400 long (*arg_to_user)(void __user *uarg, void *karg, unsigned int cmd);
cf439d5f
SA
401};
402
403static const struct media_ioctl_info ioctl_info[] = {
6975264c
SA
404 MEDIA_IOC(DEVICE_INFO, media_device_get_info),
405 MEDIA_IOC(ENUM_ENTITIES, media_device_enum_entities),
406 MEDIA_IOC(ENUM_LINKS, media_device_enum_links),
407 MEDIA_IOC(SETUP_LINK, media_device_setup_link),
408 MEDIA_IOC(G_TOPOLOGY, media_device_get_topology),
cf439d5f
SA
409};
410
140d8816 411static long media_device_ioctl(struct file *filp, unsigned int cmd,
bcd5081b 412 unsigned long __arg)
140d8816
LP
413{
414 struct media_devnode *devnode = media_devnode_data(filp);
a087ce70 415 struct media_device *dev = devnode->media_dev;
6975264c 416 const struct media_ioctl_info *info;
bcd5081b
SA
417 void __user *arg = (void __user *)__arg;
418 char __karg[256], *karg = __karg;
140d8816
LP
419 long ret;
420
cf439d5f
SA
421 if (_IOC_NR(cmd) >= ARRAY_SIZE(ioctl_info)
422 || ioctl_info[_IOC_NR(cmd)].cmd != cmd)
423 return -ENOIOCTLCMD;
424
6975264c
SA
425 info = &ioctl_info[_IOC_NR(cmd)];
426
bcd5081b
SA
427 if (_IOC_SIZE(info->cmd) > sizeof(__karg)) {
428 karg = kmalloc(_IOC_SIZE(info->cmd), GFP_KERNEL);
429 if (!karg)
430 return -ENOMEM;
431 }
432
433 if (info->arg_from_user) {
434 ret = info->arg_from_user(karg, arg, cmd);
435 if (ret)
436 goto out_free;
437 }
438
e2c91d4d 439 mutex_lock(&dev->graph_mutex);
bcd5081b 440 ret = info->fn(dev, karg);
e2c91d4d 441 mutex_unlock(&dev->graph_mutex);
140d8816 442
bcd5081b
SA
443 if (!ret && info->arg_to_user)
444 ret = info->arg_to_user(arg, karg, cmd);
445
446out_free:
447 if (karg != __karg)
448 kfree(karg);
449
140d8816
LP
450 return ret;
451}
452
b0a1f2a8
SA
453#ifdef CONFIG_COMPAT
454
455struct media_links_enum32 {
456 __u32 entity;
457 compat_uptr_t pads; /* struct media_pad_desc * */
458 compat_uptr_t links; /* struct media_link_desc * */
459 __u32 reserved[4];
460};
461
462static long media_device_enum_links32(struct media_device *mdev,
463 struct media_links_enum32 __user *ulinks)
464{
465 struct media_links_enum links;
466 compat_uptr_t pads_ptr, links_ptr;
467
468 memset(&links, 0, sizeof(links));
469
470 if (get_user(links.entity, &ulinks->entity)
471 || get_user(pads_ptr, &ulinks->pads)
472 || get_user(links_ptr, &ulinks->links))
473 return -EFAULT;
474
475 links.pads = compat_ptr(pads_ptr);
476 links.links = compat_ptr(links_ptr);
477
bcd5081b 478 return media_device_enum_links(mdev, &links);
b0a1f2a8
SA
479}
480
481#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
482
483static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
484 unsigned long arg)
485{
486 struct media_devnode *devnode = media_devnode_data(filp);
a087ce70 487 struct media_device *dev = devnode->media_dev;
b0a1f2a8
SA
488 long ret;
489
490 switch (cmd) {
b0a1f2a8
SA
491 case MEDIA_IOC_ENUM_LINKS32:
492 mutex_lock(&dev->graph_mutex);
493 ret = media_device_enum_links32(dev,
494 (struct media_links_enum32 __user *)arg);
495 mutex_unlock(&dev->graph_mutex);
496 break;
497
498 default:
f7369627 499 return media_device_ioctl(filp, cmd, arg);
b0a1f2a8
SA
500 }
501
502 return ret;
503}
504#endif /* CONFIG_COMPAT */
505
176fb0d1
LP
506static const struct media_file_operations media_device_fops = {
507 .owner = THIS_MODULE,
140d8816
LP
508 .open = media_device_open,
509 .ioctl = media_device_ioctl,
b0a1f2a8
SA
510#ifdef CONFIG_COMPAT
511 .compat_ioctl = media_device_compat_ioctl,
512#endif /* CONFIG_COMPAT */
140d8816 513 .release = media_device_close,
176fb0d1
LP
514};
515
516/* -----------------------------------------------------------------------------
517 * sysfs
518 */
519
520static ssize_t show_model(struct device *cd,
521 struct device_attribute *attr, char *buf)
522{
a087ce70
MCC
523 struct media_devnode *devnode = to_media_devnode(cd);
524 struct media_device *mdev = devnode->media_dev;
176fb0d1
LP
525
526 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
527}
528
529static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
530
531/* -----------------------------------------------------------------------------
532 * Registration/unregistration
533 */
534
535static void media_device_release(struct media_devnode *mdev)
536{
8f5a3188 537 dev_dbg(mdev->parent, "Media device released\n");
176fb0d1
LP
538}
539
b2ed8af9
MCC
540/**
541 * media_device_register_entity - Register an entity with a media device
542 * @mdev: The media device
543 * @entity: The entity
544 */
545int __must_check media_device_register_entity(struct media_device *mdev,
546 struct media_entity *entity)
547{
afcbdb55 548 struct media_entity_notify *notify, *next;
b2ed8af9 549 unsigned int i;
ba0dd729 550 int ret;
b2ed8af9
MCC
551
552 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
553 entity->function == MEDIA_ENT_F_UNKNOWN)
554 dev_warn(mdev->dev,
555 "Entity type for entity %s was not initialized!\n",
556 entity->name);
557
558 /* Warn if we apparently re-register an entity */
559 WARN_ON(entity->graph_obj.mdev != NULL);
560 entity->graph_obj.mdev = mdev;
561 INIT_LIST_HEAD(&entity->links);
562 entity->num_links = 0;
563 entity->num_backlinks = 0;
564
ba0dd729
MCC
565 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
566 return -ENOMEM;
567
e2c91d4d 568 mutex_lock(&mdev->graph_mutex);
665faa97 569
ba0dd729
MCC
570 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
571 &entity->internal_idx);
572 if (ret < 0) {
e2c91d4d 573 mutex_unlock(&mdev->graph_mutex);
ba0dd729 574 return ret;
665faa97
SA
575 }
576
577 mdev->entity_internal_idx_max =
578 max(mdev->entity_internal_idx_max, entity->internal_idx);
579
b2ed8af9
MCC
580 /* Initialize media_gobj embedded at the entity */
581 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
582
583 /* Initialize objects at the pads */
584 for (i = 0; i < entity->num_pads; i++)
585 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
586 &entity->pads[i].graph_obj);
587
afcbdb55
SK
588 /* invoke entity_notify callbacks */
589 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
590 (notify)->notify(entity, notify->notify_data);
591 }
592
0c426c47
SA
593 if (mdev->entity_internal_idx_max
594 >= mdev->pm_count_walk.ent_enum.idx_max) {
595 struct media_entity_graph new = { .top = 0 };
596
597 /*
598 * Initialise the new graph walk before cleaning up
599 * the old one in order not to spoil the graph walk
600 * object of the media device if graph walk init fails.
601 */
602 ret = media_entity_graph_walk_init(&new, mdev);
603 if (ret) {
604 mutex_unlock(&mdev->graph_mutex);
605 return ret;
606 }
607 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
608 mdev->pm_count_walk = new;
609 }
610 mutex_unlock(&mdev->graph_mutex);
611
b2ed8af9
MCC
612 return 0;
613}
614EXPORT_SYMBOL_GPL(media_device_register_entity);
615
821ed366 616static void __media_device_unregister_entity(struct media_entity *entity)
b2ed8af9
MCC
617{
618 struct media_device *mdev = entity->graph_obj.mdev;
619 struct media_link *link, *tmp;
620 struct media_interface *intf;
621 unsigned int i;
622
665faa97
SA
623 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
624
b2ed8af9
MCC
625 /* Remove all interface links pointing to this entity */
626 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
627 list_for_each_entry_safe(link, tmp, &intf->links, list) {
628 if (link->entity == entity)
629 __media_remove_intf_link(link);
630 }
631 }
632
633 /* Remove all data links that belong to this entity */
634 __media_entity_remove_links(entity);
635
636 /* Remove all pads that belong to this entity */
637 for (i = 0; i < entity->num_pads; i++)
638 media_gobj_destroy(&entity->pads[i].graph_obj);
639
640 /* Remove the entity */
641 media_gobj_destroy(&entity->graph_obj);
642
afcbdb55
SK
643 /* invoke entity_notify callbacks to handle entity removal?? */
644
b2ed8af9
MCC
645 entity->graph_obj.mdev = NULL;
646}
821ed366
MCC
647
648void media_device_unregister_entity(struct media_entity *entity)
649{
650 struct media_device *mdev = entity->graph_obj.mdev;
651
652 if (mdev == NULL)
653 return;
654
e2c91d4d 655 mutex_lock(&mdev->graph_mutex);
821ed366 656 __media_device_unregister_entity(entity);
e2c91d4d 657 mutex_unlock(&mdev->graph_mutex);
821ed366 658}
b2ed8af9
MCC
659EXPORT_SYMBOL_GPL(media_device_unregister_entity);
660
176fb0d1 661/**
9832e155 662 * media_device_init() - initialize a media device
176fb0d1
LP
663 * @mdev: The media device
664 *
665 * The caller is responsible for initializing the media device before
666 * registration. The following fields must be set:
667 *
668 * - dev must point to the parent device
669 * - model must be filled with the device model name
670 */
9832e155 671void media_device_init(struct media_device *mdev)
176fb0d1 672{
53e269c1 673 INIT_LIST_HEAD(&mdev->entities);
57cf79b7 674 INIT_LIST_HEAD(&mdev->interfaces);
9155d859
MCC
675 INIT_LIST_HEAD(&mdev->pads);
676 INIT_LIST_HEAD(&mdev->links);
afcbdb55 677 INIT_LIST_HEAD(&mdev->entity_notify);
503c3d82 678 mutex_init(&mdev->graph_mutex);
665faa97 679 ida_init(&mdev->entity_internal_idx);
53e269c1 680
9832e155
JMC
681 dev_dbg(mdev->dev, "Media device initialized\n");
682}
683EXPORT_SYMBOL_GPL(media_device_init);
684
9832e155
JMC
685void media_device_cleanup(struct media_device *mdev)
686{
665faa97
SA
687 ida_destroy(&mdev->entity_internal_idx);
688 mdev->entity_internal_idx_max = 0;
0c426c47 689 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
9832e155
JMC
690 mutex_destroy(&mdev->graph_mutex);
691}
692EXPORT_SYMBOL_GPL(media_device_cleanup);
693
9832e155
JMC
694int __must_check __media_device_register(struct media_device *mdev,
695 struct module *owner)
696{
a087ce70 697 struct media_devnode *devnode;
9832e155
JMC
698 int ret;
699
a087ce70
MCC
700 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
701 if (!devnode)
702 return -ENOMEM;
703
176fb0d1 704 /* Register the device node. */
a087ce70
MCC
705 mdev->devnode = devnode;
706 devnode->fops = &media_device_fops;
707 devnode->parent = mdev->dev;
708 devnode->release = media_device_release;
8a950796
JMC
709
710 /* Set version 0 to indicate user-space that the graph is static */
711 mdev->topology_version = 0;
712
a087ce70
MCC
713 ret = media_devnode_register(mdev, devnode, owner);
714 if (ret < 0) {
5b28dde5 715 /* devnode free is handled in media_devnode_*() */
a087ce70 716 mdev->devnode = NULL;
176fb0d1 717 return ret;
a087ce70 718 }
176fb0d1 719
a087ce70 720 ret = device_create_file(&devnode->dev, &dev_attr_model);
176fb0d1 721 if (ret < 0) {
5b28dde5 722 /* devnode free is handled in media_devnode_*() */
a087ce70 723 mdev->devnode = NULL;
6f0dd24a 724 media_devnode_unregister_prepare(devnode);
a087ce70 725 media_devnode_unregister(devnode);
176fb0d1
LP
726 return ret;
727 }
728
8f5a3188
MCC
729 dev_dbg(mdev->dev, "Media device registered\n");
730
176fb0d1
LP
731 return 0;
732}
85de721c 733EXPORT_SYMBOL_GPL(__media_device_register);
176fb0d1 734
afcbdb55
SK
735int __must_check media_device_register_entity_notify(struct media_device *mdev,
736 struct media_entity_notify *nptr)
737{
e2c91d4d 738 mutex_lock(&mdev->graph_mutex);
afcbdb55 739 list_add_tail(&nptr->list, &mdev->entity_notify);
e2c91d4d 740 mutex_unlock(&mdev->graph_mutex);
afcbdb55
SK
741 return 0;
742}
743EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
744
745/*
746 * Note: Should be called with mdev->lock held.
747 */
748static void __media_device_unregister_entity_notify(struct media_device *mdev,
749 struct media_entity_notify *nptr)
750{
751 list_del(&nptr->list);
752}
753
754void media_device_unregister_entity_notify(struct media_device *mdev,
755 struct media_entity_notify *nptr)
756{
e2c91d4d 757 mutex_lock(&mdev->graph_mutex);
afcbdb55 758 __media_device_unregister_entity_notify(mdev, nptr);
e2c91d4d 759 mutex_unlock(&mdev->graph_mutex);
afcbdb55
SK
760}
761EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
762
176fb0d1
LP
763void media_device_unregister(struct media_device *mdev)
764{
53e269c1
LP
765 struct media_entity *entity;
766 struct media_entity *next;
d47109fa 767 struct media_interface *intf, *tmp_intf;
afcbdb55 768 struct media_entity_notify *notify, *nextp;
d47109fa 769
821ed366
MCC
770 if (mdev == NULL)
771 return;
772
e2c91d4d 773 mutex_lock(&mdev->graph_mutex);
821ed366 774
223d19c5 775 /* Check if mdev was ever registered at all */
a087ce70 776 if (!media_devnode_is_registered(mdev->devnode)) {
e2c91d4d 777 mutex_unlock(&mdev->graph_mutex);
223d19c5 778 return;
821ed366 779 }
223d19c5 780
6f0dd24a
SK
781 /* Clear the devnode register bit to avoid races with media dev open */
782 media_devnode_unregister_prepare(mdev->devnode);
783
f50d5166
MCC
784 /* Remove all entities from the media device */
785 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
821ed366 786 __media_device_unregister_entity(entity);
f50d5166 787
afcbdb55
SK
788 /* Remove all entity_notify callbacks from the media device */
789 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
790 __media_device_unregister_entity_notify(mdev, notify);
791
d47109fa 792 /* Remove all interfaces from the media device */
d47109fa
MCC
793 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
794 graph_obj.list) {
795 __media_remove_intf_links(intf);
c350ef83 796 media_gobj_destroy(&intf->graph_obj);
d47109fa
MCC
797 kfree(intf);
798 }
821ed366 799
e2c91d4d 800 mutex_unlock(&mdev->graph_mutex);
53e269c1 801
a087ce70
MCC
802 dev_dbg(mdev->dev, "Media device unregistered\n");
803
6f0dd24a
SK
804 device_remove_file(&mdev->devnode->dev, &dev_attr_model);
805 media_devnode_unregister(mdev->devnode);
806 /* devnode free is handled in media_devnode_*() */
807 mdev->devnode = NULL;
176fb0d1
LP
808}
809EXPORT_SYMBOL_GPL(media_device_unregister);
53e269c1 810
d062f911
SK
811static void media_device_release_devres(struct device *dev, void *res)
812{
813}
814
d062f911
SK
815struct media_device *media_device_get_devres(struct device *dev)
816{
817 struct media_device *mdev;
818
819 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
820 if (mdev)
821 return mdev;
822
823 mdev = devres_alloc(media_device_release_devres,
824 sizeof(struct media_device), GFP_KERNEL);
825 if (!mdev)
826 return NULL;
827 return devres_get(dev, mdev, NULL, NULL);
828}
829EXPORT_SYMBOL_GPL(media_device_get_devres);
830
d062f911
SK
831struct media_device *media_device_find_devres(struct device *dev)
832{
833 return devres_find(dev, media_device_release_devres, NULL, NULL);
834}
835EXPORT_SYMBOL_GPL(media_device_find_devres);
e576d60b 836
b34ecd5a 837#if IS_ENABLED(CONFIG_PCI)
6cf5dad1
MCC
838void media_device_pci_init(struct media_device *mdev,
839 struct pci_dev *pci_dev,
840 const char *name)
41b44e35 841{
41b44e35
MCC
842 mdev->dev = &pci_dev->dev;
843
844 if (name)
845 strlcpy(mdev->model, name, sizeof(mdev->model));
846 else
847 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
848
849 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
850
851 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
852 | pci_dev->subsystem_device;
853
854 mdev->driver_version = LINUX_VERSION_CODE;
855
856 media_device_init(mdev);
41b44e35
MCC
857}
858EXPORT_SYMBOL_GPL(media_device_pci_init);
b34ecd5a 859#endif
41b44e35 860
b34ecd5a 861#if IS_ENABLED(CONFIG_USB)
6cf5dad1
MCC
862void __media_device_usb_init(struct media_device *mdev,
863 struct usb_device *udev,
864 const char *board_name,
865 const char *driver_name)
41b44e35 866{
41b44e35
MCC
867 mdev->dev = &udev->dev;
868
869 if (driver_name)
870 strlcpy(mdev->driver_name, driver_name,
871 sizeof(mdev->driver_name));
872
873 if (board_name)
874 strlcpy(mdev->model, board_name, sizeof(mdev->model));
875 else if (udev->product)
876 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
877 else
878 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
879 if (udev->serial)
880 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
881 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
882 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
883 mdev->driver_version = LINUX_VERSION_CODE;
884
885 media_device_init(mdev);
41b44e35
MCC
886}
887EXPORT_SYMBOL_GPL(__media_device_usb_init);
b34ecd5a 888#endif
41b44e35
MCC
889
890
e576d60b 891#endif /* CONFIG_MEDIA_CONTROLLER */