Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * attribute_container.c - implementation of a simple container for classes |
| 4 | * |
| 5 | * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com> |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * The basic idea here is to enable a device to be attached to an |
| 8 | * aritrary numer of classes without having to allocate storage for them. |
| 9 | * Instead, the contained classes select the devices they need to attach |
| 10 | * to via a matching function. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/attribute_container.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/module.h> |
Michael S. Tsirkin | f8916c1 | 2007-06-10 22:39:12 +0300 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 21 | #include "base.h" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /* This is a private structure used to tie the classdev and the |
| 24 | * container .. it should never be visible outside this file */ |
| 25 | struct internal_container { |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 26 | struct klist_node node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct attribute_container *cont; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 28 | struct device classdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 31 | static void internal_container_klist_get(struct klist_node *n) |
| 32 | { |
| 33 | struct internal_container *ic = |
| 34 | container_of(n, struct internal_container, node); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 35 | get_device(&ic->classdev); |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static void internal_container_klist_put(struct klist_node *n) |
| 39 | { |
| 40 | struct internal_container *ic = |
| 41 | container_of(n, struct internal_container, node); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 42 | put_device(&ic->classdev); |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /** |
| 47 | * attribute_container_classdev_to_container - given a classdev, return the container |
| 48 | * |
| 49 | * @classdev: the class device created by attribute_container_add_device. |
| 50 | * |
| 51 | * Returns the container associated with this classdev. |
| 52 | */ |
| 53 | struct attribute_container * |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 54 | attribute_container_classdev_to_container(struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | struct internal_container *ic = |
| 57 | container_of(classdev, struct internal_container, classdev); |
| 58 | return ic->cont; |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container); |
| 61 | |
Denis Cheng | db1118a | 2007-12-06 02:24:40 +0800 | [diff] [blame] | 62 | static LIST_HEAD(attribute_container_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 64 | static DEFINE_MUTEX(attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * attribute_container_register - register an attribute container |
| 68 | * |
| 69 | * @cont: The container to register. This must be allocated by the |
| 70 | * callee and should also be zeroed by it. |
| 71 | */ |
| 72 | int |
| 73 | attribute_container_register(struct attribute_container *cont) |
| 74 | { |
| 75 | INIT_LIST_HEAD(&cont->node); |
Tina Johnson | 287f9bd | 2014-08-12 02:54:09 +0530 | [diff] [blame] | 76 | klist_init(&cont->containers, internal_container_klist_get, |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 77 | internal_container_klist_put); |
Tina Johnson | 24a7d36 | 2014-08-12 02:54:08 +0530 | [diff] [blame] | 78 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 79 | mutex_lock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | list_add_tail(&cont->node, &attribute_container_list); |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 81 | mutex_unlock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(attribute_container_register); |
| 86 | |
| 87 | /** |
| 88 | * attribute_container_unregister - remove a container registration |
| 89 | * |
| 90 | * @cont: previously registered container to remove |
| 91 | */ |
| 92 | int |
| 93 | attribute_container_unregister(struct attribute_container *cont) |
| 94 | { |
| 95 | int retval = -EBUSY; |
Cosmin Dragomir | 481026d | 2015-03-08 12:30:14 +0200 | [diff] [blame] | 96 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 97 | mutex_lock(&attribute_container_mutex); |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 98 | spin_lock(&cont->containers.k_lock); |
| 99 | if (!list_empty(&cont->containers.k_list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | goto out; |
| 101 | retval = 0; |
| 102 | list_del(&cont->node); |
| 103 | out: |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 104 | spin_unlock(&cont->containers.k_lock); |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 105 | mutex_unlock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return retval; |
Tina Johnson | 24a7d36 | 2014-08-12 02:54:08 +0530 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL_GPL(attribute_container_unregister); |
| 110 | |
| 111 | /* private function used as class release */ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 112 | static void attribute_container_release(struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Tina Johnson | 24a7d36 | 2014-08-12 02:54:08 +0530 | [diff] [blame] | 114 | struct internal_container *ic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | = container_of(classdev, struct internal_container, classdev); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 116 | struct device *dev = classdev->parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | kfree(ic); |
| 119 | put_device(dev); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * attribute_container_add_device - see if any container is interested in dev |
| 124 | * |
| 125 | * @dev: device to add attributes to |
| 126 | * @fn: function to trigger addition of class device. |
| 127 | * |
| 128 | * This function allocates storage for the class device(s) to be |
| 129 | * attached to dev (one for each matching attribute_container). If no |
| 130 | * fn is provided, the code will simply register the class device via |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 131 | * device_add. If a function is provided, it is expected to add |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * the class device at the appropriate time. One of the things that |
| 133 | * might be necessary is to allocate and initialise the classdev and |
| 134 | * then add it a later time. To do this, call this routine for |
| 135 | * allocation and initialisation and then use |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 136 | * attribute_container_device_trigger() to call device_add() on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * it. Note: after this, the class device contains a reference to dev |
| 138 | * which is not relinquished until the release of the classdev. |
| 139 | */ |
| 140 | void |
| 141 | attribute_container_add_device(struct device *dev, |
| 142 | int (*fn)(struct attribute_container *, |
| 143 | struct device *, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 144 | struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | struct attribute_container *cont; |
| 147 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 148 | mutex_lock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 150 | struct internal_container *ic; |
| 151 | |
| 152 | if (attribute_container_no_classdevs(cont)) |
| 153 | continue; |
| 154 | |
| 155 | if (!cont->match(cont, dev)) |
| 156 | continue; |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 157 | |
| 158 | ic = kzalloc(sizeof(*ic), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (!ic) { |
Joe Perches | a369a7e | 2012-10-28 01:05:41 -0700 | [diff] [blame] | 160 | dev_err(dev, "failed to allocate class container\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | continue; |
| 162 | } |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | ic->cont = cont; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 165 | device_initialize(&ic->classdev); |
| 166 | ic->classdev.parent = get_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | ic->classdev.class = cont->class; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 168 | cont->class->dev_release = attribute_container_release; |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 169 | dev_set_name(&ic->classdev, "%s", dev_name(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (fn) |
| 171 | fn(cont, dev, &ic->classdev); |
| 172 | else |
| 173 | attribute_container_add_class_device(&ic->classdev); |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 174 | klist_add_tail(&ic->node, &cont->containers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 176 | mutex_unlock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 179 | /* FIXME: can't break out of this unless klist_iter_exit is also |
| 180 | * called before doing the break |
| 181 | */ |
| 182 | #define klist_for_each_entry(pos, head, member, iter) \ |
| 183 | for (klist_iter_init(head, iter); (pos = ({ \ |
| 184 | struct klist_node *n = klist_next(iter); \ |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 185 | n ? container_of(n, typeof(*pos), member) : \ |
| 186 | ({ klist_iter_exit(iter) ; NULL; }); \ |
Tina Johnson | 287f9bd | 2014-08-12 02:54:09 +0530 | [diff] [blame] | 187 | })) != NULL;) |
Tina Johnson | 24a7d36 | 2014-08-12 02:54:08 +0530 | [diff] [blame] | 188 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /** |
| 191 | * attribute_container_remove_device - make device eligible for removal. |
| 192 | * |
| 193 | * @dev: The generic device |
| 194 | * @fn: A function to call to remove the device |
| 195 | * |
| 196 | * This routine triggers device removal. If fn is NULL, then it is |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 197 | * simply done via device_unregister (note that if something |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | * still has a reference to the classdev, then the memory occupied |
| 199 | * will not be freed until the classdev is released). If you want a |
| 200 | * two phase release: remove from visibility and then delete the |
| 201 | * device, then you should use this routine with a fn that calls |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 202 | * device_del() and then use attribute_container_device_trigger() |
| 203 | * to do the final put on the classdev. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | */ |
| 205 | void |
| 206 | attribute_container_remove_device(struct device *dev, |
| 207 | void (*fn)(struct attribute_container *, |
| 208 | struct device *, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 209 | struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | struct attribute_container *cont; |
| 212 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 213 | mutex_lock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | list_for_each_entry(cont, &attribute_container_list, node) { |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 215 | struct internal_container *ic; |
| 216 | struct klist_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | if (attribute_container_no_classdevs(cont)) |
| 219 | continue; |
| 220 | |
| 221 | if (!cont->match(cont, dev)) |
| 222 | continue; |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 223 | |
| 224 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 225 | if (dev != ic->classdev.parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | continue; |
Linus Torvalds | caf39e8 | 2005-09-07 18:44:33 -0700 | [diff] [blame] | 227 | klist_del(&ic->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (fn) |
| 229 | fn(cont, dev, &ic->classdev); |
| 230 | else { |
| 231 | attribute_container_remove_attrs(&ic->classdev); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 232 | device_unregister(&ic->classdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 236 | mutex_unlock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Gabriel Krisman Bertazi | 7c1ef33 | 2020-01-06 13:58:15 -0500 | [diff] [blame] | 239 | static int |
| 240 | do_attribute_container_device_trigger_safe(struct device *dev, |
| 241 | struct attribute_container *cont, |
| 242 | int (*fn)(struct attribute_container *, |
| 243 | struct device *, struct device *), |
| 244 | int (*undo)(struct attribute_container *, |
| 245 | struct device *, struct device *)) |
| 246 | { |
| 247 | int ret; |
| 248 | struct internal_container *ic, *failed; |
| 249 | struct klist_iter iter; |
| 250 | |
| 251 | if (attribute_container_no_classdevs(cont)) |
| 252 | return fn(cont, dev, NULL); |
| 253 | |
| 254 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
| 255 | if (dev == ic->classdev.parent) { |
| 256 | ret = fn(cont, dev, &ic->classdev); |
| 257 | if (ret) { |
| 258 | failed = ic; |
| 259 | klist_iter_exit(&iter); |
| 260 | goto fail; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | return 0; |
| 265 | |
| 266 | fail: |
| 267 | if (!undo) |
| 268 | return ret; |
| 269 | |
| 270 | /* Attempt to undo the work partially done. */ |
| 271 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
| 272 | if (ic == failed) { |
| 273 | klist_iter_exit(&iter); |
| 274 | break; |
| 275 | } |
| 276 | if (dev == ic->classdev.parent) |
| 277 | undo(cont, dev, &ic->classdev); |
| 278 | } |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * attribute_container_device_trigger_safe - execute a trigger for each |
| 284 | * matching classdev or fail all of them. |
| 285 | * |
| 286 | * @dev: The generic device to run the trigger for |
| 287 | * @fn the function to execute for each classdev. |
| 288 | * @undo A function to undo the work previously done in case of error |
| 289 | * |
| 290 | * This function is a safe version of |
| 291 | * attribute_container_device_trigger. It stops on the first error and |
| 292 | * undo the partial work that has been done, on previous classdev. It |
| 293 | * is guaranteed that either they all succeeded, or none of them |
| 294 | * succeeded. |
| 295 | */ |
| 296 | int |
| 297 | attribute_container_device_trigger_safe(struct device *dev, |
| 298 | int (*fn)(struct attribute_container *, |
| 299 | struct device *, |
| 300 | struct device *), |
| 301 | int (*undo)(struct attribute_container *, |
| 302 | struct device *, |
| 303 | struct device *)) |
| 304 | { |
| 305 | struct attribute_container *cont, *failed = NULL; |
| 306 | int ret = 0; |
| 307 | |
| 308 | mutex_lock(&attribute_container_mutex); |
| 309 | |
| 310 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 311 | |
| 312 | if (!cont->match(cont, dev)) |
| 313 | continue; |
| 314 | |
| 315 | ret = do_attribute_container_device_trigger_safe(dev, cont, |
| 316 | fn, undo); |
| 317 | if (ret) { |
| 318 | failed = cont; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (ret && !WARN_ON(!undo)) { |
| 324 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 325 | |
| 326 | if (failed == cont) |
| 327 | break; |
| 328 | |
| 329 | if (!cont->match(cont, dev)) |
| 330 | continue; |
| 331 | |
| 332 | do_attribute_container_device_trigger_safe(dev, cont, |
| 333 | undo, NULL); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | mutex_unlock(&attribute_container_mutex); |
| 338 | return ret; |
| 339 | |
| 340 | } |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /** |
| 343 | * attribute_container_device_trigger - execute a trigger for each matching classdev |
| 344 | * |
| 345 | * @dev: The generic device to run the trigger for |
| 346 | * @fn the function to execute for each classdev. |
| 347 | * |
Xiubo Li | 03aca7b | 2016-06-01 09:18:40 +0800 | [diff] [blame] | 348 | * This function is for executing a trigger when you need to know both |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | * the container and the classdev. If you only care about the |
| 350 | * container, then use attribute_container_trigger() instead. |
| 351 | */ |
| 352 | void |
Tina Johnson | 24a7d36 | 2014-08-12 02:54:08 +0530 | [diff] [blame] | 353 | attribute_container_device_trigger(struct device *dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | int (*fn)(struct attribute_container *, |
| 355 | struct device *, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 356 | struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | struct attribute_container *cont; |
| 359 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 360 | mutex_lock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | list_for_each_entry(cont, &attribute_container_list, node) { |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 362 | struct internal_container *ic; |
| 363 | struct klist_iter iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | if (!cont->match(cont, dev)) |
| 366 | continue; |
| 367 | |
James Bottomley | ebd8bb7 | 2005-08-15 16:13:19 -0500 | [diff] [blame] | 368 | if (attribute_container_no_classdevs(cont)) { |
| 369 | fn(cont, dev, NULL); |
| 370 | continue; |
| 371 | } |
| 372 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 373 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 374 | if (dev == ic->classdev.parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | fn(cont, dev, &ic->classdev); |
| 376 | } |
| 377 | } |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 378 | mutex_unlock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | /** |
| 382 | * attribute_container_trigger - trigger a function for each matching container |
| 383 | * |
| 384 | * @dev: The generic device to activate the trigger for |
| 385 | * @fn: the function to trigger |
| 386 | * |
| 387 | * This routine triggers a function that only needs to know the |
| 388 | * matching containers (not the classdev) associated with a device. |
| 389 | * It is more lightweight than attribute_container_device_trigger, so |
| 390 | * should be used in preference unless the triggering function |
| 391 | * actually needs to know the classdev. |
| 392 | */ |
| 393 | void |
| 394 | attribute_container_trigger(struct device *dev, |
| 395 | int (*fn)(struct attribute_container *, |
| 396 | struct device *)) |
| 397 | { |
| 398 | struct attribute_container *cont; |
| 399 | |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 400 | mutex_lock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | list_for_each_entry(cont, &attribute_container_list, node) { |
| 402 | if (cont->match(cont, dev)) |
| 403 | fn(cont, dev); |
| 404 | } |
Matthias Kaehlcke | 61a2f59 | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 405 | mutex_unlock(&attribute_container_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * attribute_container_add_attrs - add attributes |
| 410 | * |
| 411 | * @classdev: The class device |
| 412 | * |
| 413 | * This simply creates all the class device sysfs files from the |
| 414 | * attributes listed in the container |
| 415 | */ |
| 416 | int |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 417 | attribute_container_add_attrs(struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | struct attribute_container *cont = |
| 420 | attribute_container_classdev_to_container(classdev); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 421 | struct device_attribute **attrs = cont->attrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | int i, error; |
| 423 | |
James Bottomley | fd11097 | 2008-01-02 18:48:47 -0600 | [diff] [blame] | 424 | BUG_ON(attrs && cont->grp); |
| 425 | |
| 426 | if (!attrs && !cont->grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | |
James Bottomley | fd11097 | 2008-01-02 18:48:47 -0600 | [diff] [blame] | 429 | if (cont->grp) |
| 430 | return sysfs_create_group(&classdev->kobj, cont->grp); |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | for (i = 0; attrs[i]; i++) { |
James Bottomley | ebd09ec | 2010-03-20 12:44:12 -0500 | [diff] [blame] | 433 | sysfs_attr_init(&attrs[i]->attr); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 434 | error = device_create_file(classdev, attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (error) |
| 436 | return error; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | /** |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 443 | * attribute_container_add_class_device - same function as device_add |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | * |
| 445 | * @classdev: the class device to add |
| 446 | * |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 447 | * This performs essentially the same function as device_add except for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | * attribute containers, namely add the classdev to the system and then |
| 449 | * create the attribute files |
| 450 | */ |
| 451 | int |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 452 | attribute_container_add_class_device(struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 454 | int error = device_add(classdev); |
Cosmin Dragomir | 481026d | 2015-03-08 12:30:14 +0200 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (error) |
| 457 | return error; |
| 458 | return attribute_container_add_attrs(classdev); |
| 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * attribute_container_add_class_device_adapter - simple adapter for triggers |
| 463 | * |
| 464 | * This function is identical to attribute_container_add_class_device except |
| 465 | * that it is designed to be called from the triggers |
| 466 | */ |
| 467 | int |
| 468 | attribute_container_add_class_device_adapter(struct attribute_container *cont, |
| 469 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 470 | struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | return attribute_container_add_class_device(classdev); |
| 473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * attribute_container_remove_attrs - remove any attribute files |
| 477 | * |
| 478 | * @classdev: The class device to remove the files from |
| 479 | * |
| 480 | */ |
| 481 | void |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 482 | attribute_container_remove_attrs(struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
| 484 | struct attribute_container *cont = |
| 485 | attribute_container_classdev_to_container(classdev); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 486 | struct device_attribute **attrs = cont->attrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | int i; |
| 488 | |
James Bottomley | fd11097 | 2008-01-02 18:48:47 -0600 | [diff] [blame] | 489 | if (!attrs && !cont->grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return; |
| 491 | |
James Bottomley | fd11097 | 2008-01-02 18:48:47 -0600 | [diff] [blame] | 492 | if (cont->grp) { |
| 493 | sysfs_remove_group(&classdev->kobj, cont->grp); |
| 494 | return ; |
| 495 | } |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | for (i = 0; attrs[i]; i++) |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 498 | device_remove_file(classdev, attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * attribute_container_class_device_del - equivalent of class_device_del |
| 503 | * |
| 504 | * @classdev: the class device |
| 505 | * |
| 506 | * This function simply removes all the attribute files and then calls |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 507 | * device_del. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | */ |
| 509 | void |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 510 | attribute_container_class_device_del(struct device *classdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
| 512 | attribute_container_remove_attrs(classdev); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 513 | device_del(classdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 516 | /** |
| 517 | * attribute_container_find_class_device - find the corresponding class_device |
| 518 | * |
| 519 | * @cont: the container |
| 520 | * @dev: the generic device |
| 521 | * |
| 522 | * Looks up the device in the container's list of class devices and returns |
| 523 | * the corresponding class_device. |
| 524 | */ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 525 | struct device * |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 526 | attribute_container_find_class_device(struct attribute_container *cont, |
| 527 | struct device *dev) |
| 528 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 529 | struct device *cdev = NULL; |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 530 | struct internal_container *ic; |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 531 | struct klist_iter iter; |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 532 | |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 533 | klist_for_each_entry(ic, &cont->containers, node, &iter) { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 534 | if (ic->classdev.parent == dev) { |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 535 | cdev = &ic->classdev; |
James Bottomley | 53c165e | 2005-08-22 10:06:19 -0500 | [diff] [blame] | 536 | /* FIXME: must exit iterator then break */ |
| 537 | klist_iter_exit(&iter); |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | } |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 541 | |
| 542 | return cdev; |
| 543 | } |
| 544 | EXPORT_SYMBOL_GPL(attribute_container_find_class_device); |