4 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6 **-----------------------------------------------------------------------------
8 ** This program is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License
10 ** version 2 as published by the Free Software Foundation.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 **-----------------------------------------------------------------------------
23 #include <linux/device.h>
24 #include <linux/enclosure.h>
25 #include <linux/err.h>
26 #include <linux/list.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
31 static LIST_HEAD(container_list);
32 static DEFINE_MUTEX(container_list_lock);
33 static struct class enclosure_class;
36 * enclosure_find - find an enclosure given a parent device
37 * @dev: the parent to match against
38 * @start: Optional enclosure device to start from (NULL if none)
40 * Looks through the list of registered enclosures to find all those
41 * with @dev as a parent. Returns NULL if no enclosure is
42 * found. @start can be used as a starting point to obtain multiple
43 * enclosures per parent (should begin with NULL and then be set to
44 * each returned enclosure device). Obtains a reference to the
45 * enclosure class device which must be released with device_put().
46 * If @start is not NULL, a reference must be taken on it which is
47 * released before returning (this allows a loop through all
48 * enclosures to exit with only the reference on the enclosure of
49 * interest held). Note that the @dev may correspond to the actual
50 * device housing the enclosure, in which case no iteration via @start
53 struct enclosure_device *enclosure_find(struct device *dev,
54 struct enclosure_device *start)
56 struct enclosure_device *edev;
58 mutex_lock(&container_list_lock);
59 edev = list_prepare_entry(start, &container_list, node);
61 put_device(&start->edev);
63 list_for_each_entry_continue(edev, &container_list, node) {
64 struct device *parent = edev->edev.parent;
65 /* parent might not be immediate, so iterate up to
66 * the root of the tree if necessary */
69 get_device(&edev->edev);
70 mutex_unlock(&container_list_lock);
73 parent = parent->parent;
76 mutex_unlock(&container_list_lock);
80 EXPORT_SYMBOL_GPL(enclosure_find);
83 * enclosure_for_each_device - calls a function for each enclosure
84 * @fn: the function to call
85 * @data: the data to pass to each call
87 * Loops over all the enclosures calling the function.
89 * Note, this function uses a mutex which will be held across calls to
90 * @fn, so it must have non atomic context, and @fn may (although it
91 * should not) sleep or otherwise cause the mutex to be held for
94 int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
98 struct enclosure_device *edev;
100 mutex_lock(&container_list_lock);
101 list_for_each_entry(edev, &container_list, node) {
102 error = fn(edev, data);
106 mutex_unlock(&container_list_lock);
110 EXPORT_SYMBOL_GPL(enclosure_for_each_device);
113 * enclosure_register - register device as an enclosure
115 * @dev: device containing the enclosure
116 * @components: number of components in the enclosure
118 * This sets up the device for being an enclosure. Note that @dev does
119 * not have to be a dedicated enclosure device. It may be some other type
120 * of device that additionally responds to enclosure services
122 struct enclosure_device *
123 enclosure_register(struct device *dev, const char *name, int components,
124 struct enclosure_component_callbacks *cb)
126 struct enclosure_device *edev =
127 kzalloc(sizeof(struct enclosure_device) +
128 sizeof(struct enclosure_component)*components,
135 return ERR_PTR(-ENOMEM);
137 edev->components = components;
139 edev->edev.class = &enclosure_class;
140 edev->edev.parent = get_device(dev);
142 dev_set_name(&edev->edev, "%s", name);
143 err = device_register(&edev->edev);
147 for (i = 0; i < components; i++)
148 edev->component[i].number = -1;
150 mutex_lock(&container_list_lock);
151 list_add_tail(&edev->node, &container_list);
152 mutex_unlock(&container_list_lock);
157 put_device(edev->edev.parent);
161 EXPORT_SYMBOL_GPL(enclosure_register);
163 static struct enclosure_component_callbacks enclosure_null_callbacks;
166 * enclosure_unregister - remove an enclosure
168 * @edev: the registered enclosure to remove;
170 void enclosure_unregister(struct enclosure_device *edev)
174 mutex_lock(&container_list_lock);
175 list_del(&edev->node);
176 mutex_unlock(&container_list_lock);
178 for (i = 0; i < edev->components; i++)
179 if (edev->component[i].number != -1)
180 device_unregister(&edev->component[i].cdev);
182 /* prevent any callbacks into service user */
183 edev->cb = &enclosure_null_callbacks;
184 device_unregister(&edev->edev);
186 EXPORT_SYMBOL_GPL(enclosure_unregister);
188 #define ENCLOSURE_NAME_SIZE 64
190 static void enclosure_link_name(struct enclosure_component *cdev, char *name)
192 strcpy(name, "enclosure_device:");
193 strcat(name, dev_name(&cdev->cdev));
196 static void enclosure_remove_links(struct enclosure_component *cdev)
198 char name[ENCLOSURE_NAME_SIZE];
200 enclosure_link_name(cdev, name);
201 sysfs_remove_link(&cdev->dev->kobj, name);
202 sysfs_remove_link(&cdev->cdev.kobj, "device");
205 static int enclosure_add_links(struct enclosure_component *cdev)
208 char name[ENCLOSURE_NAME_SIZE];
210 error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
214 enclosure_link_name(cdev, name);
215 error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
217 sysfs_remove_link(&cdev->cdev.kobj, "device");
222 static void enclosure_release(struct device *cdev)
224 struct enclosure_device *edev = to_enclosure_device(cdev);
226 put_device(cdev->parent);
230 static void enclosure_component_release(struct device *dev)
232 struct enclosure_component *cdev = to_enclosure_component(dev);
235 enclosure_remove_links(cdev);
236 put_device(cdev->dev);
238 put_device(dev->parent);
241 static struct attribute_group *enclosure_groups[];
244 * enclosure_component_register - add a particular component to an enclosure
245 * @edev: the enclosure to add the component
246 * @num: the device number
247 * @type: the type of component being added
248 * @name: an optional name to appear in sysfs (leave NULL if none)
250 * Registers the component. The name is optional for enclosures that
251 * give their components a unique name. If not, leave the field NULL
252 * and a name will be assigned.
254 * Returns a pointer to the enclosure component or an error.
256 struct enclosure_component *
257 enclosure_component_register(struct enclosure_device *edev,
259 enum enclosure_component_type type,
262 struct enclosure_component *ecomp;
266 if (number >= edev->components)
267 return ERR_PTR(-EINVAL);
269 ecomp = &edev->component[number];
271 if (ecomp->number != -1)
272 return ERR_PTR(-EINVAL);
275 ecomp->number = number;
277 cdev->parent = get_device(&edev->edev);
279 dev_set_name(cdev, "%s", name);
281 dev_set_name(cdev, "%u", number);
283 cdev->release = enclosure_component_release;
284 cdev->groups = enclosure_groups;
286 err = device_register(cdev);
292 EXPORT_SYMBOL_GPL(enclosure_component_register);
295 * enclosure_add_device - add a device as being part of an enclosure
296 * @edev: the enclosure device being added to.
297 * @num: the number of the component
298 * @dev: the device being added
300 * Declares a real device to reside in slot (or identifier) @num of an
301 * enclosure. This will cause the relevant sysfs links to appear.
302 * This function may also be used to change a device associated with
303 * an enclosure without having to call enclosure_remove_device() in
306 * Returns zero on success or an error.
308 int enclosure_add_device(struct enclosure_device *edev, int component,
311 struct enclosure_component *cdev;
313 if (!edev || component >= edev->components)
316 cdev = &edev->component[component];
319 enclosure_remove_links(cdev);
321 put_device(cdev->dev);
322 cdev->dev = get_device(dev);
323 return enclosure_add_links(cdev);
325 EXPORT_SYMBOL_GPL(enclosure_add_device);
328 * enclosure_remove_device - remove a device from an enclosure
329 * @edev: the enclosure device
330 * @num: the number of the component to remove
332 * Returns zero on success or an error.
335 int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
337 struct enclosure_component *cdev;
343 for (i = 0; i < edev->components; i++) {
344 cdev = &edev->component[i];
345 if (cdev->dev == dev) {
346 enclosure_remove_links(cdev);
347 device_del(&cdev->cdev);
350 return device_add(&cdev->cdev);
355 EXPORT_SYMBOL_GPL(enclosure_remove_device);
361 static ssize_t enclosure_show_components(struct device *cdev,
362 struct device_attribute *attr,
365 struct enclosure_device *edev = to_enclosure_device(cdev);
367 return snprintf(buf, 40, "%d\n", edev->components);
370 static struct device_attribute enclosure_attrs[] = {
371 __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
375 static struct class enclosure_class = {
377 .owner = THIS_MODULE,
378 .dev_release = enclosure_release,
379 .dev_attrs = enclosure_attrs,
382 static const char *const enclosure_status [] = {
383 [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
384 [ENCLOSURE_STATUS_OK] = "OK",
385 [ENCLOSURE_STATUS_CRITICAL] = "critical",
386 [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
387 [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
388 [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
389 [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
390 [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
393 static const char *const enclosure_type [] = {
394 [ENCLOSURE_COMPONENT_DEVICE] = "device",
395 [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
398 static ssize_t get_component_fault(struct device *cdev,
399 struct device_attribute *attr, char *buf)
401 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
402 struct enclosure_component *ecomp = to_enclosure_component(cdev);
404 if (edev->cb->get_fault)
405 edev->cb->get_fault(edev, ecomp);
406 return snprintf(buf, 40, "%d\n", ecomp->fault);
409 static ssize_t set_component_fault(struct device *cdev,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
413 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
414 struct enclosure_component *ecomp = to_enclosure_component(cdev);
415 int val = simple_strtoul(buf, NULL, 0);
417 if (edev->cb->set_fault)
418 edev->cb->set_fault(edev, ecomp, val);
422 static ssize_t get_component_status(struct device *cdev,
423 struct device_attribute *attr,char *buf)
425 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
426 struct enclosure_component *ecomp = to_enclosure_component(cdev);
428 if (edev->cb->get_status)
429 edev->cb->get_status(edev, ecomp);
430 return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
433 static ssize_t set_component_status(struct device *cdev,
434 struct device_attribute *attr,
435 const char *buf, size_t count)
437 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
438 struct enclosure_component *ecomp = to_enclosure_component(cdev);
441 for (i = 0; enclosure_status[i]; i++) {
442 if (strncmp(buf, enclosure_status[i],
443 strlen(enclosure_status[i])) == 0 &&
444 (buf[strlen(enclosure_status[i])] == '\n' ||
445 buf[strlen(enclosure_status[i])] == '\0'))
449 if (enclosure_status[i] && edev->cb->set_status) {
450 edev->cb->set_status(edev, ecomp, i);
456 static ssize_t get_component_active(struct device *cdev,
457 struct device_attribute *attr, char *buf)
459 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
460 struct enclosure_component *ecomp = to_enclosure_component(cdev);
462 if (edev->cb->get_active)
463 edev->cb->get_active(edev, ecomp);
464 return snprintf(buf, 40, "%d\n", ecomp->active);
467 static ssize_t set_component_active(struct device *cdev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
471 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
472 struct enclosure_component *ecomp = to_enclosure_component(cdev);
473 int val = simple_strtoul(buf, NULL, 0);
475 if (edev->cb->set_active)
476 edev->cb->set_active(edev, ecomp, val);
480 static ssize_t get_component_locate(struct device *cdev,
481 struct device_attribute *attr, char *buf)
483 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
484 struct enclosure_component *ecomp = to_enclosure_component(cdev);
486 if (edev->cb->get_locate)
487 edev->cb->get_locate(edev, ecomp);
488 return snprintf(buf, 40, "%d\n", ecomp->locate);
491 static ssize_t set_component_locate(struct device *cdev,
492 struct device_attribute *attr,
493 const char *buf, size_t count)
495 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
496 struct enclosure_component *ecomp = to_enclosure_component(cdev);
497 int val = simple_strtoul(buf, NULL, 0);
499 if (edev->cb->set_locate)
500 edev->cb->set_locate(edev, ecomp, val);
504 static ssize_t get_component_type(struct device *cdev,
505 struct device_attribute *attr, char *buf)
507 struct enclosure_component *ecomp = to_enclosure_component(cdev);
509 return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
513 static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
514 set_component_fault);
515 static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
516 set_component_status);
517 static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
518 set_component_active);
519 static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
520 set_component_locate);
521 static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
523 static struct attribute *enclosure_component_attrs[] = {
524 &dev_attr_fault.attr,
525 &dev_attr_status.attr,
526 &dev_attr_active.attr,
527 &dev_attr_locate.attr,
532 static struct attribute_group enclosure_group = {
533 .attrs = enclosure_component_attrs,
536 static struct attribute_group *enclosure_groups[] = {
541 static int __init enclosure_init(void)
545 err = class_register(&enclosure_class);
552 static void __exit enclosure_exit(void)
554 class_unregister(&enclosure_class);
557 module_init(enclosure_init);
558 module_exit(enclosure_exit);
560 MODULE_AUTHOR("James Bottomley");
561 MODULE_DESCRIPTION("Enclosure Services");
562 MODULE_LICENSE("GPL v2");