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 /* -----------------------------------------------------------------------------
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
)
56 static int media_device_close(struct file
*filp
)
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
));
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
)))
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
);
103 spin_unlock(&mdev
->lock
);
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
)))
118 ent
= find_entity(mdev
, u_ent
.id
);
123 u_ent
.id
= media_entity_id(ent
);
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
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
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
)))
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
);
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
)))
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
)
198 memset(&klink_desc
, 0, sizeof(klink_desc
));
199 media_device_kpad_to_upad(link
->source
,
201 media_device_kpad_to_upad(link
->sink
,
203 klink_desc
.flags
= link
->flags
;
204 if (copy_to_user(ulink_desc
, &klink_desc
,
205 sizeof(*ulink_desc
)))
214 static long media_device_enum_links(struct media_device
*mdev
,
215 struct media_links_enum __user
*ulinks
)
217 struct media_links_enum links
;
220 if (copy_from_user(&links
, ulinks
, sizeof(links
)))
223 rval
= __media_device_enum_links(mdev
, &links
);
227 if (copy_to_user(ulinks
, &links
, sizeof(*ulinks
)))
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
;
242 if (copy_from_user(&ulink
, _ulink
, sizeof(ulink
)))
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
)
253 if (ulink
.source
.index
>= source
->num_pads
||
254 ulink
.sink
.index
>= sink
->num_pads
)
257 link
= media_entity_find_link(&source
->pads
[ulink
.source
.index
],
258 &sink
->pads
[ulink
.sink
.index
]);
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
)))
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
;
285 topo
->topology_version
= mdev
->topology_version
;
287 /* Get entities and number of entities */
289 uentity
= media_get_uptr(topo
->ptr_entities
);
290 media_device_for_each_entity(entity
, mdev
) {
295 if (i
> topo
->num_entities
) {
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
)))
311 topo
->num_entities
= i
;
313 /* Get interfaces and number of interfaces */
315 uintf
= media_get_uptr(topo
->ptr_interfaces
);
316 media_device_for_each_intf(intf
, mdev
) {
321 if (i
> topo
->num_interfaces
) {
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
)))
346 topo
->num_interfaces
= i
;
348 /* Get pads and number of pads */
350 upad
= media_get_uptr(topo
->ptr_pads
);
351 media_device_for_each_pad(pad
, mdev
) {
356 if (i
> topo
->num_pads
) {
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
)))
374 /* Get links and number of links */
376 ulink
= media_get_uptr(topo
->ptr_links
);
377 media_device_for_each_link(link
, mdev
) {
378 if (link
->is_backlink
)
386 if (i
> topo
->num_links
) {
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
)))
408 static long media_device_get_topology(struct media_device
*mdev
,
409 struct media_v2_topology __user
*utopo
)
411 struct media_v2_topology ktopo
;
414 if (copy_from_user(&ktopo
, utopo
, sizeof(ktopo
)))
417 ret
= __media_device_get_topology(mdev
, &ktopo
);
421 if (copy_to_user(utopo
, &ktopo
, sizeof(*utopo
)))
427 static long media_device_ioctl(struct file
*filp
, unsigned int cmd
,
430 struct media_devnode
*devnode
= media_devnode_data(filp
);
431 struct media_device
*dev
= to_media_device(devnode
);
435 case MEDIA_IOC_DEVICE_INFO
:
436 ret
= media_device_get_info(dev
,
437 (struct media_device_info __user
*)arg
);
440 case MEDIA_IOC_ENUM_ENTITIES
:
441 ret
= media_device_enum_entities(dev
,
442 (struct media_entity_desc __user
*)arg
);
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
);
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
);
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
);
475 struct media_links_enum32
{
477 compat_uptr_t pads
; /* struct media_pad_desc * */
478 compat_uptr_t links
; /* struct media_link_desc * */
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
))
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
,
506 struct media_devnode
*devnode
= media_devnode_data(filp
);
507 struct media_device
*dev
= to_media_device(devnode
);
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
);
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
,
537 .compat_ioctl
= media_device_compat_ioctl
,
538 #endif /* CONFIG_COMPAT */
539 .release
= media_device_close
,
542 /* -----------------------------------------------------------------------------
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
;
577 if (entity
->function
== MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN
||
578 entity
->function
== MEDIA_ENT_F_UNKNOWN
)
580 "Entity type for entity %s was not initialized!\n",
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
))
593 spin_lock(&mdev
->lock
);
595 ret
= ida_get_new_above(&mdev
->entity_internal_idx
, 1,
596 &entity
->internal_idx
);
598 spin_unlock(&mdev
->lock
);
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
);
632 mutex_unlock(&mdev
->graph_mutex
);
635 media_entity_graph_walk_cleanup(&mdev
->pm_count_walk
);
636 mdev
->pm_count_walk
= new;
638 mutex_unlock(&mdev
->graph_mutex
);
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
;
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
;
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
)
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
);
740 ret
= device_create_file(&mdev
->devnode
.dev
, &dev_attr_model
);
742 media_devnode_unregister(&mdev
->devnode
);
746 dev_dbg(mdev
->dev
, "Media device registered\n");
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
);
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
;
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
);
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
,
809 __media_remove_intf_links(intf
);
810 media_gobj_destroy(&intf
->graph_obj
);
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
);
835 mdev
= devres_alloc(media_device_release_devres
,
836 sizeof(struct media_device
), GFP_KERNEL
);
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
,
854 mdev
->dev
= &pci_dev
->dev
;
857 strlcpy(mdev
->model
, name
, sizeof(mdev
->model
));
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
);
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
)
879 mdev
->dev
= &udev
->dev
;
882 strlcpy(mdev
->driver_name
, driver_name
,
883 sizeof(mdev
->driver_name
));
886 strlcpy(mdev
->model
, board_name
, sizeof(mdev
->model
));
887 else if (udev
->product
)
888 strlcpy(mdev
->model
, udev
->product
, sizeof(mdev
->model
));
890 strlcpy(mdev
->model
, "unknown model", sizeof(mdev
->model
));
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
);
900 EXPORT_SYMBOL_GPL(__media_device_usb_init
);
903 #endif /* CONFIG_MEDIA_CONTROLLER */