PCI: pci-stub: ignore zero-length id parameters
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / enclosure.c
blob68e4cd7d321ac7715a5bf47420ce1bc0c4bc8639
1 /*
2 * Enclosure Services
4 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6 **-----------------------------------------------------------------------------
7 **
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;
35 /**
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
51 * is required.
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);
60 if (start)
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 */
67 while (parent) {
68 if (parent == dev) {
69 get_device(&edev->edev);
70 mutex_unlock(&container_list_lock);
71 return edev;
73 parent = parent->parent;
76 mutex_unlock(&container_list_lock);
78 return NULL;
80 EXPORT_SYMBOL_GPL(enclosure_find);
82 /**
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
92 * indefinite periods
94 int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
95 void *data)
97 int error = 0;
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);
103 if (error)
104 break;
106 mutex_unlock(&container_list_lock);
108 return error;
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,
129 GFP_KERNEL);
130 int err, i;
132 BUG_ON(!cb);
134 if (!edev)
135 return ERR_PTR(-ENOMEM);
137 edev->components = components;
139 edev->edev.class = &enclosure_class;
140 edev->edev.parent = get_device(dev);
141 edev->cb = cb;
142 dev_set_name(&edev->edev, "%s", name);
143 err = device_register(&edev->edev);
144 if (err)
145 goto err;
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);
154 return edev;
156 err:
157 put_device(edev->edev.parent);
158 kfree(edev);
159 return ERR_PTR(err);
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)
172 int i;
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)
207 int error;
208 char name[ENCLOSURE_NAME_SIZE];
210 error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
211 if (error)
212 return error;
214 enclosure_link_name(cdev, name);
215 error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
216 if (error)
217 sysfs_remove_link(&cdev->cdev.kobj, "device");
219 return error;
222 static void enclosure_release(struct device *cdev)
224 struct enclosure_device *edev = to_enclosure_device(cdev);
226 put_device(cdev->parent);
227 kfree(edev);
230 static void enclosure_component_release(struct device *dev)
232 struct enclosure_component *cdev = to_enclosure_component(dev);
234 if (cdev->dev) {
235 enclosure_remove_links(cdev);
236 put_device(cdev->dev);
238 put_device(dev->parent);
241 static const 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,
258 unsigned int number,
259 enum enclosure_component_type type,
260 const char *name)
262 struct enclosure_component *ecomp;
263 struct device *cdev;
264 int err;
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);
274 ecomp->type = type;
275 ecomp->number = number;
276 cdev = &ecomp->cdev;
277 cdev->parent = get_device(&edev->edev);
278 if (name && name[0])
279 dev_set_name(cdev, "%s", name);
280 else
281 dev_set_name(cdev, "%u", number);
283 cdev->release = enclosure_component_release;
284 cdev->groups = enclosure_groups;
286 err = device_register(cdev);
287 if (err) {
288 ecomp->number = -1;
289 put_device(cdev);
290 return ERR_PTR(err);
293 return ecomp;
295 EXPORT_SYMBOL_GPL(enclosure_component_register);
298 * enclosure_add_device - add a device as being part of an enclosure
299 * @edev: the enclosure device being added to.
300 * @num: the number of the component
301 * @dev: the device being added
303 * Declares a real device to reside in slot (or identifier) @num of an
304 * enclosure. This will cause the relevant sysfs links to appear.
305 * This function may also be used to change a device associated with
306 * an enclosure without having to call enclosure_remove_device() in
307 * between.
309 * Returns zero on success or an error.
311 int enclosure_add_device(struct enclosure_device *edev, int component,
312 struct device *dev)
314 struct enclosure_component *cdev;
316 if (!edev || component >= edev->components)
317 return -EINVAL;
319 cdev = &edev->component[component];
321 if (cdev->dev == dev)
322 return -EEXIST;
324 if (cdev->dev)
325 enclosure_remove_links(cdev);
327 put_device(cdev->dev);
328 cdev->dev = get_device(dev);
329 return enclosure_add_links(cdev);
331 EXPORT_SYMBOL_GPL(enclosure_add_device);
334 * enclosure_remove_device - remove a device from an enclosure
335 * @edev: the enclosure device
336 * @num: the number of the component to remove
338 * Returns zero on success or an error.
341 int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
343 struct enclosure_component *cdev;
344 int i;
346 if (!edev || !dev)
347 return -EINVAL;
349 for (i = 0; i < edev->components; i++) {
350 cdev = &edev->component[i];
351 if (cdev->dev == dev) {
352 enclosure_remove_links(cdev);
353 device_del(&cdev->cdev);
354 put_device(dev);
355 cdev->dev = NULL;
356 return device_add(&cdev->cdev);
359 return -ENODEV;
361 EXPORT_SYMBOL_GPL(enclosure_remove_device);
364 * sysfs pieces below
367 static ssize_t enclosure_show_components(struct device *cdev,
368 struct device_attribute *attr,
369 char *buf)
371 struct enclosure_device *edev = to_enclosure_device(cdev);
373 return snprintf(buf, 40, "%d\n", edev->components);
376 static struct device_attribute enclosure_attrs[] = {
377 __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
378 __ATTR_NULL
381 static struct class enclosure_class = {
382 .name = "enclosure",
383 .owner = THIS_MODULE,
384 .dev_release = enclosure_release,
385 .dev_attrs = enclosure_attrs,
388 static const char *const enclosure_status [] = {
389 [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
390 [ENCLOSURE_STATUS_OK] = "OK",
391 [ENCLOSURE_STATUS_CRITICAL] = "critical",
392 [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
393 [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
394 [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
395 [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
396 [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
397 [ENCLOSURE_STATUS_MAX] = NULL,
400 static const char *const enclosure_type [] = {
401 [ENCLOSURE_COMPONENT_DEVICE] = "device",
402 [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
405 static ssize_t get_component_fault(struct device *cdev,
406 struct device_attribute *attr, char *buf)
408 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
409 struct enclosure_component *ecomp = to_enclosure_component(cdev);
411 if (edev->cb->get_fault)
412 edev->cb->get_fault(edev, ecomp);
413 return snprintf(buf, 40, "%d\n", ecomp->fault);
416 static ssize_t set_component_fault(struct device *cdev,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
420 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
421 struct enclosure_component *ecomp = to_enclosure_component(cdev);
422 int val = simple_strtoul(buf, NULL, 0);
424 if (edev->cb->set_fault)
425 edev->cb->set_fault(edev, ecomp, val);
426 return count;
429 static ssize_t get_component_status(struct device *cdev,
430 struct device_attribute *attr,char *buf)
432 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
433 struct enclosure_component *ecomp = to_enclosure_component(cdev);
435 if (edev->cb->get_status)
436 edev->cb->get_status(edev, ecomp);
437 return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
440 static ssize_t set_component_status(struct device *cdev,
441 struct device_attribute *attr,
442 const char *buf, size_t count)
444 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
445 struct enclosure_component *ecomp = to_enclosure_component(cdev);
446 int i;
448 for (i = 0; enclosure_status[i]; i++) {
449 if (strncmp(buf, enclosure_status[i],
450 strlen(enclosure_status[i])) == 0 &&
451 (buf[strlen(enclosure_status[i])] == '\n' ||
452 buf[strlen(enclosure_status[i])] == '\0'))
453 break;
456 if (enclosure_status[i] && edev->cb->set_status) {
457 edev->cb->set_status(edev, ecomp, i);
458 return count;
459 } else
460 return -EINVAL;
463 static ssize_t get_component_active(struct device *cdev,
464 struct device_attribute *attr, char *buf)
466 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
467 struct enclosure_component *ecomp = to_enclosure_component(cdev);
469 if (edev->cb->get_active)
470 edev->cb->get_active(edev, ecomp);
471 return snprintf(buf, 40, "%d\n", ecomp->active);
474 static ssize_t set_component_active(struct device *cdev,
475 struct device_attribute *attr,
476 const char *buf, size_t count)
478 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
479 struct enclosure_component *ecomp = to_enclosure_component(cdev);
480 int val = simple_strtoul(buf, NULL, 0);
482 if (edev->cb->set_active)
483 edev->cb->set_active(edev, ecomp, val);
484 return count;
487 static ssize_t get_component_locate(struct device *cdev,
488 struct device_attribute *attr, char *buf)
490 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
491 struct enclosure_component *ecomp = to_enclosure_component(cdev);
493 if (edev->cb->get_locate)
494 edev->cb->get_locate(edev, ecomp);
495 return snprintf(buf, 40, "%d\n", ecomp->locate);
498 static ssize_t set_component_locate(struct device *cdev,
499 struct device_attribute *attr,
500 const char *buf, size_t count)
502 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
503 struct enclosure_component *ecomp = to_enclosure_component(cdev);
504 int val = simple_strtoul(buf, NULL, 0);
506 if (edev->cb->set_locate)
507 edev->cb->set_locate(edev, ecomp, val);
508 return count;
511 static ssize_t get_component_type(struct device *cdev,
512 struct device_attribute *attr, char *buf)
514 struct enclosure_component *ecomp = to_enclosure_component(cdev);
516 return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
520 static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
521 set_component_fault);
522 static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
523 set_component_status);
524 static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
525 set_component_active);
526 static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
527 set_component_locate);
528 static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
530 static struct attribute *enclosure_component_attrs[] = {
531 &dev_attr_fault.attr,
532 &dev_attr_status.attr,
533 &dev_attr_active.attr,
534 &dev_attr_locate.attr,
535 &dev_attr_type.attr,
536 NULL
539 static struct attribute_group enclosure_group = {
540 .attrs = enclosure_component_attrs,
543 static const struct attribute_group *enclosure_groups[] = {
544 &enclosure_group,
545 NULL
548 static int __init enclosure_init(void)
550 int err;
552 err = class_register(&enclosure_class);
553 if (err)
554 return err;
556 return 0;
559 static void __exit enclosure_exit(void)
561 class_unregister(&enclosure_class);
564 module_init(enclosure_init);
565 module_exit(enclosure_exit);
567 MODULE_AUTHOR("James Bottomley");
568 MODULE_DESCRIPTION("Enclosure Services");
569 MODULE_LICENSE("GPL v2");