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