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
;
34 static struct class enclosure_component_class
;
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 device_put().
45 struct enclosure_device
*enclosure_find(struct device
*dev
)
47 struct enclosure_device
*edev
;
49 mutex_lock(&container_list_lock
);
50 list_for_each_entry(edev
, &container_list
, node
) {
51 if (edev
->edev
.parent
== dev
) {
52 get_device(&edev
->edev
);
53 mutex_unlock(&container_list_lock
);
57 mutex_unlock(&container_list_lock
);
61 EXPORT_SYMBOL_GPL(enclosure_find
);
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
75 int enclosure_for_each_device(int (*fn
)(struct enclosure_device
*, void *),
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
);
87 mutex_unlock(&container_list_lock
);
91 EXPORT_SYMBOL_GPL(enclosure_for_each_device
);
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
,
116 return ERR_PTR(-ENOMEM
);
118 edev
->components
= components
;
120 edev
->edev
.class = &enclosure_class
;
121 edev
->edev
.parent
= get_device(dev
);
123 snprintf(edev
->edev
.bus_id
, BUS_ID_SIZE
, "%s", name
);
124 err
= device_register(&edev
->edev
);
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
);
138 put_device(edev
->edev
.parent
);
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
)
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 device_unregister(&edev
->component
[i
].cdev
);
163 /* prevent any callbacks into service user */
164 edev
->cb
= &enclosure_null_callbacks
;
165 device_unregister(&edev
->edev
);
167 EXPORT_SYMBOL_GPL(enclosure_unregister
);
169 static void enclosure_release(struct device
*cdev
)
171 struct enclosure_device
*edev
= to_enclosure_device(cdev
);
173 put_device(cdev
->parent
);
177 static void enclosure_component_release(struct device
*dev
)
179 struct enclosure_component
*cdev
= to_enclosure_component(dev
);
181 put_device(cdev
->dev
);
182 put_device(dev
->parent
);
186 * enclosure_component_register - add a particular component to an enclosure
187 * @edev: the enclosure to add the component
188 * @num: the device number
189 * @type: the type of component being added
190 * @name: an optional name to appear in sysfs (leave NULL if none)
192 * Registers the component. The name is optional for enclosures that
193 * give their components a unique name. If not, leave the field NULL
194 * and a name will be assigned.
196 * Returns a pointer to the enclosure component or an error.
198 struct enclosure_component
*
199 enclosure_component_register(struct enclosure_device
*edev
,
201 enum enclosure_component_type type
,
204 struct enclosure_component
*ecomp
;
208 if (number
>= edev
->components
)
209 return ERR_PTR(-EINVAL
);
211 ecomp
= &edev
->component
[number
];
213 if (ecomp
->number
!= -1)
214 return ERR_PTR(-EINVAL
);
217 ecomp
->number
= number
;
219 cdev
->parent
= get_device(&edev
->edev
);
220 cdev
->class = &enclosure_component_class
;
222 snprintf(cdev
->bus_id
, BUS_ID_SIZE
, "%s", name
);
224 snprintf(cdev
->bus_id
, BUS_ID_SIZE
, "%u", number
);
226 err
= device_register(cdev
);
232 EXPORT_SYMBOL_GPL(enclosure_component_register
);
235 * enclosure_add_device - add a device as being part of an enclosure
236 * @edev: the enclosure device being added to.
237 * @num: the number of the component
238 * @dev: the device being added
240 * Declares a real device to reside in slot (or identifier) @num of an
241 * enclosure. This will cause the relevant sysfs links to appear.
242 * This function may also be used to change a device associated with
243 * an enclosure without having to call enclosure_remove_device() in
246 * Returns zero on success or an error.
248 int enclosure_add_device(struct enclosure_device
*edev
, int component
,
251 struct enclosure_component
*cdev
;
253 if (!edev
|| component
>= edev
->components
)
256 cdev
= &edev
->component
[component
];
258 device_del(&cdev
->cdev
);
259 put_device(cdev
->dev
);
260 cdev
->dev
= get_device(dev
);
261 return device_add(&cdev
->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 enclosure_component
*cdev
;
277 if (!edev
|| component
>= edev
->components
)
280 cdev
= &edev
->component
[component
];
282 device_del(&cdev
->cdev
);
283 put_device(cdev
->dev
);
285 return device_add(&cdev
->cdev
);
287 EXPORT_SYMBOL_GPL(enclosure_remove_device
);
293 static ssize_t
enclosure_show_components(struct device
*cdev
,
294 struct device_attribute
*attr
,
297 struct enclosure_device
*edev
= to_enclosure_device(cdev
);
299 return snprintf(buf
, 40, "%d\n", edev
->components
);
302 static struct device_attribute enclosure_attrs
[] = {
303 __ATTR(components
, S_IRUGO
, enclosure_show_components
, NULL
),
307 static struct class enclosure_class
= {
309 .owner
= THIS_MODULE
,
310 .dev_release
= enclosure_release
,
311 .dev_attrs
= enclosure_attrs
,
314 static const char *const enclosure_status
[] = {
315 [ENCLOSURE_STATUS_UNSUPPORTED
] = "unsupported",
316 [ENCLOSURE_STATUS_OK
] = "OK",
317 [ENCLOSURE_STATUS_CRITICAL
] = "critical",
318 [ENCLOSURE_STATUS_NON_CRITICAL
] = "non-critical",
319 [ENCLOSURE_STATUS_UNRECOVERABLE
] = "unrecoverable",
320 [ENCLOSURE_STATUS_NOT_INSTALLED
] = "not installed",
321 [ENCLOSURE_STATUS_UNKNOWN
] = "unknown",
322 [ENCLOSURE_STATUS_UNAVAILABLE
] = "unavailable",
325 static const char *const enclosure_type
[] = {
326 [ENCLOSURE_COMPONENT_DEVICE
] = "device",
327 [ENCLOSURE_COMPONENT_ARRAY_DEVICE
] = "array device",
330 static ssize_t
get_component_fault(struct device
*cdev
,
331 struct device_attribute
*attr
, char *buf
)
333 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
334 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
336 if (edev
->cb
->get_fault
)
337 edev
->cb
->get_fault(edev
, ecomp
);
338 return snprintf(buf
, 40, "%d\n", ecomp
->fault
);
341 static ssize_t
set_component_fault(struct device
*cdev
,
342 struct device_attribute
*attr
,
343 const char *buf
, size_t count
)
345 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
346 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
347 int val
= simple_strtoul(buf
, NULL
, 0);
349 if (edev
->cb
->set_fault
)
350 edev
->cb
->set_fault(edev
, ecomp
, val
);
354 static ssize_t
get_component_status(struct device
*cdev
,
355 struct device_attribute
*attr
,char *buf
)
357 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
358 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
360 if (edev
->cb
->get_status
)
361 edev
->cb
->get_status(edev
, ecomp
);
362 return snprintf(buf
, 40, "%s\n", enclosure_status
[ecomp
->status
]);
365 static ssize_t
set_component_status(struct device
*cdev
,
366 struct device_attribute
*attr
,
367 const char *buf
, size_t count
)
369 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
370 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
373 for (i
= 0; enclosure_status
[i
]; i
++) {
374 if (strncmp(buf
, enclosure_status
[i
],
375 strlen(enclosure_status
[i
])) == 0 &&
376 (buf
[strlen(enclosure_status
[i
])] == '\n' ||
377 buf
[strlen(enclosure_status
[i
])] == '\0'))
381 if (enclosure_status
[i
] && edev
->cb
->set_status
) {
382 edev
->cb
->set_status(edev
, ecomp
, i
);
388 static ssize_t
get_component_active(struct device
*cdev
,
389 struct device_attribute
*attr
, char *buf
)
391 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
392 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
394 if (edev
->cb
->get_active
)
395 edev
->cb
->get_active(edev
, ecomp
);
396 return snprintf(buf
, 40, "%d\n", ecomp
->active
);
399 static ssize_t
set_component_active(struct device
*cdev
,
400 struct device_attribute
*attr
,
401 const char *buf
, size_t count
)
403 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
404 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
405 int val
= simple_strtoul(buf
, NULL
, 0);
407 if (edev
->cb
->set_active
)
408 edev
->cb
->set_active(edev
, ecomp
, val
);
412 static ssize_t
get_component_locate(struct device
*cdev
,
413 struct device_attribute
*attr
, char *buf
)
415 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
416 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
418 if (edev
->cb
->get_locate
)
419 edev
->cb
->get_locate(edev
, ecomp
);
420 return snprintf(buf
, 40, "%d\n", ecomp
->locate
);
423 static ssize_t
set_component_locate(struct device
*cdev
,
424 struct device_attribute
*attr
,
425 const char *buf
, size_t count
)
427 struct enclosure_device
*edev
= to_enclosure_device(cdev
->parent
);
428 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
429 int val
= simple_strtoul(buf
, NULL
, 0);
431 if (edev
->cb
->set_locate
)
432 edev
->cb
->set_locate(edev
, ecomp
, val
);
436 static ssize_t
get_component_type(struct device
*cdev
,
437 struct device_attribute
*attr
, char *buf
)
439 struct enclosure_component
*ecomp
= to_enclosure_component(cdev
);
441 return snprintf(buf
, 40, "%s\n", enclosure_type
[ecomp
->type
]);
445 static struct device_attribute enclosure_component_attrs
[] = {
446 __ATTR(fault
, S_IRUGO
| S_IWUSR
, get_component_fault
,
447 set_component_fault
),
448 __ATTR(status
, S_IRUGO
| S_IWUSR
, get_component_status
,
449 set_component_status
),
450 __ATTR(active
, S_IRUGO
| S_IWUSR
, get_component_active
,
451 set_component_active
),
452 __ATTR(locate
, S_IRUGO
| S_IWUSR
, get_component_locate
,
453 set_component_locate
),
454 __ATTR(type
, S_IRUGO
, get_component_type
, NULL
),
458 static struct class enclosure_component_class
= {
459 .name
= "enclosure_component",
460 .owner
= THIS_MODULE
,
461 .dev_attrs
= enclosure_component_attrs
,
462 .dev_release
= enclosure_component_release
,
465 static int __init
enclosure_init(void)
469 err
= class_register(&enclosure_class
);
472 err
= class_register(&enclosure_component_class
);
478 class_unregister(&enclosure_class
);
483 static void __exit
enclosure_exit(void)
485 class_unregister(&enclosure_component_class
);
486 class_unregister(&enclosure_class
);
489 module_init(enclosure_init
);
490 module_exit(enclosure_exit
);
492 MODULE_AUTHOR("James Bottomley");
493 MODULE_DESCRIPTION("Enclosure Services");
494 MODULE_LICENSE("GPL v2");