1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * drivers/acpi/device_sysfs.c - ACPI device sysfs attributes and modalias. 4 * 5 * Copyright (C) 2015, Intel Corp. 6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 7 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 8 * 9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 * 11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 12 */ 13 14#include <linux/acpi.h> 15#include <linux/device.h> 16#include <linux/export.h> 17#include <linux/nls.h> 18 19#include "internal.h" 20 21static ssize_t acpi_object_path(acpi_handle handle, char *buf) 22{ 23 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; 24 int result; 25 26 result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path); 27 if (result) 28 return result; 29 30 result = sprintf(buf, "%s\n", (char *)path.pointer); 31 kfree(path.pointer); 32 return result; 33} 34 35struct acpi_data_node_attr { 36 struct attribute attr; 37 ssize_t (*show)(struct acpi_data_node *, char *); 38 ssize_t (*store)(struct acpi_data_node *, const char *, size_t count); 39}; 40 41#define DATA_NODE_ATTR(_name) \ 42 static struct acpi_data_node_attr data_node_##_name = \ 43 __ATTR(_name, 0444, data_node_show_##_name, NULL) 44 45static ssize_t data_node_show_path(struct acpi_data_node *dn, char *buf) 46{ 47 return dn->handle ? acpi_object_path(dn->handle, buf) : 0; 48} 49 50DATA_NODE_ATTR(path); 51 52static struct attribute *acpi_data_node_default_attrs[] = { 53 &data_node_path.attr, 54 NULL 55}; 56 57#define to_data_node(k) container_of(k, struct acpi_data_node, kobj) 58#define to_attr(a) container_of(a, struct acpi_data_node_attr, attr) 59 60static ssize_t acpi_data_node_attr_show(struct kobject *kobj, 61 struct attribute *attr, char *buf) 62{ 63 struct acpi_data_node *dn = to_data_node(kobj); 64 struct acpi_data_node_attr *dn_attr = to_attr(attr); 65 66 return dn_attr->show ? dn_attr->show(dn, buf) : -ENXIO; 67} 68 69static const struct sysfs_ops acpi_data_node_sysfs_ops = { 70 .show = acpi_data_node_attr_show, 71}; 72 73static void acpi_data_node_release(struct kobject *kobj) 74{ 75 struct acpi_data_node *dn = to_data_node(kobj); 76 complete(&dn->kobj_done); 77} 78 79static struct kobj_type acpi_data_node_ktype = { 80 .sysfs_ops = &acpi_data_node_sysfs_ops, 81 .default_attrs = acpi_data_node_default_attrs, 82 .release = acpi_data_node_release, 83}; 84 85static void acpi_expose_nondev_subnodes(struct kobject *kobj, 86 struct acpi_device_data *data) 87{ 88 struct list_head *list = &data->subnodes; 89 struct acpi_data_node *dn; 90 91 if (list_empty(list)) 92 return; 93 94 list_for_each_entry(dn, list, sibling) { 95 int ret; 96 97 init_completion(&dn->kobj_done); 98 ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype, 99 kobj, "%s", dn->name); 100 if (!ret) 101 acpi_expose_nondev_subnodes(&dn->kobj, &dn->data); 102 else if (dn->handle) 103 acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret); 104 } 105} 106 107static void acpi_hide_nondev_subnodes(struct acpi_device_data *data) 108{ 109 struct list_head *list = &data->subnodes; 110 struct acpi_data_node *dn; 111 112 if (list_empty(list)) 113 return; 114 115 list_for_each_entry_reverse(dn, list, sibling) { 116 acpi_hide_nondev_subnodes(&dn->data); 117 kobject_put(&dn->kobj); 118 } 119} 120 121/** 122 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent 123 * @acpi_dev: ACPI device object. 124 * @modalias: Buffer to print into. 125 * @size: Size of the buffer. 126 * 127 * Creates hid/cid(s) string needed for modalias and uevent 128 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: 129 * char *modalias: "acpi:IBM0001:ACPI0001" 130 * Return: 0: no _HID and no _CID 131 * -EINVAL: output error 132 * -ENOMEM: output is truncated 133*/ 134static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias, 135 int size) 136{ 137 int len; 138 int count; 139 struct acpi_hardware_id *id; 140 141 /* Avoid unnecessarily loading modules for non present devices. */ 142 if (!acpi_device_is_present(acpi_dev)) 143 return 0; 144 145 /* 146 * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should 147 * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the 148 * device's list. 149 */ 150 count = 0; 151 list_for_each_entry(id, &acpi_dev->pnp.ids, list) 152 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID)) 153 count++; 154 155 if (!count) 156 return 0; 157 158 len = snprintf(modalias, size, "acpi:"); 159 if (len >= size) 160 return -ENOMEM; 161 162 size -= len; 163 164 list_for_each_entry(id, &acpi_dev->pnp.ids, list) { 165 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID)) 166 continue; 167 168 count = snprintf(&modalias[len], size, "%s:", id->id); 169 if (count < 0) 170 return -EINVAL; 171 172 if (count >= size) 173 return -ENOMEM; 174 175 len += count; 176 size -= count; 177 } 178 modalias[len] = '\0'; 179 return len; 180} 181 182/** 183 * create_of_modalias - Creates DT compatible string for modalias and uevent 184 * @acpi_dev: ACPI device object. 185 * @modalias: Buffer to print into. 186 * @size: Size of the buffer. 187 * 188 * Expose DT compatible modalias as of:NnameTCcompatible. This function should 189 * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of 190 * ACPI/PNP IDs. 191 */ 192static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias, 193 int size) 194{ 195 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 196 const union acpi_object *of_compatible, *obj; 197 acpi_status status; 198 int len, count; 199 int i, nval; 200 char *c; 201 202 status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf); 203 if (ACPI_FAILURE(status)) 204 return -ENODEV; 205 206 /* DT strings are all in lower case */ 207 for (c = buf.pointer; *c != '\0'; c++) 208 *c = tolower(*c); 209 210 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer); 211 ACPI_FREE(buf.pointer); 212 213 if (len >= size) 214 return -ENOMEM; 215 216 size -= len; 217 218 of_compatible = acpi_dev->data.of_compatible; 219 if (of_compatible->type == ACPI_TYPE_PACKAGE) { 220 nval = of_compatible->package.count; 221 obj = of_compatible->package.elements; 222 } else { /* Must be ACPI_TYPE_STRING. */ 223 nval = 1; 224 obj = of_compatible; 225 } 226 for (i = 0; i < nval; i++, obj++) { 227 count = snprintf(&modalias[len], size, "C%s", 228 obj->string.pointer); 229 if (count < 0) 230 return -EINVAL; 231 232 if (count >= size) 233 return -ENOMEM; 234 235 len += count; 236 size -= count; 237 } 238 modalias[len] = '\0'; 239 return len; 240} 241 242int __acpi_device_uevent_modalias(struct acpi_device *adev, 243 struct kobj_uevent_env *env) 244{ 245 int len; 246 247 if (!adev) 248 return -ENODEV; 249 250 if (list_empty(&adev->pnp.ids)) 251 return 0; 252 253 if (add_uevent_var(env, "MODALIAS=")) 254 return -ENOMEM; 255 256 if (adev->data.of_compatible) 257 len = create_of_modalias(adev, &env->buf[env->buflen - 1], 258 sizeof(env->buf) - env->buflen); 259 else 260 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1], 261 sizeof(env->buf) - env->buflen); 262 if (len < 0) 263 return len; 264 265 env->buflen += len; 266 267 return 0; 268} 269 270/** 271 * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices. 272 * 273 * Create the uevent modalias field for ACPI-enumerated devices. 274 * 275 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with 276 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001". 277 */ 278int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) 279{ 280 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env); 281} 282EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias); 283 284static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size) 285{ 286 int len, count; 287 288 if (!adev) 289 return -ENODEV; 290 291 if (list_empty(&adev->pnp.ids)) 292 return 0; 293 294 len = create_pnp_modalias(adev, buf, size - 1); 295 if (len < 0) { 296 return len; 297 } else if (len > 0) { 298 buf[len++] = '\n'; 299 size -= len; 300 } 301 if (!adev->data.of_compatible) 302 return len; 303 304 count = create_of_modalias(adev, buf + len, size - 1); 305 if (count < 0) { 306 return count; 307 } else if (count > 0) { 308 len += count; 309 buf[len++] = '\n'; 310 } 311 312 return len; 313} 314 315/** 316 * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices. 317 * 318 * Create the modalias sysfs attribute for ACPI-enumerated devices. 319 * 320 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with 321 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001". 322 */ 323int acpi_device_modalias(struct device *dev, char *buf, int size) 324{ 325 return __acpi_device_modalias(acpi_companion_match(dev), buf, size); 326} 327EXPORT_SYMBOL_GPL(acpi_device_modalias); 328 329static ssize_t 330modalias_show(struct device *dev, struct device_attribute *attr, char *buf) 331{ 332 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024); 333} 334static DEVICE_ATTR_RO(modalias); 335 336static ssize_t real_power_state_show(struct device *dev, 337 struct device_attribute *attr, char *buf) 338{ 339 struct acpi_device *adev = to_acpi_device(dev); 340 int state; 341 int ret; 342 343 ret = acpi_device_get_power(adev, &state); 344 if (ret) 345 return ret; 346 347 return sprintf(buf, "%s\n", acpi_power_state_string(state)); 348} 349 350static DEVICE_ATTR_RO(real_power_state); 351 352static ssize_t power_state_show(struct device *dev, 353 struct device_attribute *attr, char *buf) 354{ 355 struct acpi_device *adev = to_acpi_device(dev); 356 357 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state)); 358} 359 360static DEVICE_ATTR_RO(power_state); 361 362static ssize_t 363eject_store(struct device *d, struct device_attribute *attr, 364 const char *buf, size_t count) 365{ 366 struct acpi_device *acpi_device = to_acpi_device(d); 367 acpi_object_type not_used; 368 acpi_status status; 369 370 if (!count || buf[0] != '1') 371 return -EINVAL; 372 373 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled) 374 && !acpi_device->driver) 375 return -ENODEV; 376 377 status = acpi_get_type(acpi_device->handle, ¬_used); 378 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable) 379 return -ENODEV; 380 381 get_device(&acpi_device->dev); 382 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT); 383 if (ACPI_SUCCESS(status)) 384 return count; 385 386 put_device(&acpi_device->dev); 387 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT, 388 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL); 389 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN; 390} 391 392static DEVICE_ATTR_WO(eject); 393 394static ssize_t 395hid_show(struct device *dev, struct device_attribute *attr, char *buf) 396{ 397 struct acpi_device *acpi_dev = to_acpi_device(dev); 398 399 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev)); 400} 401static DEVICE_ATTR_RO(hid); 402 403static ssize_t uid_show(struct device *dev, 404 struct device_attribute *attr, char *buf) 405{ 406 struct acpi_device *acpi_dev = to_acpi_device(dev); 407 408 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id); 409} 410static DEVICE_ATTR_RO(uid); 411 412static ssize_t adr_show(struct device *dev, 413 struct device_attribute *attr, char *buf) 414{ 415 struct acpi_device *acpi_dev = to_acpi_device(dev); 416 417 if (acpi_dev->pnp.bus_address > U32_MAX) 418 return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address); 419 else 420 return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address); 421} 422static DEVICE_ATTR_RO(adr); 423 424static ssize_t path_show(struct device *dev, 425 struct device_attribute *attr, char *buf) 426{ 427 struct acpi_device *acpi_dev = to_acpi_device(dev); 428 429 return acpi_object_path(acpi_dev->handle, buf); 430} 431static DEVICE_ATTR_RO(path); 432 433/* sysfs file that shows description text from the ACPI _STR method */ 434static ssize_t description_show(struct device *dev, 435 struct device_attribute *attr, 436 char *buf) { 437 struct acpi_device *acpi_dev = to_acpi_device(dev); 438 int result; 439 440 if (acpi_dev->pnp.str_obj == NULL) 441 return 0; 442 443 /* 444 * The _STR object contains a Unicode identifier for a device. 445 * We need to convert to utf-8 so it can be displayed. 446 */ 447 result = utf16s_to_utf8s( 448 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, 449 acpi_dev->pnp.str_obj->buffer.length, 450 UTF16_LITTLE_ENDIAN, buf, 451 PAGE_SIZE - 1); 452 453 buf[result++] = '\n'; 454 455 return result; 456} 457static DEVICE_ATTR_RO(description); 458 459static ssize_t 460sun_show(struct device *dev, struct device_attribute *attr, 461 char *buf) { 462 struct acpi_device *acpi_dev = to_acpi_device(dev); 463 acpi_status status; 464 unsigned long long sun; 465 466 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun); 467 if (ACPI_FAILURE(status)) 468 return -EIO; 469 470 return sprintf(buf, "%llu\n", sun); 471} 472static DEVICE_ATTR_RO(sun); 473 474static ssize_t 475hrv_show(struct device *dev, struct device_attribute *attr, 476 char *buf) { 477 struct acpi_device *acpi_dev = to_acpi_device(dev); 478 acpi_status status; 479 unsigned long long hrv; 480 481 status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv); 482 if (ACPI_FAILURE(status)) 483 return -EIO; 484 485 return sprintf(buf, "%llu\n", hrv); 486} 487static DEVICE_ATTR_RO(hrv); 488 489static ssize_t status_show(struct device *dev, struct device_attribute *attr, 490 char *buf) { 491 struct acpi_device *acpi_dev = to_acpi_device(dev); 492 acpi_status status; 493 unsigned long long sta; 494 495 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta); 496 if (ACPI_FAILURE(status)) 497 return -EIO; 498 499 return sprintf(buf, "%llu\n", sta); 500} 501static DEVICE_ATTR_RO(status); 502 503/** 504 * acpi_device_setup_files - Create sysfs attributes of an ACPI device. 505 * @dev: ACPI device object. 506 */ 507int acpi_device_setup_files(struct acpi_device *dev) 508{ 509 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 510 acpi_status status; 511 int result = 0; 512 513 /* 514 * Devices gotten from FADT don't have a "path" attribute 515 */ 516 if (dev->handle) { 517 result = device_create_file(&dev->dev, &dev_attr_path); 518 if (result) 519 goto end; 520 } 521 522 if (!list_empty(&dev->pnp.ids)) { 523 result = device_create_file(&dev->dev, &dev_attr_hid); 524 if (result) 525 goto end; 526 527 result = device_create_file(&dev->dev, &dev_attr_modalias); 528 if (result) 529 goto end; 530 } 531 532 /* 533 * If device has _STR, 'description' file is created 534 */ 535 if (acpi_has_method(dev->handle, "_STR")) { 536 status = acpi_evaluate_object(dev->handle, "_STR", 537 NULL, &buffer); 538 if (ACPI_FAILURE(status)) 539 buffer.pointer = NULL; 540 dev->pnp.str_obj = buffer.pointer; 541 result = device_create_file(&dev->dev, &dev_attr_description); 542 if (result) 543 goto end; 544 } 545 546 if (dev->pnp.type.bus_address) 547 result = device_create_file(&dev->dev, &dev_attr_adr); 548 if (dev->pnp.unique_id) 549 result = device_create_file(&dev->dev, &dev_attr_uid); 550 551 if (acpi_has_method(dev->handle, "_SUN")) { 552 result = device_create_file(&dev->dev, &dev_attr_sun); 553 if (result) 554 goto end; 555 } 556 557 if (acpi_has_method(dev->handle, "_HRV")) { 558 result = device_create_file(&dev->dev, &dev_attr_hrv); 559 if (result) 560 goto end; 561 } 562 563 if (acpi_has_method(dev->handle, "_STA")) { 564 result = device_create_file(&dev->dev, &dev_attr_status); 565 if (result) 566 goto end; 567 } 568 569 /* 570 * If device has _EJ0, 'eject' file is created that is used to trigger 571 * hot-removal function from userland. 572 */ 573 if (acpi_has_method(dev->handle, "_EJ0")) { 574 result = device_create_file(&dev->dev, &dev_attr_eject); 575 if (result) 576 return result; 577 } 578 579 if (dev->flags.power_manageable) { 580 result = device_create_file(&dev->dev, &dev_attr_power_state); 581 if (result) 582 return result; 583 584 if (dev->power.flags.power_resources) 585 result = device_create_file(&dev->dev, 586 &dev_attr_real_power_state); 587 } 588 589 acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data); 590 591end: 592 return result; 593} 594 595/** 596 * acpi_device_remove_files - Remove sysfs attributes of an ACPI device. 597 * @dev: ACPI device object. 598 */ 599void acpi_device_remove_files(struct acpi_device *dev) 600{ 601 acpi_hide_nondev_subnodes(&dev->data); 602 603 if (dev->flags.power_manageable) { 604 device_remove_file(&dev->dev, &dev_attr_power_state); 605 if (dev->power.flags.power_resources) 606 device_remove_file(&dev->dev, 607 &dev_attr_real_power_state); 608 } 609 610 /* 611 * If device has _STR, remove 'description' file 612 */ 613 if (acpi_has_method(dev->handle, "_STR")) { 614 kfree(dev->pnp.str_obj); 615 device_remove_file(&dev->dev, &dev_attr_description); 616 } 617 /* 618 * If device has _EJ0, remove 'eject' file. 619 */ 620 if (acpi_has_method(dev->handle, "_EJ0")) 621 device_remove_file(&dev->dev, &dev_attr_eject); 622 623 if (acpi_has_method(dev->handle, "_SUN")) 624 device_remove_file(&dev->dev, &dev_attr_sun); 625 626 if (acpi_has_method(dev->handle, "_HRV")) 627 device_remove_file(&dev->dev, &dev_attr_hrv); 628 629 if (dev->pnp.unique_id) 630 device_remove_file(&dev->dev, &dev_attr_uid); 631 if (dev->pnp.type.bus_address) 632 device_remove_file(&dev->dev, &dev_attr_adr); 633 device_remove_file(&dev->dev, &dev_attr_modalias); 634 device_remove_file(&dev->dev, &dev_attr_hid); 635 if (acpi_has_method(dev->handle, "_STA")) 636 device_remove_file(&dev->dev, &dev_attr_status); 637 if (dev->handle) 638 device_remove_file(&dev->dev, &dev_attr_path); 639} 640