| 1 | |
| 2 | |
| 3 | * Enclosure Services
|
| 4 | *
|
| 5 | * Copyright (C) 2008 James Bottomley <[email protected]>
|
| 6 | *
|
| 7 | **-----------------------------------------------------------------------------
|
| 8 | **
|
| 9 | **
|
| 10 | **-----------------------------------------------------------------------------
|
| 11 |
|
| 12 | #include <linux/device.h>
|
| 13 | #include <linux/enclosure.h>
|
| 14 | #include <linux/err.h>
|
| 15 | #include <linux/list.h>
|
| 16 | #include <linux/kernel.h>
|
| 17 | #include <linux/module.h>
|
| 18 | #include <linux/mutex.h>
|
| 19 | #include <linux/slab.h>
|
| 20 | #include <linux/string_choices.h>
|
| 21 |
|
| 22 | static LIST_HEAD(container_list);
|
| 23 | static DEFINE_MUTEX(container_list_lock);
|
| 24 | static struct class enclosure_class;
|
| 25 |
|
| 26 | |
| 27 | * enclosure_find - find an enclosure given a parent device
|
| 28 | * @dev: the parent to match against
|
| 29 | * @start: Optional enclosure device to start from (NULL if none)
|
| 30 | *
|
| 31 | * Looks through the list of registered enclosures to find all those
|
| 32 | * with @dev as a parent. Returns NULL if no enclosure is
|
| 33 | * found. @start can be used as a starting point to obtain multiple
|
| 34 | * enclosures per parent (should begin with NULL and then be set to
|
| 35 | * each returned enclosure device). Obtains a reference to the
|
| 36 | * enclosure class device which must be released with put_device().
|
| 37 | * If @start is not NULL, a reference must be taken on it which is
|
| 38 | * released before returning (this allows a loop through all
|
| 39 | * enclosures to exit with only the reference on the enclosure of
|
| 40 | * interest held). Note that the @dev may correspond to the actual
|
| 41 | * device housing the enclosure, in which case no iteration via @start
|
| 42 | * is required.
|
| 43 |
|
| 44 | struct enclosure_device *enclosure_find(struct device *dev,
|
| 45 | struct enclosure_device *start)
|
| 46 | {
|
| 47 | struct enclosure_device *edev;
|
| 48 |
|
| 49 | mutex_lock(&container_list_lock);
|
| 50 | edev = list_prepare_entry(start, &container_list, node);
|
| 51 | if (start)
|
| 52 | put_device(&start->edev);
|
| 53 |
|
| 54 | list_for_each_entry_continue(edev, &container_list, node) {
|
| 55 | struct device *parent = edev->edev.parent;
|
| 56 | |
| 57 | * the root of the tree if necessary
|
| 58 | while (parent) {
|
| 59 | if (parent == dev) {
|
| 60 | get_device(&edev->edev);
|
| 61 | mutex_unlock(&container_list_lock);
|
| 62 | return edev;
|
| 63 | }
|
| 64 | parent = parent->parent;
|
| 65 | }
|
| 66 | }
|
| 67 | mutex_unlock(&container_list_lock);
|
| 68 |
|
| 69 | return NULL;
|
| 70 | }
|
| 71 | EXPORT_SYMBOL_GPL(enclosure_find);
|
| 72 |
|
| 73 | |
| 74 | * enclosure_for_each_device - calls a function for each enclosure
|
| 75 | * @fn: the function to call
|
| 76 | * @data: the data to pass to each call
|
| 77 | *
|
| 78 | * Loops over all the enclosures calling the function.
|
| 79 | *
|
| 80 | * Note, this function uses a mutex which will be held across calls to
|
| 81 | * @fn, so it must have non atomic context, and @fn may (although it
|
| 82 | * should not) sleep or otherwise cause the mutex to be held for
|
| 83 | * indefinite periods
|
| 84 |
|
| 85 | int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *),
|
| 86 | void *data)
|
| 87 | {
|
| 88 | int error = 0;
|
| 89 | struct enclosure_device *edev;
|
| 90 |
|
| 91 | mutex_lock(&container_list_lock);
|
| 92 | list_for_each_entry(edev, &container_list, node) {
|
| 93 | error = fn(edev, data);
|
| 94 | if (error)
|
| 95 | break;
|
| 96 | }
|
| 97 | mutex_unlock(&container_list_lock);
|
| 98 |
|
| 99 | return error;
|
| 100 | }
|
| 101 | EXPORT_SYMBOL_GPL(enclosure_for_each_device);
|
| 102 |
|
| 103 | |
| 104 | * enclosure_register - register device as an enclosure
|
| 105 | *
|
| 106 | * @dev: device containing the enclosure
|
| 107 | * @name: chosen device name
|
| 108 | * @components: number of components in the enclosure
|
| 109 | * @cb: platform call-backs
|
| 110 | *
|
| 111 | * This sets up the device for being an enclosure. Note that @dev does
|
| 112 | * not have to be a dedicated enclosure device. It may be some other type
|
| 113 | * of device that additionally responds to enclosure services
|
| 114 |
|
| 115 | struct enclosure_device *
|
| 116 | enclosure_register(struct device *dev, const char *name, int components,
|
| 117 | struct enclosure_component_callbacks *cb)
|
| 118 | {
|
| 119 | struct enclosure_device *edev =
|
| 120 | kzalloc_flex(*edev, component, components);
|
| 121 | int err, i;
|
| 122 |
|
| 123 | BUG_ON(!cb);
|
| 124 |
|
| 125 | if (!edev)
|
| 126 | return ERR_PTR(-ENOMEM);
|
| 127 |
|
| 128 | edev->components = components;
|
| 129 |
|
| 130 | edev->edev.class = &enclosure_class;
|
| 131 | edev->edev.parent = get_device(dev);
|
| 132 | edev->cb = cb;
|
| 133 | dev_set_name(&edev->edev, "%s", name);
|
| 134 | err = device_register(&edev->edev);
|
| 135 | if (err)
|
| 136 | goto err;
|
| 137 |
|
| 138 | for (i = 0; i < components; i++) {
|
| 139 | edev->component[i].number = -1;
|
| 140 | edev->component[i].slot = -1;
|
| 141 | edev->component[i].power_status = -1;
|
| 142 | }
|
| 143 |
|
| 144 | mutex_lock(&container_list_lock);
|
| 145 | list_add_tail(&edev->node, &container_list);
|
| 146 | mutex_unlock(&container_list_lock);
|
| 147 |
|
| 148 | return edev;
|
| 149 |
|
| 150 | err:
|
| 151 | put_device(edev->edev.parent);
|
| 152 | kfree(edev);
|
| 153 | return ERR_PTR(err);
|
| 154 | }
|
| 155 | EXPORT_SYMBOL_GPL(enclosure_register);
|
| 156 |
|
| 157 | static struct enclosure_component_callbacks enclosure_null_callbacks;
|
| 158 |
|
| 159 | |
| 160 | * enclosure_unregister - remove an enclosure
|
| 161 | *
|
| 162 | * @edev: the registered enclosure to remove;
|
| 163 |
|
| 164 | void enclosure_unregister(struct enclosure_device *edev)
|
| 165 | {
|
| 166 | int i;
|
| 167 |
|
| 168 | mutex_lock(&container_list_lock);
|
| 169 | list_del(&edev->node);
|
| 170 | mutex_unlock(&container_list_lock);
|
| 171 |
|
| 172 | for (i = 0; i < edev->components; i++)
|
| 173 | if (edev->component[i].number != -1)
|
| 174 | device_unregister(&edev->component[i].cdev);
|
| 175 |
|
| 176 |
|
| 177 | edev->cb = &enclosure_null_callbacks;
|
| 178 | device_unregister(&edev->edev);
|
| 179 | }
|
| 180 | EXPORT_SYMBOL_GPL(enclosure_unregister);
|
| 181 |
|
| 182 | #define ENCLOSURE_NAME_SIZE 64
|
| 183 | #define COMPONENT_NAME_SIZE 64
|
| 184 |
|
| 185 | static void enclosure_link_name(struct enclosure_component *cdev, char *name)
|
| 186 | {
|
| 187 | snprintf(name, ENCLOSURE_NAME_SIZE, "enclosure_device:%s",
|
| 188 | dev_name(&cdev->cdev));
|
| 189 | }
|
| 190 |
|
| 191 | static void enclosure_remove_links(struct enclosure_component *cdev)
|
| 192 | {
|
| 193 | char name[ENCLOSURE_NAME_SIZE];
|
| 194 |
|
| 195 | enclosure_link_name(cdev, name);
|
| 196 |
|
| 197 | |
| 198 | * In odd circumstances, like multipath devices, something else may
|
| 199 | * already have removed the links, so check for this condition first.
|
| 200 |
|
| 201 | if (cdev->dev->kobj.sd)
|
| 202 | sysfs_remove_link(&cdev->dev->kobj, name);
|
| 203 |
|
| 204 | if (cdev->cdev.kobj.sd)
|
| 205 | sysfs_remove_link(&cdev->cdev.kobj, "device");
|
| 206 | }
|
| 207 |
|
| 208 | static int enclosure_add_links(struct enclosure_component *cdev)
|
| 209 | {
|
| 210 | int error;
|
| 211 | char name[ENCLOSURE_NAME_SIZE];
|
| 212 |
|
| 213 | error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
|
| 214 | if (error)
|
| 215 | return error;
|
| 216 |
|
| 217 | enclosure_link_name(cdev, name);
|
| 218 | error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
|
| 219 | if (error)
|
| 220 | sysfs_remove_link(&cdev->cdev.kobj, "device");
|
| 221 |
|
| 222 | return error;
|
| 223 | }
|
| 224 |
|
| 225 | static void enclosure_release(struct device *cdev)
|
| 226 | {
|
| 227 | struct enclosure_device *edev = to_enclosure_device(cdev);
|
| 228 |
|
| 229 | put_device(cdev->parent);
|
| 230 | kfree(edev);
|
| 231 | }
|
| 232 |
|
| 233 | static void enclosure_component_release(struct device *dev)
|
| 234 | {
|
| 235 | struct enclosure_component *cdev = to_enclosure_component(dev);
|
| 236 |
|
| 237 | if (cdev->dev) {
|
| 238 | enclosure_remove_links(cdev);
|
| 239 | put_device(cdev->dev);
|
| 240 | }
|
| 241 | put_device(dev->parent);
|
| 242 | }
|
| 243 |
|
| 244 | static struct enclosure_component *
|
| 245 | enclosure_component_find_by_name(struct enclosure_device *edev,
|
| 246 | const char *name)
|
| 247 | {
|
| 248 | int i;
|
| 249 | const char *cname;
|
| 250 | struct enclosure_component *ecomp;
|
| 251 |
|
| 252 | if (!edev || !name || !name[0])
|
| 253 | return NULL;
|
| 254 |
|
| 255 | for (i = 0; i < edev->components; i++) {
|
| 256 | ecomp = &edev->component[i];
|
| 257 | cname = dev_name(&ecomp->cdev);
|
| 258 | if (ecomp->number != -1 &&
|
| 259 | cname && cname[0] &&
|
| 260 | !strcmp(cname, name))
|
| 261 | return ecomp;
|
| 262 | }
|
| 263 |
|
| 264 | return NULL;
|
| 265 | }
|
| 266 |
|
| 267 | static const struct attribute_group *enclosure_component_groups[];
|
| 268 |
|
| 269 | |
| 270 | * enclosure_component_alloc - prepare a new enclosure component
|
| 271 | * @edev: the enclosure to add the component
|
| 272 | * @number: the device number
|
| 273 | * @type: the type of component being added
|
| 274 | * @name: an optional name to appear in sysfs (leave NULL if none)
|
| 275 | *
|
| 276 | * The name is optional for enclosures that give their components a unique
|
| 277 | * name. If not, leave the field NULL and a name will be assigned.
|
| 278 | *
|
| 279 | * Returns a pointer to the enclosure component or an error.
|
| 280 |
|
| 281 | struct enclosure_component *
|
| 282 | enclosure_component_alloc(struct enclosure_device *edev,
|
| 283 | unsigned int number,
|
| 284 | enum enclosure_component_type type,
|
| 285 | const char *name)
|
| 286 | {
|
| 287 | struct enclosure_component *ecomp;
|
| 288 | struct device *cdev;
|
| 289 | int i;
|
| 290 | char newname[COMPONENT_NAME_SIZE];
|
| 291 |
|
| 292 | if (number >= edev->components)
|
| 293 | return ERR_PTR(-EINVAL);
|
| 294 |
|
| 295 | ecomp = &edev->component[number];
|
| 296 |
|
| 297 | if (ecomp->number != -1)
|
| 298 | return ERR_PTR(-EINVAL);
|
| 299 |
|
| 300 | ecomp->type = type;
|
| 301 | ecomp->number = number;
|
| 302 | cdev = &ecomp->cdev;
|
| 303 | cdev->parent = get_device(&edev->edev);
|
| 304 |
|
| 305 | if (name && name[0]) {
|
| 306 | |
| 307 | * with non unique names. Registering duplicates in sysfs
|
| 308 | * will lead to warnings during bootup. So make the names
|
| 309 | * unique by appending consecutive numbers -1, -2, ...
|
| 310 | i = 1;
|
| 311 | snprintf(newname, COMPONENT_NAME_SIZE,
|
| 312 | "%s", name);
|
| 313 | while (enclosure_component_find_by_name(edev, newname))
|
| 314 | snprintf(newname, COMPONENT_NAME_SIZE,
|
| 315 | "%s-%i", name, i++);
|
| 316 | dev_set_name(cdev, "%s", newname);
|
| 317 | } else
|
| 318 | dev_set_name(cdev, "%u", number);
|
| 319 |
|
| 320 | cdev->release = enclosure_component_release;
|
| 321 | cdev->groups = enclosure_component_groups;
|
| 322 |
|
| 323 | return ecomp;
|
| 324 | }
|
| 325 | EXPORT_SYMBOL_GPL(enclosure_component_alloc);
|
| 326 |
|
| 327 | |
| 328 | * enclosure_component_register - publishes an initialized enclosure component
|
| 329 | * @ecomp: component to add
|
| 330 | *
|
| 331 | * Returns 0 on successful registration, releases the component otherwise
|
| 332 |
|
| 333 | int enclosure_component_register(struct enclosure_component *ecomp)
|
| 334 | {
|
| 335 | struct device *cdev;
|
| 336 | int err;
|
| 337 |
|
| 338 | cdev = &ecomp->cdev;
|
| 339 | err = device_register(cdev);
|
| 340 | if (err) {
|
| 341 | ecomp->number = -1;
|
| 342 | put_device(cdev);
|
| 343 | return err;
|
| 344 | }
|
| 345 |
|
| 346 | return 0;
|
| 347 | }
|
| 348 | EXPORT_SYMBOL_GPL(enclosure_component_register);
|
| 349 |
|
| 350 | |
| 351 | * enclosure_add_device - add a device as being part of an enclosure
|
| 352 | * @edev: the enclosure device being added to.
|
| 353 | * @component: the number of the component
|
| 354 | * @dev: the device being added
|
| 355 | *
|
| 356 | * Declares a real device to reside in slot (or identifier) @num of an
|
| 357 | * enclosure. This will cause the relevant sysfs links to appear.
|
| 358 | * This function may also be used to change a device associated with
|
| 359 | * an enclosure without having to call enclosure_remove_device() in
|
| 360 | * between.
|
| 361 | *
|
| 362 | * Returns zero on success or an error.
|
| 363 |
|
| 364 | int enclosure_add_device(struct enclosure_device *edev, int component,
|
| 365 | struct device *dev)
|
| 366 | {
|
| 367 | struct enclosure_component *cdev;
|
| 368 | int err;
|
| 369 |
|
| 370 | if (!edev || component >= edev->components)
|
| 371 | return -EINVAL;
|
| 372 |
|
| 373 | cdev = &edev->component[component];
|
| 374 |
|
| 375 | if (cdev->dev == dev)
|
| 376 | return -EEXIST;
|
| 377 |
|
| 378 | if (cdev->dev) {
|
| 379 | enclosure_remove_links(cdev);
|
| 380 | put_device(cdev->dev);
|
| 381 | }
|
| 382 | cdev->dev = get_device(dev);
|
| 383 | err = enclosure_add_links(cdev);
|
| 384 | if (err) {
|
| 385 | put_device(cdev->dev);
|
| 386 | cdev->dev = NULL;
|
| 387 | }
|
| 388 | return err;
|
| 389 | }
|
| 390 | EXPORT_SYMBOL_GPL(enclosure_add_device);
|
| 391 |
|
| 392 | |
| 393 | * enclosure_remove_device - remove a device from an enclosure
|
| 394 | * @edev: the enclosure device
|
| 395 | * @dev: device to remove/put
|
| 396 | *
|
| 397 | * Returns zero on success or an error.
|
| 398 | *
|
| 399 |
|
| 400 | int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
|
| 401 | {
|
| 402 | struct enclosure_component *cdev;
|
| 403 | int i;
|
| 404 |
|
| 405 | if (!edev || !dev)
|
| 406 | return -EINVAL;
|
| 407 |
|
| 408 | for (i = 0; i < edev->components; i++) {
|
| 409 | cdev = &edev->component[i];
|
| 410 | if (cdev->dev == dev) {
|
| 411 | enclosure_remove_links(cdev);
|
| 412 | put_device(dev);
|
| 413 | cdev->dev = NULL;
|
| 414 | return 0;
|
| 415 | }
|
| 416 | }
|
| 417 | return -ENODEV;
|
| 418 | }
|
| 419 | EXPORT_SYMBOL_GPL(enclosure_remove_device);
|
| 420 |
|
| 421 | |
| 422 | * sysfs pieces below
|
| 423 |
|
| 424 |
|
| 425 | static ssize_t components_show(struct device *cdev,
|
| 426 | struct device_attribute *attr, char *buf)
|
| 427 | {
|
| 428 | struct enclosure_device *edev = to_enclosure_device(cdev);
|
| 429 |
|
| 430 | return sysfs_emit(buf, "%d\n", edev->components);
|
| 431 | }
|
| 432 | static DEVICE_ATTR_RO(components);
|
| 433 |
|
| 434 | static ssize_t id_show(struct device *cdev,
|
| 435 | struct device_attribute *attr,
|
| 436 | char *buf)
|
| 437 | {
|
| 438 | struct enclosure_device *edev = to_enclosure_device(cdev);
|
| 439 |
|
| 440 | if (edev->cb->show_id)
|
| 441 | return edev->cb->show_id(edev, buf);
|
| 442 | return -EINVAL;
|
| 443 | }
|
| 444 | static DEVICE_ATTR_RO(id);
|
| 445 |
|
| 446 | static struct attribute *enclosure_class_attrs[] = {
|
| 447 | &dev_attr_components.attr,
|
| 448 | &dev_attr_id.attr,
|
| 449 | NULL,
|
| 450 | };
|
| 451 | ATTRIBUTE_GROUPS(enclosure_class);
|
| 452 |
|
| 453 | static struct class enclosure_class = {
|
| 454 | .name = "enclosure",
|
| 455 | .dev_release = enclosure_release,
|
| 456 | .dev_groups = enclosure_class_groups,
|
| 457 | };
|
| 458 |
|
| 459 | static const char *const enclosure_status[] = {
|
| 460 | [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
|
| 461 | [ENCLOSURE_STATUS_OK] = "OK",
|
| 462 | [ENCLOSURE_STATUS_CRITICAL] = "critical",
|
| 463 | [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
|
| 464 | [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
|
| 465 | [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
|
| 466 | [ENCLOSURE_STATUS_UNKNOWN] = "unknown",
|
| 467 | [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
|
| 468 | [ENCLOSURE_STATUS_MAX] = NULL,
|
| 469 | };
|
| 470 |
|
| 471 | static const char *const enclosure_type[] = {
|
| 472 | [ENCLOSURE_COMPONENT_DEVICE] = "device",
|
| 473 | [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
|
| 474 | };
|
| 475 |
|
| 476 | static ssize_t get_component_fault(struct device *cdev,
|
| 477 | struct device_attribute *attr, char *buf)
|
| 478 | {
|
| 479 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 480 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 481 |
|
| 482 | if (edev->cb->get_fault)
|
| 483 | edev->cb->get_fault(edev, ecomp);
|
| 484 | return sysfs_emit(buf, "%d\n", ecomp->fault);
|
| 485 | }
|
| 486 |
|
| 487 | static ssize_t set_component_fault(struct device *cdev,
|
| 488 | struct device_attribute *attr,
|
| 489 | const char *buf, size_t count)
|
| 490 | {
|
| 491 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 492 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 493 | int val = simple_strtoul(buf, NULL, 0);
|
| 494 |
|
| 495 | if (edev->cb->set_fault)
|
| 496 | edev->cb->set_fault(edev, ecomp, val);
|
| 497 | return count;
|
| 498 | }
|
| 499 |
|
| 500 | static ssize_t get_component_status(struct device *cdev,
|
| 501 | struct device_attribute *attr,char *buf)
|
| 502 | {
|
| 503 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 504 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 505 |
|
| 506 | if (edev->cb->get_status)
|
| 507 | edev->cb->get_status(edev, ecomp);
|
| 508 | return sysfs_emit(buf, "%s\n", enclosure_status[ecomp->status]);
|
| 509 | }
|
| 510 |
|
| 511 | static ssize_t set_component_status(struct device *cdev,
|
| 512 | struct device_attribute *attr,
|
| 513 | const char *buf, size_t count)
|
| 514 | {
|
| 515 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 516 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 517 | int i;
|
| 518 |
|
| 519 | for (i = 0; enclosure_status[i]; i++) {
|
| 520 | if (strncmp(buf, enclosure_status[i],
|
| 521 | strlen(enclosure_status[i])) == 0 &&
|
| 522 | (buf[strlen(enclosure_status[i])] == '\n' ||
|
| 523 | buf[strlen(enclosure_status[i])] == '\0'))
|
| 524 | break;
|
| 525 | }
|
| 526 |
|
| 527 | if (enclosure_status[i] && edev->cb->set_status) {
|
| 528 | edev->cb->set_status(edev, ecomp, i);
|
| 529 | return count;
|
| 530 | } else
|
| 531 | return -EINVAL;
|
| 532 | }
|
| 533 |
|
| 534 | static ssize_t get_component_active(struct device *cdev,
|
| 535 | struct device_attribute *attr, char *buf)
|
| 536 | {
|
| 537 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 538 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 539 |
|
| 540 | if (edev->cb->get_active)
|
| 541 | edev->cb->get_active(edev, ecomp);
|
| 542 | return sysfs_emit(buf, "%d\n", ecomp->active);
|
| 543 | }
|
| 544 |
|
| 545 | static ssize_t set_component_active(struct device *cdev,
|
| 546 | struct device_attribute *attr,
|
| 547 | const char *buf, size_t count)
|
| 548 | {
|
| 549 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 550 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 551 | int val = simple_strtoul(buf, NULL, 0);
|
| 552 |
|
| 553 | if (edev->cb->set_active)
|
| 554 | edev->cb->set_active(edev, ecomp, val);
|
| 555 | return count;
|
| 556 | }
|
| 557 |
|
| 558 | static ssize_t get_component_locate(struct device *cdev,
|
| 559 | struct device_attribute *attr, char *buf)
|
| 560 | {
|
| 561 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 562 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 563 |
|
| 564 | if (edev->cb->get_locate)
|
| 565 | edev->cb->get_locate(edev, ecomp);
|
| 566 | return sysfs_emit(buf, "%d\n", ecomp->locate);
|
| 567 | }
|
| 568 |
|
| 569 | static ssize_t set_component_locate(struct device *cdev,
|
| 570 | struct device_attribute *attr,
|
| 571 | const char *buf, size_t count)
|
| 572 | {
|
| 573 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 574 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 575 | int val = simple_strtoul(buf, NULL, 0);
|
| 576 |
|
| 577 | if (edev->cb->set_locate)
|
| 578 | edev->cb->set_locate(edev, ecomp, val);
|
| 579 | return count;
|
| 580 | }
|
| 581 |
|
| 582 | static ssize_t get_component_power_status(struct device *cdev,
|
| 583 | struct device_attribute *attr,
|
| 584 | char *buf)
|
| 585 | {
|
| 586 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 587 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 588 |
|
| 589 | if (edev->cb->get_power_status)
|
| 590 | edev->cb->get_power_status(edev, ecomp);
|
| 591 |
|
| 592 |
|
| 593 | if (ecomp->power_status == -1)
|
| 594 | return (edev->cb->get_power_status) ? -EIO : -ENOTTY;
|
| 595 |
|
| 596 | return sysfs_emit(buf, "%s\n", str_on_off(ecomp->power_status));
|
| 597 | }
|
| 598 |
|
| 599 | static ssize_t set_component_power_status(struct device *cdev,
|
| 600 | struct device_attribute *attr,
|
| 601 | const char *buf, size_t count)
|
| 602 | {
|
| 603 | struct enclosure_device *edev = to_enclosure_device(cdev->parent);
|
| 604 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 605 | int val;
|
| 606 |
|
| 607 | if (strncmp(buf, "on", 2) == 0 &&
|
| 608 | (buf[2] == '\n' || buf[2] == '\0'))
|
| 609 | val = 1;
|
| 610 | else if (strncmp(buf, "off", 3) == 0 &&
|
| 611 | (buf[3] == '\n' || buf[3] == '\0'))
|
| 612 | val = 0;
|
| 613 | else
|
| 614 | return -EINVAL;
|
| 615 |
|
| 616 | if (edev->cb->set_power_status)
|
| 617 | edev->cb->set_power_status(edev, ecomp, val);
|
| 618 | return count;
|
| 619 | }
|
| 620 |
|
| 621 | static ssize_t get_component_type(struct device *cdev,
|
| 622 | struct device_attribute *attr, char *buf)
|
| 623 | {
|
| 624 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 625 |
|
| 626 | return sysfs_emit(buf, "%s\n", enclosure_type[ecomp->type]);
|
| 627 | }
|
| 628 |
|
| 629 | static ssize_t get_component_slot(struct device *cdev,
|
| 630 | struct device_attribute *attr, char *buf)
|
| 631 | {
|
| 632 | struct enclosure_component *ecomp = to_enclosure_component(cdev);
|
| 633 | int slot;
|
| 634 |
|
| 635 |
|
| 636 | if (ecomp->slot >= 0)
|
| 637 | slot = ecomp->slot;
|
| 638 | else
|
| 639 | slot = ecomp->number;
|
| 640 |
|
| 641 | return sysfs_emit(buf, "%d\n", slot);
|
| 642 | }
|
| 643 |
|
| 644 | static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
|
| 645 | set_component_fault);
|
| 646 | static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
|
| 647 | set_component_status);
|
| 648 | static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
|
| 649 | set_component_active);
|
| 650 | static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
|
| 651 | set_component_locate);
|
| 652 | static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status,
|
| 653 | set_component_power_status);
|
| 654 | static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
|
| 655 | static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL);
|
| 656 |
|
| 657 | static struct attribute *enclosure_component_attrs[] = {
|
| 658 | &dev_attr_fault.attr,
|
| 659 | &dev_attr_status.attr,
|
| 660 | &dev_attr_active.attr,
|
| 661 | &dev_attr_locate.attr,
|
| 662 | &dev_attr_power_status.attr,
|
| 663 | &dev_attr_type.attr,
|
| 664 | &dev_attr_slot.attr,
|
| 665 | NULL
|
| 666 | };
|
| 667 | ATTRIBUTE_GROUPS(enclosure_component);
|
| 668 |
|
| 669 | static int __init enclosure_init(void)
|
| 670 | {
|
| 671 | return class_register(&enclosure_class);
|
| 672 | }
|
| 673 |
|
| 674 | static void __exit enclosure_exit(void)
|
| 675 | {
|
| 676 | class_unregister(&enclosure_class);
|
| 677 | }
|
| 678 |
|
| 679 | module_init(enclosure_init);
|
| 680 | module_exit(enclosure_exit);
|
| 681 |
|
| 682 | MODULE_AUTHOR("James Bottomley");
|
| 683 | MODULE_DESCRIPTION("Enclosure Services");
|
| 684 | MODULE_LICENSE("GPL v2");
|