Path: blob/master/drivers/base/attribute_container.c
15109 views
/*1* attribute_container.c - implementation of a simple container for classes2*3* Copyright (c) 2005 - James Bottomley <[email protected]>4*5* This file is licensed under GPLv26*7* The basic idea here is to enable a device to be attached to an8* aritrary numer of classes without having to allocate storage for them.9* Instead, the contained classes select the devices they need to attach10* to via a matching function.11*/1213#include <linux/attribute_container.h>14#include <linux/init.h>15#include <linux/device.h>16#include <linux/kernel.h>17#include <linux/slab.h>18#include <linux/list.h>19#include <linux/module.h>20#include <linux/mutex.h>2122#include "base.h"2324/* This is a private structure used to tie the classdev and the25* container .. it should never be visible outside this file */26struct internal_container {27struct klist_node node;28struct attribute_container *cont;29struct device classdev;30};3132static void internal_container_klist_get(struct klist_node *n)33{34struct internal_container *ic =35container_of(n, struct internal_container, node);36get_device(&ic->classdev);37}3839static void internal_container_klist_put(struct klist_node *n)40{41struct internal_container *ic =42container_of(n, struct internal_container, node);43put_device(&ic->classdev);44}454647/**48* attribute_container_classdev_to_container - given a classdev, return the container49*50* @classdev: the class device created by attribute_container_add_device.51*52* Returns the container associated with this classdev.53*/54struct attribute_container *55attribute_container_classdev_to_container(struct device *classdev)56{57struct internal_container *ic =58container_of(classdev, struct internal_container, classdev);59return ic->cont;60}61EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);6263static LIST_HEAD(attribute_container_list);6465static DEFINE_MUTEX(attribute_container_mutex);6667/**68* attribute_container_register - register an attribute container69*70* @cont: The container to register. This must be allocated by the71* callee and should also be zeroed by it.72*/73int74attribute_container_register(struct attribute_container *cont)75{76INIT_LIST_HEAD(&cont->node);77klist_init(&cont->containers,internal_container_klist_get,78internal_container_klist_put);7980mutex_lock(&attribute_container_mutex);81list_add_tail(&cont->node, &attribute_container_list);82mutex_unlock(&attribute_container_mutex);8384return 0;85}86EXPORT_SYMBOL_GPL(attribute_container_register);8788/**89* attribute_container_unregister - remove a container registration90*91* @cont: previously registered container to remove92*/93int94attribute_container_unregister(struct attribute_container *cont)95{96int retval = -EBUSY;97mutex_lock(&attribute_container_mutex);98spin_lock(&cont->containers.k_lock);99if (!list_empty(&cont->containers.k_list))100goto out;101retval = 0;102list_del(&cont->node);103out:104spin_unlock(&cont->containers.k_lock);105mutex_unlock(&attribute_container_mutex);106return retval;107108}109EXPORT_SYMBOL_GPL(attribute_container_unregister);110111/* private function used as class release */112static void attribute_container_release(struct device *classdev)113{114struct internal_container *ic115= container_of(classdev, struct internal_container, classdev);116struct device *dev = classdev->parent;117118kfree(ic);119put_device(dev);120}121122/**123* attribute_container_add_device - see if any container is interested in dev124*125* @dev: device to add attributes to126* @fn: function to trigger addition of class device.127*128* This function allocates storage for the class device(s) to be129* attached to dev (one for each matching attribute_container). If no130* fn is provided, the code will simply register the class device via131* device_add. If a function is provided, it is expected to add132* the class device at the appropriate time. One of the things that133* might be necessary is to allocate and initialise the classdev and134* then add it a later time. To do this, call this routine for135* allocation and initialisation and then use136* attribute_container_device_trigger() to call device_add() on137* it. Note: after this, the class device contains a reference to dev138* which is not relinquished until the release of the classdev.139*/140void141attribute_container_add_device(struct device *dev,142int (*fn)(struct attribute_container *,143struct device *,144struct device *))145{146struct attribute_container *cont;147148mutex_lock(&attribute_container_mutex);149list_for_each_entry(cont, &attribute_container_list, node) {150struct internal_container *ic;151152if (attribute_container_no_classdevs(cont))153continue;154155if (!cont->match(cont, dev))156continue;157158ic = kzalloc(sizeof(*ic), GFP_KERNEL);159if (!ic) {160dev_printk(KERN_ERR, dev, "failed to allocate class container\n");161continue;162}163164ic->cont = cont;165device_initialize(&ic->classdev);166ic->classdev.parent = get_device(dev);167ic->classdev.class = cont->class;168cont->class->dev_release = attribute_container_release;169dev_set_name(&ic->classdev, dev_name(dev));170if (fn)171fn(cont, dev, &ic->classdev);172else173attribute_container_add_class_device(&ic->classdev);174klist_add_tail(&ic->node, &cont->containers);175}176mutex_unlock(&attribute_container_mutex);177}178179/* FIXME: can't break out of this unless klist_iter_exit is also180* called before doing the break181*/182#define klist_for_each_entry(pos, head, member, iter) \183for (klist_iter_init(head, iter); (pos = ({ \184struct klist_node *n = klist_next(iter); \185n ? container_of(n, typeof(*pos), member) : \186({ klist_iter_exit(iter) ; NULL; }); \187}) ) != NULL; )188189190/**191* attribute_container_remove_device - make device eligible for removal.192*193* @dev: The generic device194* @fn: A function to call to remove the device195*196* This routine triggers device removal. If fn is NULL, then it is197* simply done via device_unregister (note that if something198* still has a reference to the classdev, then the memory occupied199* will not be freed until the classdev is released). If you want a200* two phase release: remove from visibility and then delete the201* device, then you should use this routine with a fn that calls202* device_del() and then use attribute_container_device_trigger()203* to do the final put on the classdev.204*/205void206attribute_container_remove_device(struct device *dev,207void (*fn)(struct attribute_container *,208struct device *,209struct device *))210{211struct attribute_container *cont;212213mutex_lock(&attribute_container_mutex);214list_for_each_entry(cont, &attribute_container_list, node) {215struct internal_container *ic;216struct klist_iter iter;217218if (attribute_container_no_classdevs(cont))219continue;220221if (!cont->match(cont, dev))222continue;223224klist_for_each_entry(ic, &cont->containers, node, &iter) {225if (dev != ic->classdev.parent)226continue;227klist_del(&ic->node);228if (fn)229fn(cont, dev, &ic->classdev);230else {231attribute_container_remove_attrs(&ic->classdev);232device_unregister(&ic->classdev);233}234}235}236mutex_unlock(&attribute_container_mutex);237}238239/**240* attribute_container_device_trigger - execute a trigger for each matching classdev241*242* @dev: The generic device to run the trigger for243* @fn the function to execute for each classdev.244*245* This funcion is for executing a trigger when you need to know both246* the container and the classdev. If you only care about the247* container, then use attribute_container_trigger() instead.248*/249void250attribute_container_device_trigger(struct device *dev,251int (*fn)(struct attribute_container *,252struct device *,253struct device *))254{255struct attribute_container *cont;256257mutex_lock(&attribute_container_mutex);258list_for_each_entry(cont, &attribute_container_list, node) {259struct internal_container *ic;260struct klist_iter iter;261262if (!cont->match(cont, dev))263continue;264265if (attribute_container_no_classdevs(cont)) {266fn(cont, dev, NULL);267continue;268}269270klist_for_each_entry(ic, &cont->containers, node, &iter) {271if (dev == ic->classdev.parent)272fn(cont, dev, &ic->classdev);273}274}275mutex_unlock(&attribute_container_mutex);276}277278/**279* attribute_container_trigger - trigger a function for each matching container280*281* @dev: The generic device to activate the trigger for282* @fn: the function to trigger283*284* This routine triggers a function that only needs to know the285* matching containers (not the classdev) associated with a device.286* It is more lightweight than attribute_container_device_trigger, so287* should be used in preference unless the triggering function288* actually needs to know the classdev.289*/290void291attribute_container_trigger(struct device *dev,292int (*fn)(struct attribute_container *,293struct device *))294{295struct attribute_container *cont;296297mutex_lock(&attribute_container_mutex);298list_for_each_entry(cont, &attribute_container_list, node) {299if (cont->match(cont, dev))300fn(cont, dev);301}302mutex_unlock(&attribute_container_mutex);303}304305/**306* attribute_container_add_attrs - add attributes307*308* @classdev: The class device309*310* This simply creates all the class device sysfs files from the311* attributes listed in the container312*/313int314attribute_container_add_attrs(struct device *classdev)315{316struct attribute_container *cont =317attribute_container_classdev_to_container(classdev);318struct device_attribute **attrs = cont->attrs;319int i, error;320321BUG_ON(attrs && cont->grp);322323if (!attrs && !cont->grp)324return 0;325326if (cont->grp)327return sysfs_create_group(&classdev->kobj, cont->grp);328329for (i = 0; attrs[i]; i++) {330sysfs_attr_init(&attrs[i]->attr);331error = device_create_file(classdev, attrs[i]);332if (error)333return error;334}335336return 0;337}338339/**340* attribute_container_add_class_device - same function as device_add341*342* @classdev: the class device to add343*344* This performs essentially the same function as device_add except for345* attribute containers, namely add the classdev to the system and then346* create the attribute files347*/348int349attribute_container_add_class_device(struct device *classdev)350{351int error = device_add(classdev);352if (error)353return error;354return attribute_container_add_attrs(classdev);355}356357/**358* attribute_container_add_class_device_adapter - simple adapter for triggers359*360* This function is identical to attribute_container_add_class_device except361* that it is designed to be called from the triggers362*/363int364attribute_container_add_class_device_adapter(struct attribute_container *cont,365struct device *dev,366struct device *classdev)367{368return attribute_container_add_class_device(classdev);369}370371/**372* attribute_container_remove_attrs - remove any attribute files373*374* @classdev: The class device to remove the files from375*376*/377void378attribute_container_remove_attrs(struct device *classdev)379{380struct attribute_container *cont =381attribute_container_classdev_to_container(classdev);382struct device_attribute **attrs = cont->attrs;383int i;384385if (!attrs && !cont->grp)386return;387388if (cont->grp) {389sysfs_remove_group(&classdev->kobj, cont->grp);390return ;391}392393for (i = 0; attrs[i]; i++)394device_remove_file(classdev, attrs[i]);395}396397/**398* attribute_container_class_device_del - equivalent of class_device_del399*400* @classdev: the class device401*402* This function simply removes all the attribute files and then calls403* device_del.404*/405void406attribute_container_class_device_del(struct device *classdev)407{408attribute_container_remove_attrs(classdev);409device_del(classdev);410}411412/**413* attribute_container_find_class_device - find the corresponding class_device414*415* @cont: the container416* @dev: the generic device417*418* Looks up the device in the container's list of class devices and returns419* the corresponding class_device.420*/421struct device *422attribute_container_find_class_device(struct attribute_container *cont,423struct device *dev)424{425struct device *cdev = NULL;426struct internal_container *ic;427struct klist_iter iter;428429klist_for_each_entry(ic, &cont->containers, node, &iter) {430if (ic->classdev.parent == dev) {431cdev = &ic->classdev;432/* FIXME: must exit iterator then break */433klist_iter_exit(&iter);434break;435}436}437438return cdev;439}440EXPORT_SYMBOL_GPL(attribute_container_find_class_device);441442443