V4L/DVB (7615): em28xx: Provide the proper support for switching between analog/digital
[linux-2.6/mini2440.git] / drivers / misc / enclosure.c
blobfafb57fed761c8f1e6fa5c91f136c139593e0211
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 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);
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->edev.class = &enclosure_class;
121 edev->edev.parent = get_device(dev);
122 edev->cb = cb;
123 snprintf(edev->edev.bus_id, BUS_ID_SIZE, "%s", name);
124 err = device_register(&edev->edev);
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->edev.parent);
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 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);
174 kfree(edev);
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,
200 unsigned int number,
201 enum enclosure_component_type type,
202 const char *name)
204 struct enclosure_component *ecomp;
205 struct device *cdev;
206 int err;
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);
216 ecomp->type = type;
217 ecomp->number = number;
218 cdev = &ecomp->cdev;
219 cdev->parent = get_device(&edev->edev);
220 cdev->class = &enclosure_component_class;
221 if (name)
222 snprintf(cdev->bus_id, BUS_ID_SIZE, "%s", name);
223 else
224 snprintf(cdev->bus_id, BUS_ID_SIZE, "%u", number);
226 err = device_register(cdev);
227 if (err)
228 ERR_PTR(err);
230 return ecomp;
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
244 * between.
246 * Returns zero on success or an error.
248 int enclosure_add_device(struct enclosure_device *edev, int component,
249 struct device *dev)
251 struct enclosure_component *cdev;
253 if (!edev || component >= edev->components)
254 return -EINVAL;
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)
278 return -EINVAL;
280 cdev = &edev->component[component];
282 device_del(&cdev->cdev);
283 put_device(cdev->dev);
284 cdev->dev = NULL;
285 return device_add(&cdev->cdev);
287 EXPORT_SYMBOL_GPL(enclosure_remove_device);
290 * sysfs pieces below
293 static ssize_t enclosure_show_components(struct device *cdev,
294 struct device_attribute *attr,
295 char *buf)
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),
304 __ATTR_NULL
307 static struct class enclosure_class = {
308 .name = "enclosure",
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);
351 return count;
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);
371 int i;
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'))
378 break;
381 if (enclosure_status[i] && edev->cb->set_status) {
382 edev->cb->set_status(edev, ecomp, i);
383 return count;
384 } else
385 return -EINVAL;
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);
409 return count;
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);
433 return count;
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),
455 __ATTR_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)
467 int err;
469 err = class_register(&enclosure_class);
470 if (err)
471 return err;
472 err = class_register(&enclosure_component_class);
473 if (err)
474 goto err_out;
476 return 0;
477 err_out:
478 class_unregister(&enclosure_class);
480 return err;
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");