Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / media-device.c
blob6e43c95629ea8e8fc550b6fc63bf56b5c627b1ac
1 /*
2 * Media device
4 * Copyright (C) 2010 Nokia Corporation
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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
23 /* We need to access legacy defines from linux/media.h */
24 #define __NEED_MEDIA_LEGACY_API
26 #include <linux/compat.h>
27 #include <linux/export.h>
28 #include <linux/idr.h>
29 #include <linux/ioctl.h>
30 #include <linux/media.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/usb.h>
36 #include <media/media-device.h>
37 #include <media/media-devnode.h>
38 #include <media/media-entity.h>
40 #ifdef CONFIG_MEDIA_CONTROLLER
42 /* -----------------------------------------------------------------------------
43 * Userspace API
46 static inline void __user *media_get_uptr(__u64 arg)
48 return (void __user *)(uintptr_t)arg;
51 static int media_device_open(struct file *filp)
53 return 0;
56 static int media_device_close(struct file *filp)
58 return 0;
61 static int media_device_get_info(struct media_device *dev,
62 struct media_device_info __user *__info)
64 struct media_device_info info;
66 memset(&info, 0, sizeof(info));
68 if (dev->driver_name[0])
69 strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
70 else
71 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
73 strlcpy(info.model, dev->model, sizeof(info.model));
74 strlcpy(info.serial, dev->serial, sizeof(info.serial));
75 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
77 info.media_version = MEDIA_API_VERSION;
78 info.hw_revision = dev->hw_revision;
79 info.driver_version = dev->driver_version;
81 if (copy_to_user(__info, &info, sizeof(*__info)))
82 return -EFAULT;
83 return 0;
86 static struct media_entity *find_entity(struct media_device *mdev, u32 id)
88 struct media_entity *entity;
89 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
91 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
93 spin_lock(&mdev->lock);
95 media_device_for_each_entity(entity, mdev) {
96 if (((media_entity_id(entity) == id) && !next) ||
97 ((media_entity_id(entity) > id) && next)) {
98 spin_unlock(&mdev->lock);
99 return entity;
103 spin_unlock(&mdev->lock);
105 return NULL;
108 static long media_device_enum_entities(struct media_device *mdev,
109 struct media_entity_desc __user *uent)
111 struct media_entity *ent;
112 struct media_entity_desc u_ent;
114 memset(&u_ent, 0, sizeof(u_ent));
115 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
116 return -EFAULT;
118 ent = find_entity(mdev, u_ent.id);
120 if (ent == NULL)
121 return -EINVAL;
123 u_ent.id = media_entity_id(ent);
124 if (ent->name)
125 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
126 u_ent.type = ent->function;
127 u_ent.revision = 0; /* Unused */
128 u_ent.flags = ent->flags;
129 u_ent.group_id = 0; /* Unused */
130 u_ent.pads = ent->num_pads;
131 u_ent.links = ent->num_links - ent->num_backlinks;
134 * Workaround for a bug at media-ctl <= v1.10 that makes it to
135 * do the wrong thing if the entity function doesn't belong to
136 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
137 * Ranges.
139 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
140 * or, otherwise, will be silently ignored by media-ctl when
141 * printing the graphviz diagram. So, map them into the devnode
142 * old range.
144 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
145 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
146 if (is_media_entity_v4l2_subdev(ent))
147 u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
148 else if (ent->function != MEDIA_ENT_F_IO_V4L)
149 u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
152 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
153 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
154 return -EFAULT;
155 return 0;
158 static void media_device_kpad_to_upad(const struct media_pad *kpad,
159 struct media_pad_desc *upad)
161 upad->entity = media_entity_id(kpad->entity);
162 upad->index = kpad->index;
163 upad->flags = kpad->flags;
166 static long __media_device_enum_links(struct media_device *mdev,
167 struct media_links_enum *links)
169 struct media_entity *entity;
171 entity = find_entity(mdev, links->entity);
172 if (entity == NULL)
173 return -EINVAL;
175 if (links->pads) {
176 unsigned int p;
178 for (p = 0; p < entity->num_pads; p++) {
179 struct media_pad_desc pad;
181 memset(&pad, 0, sizeof(pad));
182 media_device_kpad_to_upad(&entity->pads[p], &pad);
183 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
184 return -EFAULT;
188 if (links->links) {
189 struct media_link *link;
190 struct media_link_desc __user *ulink_desc = links->links;
192 list_for_each_entry(link, &entity->links, list) {
193 struct media_link_desc klink_desc;
195 /* Ignore backlinks. */
196 if (link->source->entity != entity)
197 continue;
198 memset(&klink_desc, 0, sizeof(klink_desc));
199 media_device_kpad_to_upad(link->source,
200 &klink_desc.source);
201 media_device_kpad_to_upad(link->sink,
202 &klink_desc.sink);
203 klink_desc.flags = link->flags;
204 if (copy_to_user(ulink_desc, &klink_desc,
205 sizeof(*ulink_desc)))
206 return -EFAULT;
207 ulink_desc++;
211 return 0;
214 static long media_device_enum_links(struct media_device *mdev,
215 struct media_links_enum __user *ulinks)
217 struct media_links_enum links;
218 int rval;
220 if (copy_from_user(&links, ulinks, sizeof(links)))
221 return -EFAULT;
223 rval = __media_device_enum_links(mdev, &links);
224 if (rval < 0)
225 return rval;
227 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
228 return -EFAULT;
230 return 0;
233 static long media_device_setup_link(struct media_device *mdev,
234 struct media_link_desc __user *_ulink)
236 struct media_link *link = NULL;
237 struct media_link_desc ulink;
238 struct media_entity *source;
239 struct media_entity *sink;
240 int ret;
242 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
243 return -EFAULT;
245 /* Find the source and sink entities and link.
247 source = find_entity(mdev, ulink.source.entity);
248 sink = find_entity(mdev, ulink.sink.entity);
250 if (source == NULL || sink == NULL)
251 return -EINVAL;
253 if (ulink.source.index >= source->num_pads ||
254 ulink.sink.index >= sink->num_pads)
255 return -EINVAL;
257 link = media_entity_find_link(&source->pads[ulink.source.index],
258 &sink->pads[ulink.sink.index]);
259 if (link == NULL)
260 return -EINVAL;
262 /* Setup the link on both entities. */
263 ret = __media_entity_setup_link(link, ulink.flags);
265 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
266 return -EFAULT;
268 return ret;
271 static long __media_device_get_topology(struct media_device *mdev,
272 struct media_v2_topology *topo)
274 struct media_entity *entity;
275 struct media_interface *intf;
276 struct media_pad *pad;
277 struct media_link *link;
278 struct media_v2_entity kentity, __user *uentity;
279 struct media_v2_interface kintf, __user *uintf;
280 struct media_v2_pad kpad, __user *upad;
281 struct media_v2_link klink, __user *ulink;
282 unsigned int i;
283 int ret = 0;
285 topo->topology_version = mdev->topology_version;
287 /* Get entities and number of entities */
288 i = 0;
289 uentity = media_get_uptr(topo->ptr_entities);
290 media_device_for_each_entity(entity, mdev) {
291 i++;
292 if (ret || !uentity)
293 continue;
295 if (i > topo->num_entities) {
296 ret = -ENOSPC;
297 continue;
300 /* Copy fields to userspace struct if not error */
301 memset(&kentity, 0, sizeof(kentity));
302 kentity.id = entity->graph_obj.id;
303 kentity.function = entity->function;
304 strncpy(kentity.name, entity->name,
305 sizeof(kentity.name));
307 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
308 ret = -EFAULT;
309 uentity++;
311 topo->num_entities = i;
313 /* Get interfaces and number of interfaces */
314 i = 0;
315 uintf = media_get_uptr(topo->ptr_interfaces);
316 media_device_for_each_intf(intf, mdev) {
317 i++;
318 if (ret || !uintf)
319 continue;
321 if (i > topo->num_interfaces) {
322 ret = -ENOSPC;
323 continue;
326 memset(&kintf, 0, sizeof(kintf));
328 /* Copy intf fields to userspace struct */
329 kintf.id = intf->graph_obj.id;
330 kintf.intf_type = intf->type;
331 kintf.flags = intf->flags;
333 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
334 struct media_intf_devnode *devnode;
336 devnode = intf_to_devnode(intf);
338 kintf.devnode.major = devnode->major;
339 kintf.devnode.minor = devnode->minor;
342 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
343 ret = -EFAULT;
344 uintf++;
346 topo->num_interfaces = i;
348 /* Get pads and number of pads */
349 i = 0;
350 upad = media_get_uptr(topo->ptr_pads);
351 media_device_for_each_pad(pad, mdev) {
352 i++;
353 if (ret || !upad)
354 continue;
356 if (i > topo->num_pads) {
357 ret = -ENOSPC;
358 continue;
361 memset(&kpad, 0, sizeof(kpad));
363 /* Copy pad fields to userspace struct */
364 kpad.id = pad->graph_obj.id;
365 kpad.entity_id = pad->entity->graph_obj.id;
366 kpad.flags = pad->flags;
368 if (copy_to_user(upad, &kpad, sizeof(kpad)))
369 ret = -EFAULT;
370 upad++;
372 topo->num_pads = i;
374 /* Get links and number of links */
375 i = 0;
376 ulink = media_get_uptr(topo->ptr_links);
377 media_device_for_each_link(link, mdev) {
378 if (link->is_backlink)
379 continue;
381 i++;
383 if (ret || !ulink)
384 continue;
386 if (i > topo->num_links) {
387 ret = -ENOSPC;
388 continue;
391 memset(&klink, 0, sizeof(klink));
393 /* Copy link fields to userspace struct */
394 klink.id = link->graph_obj.id;
395 klink.source_id = link->gobj0->id;
396 klink.sink_id = link->gobj1->id;
397 klink.flags = link->flags;
399 if (copy_to_user(ulink, &klink, sizeof(klink)))
400 ret = -EFAULT;
401 ulink++;
403 topo->num_links = i;
405 return ret;
408 static long media_device_get_topology(struct media_device *mdev,
409 struct media_v2_topology __user *utopo)
411 struct media_v2_topology ktopo;
412 int ret;
414 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
415 return -EFAULT;
417 ret = __media_device_get_topology(mdev, &ktopo);
418 if (ret < 0)
419 return ret;
421 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
422 return -EFAULT;
424 return 0;
427 static long media_device_ioctl(struct file *filp, unsigned int cmd,
428 unsigned long arg)
430 struct media_devnode *devnode = media_devnode_data(filp);
431 struct media_device *dev = to_media_device(devnode);
432 long ret;
434 switch (cmd) {
435 case MEDIA_IOC_DEVICE_INFO:
436 ret = media_device_get_info(dev,
437 (struct media_device_info __user *)arg);
438 break;
440 case MEDIA_IOC_ENUM_ENTITIES:
441 ret = media_device_enum_entities(dev,
442 (struct media_entity_desc __user *)arg);
443 break;
445 case MEDIA_IOC_ENUM_LINKS:
446 mutex_lock(&dev->graph_mutex);
447 ret = media_device_enum_links(dev,
448 (struct media_links_enum __user *)arg);
449 mutex_unlock(&dev->graph_mutex);
450 break;
452 case MEDIA_IOC_SETUP_LINK:
453 mutex_lock(&dev->graph_mutex);
454 ret = media_device_setup_link(dev,
455 (struct media_link_desc __user *)arg);
456 mutex_unlock(&dev->graph_mutex);
457 break;
459 case MEDIA_IOC_G_TOPOLOGY:
460 mutex_lock(&dev->graph_mutex);
461 ret = media_device_get_topology(dev,
462 (struct media_v2_topology __user *)arg);
463 mutex_unlock(&dev->graph_mutex);
464 break;
466 default:
467 ret = -ENOIOCTLCMD;
470 return ret;
473 #ifdef CONFIG_COMPAT
475 struct media_links_enum32 {
476 __u32 entity;
477 compat_uptr_t pads; /* struct media_pad_desc * */
478 compat_uptr_t links; /* struct media_link_desc * */
479 __u32 reserved[4];
482 static long media_device_enum_links32(struct media_device *mdev,
483 struct media_links_enum32 __user *ulinks)
485 struct media_links_enum links;
486 compat_uptr_t pads_ptr, links_ptr;
488 memset(&links, 0, sizeof(links));
490 if (get_user(links.entity, &ulinks->entity)
491 || get_user(pads_ptr, &ulinks->pads)
492 || get_user(links_ptr, &ulinks->links))
493 return -EFAULT;
495 links.pads = compat_ptr(pads_ptr);
496 links.links = compat_ptr(links_ptr);
498 return __media_device_enum_links(mdev, &links);
501 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
503 static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
504 unsigned long arg)
506 struct media_devnode *devnode = media_devnode_data(filp);
507 struct media_device *dev = to_media_device(devnode);
508 long ret;
510 switch (cmd) {
511 case MEDIA_IOC_DEVICE_INFO:
512 case MEDIA_IOC_ENUM_ENTITIES:
513 case MEDIA_IOC_SETUP_LINK:
514 case MEDIA_IOC_G_TOPOLOGY:
515 return media_device_ioctl(filp, cmd, arg);
517 case MEDIA_IOC_ENUM_LINKS32:
518 mutex_lock(&dev->graph_mutex);
519 ret = media_device_enum_links32(dev,
520 (struct media_links_enum32 __user *)arg);
521 mutex_unlock(&dev->graph_mutex);
522 break;
524 default:
525 ret = -ENOIOCTLCMD;
528 return ret;
530 #endif /* CONFIG_COMPAT */
532 static const struct media_file_operations media_device_fops = {
533 .owner = THIS_MODULE,
534 .open = media_device_open,
535 .ioctl = media_device_ioctl,
536 #ifdef CONFIG_COMPAT
537 .compat_ioctl = media_device_compat_ioctl,
538 #endif /* CONFIG_COMPAT */
539 .release = media_device_close,
542 /* -----------------------------------------------------------------------------
543 * sysfs
546 static ssize_t show_model(struct device *cd,
547 struct device_attribute *attr, char *buf)
549 struct media_device *mdev = to_media_device(to_media_devnode(cd));
551 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
554 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
556 /* -----------------------------------------------------------------------------
557 * Registration/unregistration
560 static void media_device_release(struct media_devnode *mdev)
562 dev_dbg(mdev->parent, "Media device released\n");
566 * media_device_register_entity - Register an entity with a media device
567 * @mdev: The media device
568 * @entity: The entity
570 int __must_check media_device_register_entity(struct media_device *mdev,
571 struct media_entity *entity)
573 struct media_entity_notify *notify, *next;
574 unsigned int i;
575 int ret;
577 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
578 entity->function == MEDIA_ENT_F_UNKNOWN)
579 dev_warn(mdev->dev,
580 "Entity type for entity %s was not initialized!\n",
581 entity->name);
583 /* Warn if we apparently re-register an entity */
584 WARN_ON(entity->graph_obj.mdev != NULL);
585 entity->graph_obj.mdev = mdev;
586 INIT_LIST_HEAD(&entity->links);
587 entity->num_links = 0;
588 entity->num_backlinks = 0;
590 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
591 return -ENOMEM;
593 spin_lock(&mdev->lock);
595 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
596 &entity->internal_idx);
597 if (ret < 0) {
598 spin_unlock(&mdev->lock);
599 return ret;
602 mdev->entity_internal_idx_max =
603 max(mdev->entity_internal_idx_max, entity->internal_idx);
605 /* Initialize media_gobj embedded at the entity */
606 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
608 /* Initialize objects at the pads */
609 for (i = 0; i < entity->num_pads; i++)
610 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
611 &entity->pads[i].graph_obj);
613 /* invoke entity_notify callbacks */
614 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
615 (notify)->notify(entity, notify->notify_data);
618 spin_unlock(&mdev->lock);
620 mutex_lock(&mdev->graph_mutex);
621 if (mdev->entity_internal_idx_max
622 >= mdev->pm_count_walk.ent_enum.idx_max) {
623 struct media_entity_graph new = { .top = 0 };
626 * Initialise the new graph walk before cleaning up
627 * the old one in order not to spoil the graph walk
628 * object of the media device if graph walk init fails.
630 ret = media_entity_graph_walk_init(&new, mdev);
631 if (ret) {
632 mutex_unlock(&mdev->graph_mutex);
633 return ret;
635 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
636 mdev->pm_count_walk = new;
638 mutex_unlock(&mdev->graph_mutex);
640 return 0;
642 EXPORT_SYMBOL_GPL(media_device_register_entity);
644 static void __media_device_unregister_entity(struct media_entity *entity)
646 struct media_device *mdev = entity->graph_obj.mdev;
647 struct media_link *link, *tmp;
648 struct media_interface *intf;
649 unsigned int i;
651 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
653 /* Remove all interface links pointing to this entity */
654 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
655 list_for_each_entry_safe(link, tmp, &intf->links, list) {
656 if (link->entity == entity)
657 __media_remove_intf_link(link);
661 /* Remove all data links that belong to this entity */
662 __media_entity_remove_links(entity);
664 /* Remove all pads that belong to this entity */
665 for (i = 0; i < entity->num_pads; i++)
666 media_gobj_destroy(&entity->pads[i].graph_obj);
668 /* Remove the entity */
669 media_gobj_destroy(&entity->graph_obj);
671 /* invoke entity_notify callbacks to handle entity removal?? */
673 entity->graph_obj.mdev = NULL;
676 void media_device_unregister_entity(struct media_entity *entity)
678 struct media_device *mdev = entity->graph_obj.mdev;
680 if (mdev == NULL)
681 return;
683 spin_lock(&mdev->lock);
684 __media_device_unregister_entity(entity);
685 spin_unlock(&mdev->lock);
687 EXPORT_SYMBOL_GPL(media_device_unregister_entity);
690 * media_device_init() - initialize a media device
691 * @mdev: The media device
693 * The caller is responsible for initializing the media device before
694 * registration. The following fields must be set:
696 * - dev must point to the parent device
697 * - model must be filled with the device model name
699 void media_device_init(struct media_device *mdev)
701 INIT_LIST_HEAD(&mdev->entities);
702 INIT_LIST_HEAD(&mdev->interfaces);
703 INIT_LIST_HEAD(&mdev->pads);
704 INIT_LIST_HEAD(&mdev->links);
705 INIT_LIST_HEAD(&mdev->entity_notify);
706 spin_lock_init(&mdev->lock);
707 mutex_init(&mdev->graph_mutex);
708 ida_init(&mdev->entity_internal_idx);
710 dev_dbg(mdev->dev, "Media device initialized\n");
712 EXPORT_SYMBOL_GPL(media_device_init);
714 void media_device_cleanup(struct media_device *mdev)
716 ida_destroy(&mdev->entity_internal_idx);
717 mdev->entity_internal_idx_max = 0;
718 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
719 mutex_destroy(&mdev->graph_mutex);
721 EXPORT_SYMBOL_GPL(media_device_cleanup);
723 int __must_check __media_device_register(struct media_device *mdev,
724 struct module *owner)
726 int ret;
728 /* Register the device node. */
729 mdev->devnode.fops = &media_device_fops;
730 mdev->devnode.parent = mdev->dev;
731 mdev->devnode.release = media_device_release;
733 /* Set version 0 to indicate user-space that the graph is static */
734 mdev->topology_version = 0;
736 ret = media_devnode_register(&mdev->devnode, owner);
737 if (ret < 0)
738 return ret;
740 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
741 if (ret < 0) {
742 media_devnode_unregister(&mdev->devnode);
743 return ret;
746 dev_dbg(mdev->dev, "Media device registered\n");
748 return 0;
750 EXPORT_SYMBOL_GPL(__media_device_register);
752 int __must_check media_device_register_entity_notify(struct media_device *mdev,
753 struct media_entity_notify *nptr)
755 spin_lock(&mdev->lock);
756 list_add_tail(&nptr->list, &mdev->entity_notify);
757 spin_unlock(&mdev->lock);
758 return 0;
760 EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
763 * Note: Should be called with mdev->lock held.
765 static void __media_device_unregister_entity_notify(struct media_device *mdev,
766 struct media_entity_notify *nptr)
768 list_del(&nptr->list);
771 void media_device_unregister_entity_notify(struct media_device *mdev,
772 struct media_entity_notify *nptr)
774 spin_lock(&mdev->lock);
775 __media_device_unregister_entity_notify(mdev, nptr);
776 spin_unlock(&mdev->lock);
778 EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
780 void media_device_unregister(struct media_device *mdev)
782 struct media_entity *entity;
783 struct media_entity *next;
784 struct media_interface *intf, *tmp_intf;
785 struct media_entity_notify *notify, *nextp;
787 if (mdev == NULL)
788 return;
790 spin_lock(&mdev->lock);
792 /* Check if mdev was ever registered at all */
793 if (!media_devnode_is_registered(&mdev->devnode)) {
794 spin_unlock(&mdev->lock);
795 return;
798 /* Remove all entities from the media device */
799 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
800 __media_device_unregister_entity(entity);
802 /* Remove all entity_notify callbacks from the media device */
803 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
804 __media_device_unregister_entity_notify(mdev, notify);
806 /* Remove all interfaces from the media device */
807 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
808 graph_obj.list) {
809 __media_remove_intf_links(intf);
810 media_gobj_destroy(&intf->graph_obj);
811 kfree(intf);
814 spin_unlock(&mdev->lock);
816 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
817 media_devnode_unregister(&mdev->devnode);
819 dev_dbg(mdev->dev, "Media device unregistered\n");
821 EXPORT_SYMBOL_GPL(media_device_unregister);
823 static void media_device_release_devres(struct device *dev, void *res)
827 struct media_device *media_device_get_devres(struct device *dev)
829 struct media_device *mdev;
831 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
832 if (mdev)
833 return mdev;
835 mdev = devres_alloc(media_device_release_devres,
836 sizeof(struct media_device), GFP_KERNEL);
837 if (!mdev)
838 return NULL;
839 return devres_get(dev, mdev, NULL, NULL);
841 EXPORT_SYMBOL_GPL(media_device_get_devres);
843 struct media_device *media_device_find_devres(struct device *dev)
845 return devres_find(dev, media_device_release_devres, NULL, NULL);
847 EXPORT_SYMBOL_GPL(media_device_find_devres);
849 void media_device_pci_init(struct media_device *mdev,
850 struct pci_dev *pci_dev,
851 const char *name)
853 #ifdef CONFIG_PCI
854 mdev->dev = &pci_dev->dev;
856 if (name)
857 strlcpy(mdev->model, name, sizeof(mdev->model));
858 else
859 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
861 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
863 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
864 | pci_dev->subsystem_device;
866 mdev->driver_version = LINUX_VERSION_CODE;
868 media_device_init(mdev);
869 #endif
871 EXPORT_SYMBOL_GPL(media_device_pci_init);
873 void __media_device_usb_init(struct media_device *mdev,
874 struct usb_device *udev,
875 const char *board_name,
876 const char *driver_name)
878 #ifdef CONFIG_USB
879 mdev->dev = &udev->dev;
881 if (driver_name)
882 strlcpy(mdev->driver_name, driver_name,
883 sizeof(mdev->driver_name));
885 if (board_name)
886 strlcpy(mdev->model, board_name, sizeof(mdev->model));
887 else if (udev->product)
888 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
889 else
890 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
891 if (udev->serial)
892 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
893 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
894 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
895 mdev->driver_version = LINUX_VERSION_CODE;
897 media_device_init(mdev);
898 #endif
900 EXPORT_SYMBOL_GPL(__media_device_usb_init);
903 #endif /* CONFIG_MEDIA_CONTROLLER */