Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Componentized device handling. |
| 4 | * |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 5 | * This is work in progress. We gather up the component devices into a list, |
| 6 | * and bind them when instructed. At the moment, we're specific to the DRM |
| 7 | * subsystem, and only handles one master device, but this doesn't have to be |
| 8 | * the case. |
| 9 | */ |
| 10 | #include <linux/component.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kref.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mutex.h> |
| 16 | #include <linux/slab.h> |
Maciej Purski | 59e7385 | 2017-12-01 14:23:16 +0100 | [diff] [blame] | 17 | #include <linux/debugfs.h> |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 18 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 19 | /** |
| 20 | * DOC: overview |
| 21 | * |
| 22 | * The component helper allows drivers to collect a pile of sub-devices, |
| 23 | * including their bound drivers, into an aggregate driver. Various subsystems |
| 24 | * already provide functions to get hold of such components, e.g. |
| 25 | * of_clk_get_by_name(). The component helper can be used when such a |
| 26 | * subsystem-specific way to find a device is not available: The component |
| 27 | * helper fills the niche of aggregate drivers for specific hardware, where |
| 28 | * further standardization into a subsystem would not be practical. The common |
| 29 | * example is when a logical device (e.g. a DRM display driver) is spread around |
| 30 | * the SoC on various component (scanout engines, blending blocks, transcoders |
| 31 | * for various outputs and so on). |
| 32 | * |
| 33 | * The component helper also doesn't solve runtime dependencies, e.g. for system |
| 34 | * suspend and resume operations. See also :ref:`device links<device_link>`. |
| 35 | * |
| 36 | * Components are registered using component_add() and unregistered with |
| 37 | * component_del(), usually from the driver's probe and disconnect functions. |
| 38 | * |
| 39 | * Aggregate drivers first assemble a component match list of what they need |
| 40 | * using component_match_add(). This is then registered as an aggregate driver |
| 41 | * using component_master_add_with_match(), and unregistered using |
| 42 | * component_master_del(). |
| 43 | */ |
| 44 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 45 | struct component; |
| 46 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 47 | struct component_match_array { |
| 48 | void *data; |
| 49 | int (*compare)(struct device *, void *); |
| 50 | void (*release)(struct device *, void *); |
| 51 | struct component *component; |
| 52 | bool duplicate; |
| 53 | }; |
| 54 | |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 55 | struct component_match { |
| 56 | size_t alloc; |
| 57 | size_t num; |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 58 | struct component_match_array *compare; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 61 | struct master { |
| 62 | struct list_head node; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 63 | bool bound; |
| 64 | |
| 65 | const struct component_master_ops *ops; |
| 66 | struct device *dev; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 67 | struct component_match *match; |
Maciej Purski | 59e7385 | 2017-12-01 14:23:16 +0100 | [diff] [blame] | 68 | struct dentry *dentry; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | struct component { |
| 72 | struct list_head node; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 73 | struct master *master; |
| 74 | bool bound; |
| 75 | |
| 76 | const struct component_ops *ops; |
| 77 | struct device *dev; |
| 78 | }; |
| 79 | |
| 80 | static DEFINE_MUTEX(component_mutex); |
| 81 | static LIST_HEAD(component_list); |
| 82 | static LIST_HEAD(masters); |
| 83 | |
Maciej Purski | 59e7385 | 2017-12-01 14:23:16 +0100 | [diff] [blame] | 84 | #ifdef CONFIG_DEBUG_FS |
| 85 | |
| 86 | static struct dentry *component_debugfs_dir; |
| 87 | |
| 88 | static int component_devices_show(struct seq_file *s, void *data) |
| 89 | { |
| 90 | struct master *m = s->private; |
| 91 | struct component_match *match = m->match; |
| 92 | size_t i; |
| 93 | |
| 94 | mutex_lock(&component_mutex); |
| 95 | seq_printf(s, "%-40s %20s\n", "master name", "status"); |
| 96 | seq_puts(s, "-------------------------------------------------------------\n"); |
| 97 | seq_printf(s, "%-40s %20s\n\n", |
| 98 | dev_name(m->dev), m->bound ? "bound" : "not bound"); |
| 99 | |
| 100 | seq_printf(s, "%-40s %20s\n", "device name", "status"); |
| 101 | seq_puts(s, "-------------------------------------------------------------\n"); |
| 102 | for (i = 0; i < match->num; i++) { |
| 103 | struct device *d = (struct device *)match->compare[i].data; |
| 104 | |
| 105 | seq_printf(s, "%-40s %20s\n", dev_name(d), |
| 106 | match->compare[i].component ? |
| 107 | "registered" : "not registered"); |
| 108 | } |
| 109 | mutex_unlock(&component_mutex); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Yangtao Li | c0b8a87 | 2018-12-15 03:36:36 -0500 | [diff] [blame] | 114 | DEFINE_SHOW_ATTRIBUTE(component_devices); |
Maciej Purski | 59e7385 | 2017-12-01 14:23:16 +0100 | [diff] [blame] | 115 | |
| 116 | static int __init component_debug_init(void) |
| 117 | { |
| 118 | component_debugfs_dir = debugfs_create_dir("device_component", NULL); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | core_initcall(component_debug_init); |
| 124 | |
| 125 | static void component_master_debugfs_add(struct master *m) |
| 126 | { |
| 127 | m->dentry = debugfs_create_file(dev_name(m->dev), 0444, |
| 128 | component_debugfs_dir, |
| 129 | m, &component_devices_fops); |
| 130 | } |
| 131 | |
| 132 | static void component_master_debugfs_del(struct master *m) |
| 133 | { |
| 134 | debugfs_remove(m->dentry); |
| 135 | m->dentry = NULL; |
| 136 | } |
| 137 | |
| 138 | #else |
| 139 | |
| 140 | static void component_master_debugfs_add(struct master *m) |
| 141 | { } |
| 142 | |
| 143 | static void component_master_debugfs_del(struct master *m) |
| 144 | { } |
| 145 | |
| 146 | #endif |
| 147 | |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 148 | static struct master *__master_find(struct device *dev, |
| 149 | const struct component_master_ops *ops) |
| 150 | { |
| 151 | struct master *m; |
| 152 | |
| 153 | list_for_each_entry(m, &masters, node) |
| 154 | if (m->dev == dev && (!ops || m->ops == ops)) |
| 155 | return m; |
| 156 | |
| 157 | return NULL; |
| 158 | } |
| 159 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 160 | static struct component *find_component(struct master *master, |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 161 | int (*compare)(struct device *, void *), void *compare_data) |
| 162 | { |
| 163 | struct component *c; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 164 | |
| 165 | list_for_each_entry(c, &component_list, node) { |
Russell King | fcbcebc | 2014-04-18 20:16:22 +0100 | [diff] [blame] | 166 | if (c->master && c->master != master) |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 167 | continue; |
| 168 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 169 | if (compare(c->dev, compare_data)) |
| 170 | return c; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 173 | return NULL; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 174 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 175 | |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 176 | static int find_components(struct master *master) |
| 177 | { |
| 178 | struct component_match *match = master->match; |
| 179 | size_t i; |
| 180 | int ret = 0; |
| 181 | |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 182 | /* |
| 183 | * Scan the array of match functions and attach |
| 184 | * any components which are found to this master. |
| 185 | */ |
| 186 | for (i = 0; i < match->num; i++) { |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 187 | struct component_match_array *mc = &match->compare[i]; |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 188 | struct component *c; |
| 189 | |
| 190 | dev_dbg(master->dev, "Looking for component %zu\n", i); |
| 191 | |
| 192 | if (match->compare[i].component) |
| 193 | continue; |
| 194 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 195 | c = find_component(master, mc->compare, mc->data); |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 196 | if (!c) { |
| 197 | ret = -ENXIO; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 198 | break; |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master); |
| 202 | |
| 203 | /* Attach this component to the master */ |
| 204 | match->compare[i].duplicate = !!c->master; |
| 205 | match->compare[i].component = c; |
| 206 | c->master = master; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 207 | } |
| 208 | return ret; |
| 209 | } |
| 210 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 211 | /* Detach component from associated master */ |
| 212 | static void remove_component(struct master *master, struct component *c) |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 213 | { |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 214 | size_t i; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 215 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 216 | /* Detach the component from this master. */ |
| 217 | for (i = 0; i < master->match->num; i++) |
| 218 | if (master->match->compare[i].component == c) |
| 219 | master->match->compare[i].component = NULL; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Try to bring up a master. If component is NULL, we're interested in |
| 224 | * this master, otherwise it's a component which must be present to try |
| 225 | * and bring up the master. |
| 226 | * |
| 227 | * Returns 1 for successful bringup, 0 if not ready, or -ve errno. |
| 228 | */ |
| 229 | static int try_to_bring_up_master(struct master *master, |
| 230 | struct component *component) |
| 231 | { |
Russell King | c334940 | 2014-04-23 10:52:17 +0100 | [diff] [blame] | 232 | int ret; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 233 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 234 | dev_dbg(master->dev, "trying to bring up master\n"); |
| 235 | |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 236 | if (find_components(master)) { |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 237 | dev_dbg(master->dev, "master has incomplete components\n"); |
| 238 | return 0; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 239 | } |
Russell King | c334940 | 2014-04-23 10:52:17 +0100 | [diff] [blame] | 240 | |
| 241 | if (component && component->master != master) { |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 242 | dev_dbg(master->dev, "master is not for this component (%s)\n", |
| 243 | dev_name(component->dev)); |
| 244 | return 0; |
Russell King | c334940 | 2014-04-23 10:52:17 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 247 | if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) |
| 248 | return -ENOMEM; |
Russell King | c334940 | 2014-04-23 10:52:17 +0100 | [diff] [blame] | 249 | |
| 250 | /* Found all components */ |
| 251 | ret = master->ops->bind(master->dev); |
| 252 | if (ret < 0) { |
| 253 | devres_release_group(master->dev, NULL); |
| 254 | dev_info(master->dev, "master bind failed: %d\n", ret); |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 255 | return ret; |
Russell King | c334940 | 2014-04-23 10:52:17 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | master->bound = true; |
| 259 | return 1; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static int try_to_bring_up_masters(struct component *component) |
| 263 | { |
| 264 | struct master *m; |
| 265 | int ret = 0; |
| 266 | |
| 267 | list_for_each_entry(m, &masters, node) { |
Russell King | 29f1c7f | 2014-04-23 10:46:11 +0100 | [diff] [blame] | 268 | if (!m->bound) { |
| 269 | ret = try_to_bring_up_master(m, component); |
| 270 | if (ret != 0) |
| 271 | break; |
| 272 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static void take_down_master(struct master *master) |
| 279 | { |
| 280 | if (master->bound) { |
| 281 | master->ops->unbind(master->dev); |
Russell King | 9e1ccb4 | 2014-02-07 20:09:27 +0000 | [diff] [blame] | 282 | devres_release_group(master->dev, NULL); |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 283 | master->bound = false; |
| 284 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 287 | static void component_match_release(struct device *master, |
| 288 | struct component_match *match) |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 289 | { |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 290 | unsigned int i; |
| 291 | |
| 292 | for (i = 0; i < match->num; i++) { |
| 293 | struct component_match_array *mc = &match->compare[i]; |
| 294 | |
| 295 | if (mc->release) |
| 296 | mc->release(master, mc->data); |
| 297 | } |
Russell King | 9a4e784 | 2016-01-26 14:49:22 +0000 | [diff] [blame] | 298 | |
| 299 | kfree(match->compare); |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 302 | static void devm_component_match_release(struct device *dev, void *res) |
| 303 | { |
| 304 | component_match_release(dev, res); |
| 305 | } |
| 306 | |
| 307 | static int component_match_realloc(struct device *dev, |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 308 | struct component_match *match, size_t num) |
| 309 | { |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 310 | struct component_match_array *new; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 311 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 312 | if (match->alloc == num) |
| 313 | return 0; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 314 | |
Russell King | 9a4e784 | 2016-01-26 14:49:22 +0000 | [diff] [blame] | 315 | new = kmalloc_array(num, sizeof(*new), GFP_KERNEL); |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 316 | if (!new) |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 317 | return -ENOMEM; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 318 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 319 | if (match->compare) { |
| 320 | memcpy(new, match->compare, sizeof(*new) * |
| 321 | min(match->num, num)); |
Russell King | 9a4e784 | 2016-01-26 14:49:22 +0000 | [diff] [blame] | 322 | kfree(match->compare); |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 323 | } |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 324 | match->compare = new; |
| 325 | match->alloc = num; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 326 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 327 | return 0; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 330 | /** |
| 331 | * component_match_add_release - add a component match with release callback |
| 332 | * @master: device with the aggregate driver |
| 333 | * @matchptr: pointer to the list of component matches |
| 334 | * @release: release function for @compare_data |
| 335 | * @compare: compare function to match against all components |
| 336 | * @compare_data: opaque pointer passed to the @compare function |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 337 | * |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 338 | * Adds a new component match to the list stored in @matchptr, which the @master |
| 339 | * aggregate driver needs to function. The list of component matches pointed to |
| 340 | * by @matchptr must be initialized to NULL before adding the first match. |
| 341 | * |
| 342 | * The allocated match list in @matchptr is automatically released using devm |
| 343 | * actions, where upon @release will be called to free any references held by |
| 344 | * @compare_data, e.g. when @compare_data is a &device_node that must be |
| 345 | * released with of_node_put(). |
| 346 | * |
| 347 | * See also component_match_add(). |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 348 | */ |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 349 | void component_match_add_release(struct device *master, |
| 350 | struct component_match **matchptr, |
| 351 | void (*release)(struct device *, void *), |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 352 | int (*compare)(struct device *, void *), void *compare_data) |
| 353 | { |
| 354 | struct component_match *match = *matchptr; |
| 355 | |
| 356 | if (IS_ERR(match)) |
| 357 | return; |
| 358 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 359 | if (!match) { |
| 360 | match = devres_alloc(devm_component_match_release, |
| 361 | sizeof(*match), GFP_KERNEL); |
| 362 | if (!match) { |
| 363 | *matchptr = ERR_PTR(-ENOMEM); |
| 364 | return; |
| 365 | } |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 366 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 367 | devres_add(master, match); |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 368 | |
| 369 | *matchptr = match; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 372 | if (match->num == match->alloc) { |
Sudip Mukherjee | 4877bb9 | 2016-02-02 12:57:53 +0530 | [diff] [blame] | 373 | size_t new_size = match->alloc + 16; |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 374 | int ret; |
| 375 | |
| 376 | ret = component_match_realloc(master, match, new_size); |
| 377 | if (ret) { |
| 378 | *matchptr = ERR_PTR(ret); |
| 379 | return; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | match->compare[match->num].compare = compare; |
| 384 | match->compare[match->num].release = release; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 385 | match->compare[match->num].data = compare_data; |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 386 | match->compare[match->num].component = NULL; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 387 | match->num++; |
| 388 | } |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 389 | EXPORT_SYMBOL(component_match_add_release); |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 390 | |
Jon Medhurst (Tixy) | 5748048 | 2016-01-26 17:59:13 +0000 | [diff] [blame] | 391 | static void free_master(struct master *master) |
| 392 | { |
| 393 | struct component_match *match = master->match; |
| 394 | int i; |
| 395 | |
Maciej Purski | 59e7385 | 2017-12-01 14:23:16 +0100 | [diff] [blame] | 396 | component_master_debugfs_del(master); |
Jon Medhurst (Tixy) | 5748048 | 2016-01-26 17:59:13 +0000 | [diff] [blame] | 397 | list_del(&master->node); |
| 398 | |
| 399 | if (match) { |
| 400 | for (i = 0; i < match->num; i++) { |
| 401 | struct component *c = match->compare[i].component; |
| 402 | if (c) |
| 403 | c->master = NULL; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | kfree(master); |
| 408 | } |
| 409 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 410 | /** |
| 411 | * component_master_add_with_match - register an aggregate driver |
| 412 | * @dev: device with the aggregate driver |
| 413 | * @ops: callbacks for the aggregate driver |
| 414 | * @match: component match list for the aggregate driver |
| 415 | * |
| 416 | * Registers a new aggregate driver consisting of the components added to @match |
| 417 | * by calling one of the component_match_add() functions. Once all components in |
| 418 | * @match are available, it will be assembled by calling |
| 419 | * &component_master_ops.bind from @ops. Must be unregistered by calling |
| 420 | * component_master_del(). |
| 421 | */ |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 422 | int component_master_add_with_match(struct device *dev, |
| 423 | const struct component_master_ops *ops, |
| 424 | struct component_match *match) |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 425 | { |
| 426 | struct master *master; |
| 427 | int ret; |
| 428 | |
Russell King | fae9e2e | 2014-04-18 22:10:32 +0100 | [diff] [blame] | 429 | /* Reallocate the match array for its true size */ |
Russell King | ce657b1 | 2015-11-17 12:08:01 +0000 | [diff] [blame] | 430 | ret = component_match_realloc(dev, match, match->num); |
| 431 | if (ret) |
| 432 | return ret; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 433 | |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 434 | master = kzalloc(sizeof(*master), GFP_KERNEL); |
| 435 | if (!master) |
| 436 | return -ENOMEM; |
| 437 | |
| 438 | master->dev = dev; |
| 439 | master->ops = ops; |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 440 | master->match = match; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 441 | |
Maciej Purski | 59e7385 | 2017-12-01 14:23:16 +0100 | [diff] [blame] | 442 | component_master_debugfs_add(master); |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 443 | /* Add to the list of available masters. */ |
| 444 | mutex_lock(&component_mutex); |
| 445 | list_add(&master->node, &masters); |
| 446 | |
| 447 | ret = try_to_bring_up_master(master, NULL); |
| 448 | |
Jon Medhurst (Tixy) | 5748048 | 2016-01-26 17:59:13 +0000 | [diff] [blame] | 449 | if (ret < 0) |
| 450 | free_master(master); |
| 451 | |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 452 | mutex_unlock(&component_mutex); |
| 453 | |
| 454 | return ret < 0 ? ret : 0; |
| 455 | } |
Russell King | 6955b58 | 2014-04-19 11:18:01 +0100 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(component_master_add_with_match); |
| 457 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 458 | /** |
| 459 | * component_master_del - unregister an aggregate driver |
| 460 | * @dev: device with the aggregate driver |
| 461 | * @ops: callbacks for the aggregate driver |
| 462 | * |
| 463 | * Unregisters an aggregate driver registered with |
| 464 | * component_master_add_with_match(). If necessary the aggregate driver is first |
| 465 | * disassembled by calling &component_master_ops.unbind from @ops. |
| 466 | */ |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 467 | void component_master_del(struct device *dev, |
| 468 | const struct component_master_ops *ops) |
| 469 | { |
| 470 | struct master *master; |
| 471 | |
| 472 | mutex_lock(&component_mutex); |
| 473 | master = __master_find(dev, ops); |
| 474 | if (master) { |
| 475 | take_down_master(master); |
Jon Medhurst (Tixy) | 5748048 | 2016-01-26 17:59:13 +0000 | [diff] [blame] | 476 | free_master(master); |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 477 | } |
| 478 | mutex_unlock(&component_mutex); |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(component_master_del); |
| 481 | |
| 482 | static void component_unbind(struct component *component, |
| 483 | struct master *master, void *data) |
| 484 | { |
| 485 | WARN_ON(!component->bound); |
| 486 | |
| 487 | component->ops->unbind(component->dev, master->dev, data); |
| 488 | component->bound = false; |
| 489 | |
| 490 | /* Release all resources claimed in the binding of this component */ |
| 491 | devres_release_group(component->dev, component); |
| 492 | } |
| 493 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 494 | /** |
| 495 | * component_unbind_all - unbind all component to an aggregate driver |
| 496 | * @master_dev: device with the aggregate driver |
| 497 | * @data: opaque pointer, passed to all components |
| 498 | * |
| 499 | * Unbinds all components to the aggregate @dev by passing @data to their |
| 500 | * &component_ops.unbind functions. Should be called from |
| 501 | * &component_master_ops.unbind. |
| 502 | */ |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 503 | void component_unbind_all(struct device *master_dev, void *data) |
| 504 | { |
| 505 | struct master *master; |
| 506 | struct component *c; |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 507 | size_t i; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 508 | |
| 509 | WARN_ON(!mutex_is_locked(&component_mutex)); |
| 510 | |
| 511 | master = __master_find(master_dev, NULL); |
| 512 | if (!master) |
| 513 | return; |
| 514 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 515 | /* Unbind components in reverse order */ |
| 516 | for (i = master->match->num; i--; ) |
| 517 | if (!master->match->compare[i].duplicate) { |
| 518 | c = master->match->compare[i].component; |
| 519 | component_unbind(c, master, data); |
| 520 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 521 | } |
| 522 | EXPORT_SYMBOL_GPL(component_unbind_all); |
| 523 | |
| 524 | static int component_bind(struct component *component, struct master *master, |
| 525 | void *data) |
| 526 | { |
| 527 | int ret; |
| 528 | |
| 529 | /* |
| 530 | * Each component initialises inside its own devres group. |
| 531 | * This allows us to roll-back a failed component without |
| 532 | * affecting anything else. |
| 533 | */ |
| 534 | if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) |
| 535 | return -ENOMEM; |
| 536 | |
| 537 | /* |
| 538 | * Also open a group for the device itself: this allows us |
| 539 | * to release the resources claimed against the sub-device |
| 540 | * at the appropriate moment. |
| 541 | */ |
| 542 | if (!devres_open_group(component->dev, component, GFP_KERNEL)) { |
| 543 | devres_release_group(master->dev, NULL); |
| 544 | return -ENOMEM; |
| 545 | } |
| 546 | |
| 547 | dev_dbg(master->dev, "binding %s (ops %ps)\n", |
| 548 | dev_name(component->dev), component->ops); |
| 549 | |
| 550 | ret = component->ops->bind(component->dev, master->dev, data); |
| 551 | if (!ret) { |
| 552 | component->bound = true; |
| 553 | |
| 554 | /* |
| 555 | * Close the component device's group so that resources |
| 556 | * allocated in the binding are encapsulated for removal |
| 557 | * at unbind. Remove the group on the DRM device as we |
| 558 | * can clean those resources up independently. |
| 559 | */ |
| 560 | devres_close_group(component->dev, NULL); |
| 561 | devres_remove_group(master->dev, NULL); |
| 562 | |
| 563 | dev_info(master->dev, "bound %s (ops %ps)\n", |
| 564 | dev_name(component->dev), component->ops); |
| 565 | } else { |
| 566 | devres_release_group(component->dev, NULL); |
| 567 | devres_release_group(master->dev, NULL); |
| 568 | |
| 569 | dev_err(master->dev, "failed to bind %s (ops %ps): %d\n", |
| 570 | dev_name(component->dev), component->ops, ret); |
| 571 | } |
| 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 576 | /** |
| 577 | * component_bind_all - bind all component to an aggregate driver |
| 578 | * @master_dev: device with the aggregate driver |
| 579 | * @data: opaque pointer, passed to all components |
| 580 | * |
| 581 | * Binds all components to the aggregate @dev by passing @data to their |
| 582 | * &component_ops.bind functions. Should be called from |
| 583 | * &component_master_ops.bind. |
| 584 | */ |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 585 | int component_bind_all(struct device *master_dev, void *data) |
| 586 | { |
| 587 | struct master *master; |
| 588 | struct component *c; |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 589 | size_t i; |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 590 | int ret = 0; |
| 591 | |
| 592 | WARN_ON(!mutex_is_locked(&component_mutex)); |
| 593 | |
| 594 | master = __master_find(master_dev, NULL); |
| 595 | if (!master) |
| 596 | return -EINVAL; |
| 597 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 598 | /* Bind components in match order */ |
| 599 | for (i = 0; i < master->match->num; i++) |
| 600 | if (!master->match->compare[i].duplicate) { |
| 601 | c = master->match->compare[i].component; |
| 602 | ret = component_bind(c, master, data); |
| 603 | if (ret) |
| 604 | break; |
| 605 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 606 | |
| 607 | if (ret != 0) { |
Banajit Goswami | bdae566 | 2018-08-27 21:15:39 -0700 | [diff] [blame] | 608 | for (; i > 0; i--) |
| 609 | if (!master->match->compare[i - 1].duplicate) { |
| 610 | c = master->match->compare[i - 1].component; |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 611 | component_unbind(c, master, data); |
| 612 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | return ret; |
| 616 | } |
| 617 | EXPORT_SYMBOL_GPL(component_bind_all); |
| 618 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 619 | /** |
| 620 | * component_add - register a component |
| 621 | * @dev: component device |
| 622 | * @ops: component callbacks |
| 623 | * |
| 624 | * Register a new component for @dev. Functions in @ops will be called when the |
| 625 | * aggregate driver is ready to bind the overall driver by calling |
| 626 | * component_bind_all(). See also &struct component_ops. |
| 627 | * |
| 628 | * The component needs to be unregistered at driver unload/disconnect by calling |
| 629 | * component_del(). |
| 630 | */ |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 631 | int component_add(struct device *dev, const struct component_ops *ops) |
| 632 | { |
| 633 | struct component *component; |
| 634 | int ret; |
| 635 | |
| 636 | component = kzalloc(sizeof(*component), GFP_KERNEL); |
| 637 | if (!component) |
| 638 | return -ENOMEM; |
| 639 | |
| 640 | component->ops = ops; |
| 641 | component->dev = dev; |
| 642 | |
| 643 | dev_dbg(dev, "adding component (ops %ps)\n", ops); |
| 644 | |
| 645 | mutex_lock(&component_mutex); |
| 646 | list_add_tail(&component->node, &component_list); |
| 647 | |
| 648 | ret = try_to_bring_up_masters(component); |
| 649 | if (ret < 0) { |
Daniel Stone | 8e7199c | 2016-02-08 21:12:58 +0000 | [diff] [blame] | 650 | if (component->master) |
| 651 | remove_component(component->master, component); |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 652 | list_del(&component->node); |
| 653 | |
| 654 | kfree(component); |
| 655 | } |
| 656 | mutex_unlock(&component_mutex); |
| 657 | |
| 658 | return ret < 0 ? ret : 0; |
| 659 | } |
| 660 | EXPORT_SYMBOL_GPL(component_add); |
| 661 | |
Daniel Vetter | 4d69c80 | 2019-02-08 00:27:56 +0100 | [diff] [blame^] | 662 | /** |
| 663 | * component_del - unregister a component |
| 664 | * @dev: component device |
| 665 | * @ops: component callbacks |
| 666 | * |
| 667 | * Unregister a component added with component_add(). If the component is bound |
| 668 | * into an aggregate driver, this will force the entire aggregate driver, including |
| 669 | * all its components, to be unbound. |
| 670 | */ |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 671 | void component_del(struct device *dev, const struct component_ops *ops) |
| 672 | { |
| 673 | struct component *c, *component = NULL; |
| 674 | |
| 675 | mutex_lock(&component_mutex); |
| 676 | list_for_each_entry(c, &component_list, node) |
| 677 | if (c->dev == dev && c->ops == ops) { |
| 678 | list_del(&c->node); |
| 679 | component = c; |
| 680 | break; |
| 681 | } |
| 682 | |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 683 | if (component && component->master) { |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 684 | take_down_master(component->master); |
Russell King | ffc30b7 | 2014-04-18 23:05:53 +0100 | [diff] [blame] | 685 | remove_component(component->master, component); |
| 686 | } |
Russell King | 2a41e60 | 2014-01-10 23:23:37 +0000 | [diff] [blame] | 687 | |
| 688 | mutex_unlock(&component_mutex); |
| 689 | |
| 690 | WARN_ON(!component); |
| 691 | kfree(component); |
| 692 | } |
| 693 | EXPORT_SYMBOL_GPL(component_del); |
| 694 | |
| 695 | MODULE_LICENSE("GPL v2"); |