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