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 if (ret)
229 return ret;
230
231 memset(linkd->reserved, 0, sizeof(linkd->reserved));
232 return __media_entity_setup_link(link, linkd->flags);
233 }
234
media_device_get_topology(struct media_device * mdev,void * arg)235 static long media_device_get_topology(struct media_device *mdev, void *arg)
236 {
237 struct media_v2_topology *topo = arg;
238 struct media_entity *entity;
239 struct media_interface *intf;
240 struct media_pad *pad;
241 struct media_link *link;
242 struct media_v2_entity kentity, __user *uentity;
243 struct media_v2_interface kintf, __user *uintf;
244 struct media_v2_pad kpad, __user *upad;
245 struct media_v2_link klink, __user *ulink;
246 unsigned int i;
247 int ret = 0;
248
249 topo->topology_version = mdev->topology_version;
250
251 /* Get entities and number of entities */
252 i = 0;
253 uentity = media_get_uptr(topo->ptr_entities);
254 media_device_for_each_entity(entity, mdev) {
255 i++;
256 if (ret || !uentity)
257 continue;
258
259 if (i > topo->num_entities) {
260 ret = -ENOSPC;
261 continue;
262 }
263
264 /* Copy fields to userspace struct if not error */
265 memset(&kentity, 0, sizeof(kentity));
266 kentity.id = entity->graph_obj.id;
267 kentity.function = entity->function;
268 kentity.flags = entity->flags;
269 strscpy(kentity.name, entity->name,
270 sizeof(kentity.name));
271
272 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
273 ret = -EFAULT;
274 uentity++;
275 }
276 topo->num_entities = i;
277 topo->reserved1 = 0;
278
279 /* Get interfaces and number of interfaces */
280 i = 0;
281 uintf = media_get_uptr(topo->ptr_interfaces);
282 media_device_for_each_intf(intf, mdev) {
283 i++;
284 if (ret || !uintf)
285 continue;
286
287 if (i > topo->num_interfaces) {
288 ret = -ENOSPC;
289 continue;
290 }
291
292 memset(&kintf, 0, sizeof(kintf));
293
294 /* Copy intf fields to userspace struct */
295 kintf.id = intf->graph_obj.id;
296 kintf.intf_type = intf->type;
297 kintf.flags = intf->flags;
298
299 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
300 struct media_intf_devnode *devnode;
301
302 devnode = intf_to_devnode(intf);
303
304 kintf.devnode.major = devnode->major;
305 kintf.devnode.minor = devnode->minor;
306 }
307
308 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
309 ret = -EFAULT;
310 uintf++;
311 }
312 topo->num_interfaces = i;
313 topo->reserved2 = 0;
314
315 /* Get pads and number of pads */
316 i = 0;
317 upad = media_get_uptr(topo->ptr_pads);
318 media_device_for_each_pad(pad, mdev) {
319 i++;
320 if (ret || !upad)
321 continue;
322
323 if (i > topo->num_pads) {
324 ret = -ENOSPC;
325 continue;
326 }
327
328 memset(&kpad, 0, sizeof(kpad));
329
330 /* Copy pad fields to userspace struct */
331 kpad.id = pad->graph_obj.id;
332 kpad.entity_id = pad->entity->graph_obj.id;
333 kpad.flags = pad->flags;
334 kpad.index = pad->index;
335
336 if (copy_to_user(upad, &kpad, sizeof(kpad)))
337 ret = -EFAULT;
338 upad++;
339 }
340 topo->num_pads = i;
341 topo->reserved3 = 0;
342
343 /* Get links and number of links */
344 i = 0;
345 ulink = media_get_uptr(topo->ptr_links);
346 media_device_for_each_link(link, mdev) {
347 if (link->is_backlink)
348 continue;
349
350 i++;
351
352 if (ret || !ulink)
353 continue;
354
355 if (i > topo->num_links) {
356 ret = -ENOSPC;
357 continue;
358 }
359
360 memset(&klink, 0, sizeof(klink));
361
362 /* Copy link fields to userspace struct */
363 klink.id = link->graph_obj.id;
364 klink.source_id = link->gobj0->id;
365 klink.sink_id = link->gobj1->id;
366 klink.flags = link->flags;
367
368 if (copy_to_user(ulink, &klink, sizeof(klink)))
369 ret = -EFAULT;
370 ulink++;
371 }
372 topo->num_links = i;
373 topo->reserved4 = 0;
374
375 return ret;
376 }
377
media_device_request_alloc(struct media_device * mdev,void * arg)378 static long media_device_request_alloc(struct media_device *mdev, void *arg)
379 {
380 #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
381 int *alloc_fd = arg;
382
383 if (!mdev->ops || !mdev->ops->req_validate || !mdev->ops->req_queue)
384 return -ENOTTY;
385
386 return media_request_alloc(mdev, alloc_fd);
387 #else
388 return -ENOTTY;
389 #endif
390 }
391
copy_arg_from_user(void * karg,void __user * uarg,unsigned int cmd)392 static long copy_arg_from_user(void *karg, void __user *uarg, unsigned int cmd)
393 {
394 if ((_IOC_DIR(cmd) & _IOC_WRITE) &&
395 copy_from_user(karg, uarg, _IOC_SIZE(cmd)))
396 return -EFAULT;
397
398 return 0;
399 }
400
copy_arg_to_user(void __user * uarg,void * karg,unsigned int cmd)401 static long copy_arg_to_user(void __user *uarg, void *karg, unsigned int cmd)
402 {
403 if ((_IOC_DIR(cmd) & _IOC_READ) &&
404 copy_to_user(uarg, karg, _IOC_SIZE(cmd)))
405 return -EFAULT;
406
407 return 0;
408 }
409
410 /* Do acquire the graph mutex */
411 #define MEDIA_IOC_FL_GRAPH_MUTEX BIT(0)
412
413 #define MEDIA_IOC_ARG(__cmd, func, fl, from_user, to_user) \
414 [_IOC_NR(MEDIA_IOC_##__cmd)] = { \
415 .cmd = MEDIA_IOC_##__cmd, \
416 .fn = func, \
417 .flags = fl, \
418 .arg_from_user = from_user, \
419 .arg_to_user = to_user, \
420 }
421
422 #define MEDIA_IOC(__cmd, func, fl) \
423 MEDIA_IOC_ARG(__cmd, func, fl, copy_arg_from_user, copy_arg_to_user)
424
425 /* the table is indexed by _IOC_NR(cmd) */
426 struct media_ioctl_info {
427 unsigned int cmd;
428 unsigned short flags;
429 long (*fn)(struct media_device *dev, void *arg);
430 long (*arg_from_user)(void *karg, void __user *uarg, unsigned int cmd);
431 long (*arg_to_user)(void __user *uarg, void *karg, unsigned int cmd);
432 };
433
434 static const struct media_ioctl_info ioctl_info[] = {
435 MEDIA_IOC(DEVICE_INFO, media_device_get_info, MEDIA_IOC_FL_GRAPH_MUTEX),
436 MEDIA_IOC(ENUM_ENTITIES, media_device_enum_entities, MEDIA_IOC_FL_GRAPH_MUTEX),
437 MEDIA_IOC(ENUM_LINKS, media_device_enum_links, MEDIA_IOC_FL_GRAPH_MUTEX),
438 MEDIA_IOC(SETUP_LINK, media_device_setup_link, MEDIA_IOC_FL_GRAPH_MUTEX),
439 MEDIA_IOC(G_TOPOLOGY, media_device_get_topology, MEDIA_IOC_FL_GRAPH_MUTEX),
440 MEDIA_IOC(REQUEST_ALLOC, media_device_request_alloc, 0),
441 };
442
media_device_ioctl(struct file * filp,unsigned int cmd,unsigned long __arg)443 static long media_device_ioctl(struct file *filp, unsigned int cmd,
444 unsigned long __arg)
445 {
446 struct media_devnode *devnode = media_devnode_data(filp);
447 struct media_device *dev = devnode->media_dev;
448 const struct media_ioctl_info *info;
449 void __user *arg = (void __user *)__arg;
450 char __karg[256], *karg = __karg;
451 long ret;
452
453 if (_IOC_NR(cmd) >= ARRAY_SIZE(ioctl_info)
454 || ioctl_info[_IOC_NR(cmd)].cmd != cmd)
455 return -ENOIOCTLCMD;
456
457 info = &ioctl_info[_IOC_NR(cmd)];
458
459 if (_IOC_SIZE(info->cmd) > sizeof(__karg)) {
460 karg = kmalloc(_IOC_SIZE(info->cmd), GFP_KERNEL);
461 if (!karg)
462 return -ENOMEM;
463 }
464
465 if (info->arg_from_user) {
466 ret = info->arg_from_user(karg, arg, cmd);
467 if (ret)
468 goto out_free;
469 }
470
471 if (info->flags & MEDIA_IOC_FL_GRAPH_MUTEX)
472 mutex_lock(&dev->graph_mutex);
473
474 ret = info->fn(dev, karg);
475
476 if (info->flags & MEDIA_IOC_FL_GRAPH_MUTEX)
477 mutex_unlock(&dev->graph_mutex);
478
479 if (!ret && info->arg_to_user)
480 ret = info->arg_to_user(arg, karg, cmd);
481
482 out_free:
483 if (karg != __karg)
484 kfree(karg);
485
486 return ret;
487 }
488
489 #ifdef CONFIG_COMPAT
490
491 struct media_links_enum32 {
492 __u32 entity;
493 compat_uptr_t pads; /* struct media_pad_desc * */
494 compat_uptr_t links; /* struct media_link_desc * */
495 __u32 reserved[4];
496 };
497
media_device_enum_links32(struct media_device * mdev,struct media_links_enum32 __user * ulinks)498 static long media_device_enum_links32(struct media_device *mdev,
499 struct media_links_enum32 __user *ulinks)
500 {
501 struct media_links_enum links;
502 compat_uptr_t pads_ptr, links_ptr;
503 int ret;
504
505 memset(&links, 0, sizeof(links));
506
507 if (get_user(links.entity, &ulinks->entity)
508 || get_user(pads_ptr, &ulinks->pads)
509 || get_user(links_ptr, &ulinks->links))
510 return -EFAULT;
511
512 links.pads = compat_ptr(pads_ptr);
513 links.links = compat_ptr(links_ptr);
514
515 ret = media_device_enum_links(mdev, &links);
516 if (ret)
517 return ret;
518
519 if (copy_to_user(ulinks->reserved, links.reserved,
520 sizeof(ulinks->reserved)))
521 return -EFAULT;
522 return 0;
523 }
524
525 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
526
media_device_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)527 static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
528 unsigned long arg)
529 {
530 struct media_devnode *devnode = media_devnode_data(filp);
531 struct media_device *dev = devnode->media_dev;
532 long ret;
533
534 switch (cmd) {
535 case MEDIA_IOC_ENUM_LINKS32:
536 mutex_lock(&dev->graph_mutex);
537 ret = media_device_enum_links32(dev,
538 (struct media_links_enum32 __user *)arg);
539 mutex_unlock(&dev->graph_mutex);
540 break;
541
542 default:
543 return media_device_ioctl(filp, cmd, arg);
544 }
545
546 return ret;
547 }
548 #endif /* CONFIG_COMPAT */
549
550 static const struct media_file_operations media_device_fops = {
551 .owner = THIS_MODULE,
552 .open = media_device_open,
553 .ioctl = media_device_ioctl,
554 #ifdef CONFIG_COMPAT
555 .compat_ioctl = media_device_compat_ioctl,
556 #endif /* CONFIG_COMPAT */
557 .release = media_device_close,
558 };
559
560 /* -----------------------------------------------------------------------------
561 * sysfs
562 */
563
show_model(struct device * cd,struct device_attribute * attr,char * buf)564 static ssize_t show_model(struct device *cd,
565 struct device_attribute *attr, char *buf)
566 {
567 struct media_devnode *devnode = to_media_devnode(cd);
568 struct media_device *mdev = devnode->media_dev;
569
570 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
571 }
572
573 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
574
575 /* -----------------------------------------------------------------------------
576 * Registration/unregistration
577 */
578
media_device_release(struct media_devnode * devnode)579 static void media_device_release(struct media_devnode *devnode)
580 {
581 dev_dbg(devnode->parent, "Media device released\n");
582 }
583
__media_device_unregister_entity(struct media_entity * entity)584 static void __media_device_unregister_entity(struct media_entity *entity)
585 {
586 struct media_device *mdev = entity->graph_obj.mdev;
587 struct media_link *link, *tmp;
588 struct media_interface *intf;
589 unsigned int i;
590
591 ida_free(&mdev->entity_internal_idx, entity->internal_idx);
592
593 /* Remove all interface links pointing to this entity */
594 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
595 list_for_each_entry_safe(link, tmp, &intf->links, list) {
596 if (link->entity == entity)
597 __media_remove_intf_link(link);
598 }
599 }
600
601 /* Remove all data links that belong to this entity */
602 __media_entity_remove_links(entity);
603
604 /* Remove all pads that belong to this entity */
605 for (i = 0; i < entity->num_pads; i++)
606 media_gobj_destroy(&entity->pads[i].graph_obj);
607
608 /* Remove the entity */
609 media_gobj_destroy(&entity->graph_obj);
610
611 /* invoke entity_notify callbacks to handle entity removal?? */
612
613 entity->graph_obj.mdev = NULL;
614 }
615
616 /**
617 * media_device_register_entity - Register an entity with a media device
618 * @mdev: The media device
619 * @entity: The entity
620 */
media_device_register_entity(struct media_device * mdev,struct media_entity * entity)621 int __must_check media_device_register_entity(struct media_device *mdev,
622 struct media_entity *entity)
623 {
624 struct media_entity_notify *notify, *next;
625 unsigned int i;
626 int ret;
627
628 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
629 entity->function == MEDIA_ENT_F_UNKNOWN)
630 dev_warn(mdev->dev,
631 "Entity type for entity %s was not initialized!\n",
632 entity->name);
633
634 /* Warn if we apparently re-register an entity */
635 WARN_ON(entity->graph_obj.mdev != NULL);
636 entity->graph_obj.mdev = mdev;
637 INIT_LIST_HEAD(&entity->links);
638 entity->num_links = 0;
639 entity->num_backlinks = 0;
640
641 ret = ida_alloc_min(&mdev->entity_internal_idx, 1, GFP_KERNEL);
642 if (ret < 0)
643 return ret;
644 entity->internal_idx = ret;
645
646 mutex_lock(&mdev->graph_mutex);
647 mdev->entity_internal_idx_max =
648 max(mdev->entity_internal_idx_max, entity->internal_idx);
649
650 /* Initialize media_gobj embedded at the entity */
651 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
652
653 /* Initialize objects at the pads */
654 for (i = 0; i < entity->num_pads; i++)
655 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
656 &entity->pads[i].graph_obj);
657
658 /* invoke entity_notify callbacks */
659 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list)
660 notify->notify(entity, notify->notify_data);
661
662 if (mdev->entity_internal_idx_max
663 >= mdev->pm_count_walk.ent_enum.idx_max) {
664 struct media_graph new = { .top = 0 };
665
666 /*
667 * Initialise the new graph walk before cleaning up
668 * the old one in order not to spoil the graph walk
669 * object of the media device if graph walk init fails.
670 */
671 ret = media_graph_walk_init(&new, mdev);
672 if (ret) {
673 __media_device_unregister_entity(entity);
674 mutex_unlock(&mdev->graph_mutex);
675 return ret;
676 }
677 media_graph_walk_cleanup(&mdev->pm_count_walk);
678 mdev->pm_count_walk = new;
679 }
680 mutex_unlock(&mdev->graph_mutex);
681
682 return 0;
683 }
684 EXPORT_SYMBOL_GPL(media_device_register_entity);
685
media_device_unregister_entity(struct media_entity * entity)686 void media_device_unregister_entity(struct media_entity *entity)
687 {
688 struct media_device *mdev = entity->graph_obj.mdev;
689
690 if (mdev == NULL)
691 return;
692
693 mutex_lock(&mdev->graph_mutex);
694 __media_device_unregister_entity(entity);
695 mutex_unlock(&mdev->graph_mutex);
696 }
697 EXPORT_SYMBOL_GPL(media_device_unregister_entity);
698
699 /**
700 * media_device_init() - initialize a media device
701 * @mdev: The media device
702 *
703 * The caller is responsible for initializing the media device before
704 * registration. The following fields must be set:
705 *
706 * - dev must point to the parent device
707 * - model must be filled with the device model name
708 */
media_device_init(struct media_device * mdev)709 void media_device_init(struct media_device *mdev)
710 {
711 INIT_LIST_HEAD(&mdev->entities);
712 INIT_LIST_HEAD(&mdev->interfaces);
713 INIT_LIST_HEAD(&mdev->pads);
714 INIT_LIST_HEAD(&mdev->links);
715 INIT_LIST_HEAD(&mdev->entity_notify);
716
717 mutex_init(&mdev->req_queue_mutex);
718 mutex_init(&mdev->graph_mutex);
719 ida_init(&mdev->entity_internal_idx);
720
721 atomic_set(&mdev->request_id, 0);
722
723 dev_dbg(mdev->dev, "Media device initialized\n");
724 }
725 EXPORT_SYMBOL_GPL(media_device_init);
726
media_device_cleanup(struct media_device * mdev)727 void media_device_cleanup(struct media_device *mdev)
728 {
729 ida_destroy(&mdev->entity_internal_idx);
730 mdev->entity_internal_idx_max = 0;
731 media_graph_walk_cleanup(&mdev->pm_count_walk);
732 mutex_destroy(&mdev->graph_mutex);
733 mutex_destroy(&mdev->req_queue_mutex);
734 }
735 EXPORT_SYMBOL_GPL(media_device_cleanup);
736
__media_device_register(struct media_device * mdev,struct module * owner)737 int __must_check __media_device_register(struct media_device *mdev,
738 struct module *owner)
739 {
740 struct media_devnode *devnode;
741 int ret;
742
743 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
744 if (!devnode)
745 return -ENOMEM;
746
747 /* Register the device node. */
748 mdev->devnode = devnode;
749 devnode->fops = &media_device_fops;
750 devnode->parent = mdev->dev;
751 devnode->release = media_device_release;
752
753 /* Set version 0 to indicate user-space that the graph is static */
754 mdev->topology_version = 0;
755
756 ret = media_devnode_register(mdev, devnode, owner);
757 if (ret < 0) {
758 /* devnode free is handled in media_devnode_*() */
759 mdev->devnode = NULL;
760 return ret;
761 }
762
763 ret = device_create_file(&devnode->dev, &dev_attr_model);
764 if (ret < 0) {
765 /* devnode free is handled in media_devnode_*() */
766 mdev->devnode = NULL;
767 media_devnode_unregister_prepare(devnode);
768 media_devnode_unregister(devnode);
769 return ret;
770 }
771
772 dev_dbg(mdev->dev, "Media device registered\n");
773
774 return 0;
775 }
776 EXPORT_SYMBOL_GPL(__media_device_register);
777
media_device_register_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)778 int __must_check media_device_register_entity_notify(struct media_device *mdev,
779 struct media_entity_notify *nptr)
780 {
781 mutex_lock(&mdev->graph_mutex);
782 list_add_tail(&nptr->list, &mdev->entity_notify);
783 mutex_unlock(&mdev->graph_mutex);
784 return 0;
785 }
786 EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
787
788 /*
789 * Note: Should be called with mdev->lock held.
790 */
__media_device_unregister_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)791 static void __media_device_unregister_entity_notify(struct media_device *mdev,
792 struct media_entity_notify *nptr)
793 {
794 list_del(&nptr->list);
795 }
796
media_device_unregister_entity_notify(struct media_device * mdev,struct media_entity_notify * nptr)797 void media_device_unregister_entity_notify(struct media_device *mdev,
798 struct media_entity_notify *nptr)
799 {
800 mutex_lock(&mdev->graph_mutex);
801 __media_device_unregister_entity_notify(mdev, nptr);
802 mutex_unlock(&mdev->graph_mutex);
803 }
804 EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
805
media_device_unregister(struct media_device * mdev)806 void media_device_unregister(struct media_device *mdev)
807 {
808 struct media_entity *entity;
809 struct media_entity *next;
810 struct media_interface *intf, *tmp_intf;
811 struct media_entity_notify *notify, *nextp;
812
813 if (mdev == NULL)
814 return;
815
816 mutex_lock(&mdev->graph_mutex);
817
818 /* Check if mdev was ever registered at all */
819 if (!media_devnode_is_registered(mdev->devnode)) {
820 mutex_unlock(&mdev->graph_mutex);
821 return;
822 }
823
824 /* Clear the devnode register bit to avoid races with media dev open */
825 media_devnode_unregister_prepare(mdev->devnode);
826
827 /* Remove all entities from the media device */
828 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
829 __media_device_unregister_entity(entity);
830
831 /* Remove all entity_notify callbacks from the media device */
832 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
833 __media_device_unregister_entity_notify(mdev, notify);
834
835 /* Remove all interfaces from the media device */
836 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
837 graph_obj.list) {
838 /*
839 * Unlink the interface, but don't free it here; the
840 * module which created it is responsible for freeing
841 * it
842 */
843 __media_remove_intf_links(intf);
844 media_gobj_destroy(&intf->graph_obj);
845 }
846
847 mutex_unlock(&mdev->graph_mutex);
848
849 dev_dbg(mdev->dev, "Media device unregistered\n");
850
851 device_remove_file(&mdev->devnode->dev, &dev_attr_model);
852 media_devnode_unregister(mdev->devnode);
853 /* devnode free is handled in media_devnode_*() */
854 mdev->devnode = NULL;
855 }
856 EXPORT_SYMBOL_GPL(media_device_unregister);
857
858 #if IS_ENABLED(CONFIG_PCI)
media_device_pci_init(struct media_device * mdev,struct pci_dev * pci_dev,const char * name)859 void media_device_pci_init(struct media_device *mdev,
860 struct pci_dev *pci_dev,
861 const char *name)
862 {
863 mdev->dev = &pci_dev->dev;
864
865 if (name)
866 strscpy(mdev->model, name, sizeof(mdev->model));
867 else
868 strscpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
869
870 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
871
872 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
873 | pci_dev->subsystem_device;
874
875 media_device_init(mdev);
876 }
877 EXPORT_SYMBOL_GPL(media_device_pci_init);
878 #endif
879
880 #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)881 void __media_device_usb_init(struct media_device *mdev,
882 struct usb_device *udev,
883 const char *board_name,
884 const char *driver_name)
885 {
886 mdev->dev = &udev->dev;
887
888 if (driver_name)
889 strscpy(mdev->driver_name, driver_name,
890 sizeof(mdev->driver_name));
891
892 if (board_name)
893 strscpy(mdev->model, board_name, sizeof(mdev->model));
894 else if (udev->product)
895 strscpy(mdev->model, udev->product, sizeof(mdev->model));
896 else
897 strscpy(mdev->model, "unknown model", sizeof(mdev->model));
898 if (udev->serial)
899 strscpy(mdev->serial, udev->serial, sizeof(mdev->serial));
900 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
901 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
902
903 media_device_init(mdev);
904 }
905 EXPORT_SYMBOL_GPL(__media_device_usb_init);
906 #endif
907
908
909 #endif /* CONFIG_MEDIA_CONTROLLER */
910