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