Commit | Line | Data |
---|---|---|
176fb0d1 LP |
1 | /* |
2 | * Media device | |
3 | * | |
4 | * Copyright (C) 2010 Nokia Corporation | |
5 | * | |
6 | * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com> | |
7 | * Sakari Ailus <sakari.ailus@iki.fi> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * 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 | |
21 | */ | |
22 | ||
23 | #include <linux/types.h> | |
24 | #include <linux/ioctl.h> | |
25 | ||
26 | #include <media/media-device.h> | |
27 | #include <media/media-devnode.h> | |
53e269c1 | 28 | #include <media/media-entity.h> |
176fb0d1 LP |
29 | |
30 | static const struct media_file_operations media_device_fops = { | |
31 | .owner = THIS_MODULE, | |
32 | }; | |
33 | ||
34 | /* ----------------------------------------------------------------------------- | |
35 | * sysfs | |
36 | */ | |
37 | ||
38 | static ssize_t show_model(struct device *cd, | |
39 | struct device_attribute *attr, char *buf) | |
40 | { | |
41 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); | |
42 | ||
43 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); | |
44 | } | |
45 | ||
46 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); | |
47 | ||
48 | /* ----------------------------------------------------------------------------- | |
49 | * Registration/unregistration | |
50 | */ | |
51 | ||
52 | static void media_device_release(struct media_devnode *mdev) | |
53 | { | |
54 | } | |
55 | ||
56 | /** | |
57 | * media_device_register - register a media device | |
58 | * @mdev: The media device | |
59 | * | |
60 | * The caller is responsible for initializing the media device before | |
61 | * registration. The following fields must be set: | |
62 | * | |
63 | * - dev must point to the parent device | |
64 | * - model must be filled with the device model name | |
65 | */ | |
66 | int __must_check media_device_register(struct media_device *mdev) | |
67 | { | |
68 | int ret; | |
69 | ||
70 | if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0)) | |
71 | return -EINVAL; | |
72 | ||
53e269c1 LP |
73 | mdev->entity_id = 1; |
74 | INIT_LIST_HEAD(&mdev->entities); | |
75 | spin_lock_init(&mdev->lock); | |
76 | ||
176fb0d1 LP |
77 | /* Register the device node. */ |
78 | mdev->devnode.fops = &media_device_fops; | |
79 | mdev->devnode.parent = mdev->dev; | |
80 | mdev->devnode.release = media_device_release; | |
81 | ret = media_devnode_register(&mdev->devnode); | |
82 | if (ret < 0) | |
83 | return ret; | |
84 | ||
85 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); | |
86 | if (ret < 0) { | |
87 | media_devnode_unregister(&mdev->devnode); | |
88 | return ret; | |
89 | } | |
90 | ||
91 | return 0; | |
92 | } | |
93 | EXPORT_SYMBOL_GPL(media_device_register); | |
94 | ||
95 | /** | |
96 | * media_device_unregister - unregister a media device | |
97 | * @mdev: The media device | |
98 | * | |
99 | */ | |
100 | void media_device_unregister(struct media_device *mdev) | |
101 | { | |
53e269c1 LP |
102 | struct media_entity *entity; |
103 | struct media_entity *next; | |
104 | ||
105 | list_for_each_entry_safe(entity, next, &mdev->entities, list) | |
106 | media_device_unregister_entity(entity); | |
107 | ||
176fb0d1 LP |
108 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); |
109 | media_devnode_unregister(&mdev->devnode); | |
110 | } | |
111 | EXPORT_SYMBOL_GPL(media_device_unregister); | |
53e269c1 LP |
112 | |
113 | /** | |
114 | * media_device_register_entity - Register an entity with a media device | |
115 | * @mdev: The media device | |
116 | * @entity: The entity | |
117 | */ | |
118 | int __must_check media_device_register_entity(struct media_device *mdev, | |
119 | struct media_entity *entity) | |
120 | { | |
121 | /* Warn if we apparently re-register an entity */ | |
122 | WARN_ON(entity->parent != NULL); | |
123 | entity->parent = mdev; | |
124 | ||
125 | spin_lock(&mdev->lock); | |
126 | if (entity->id == 0) | |
127 | entity->id = mdev->entity_id++; | |
128 | else | |
129 | mdev->entity_id = max(entity->id + 1, mdev->entity_id); | |
130 | list_add_tail(&entity->list, &mdev->entities); | |
131 | spin_unlock(&mdev->lock); | |
132 | ||
133 | return 0; | |
134 | } | |
135 | EXPORT_SYMBOL_GPL(media_device_register_entity); | |
136 | ||
137 | /** | |
138 | * media_device_unregister_entity - Unregister an entity | |
139 | * @entity: The entity | |
140 | * | |
141 | * If the entity has never been registered this function will return | |
142 | * immediately. | |
143 | */ | |
144 | void media_device_unregister_entity(struct media_entity *entity) | |
145 | { | |
146 | struct media_device *mdev = entity->parent; | |
147 | ||
148 | if (mdev == NULL) | |
149 | return; | |
150 | ||
151 | spin_lock(&mdev->lock); | |
152 | list_del(&entity->list); | |
153 | spin_unlock(&mdev->lock); | |
154 | entity->parent = NULL; | |
155 | } | |
156 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |