Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / misc / enclosure.c
blob6fcb0e96adf4283e1554f0fcc77a17b0acebd297
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;
34 static struct class enclosure_component_class;
36 /**
37 * enclosure_find - find an enclosure given a device
38 * @dev: the device to find for
40 * Looks through the list of registered enclosures to see
41 * if it can find a match for a device. Returns NULL if no
42 * enclosure is found. Obtains a reference to the enclosure class
43 * device which must be released with class_device_put().
45 struct enclosure_device *enclosure_find(struct device *dev)
47 struct enclosure_device *edev = NULL;
49 mutex_lock(&container_list_lock);
50 list_for_each_entry(edev, &container_list, node) {
51 if (edev->cdev.dev == dev) {
52 class_device_get(&edev->cdev);
53 mutex_unlock(&container_list_lock);
54 return edev;
57 mutex_unlock(&container_list_lock);
59 return NULL;
61 EXPORT_SYMBOL_GPL(enclosure_find);
63 /**
64 * enclosure_for_each_device - calls a function for each enclosure
65 * @fn: the function to call
66 * @data: the data to pass to each call
68 * Loops over all the enclosures calling the function.
70 * Note, this function uses a mutex which will be held across calls to
71 * @fn, so it must have non atomic context, and @fn may (although it
72 * should not) sleep or otherwise cause the mutex to be held for
73 * indefinite periods
75 int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
76 void *data)
78 int error = 0;
79 struct enclosure_device *edev;
81 mutex_lock(&container_list_lock);
82 list_for_each_entry(edev, &container_list, node) {
83 error = fn(edev, data);
84 if (error)
85 break;
87 mutex_unlock(&container_list_lock);
89 return error;
91 EXPORT_SYMBOL_GPL(enclosure_for_each_device);
93 /**
94 * enclosure_register - register device as an enclosure
96 * @dev: device containing the enclosure
97 * @components: number of components in the enclosure
99 * This sets up the device for being an enclosure. Note that @dev does
100 * not have to be a dedicated enclosure device. It may be some other type
101 * of device that additionally responds to enclosure services
103 struct enclosure_device *
104 enclosure_register(struct device *dev, const char *name, int components,
105 struct enclosure_component_callbacks *cb)
107 struct enclosure_device *edev =
108 kzalloc(sizeof(struct enclosure_device) +
109 sizeof(struct enclosure_component)*components,
110 GFP_KERNEL);
111 int err, i;
113 BUG_ON(!cb);
115 if (!edev)
116 return ERR_PTR(-ENOMEM);
118 edev->components = components;
120 edev->cdev.class = &enclosure_class;
121 edev->cdev.dev = get_device(dev);
122 edev->cb = cb;
123 snprintf(edev->cdev.class_id, BUS_ID_SIZE, "%s", name);
124 err = class_device_register(&edev->cdev);
125 if (err)
126 goto err;
128 for (i = 0; i < components; i++)
129 edev->component[i].number = -1;
131 mutex_lock(&container_list_lock);
132 list_add_tail(&edev->node, &container_list);
133 mutex_unlock(&container_list_lock);
135 return edev;
137 err:
138 put_device(edev->cdev.dev);
139 kfree(edev);
140 return ERR_PTR(err);
142 EXPORT_SYMBOL_GPL(enclosure_register);
144 static struct enclosure_component_callbacks enclosure_null_callbacks;
147 * enclosure_unregister - remove an enclosure
149 * @edev: the registered enclosure to remove;
151 void enclosure_unregister(struct enclosure_device *edev)
153 int i;
155 mutex_lock(&container_list_lock);
156 list_del(&edev->node);
157 mutex_unlock(&container_list_lock);
159 for (i = 0; i < edev->components; i++)
160 if (edev->component[i].number != -1)
161 class_device_unregister(&edev->component[i].cdev);
163 /* prevent any callbacks into service user */
164 edev->cb = &enclosure_null_callbacks;
165 class_device_unregister(&edev->cdev);
167 EXPORT_SYMBOL_GPL(enclosure_unregister);
169 static void enclosure_release(struct class_device *cdev)
171 struct enclosure_device *edev = to_enclosure_device(cdev);
173 put_device(cdev->dev);
174 kfree(edev);
177 static void enclosure_component_release(struct class_device *cdev)
179 if (cdev->dev)
180 put_device(cdev->dev);
181 class_device_put(cdev->parent);
185 * enclosure_component_register - add a particular component to an enclosure
186 * @edev: the enclosure to add the component
187 * @num: the device number
188 * @type: the type of component being added
189 * @name: an optional name to appear in sysfs (leave NULL if none)
191 * Registers the component. The name is optional for enclosures that
192 * give their components a unique name. If not, leave the field NULL
193 * and a name will be assigned.
195 * Returns a pointer to the enclosure component or an error.
197 struct enclosure_component *
198 enclosure_component_register(struct enclosure_device *edev,
199 unsigned int number,
200 enum enclosure_component_type type,
201 const char *name)
203 struct enclosure_component *ecomp;
204 struct class_device *cdev;
205 int err;
207 if (number >= edev->components)
208 return ERR_PTR(-EINVAL);
210 ecomp = &edev->component[number];
212 if (ecomp->number != -1)
213 return ERR_PTR(-EINVAL);
215 ecomp->type = type;
216 ecomp->number = number;
217 cdev = &ecomp->cdev;
218 cdev->parent = class_device_get(&edev->cdev);
219 cdev->class = &enclosure_component_class;
220 if (name)
221 snprintf(cdev->class_id, BUS_ID_SIZE, "%s", name);
222 else
223 snprintf(cdev->class_id, BUS_ID_SIZE, "%u", number);
225 err = class_device_register(cdev);
226 if (err)
227 ERR_PTR(err);
229 return ecomp;
231 EXPORT_SYMBOL_GPL(enclosure_component_register);
234 * enclosure_add_device - add a device as being part of an enclosure
235 * @edev: the enclosure device being added to.
236 * @num: the number of the component
237 * @dev: the device being added
239 * Declares a real device to reside in slot (or identifier) @num of an
240 * enclosure. This will cause the relevant sysfs links to appear.
241 * This function may also be used to change a device associated with
242 * an enclosure without having to call enclosure_remove_device() in
243 * between.
245 * Returns zero on success or an error.
247 int enclosure_add_device(struct enclosure_device *edev, int component,
248 struct device *dev)
250 struct class_device *cdev;
252 if (!edev || component >= edev->components)
253 return -EINVAL;
255 cdev = &edev->component[component].cdev;
257 class_device_del(cdev);
258 if (cdev->dev)
259 put_device(cdev->dev);
260 cdev->dev = get_device(dev);
261 return class_device_add(cdev);
263 EXPORT_SYMBOL_GPL(enclosure_add_device);
266 * enclosure_remove_device - remove a device from an enclosure
267 * @edev: the enclosure device
268 * @num: the number of the component to remove
270 * Returns zero on success or an error.
273 int enclosure_remove_device(struct enclosure_device *edev, int component)
275 struct class_device *cdev;
277 if (!edev || component >= edev->components)
278 return -EINVAL;
280 cdev = &edev->component[component].cdev;
282 class_device_del(cdev);
283 if (cdev->dev)
284 put_device(cdev->dev);
285 cdev->dev = NULL;
286 return class_device_add(cdev);
288 EXPORT_SYMBOL_GPL(enclosure_remove_device);
291 * sysfs pieces below
294 static ssize_t enclosure_show_components(struct class_device *cdev, char *buf)
296 struct enclosure_device *edev = to_enclosure_device(cdev);
298 return snprintf(buf, 40, "%d\n", edev->components);
301 static struct class_device_attribute enclosure_attrs[] = {
302 __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
303 __ATTR_NULL
306 static struct class enclosure_class = {
307 .name = "enclosure",
308 .owner = THIS_MODULE,
309 .release = enclosure_release,
310 .class_dev_attrs = enclosure_attrs,
313 static const char *const enclosure_status [] = {
314 [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
315 [ENCLOSURE_STATUS_OK] = "OK",
316 [ENCLOSURE_STATUS_CRITICAL] = "critical",
317 [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
318 [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
319 [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
320 [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
321 [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
324 static const char *const enclosure_type [] = {
325 [ENCLOSURE_COMPONENT_DEVICE] = "device",
326 [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
329 static ssize_t get_component_fault(struct class_device *cdev, char *buf)
331 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
332 struct enclosure_component *ecomp = to_enclosure_component(cdev);
334 if (edev->cb->get_fault)
335 edev->cb->get_fault(edev, ecomp);
336 return snprintf(buf, 40, "%d\n", ecomp->fault);
339 static ssize_t set_component_fault(struct class_device *cdev, const char *buf,
340 size_t count)
342 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
343 struct enclosure_component *ecomp = to_enclosure_component(cdev);
344 int val = simple_strtoul(buf, NULL, 0);
346 if (edev->cb->set_fault)
347 edev->cb->set_fault(edev, ecomp, val);
348 return count;
351 static ssize_t get_component_status(struct class_device *cdev, char *buf)
353 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
354 struct enclosure_component *ecomp = to_enclosure_component(cdev);
356 if (edev->cb->get_status)
357 edev->cb->get_status(edev, ecomp);
358 return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
361 static ssize_t set_component_status(struct class_device *cdev, const char *buf,
362 size_t count)
364 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
365 struct enclosure_component *ecomp = to_enclosure_component(cdev);
366 int i;
368 for (i = 0; enclosure_status[i]; i++) {
369 if (strncmp(buf, enclosure_status[i],
370 strlen(enclosure_status[i])) == 0 &&
371 (buf[strlen(enclosure_status[i])] == '\n' ||
372 buf[strlen(enclosure_status[i])] == '\0'))
373 break;
376 if (enclosure_status[i] && edev->cb->set_status) {
377 edev->cb->set_status(edev, ecomp, i);
378 return count;
379 } else
380 return -EINVAL;
383 static ssize_t get_component_active(struct class_device *cdev, char *buf)
385 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
386 struct enclosure_component *ecomp = to_enclosure_component(cdev);
388 if (edev->cb->get_active)
389 edev->cb->get_active(edev, ecomp);
390 return snprintf(buf, 40, "%d\n", ecomp->active);
393 static ssize_t set_component_active(struct class_device *cdev, const char *buf,
394 size_t count)
396 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
397 struct enclosure_component *ecomp = to_enclosure_component(cdev);
398 int val = simple_strtoul(buf, NULL, 0);
400 if (edev->cb->set_active)
401 edev->cb->set_active(edev, ecomp, val);
402 return count;
405 static ssize_t get_component_locate(struct class_device *cdev, char *buf)
407 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
408 struct enclosure_component *ecomp = to_enclosure_component(cdev);
410 if (edev->cb->get_locate)
411 edev->cb->get_locate(edev, ecomp);
412 return snprintf(buf, 40, "%d\n", ecomp->locate);
415 static ssize_t set_component_locate(struct class_device *cdev, const char *buf,
416 size_t count)
418 struct enclosure_device *edev = to_enclosure_device(cdev->parent);
419 struct enclosure_component *ecomp = to_enclosure_component(cdev);
420 int val = simple_strtoul(buf, NULL, 0);
422 if (edev->cb->set_locate)
423 edev->cb->set_locate(edev, ecomp, val);
424 return count;
427 static ssize_t get_component_type(struct class_device *cdev, char *buf)
429 struct enclosure_component *ecomp = to_enclosure_component(cdev);
431 return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
435 static struct class_device_attribute enclosure_component_attrs[] = {
436 __ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
437 set_component_fault),
438 __ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
439 set_component_status),
440 __ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
441 set_component_active),
442 __ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
443 set_component_locate),
444 __ATTR(type, S_IRUGO, get_component_type, NULL),
445 __ATTR_NULL
448 static struct class enclosure_component_class = {
449 .name = "enclosure_component",
450 .owner = THIS_MODULE,
451 .class_dev_attrs = enclosure_component_attrs,
452 .release = enclosure_component_release,
455 static int __init enclosure_init(void)
457 int err;
459 err = class_register(&enclosure_class);
460 if (err)
461 return err;
462 err = class_register(&enclosure_component_class);
463 if (err)
464 goto err_out;
466 return 0;
467 err_out:
468 class_unregister(&enclosure_class);
470 return err;
473 static void __exit enclosure_exit(void)
475 class_unregister(&enclosure_component_class);
476 class_unregister(&enclosure_class);
479 module_init(enclosure_init);
480 module_exit(enclosure_exit);
482 MODULE_AUTHOR("James Bottomley");
483 MODULE_DESCRIPTION("Enclosure Services");
484 MODULE_LICENSE("GPL v2");