1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/acpi/device_sysfs.c - ACPI device sysfs attributes and modalias.
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>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 #include <linux/acpi.h>
15 #include <linux/device.h>
16 #include <linux/export.h>
17 #include <linux/nls.h>
21 static ssize_t acpi_object_path(acpi_handle handle, char *buf)
23 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
26 result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path);
30 result = sprintf(buf, "%s\n", (char *)path.pointer);
35 struct 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);
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)
45 static ssize_t data_node_show_path(struct acpi_data_node *dn, char *buf)
47 return dn->handle ? acpi_object_path(dn->handle, buf) : 0;
52 static struct attribute *acpi_data_node_default_attrs[] = {
56 ATTRIBUTE_GROUPS(acpi_data_node_default);
58 #define to_data_node(k) container_of(k, struct acpi_data_node, kobj)
59 #define to_attr(a) container_of(a, struct acpi_data_node_attr, attr)
61 static ssize_t acpi_data_node_attr_show(struct kobject *kobj,
62 struct attribute *attr, char *buf)
64 struct acpi_data_node *dn = to_data_node(kobj);
65 struct acpi_data_node_attr *dn_attr = to_attr(attr);
67 return dn_attr->show ? dn_attr->show(dn, buf) : -ENXIO;
70 static const struct sysfs_ops acpi_data_node_sysfs_ops = {
71 .show = acpi_data_node_attr_show,
74 static void acpi_data_node_release(struct kobject *kobj)
76 struct acpi_data_node *dn = to_data_node(kobj);
78 complete(&dn->kobj_done);
81 static struct kobj_type acpi_data_node_ktype = {
82 .sysfs_ops = &acpi_data_node_sysfs_ops,
83 .default_groups = acpi_data_node_default_groups,
84 .release = acpi_data_node_release,
87 static void acpi_expose_nondev_subnodes(struct kobject *kobj,
88 struct acpi_device_data *data)
90 struct list_head *list = &data->subnodes;
91 struct acpi_data_node *dn;
96 list_for_each_entry(dn, list, sibling) {
99 init_completion(&dn->kobj_done);
100 ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype,
101 kobj, "%s", dn->name);
103 acpi_expose_nondev_subnodes(&dn->kobj, &dn->data);
105 acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret);
109 static void acpi_hide_nondev_subnodes(struct acpi_device_data *data)
111 struct list_head *list = &data->subnodes;
112 struct acpi_data_node *dn;
114 if (list_empty(list))
117 list_for_each_entry_reverse(dn, list, sibling) {
118 acpi_hide_nondev_subnodes(&dn->data);
119 kobject_put(&dn->kobj);
124 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
125 * @acpi_dev: ACPI device object.
126 * @modalias: Buffer to print into.
127 * @size: Size of the buffer.
129 * Creates hid/cid(s) string needed for modalias and uevent
130 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
131 * char *modalias: "acpi:IBM0001:ACPI0001"
132 * Return: 0: no _HID and no _CID
133 * -EINVAL: output error
134 * -ENOMEM: output is truncated
136 static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias,
141 struct acpi_hardware_id *id;
143 /* Avoid unnecessarily loading modules for non present devices. */
144 if (!acpi_device_is_present(acpi_dev))
148 * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
149 * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
153 list_for_each_entry(id, &acpi_dev->pnp.ids, list)
154 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
160 len = snprintf(modalias, size, "acpi:");
166 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
167 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
170 count = snprintf(&modalias[len], size, "%s:", id->id);
180 modalias[len] = '\0';
185 * create_of_modalias - Creates DT compatible string for modalias and uevent
186 * @acpi_dev: ACPI device object.
187 * @modalias: Buffer to print into.
188 * @size: Size of the buffer.
190 * Expose DT compatible modalias as of:NnameTCcompatible. This function should
191 * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
194 static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
197 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
198 const union acpi_object *of_compatible, *obj;
204 status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
205 if (ACPI_FAILURE(status))
208 /* DT strings are all in lower case */
209 for (c = buf.pointer; *c != '\0'; c++)
212 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
213 ACPI_FREE(buf.pointer);
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. */
226 for (i = 0; i < nval; i++, obj++) {
227 count = snprintf(&modalias[len], size, "C%s",
228 obj->string.pointer);
238 modalias[len] = '\0';
242 int __acpi_device_uevent_modalias(struct acpi_device *adev,
243 struct kobj_uevent_env *env)
250 if (list_empty(&adev->pnp.ids))
253 if (add_uevent_var(env, "MODALIAS="))
256 if (adev->data.of_compatible)
257 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
258 sizeof(env->buf) - env->buflen);
260 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
261 sizeof(env->buf) - env->buflen);
271 * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
272 * @dev: Struct device to get ACPI device node.
273 * @env: Environment variables of the kobject uevent.
275 * Create the uevent modalias field for ACPI-enumerated devices.
277 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
278 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
280 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
282 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
284 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
286 static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
293 if (list_empty(&adev->pnp.ids))
296 len = create_pnp_modalias(adev, buf, size - 1);
299 } else if (len > 0) {
303 if (!adev->data.of_compatible)
306 count = create_of_modalias(adev, buf + len, size - 1);
309 } else if (count > 0) {
318 * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
319 * @dev: Struct device to get ACPI device node.
320 * @buf: The buffer to save pnp_modalias and of_modalias.
321 * @size: Size of buffer.
323 * Create the modalias sysfs attribute for ACPI-enumerated devices.
325 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
326 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
328 int acpi_device_modalias(struct device *dev, char *buf, int size)
330 return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
332 EXPORT_SYMBOL_GPL(acpi_device_modalias);
335 modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
337 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
339 static DEVICE_ATTR_RO(modalias);
341 static ssize_t real_power_state_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
344 struct acpi_device *adev = to_acpi_device(dev);
348 ret = acpi_device_get_power(adev, &state);
352 return sprintf(buf, "%s\n", acpi_power_state_string(state));
355 static DEVICE_ATTR_RO(real_power_state);
357 static ssize_t power_state_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 struct acpi_device *adev = to_acpi_device(dev);
362 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
365 static DEVICE_ATTR_RO(power_state);
368 eject_store(struct device *d, struct device_attribute *attr,
369 const char *buf, size_t count)
371 struct acpi_device *acpi_device = to_acpi_device(d);
372 acpi_object_type not_used;
375 if (!count || buf[0] != '1')
378 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
382 status = acpi_get_type(acpi_device->handle, ¬_used);
383 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
386 acpi_dev_get(acpi_device);
387 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
388 if (ACPI_SUCCESS(status))
391 acpi_dev_put(acpi_device);
392 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
393 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
394 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
397 static DEVICE_ATTR_WO(eject);
400 hid_show(struct device *dev, struct device_attribute *attr, char *buf)
402 struct acpi_device *acpi_dev = to_acpi_device(dev);
404 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
406 static DEVICE_ATTR_RO(hid);
408 static ssize_t uid_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
411 struct acpi_device *acpi_dev = to_acpi_device(dev);
413 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
415 static DEVICE_ATTR_RO(uid);
417 static ssize_t adr_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
420 struct acpi_device *acpi_dev = to_acpi_device(dev);
422 if (acpi_dev->pnp.bus_address > U32_MAX)
423 return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
425 return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
427 static DEVICE_ATTR_RO(adr);
429 static ssize_t path_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct acpi_device *acpi_dev = to_acpi_device(dev);
434 return acpi_object_path(acpi_dev->handle, buf);
436 static DEVICE_ATTR_RO(path);
438 /* sysfs file that shows description text from the ACPI _STR method */
439 static ssize_t description_show(struct device *dev,
440 struct device_attribute *attr,
443 struct acpi_device *acpi_dev = to_acpi_device(dev);
446 if (acpi_dev->pnp.str_obj == NULL)
450 * The _STR object contains a Unicode identifier for a device.
451 * We need to convert to utf-8 so it can be displayed.
453 result = utf16s_to_utf8s(
454 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
455 acpi_dev->pnp.str_obj->buffer.length,
456 UTF16_LITTLE_ENDIAN, buf,
459 buf[result++] = '\n';
463 static DEVICE_ATTR_RO(description);
466 sun_show(struct device *dev, struct device_attribute *attr,
469 struct acpi_device *acpi_dev = to_acpi_device(dev);
471 unsigned long long sun;
473 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
474 if (ACPI_FAILURE(status))
477 return sprintf(buf, "%llu\n", sun);
479 static DEVICE_ATTR_RO(sun);
482 hrv_show(struct device *dev, struct device_attribute *attr,
485 struct acpi_device *acpi_dev = to_acpi_device(dev);
487 unsigned long long hrv;
489 status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
490 if (ACPI_FAILURE(status))
493 return sprintf(buf, "%llu\n", hrv);
495 static DEVICE_ATTR_RO(hrv);
497 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
500 struct acpi_device *acpi_dev = to_acpi_device(dev);
502 unsigned long long sta;
504 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
505 if (ACPI_FAILURE(status))
508 return sprintf(buf, "%llu\n", sta);
510 static DEVICE_ATTR_RO(status);
513 * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
514 * @dev: ACPI device object.
516 int acpi_device_setup_files(struct acpi_device *dev)
518 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
523 * Devices gotten from FADT don't have a "path" attribute
526 result = device_create_file(&dev->dev, &dev_attr_path);
531 if (!list_empty(&dev->pnp.ids)) {
532 result = device_create_file(&dev->dev, &dev_attr_hid);
536 result = device_create_file(&dev->dev, &dev_attr_modalias);
542 * If device has _STR, 'description' file is created
544 if (acpi_has_method(dev->handle, "_STR")) {
545 status = acpi_evaluate_object(dev->handle, "_STR",
547 if (ACPI_FAILURE(status))
548 buffer.pointer = NULL;
549 dev->pnp.str_obj = buffer.pointer;
550 result = device_create_file(&dev->dev, &dev_attr_description);
555 if (dev->pnp.type.bus_address)
556 result = device_create_file(&dev->dev, &dev_attr_adr);
557 if (dev->pnp.unique_id)
558 result = device_create_file(&dev->dev, &dev_attr_uid);
560 if (acpi_has_method(dev->handle, "_SUN")) {
561 result = device_create_file(&dev->dev, &dev_attr_sun);
566 if (acpi_has_method(dev->handle, "_HRV")) {
567 result = device_create_file(&dev->dev, &dev_attr_hrv);
572 if (acpi_has_method(dev->handle, "_STA")) {
573 result = device_create_file(&dev->dev, &dev_attr_status);
579 * If device has _EJ0, 'eject' file is created that is used to trigger
580 * hot-removal function from userland.
582 if (acpi_has_method(dev->handle, "_EJ0")) {
583 result = device_create_file(&dev->dev, &dev_attr_eject);
588 if (dev->flags.power_manageable) {
589 result = device_create_file(&dev->dev, &dev_attr_power_state);
593 if (dev->power.flags.power_resources)
594 result = device_create_file(&dev->dev,
595 &dev_attr_real_power_state);
598 acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
605 * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
606 * @dev: ACPI device object.
608 void acpi_device_remove_files(struct acpi_device *dev)
610 acpi_hide_nondev_subnodes(&dev->data);
612 if (dev->flags.power_manageable) {
613 device_remove_file(&dev->dev, &dev_attr_power_state);
614 if (dev->power.flags.power_resources)
615 device_remove_file(&dev->dev,
616 &dev_attr_real_power_state);
620 * If device has _STR, remove 'description' file
622 if (acpi_has_method(dev->handle, "_STR")) {
623 kfree(dev->pnp.str_obj);
624 device_remove_file(&dev->dev, &dev_attr_description);
627 * If device has _EJ0, remove 'eject' file.
629 if (acpi_has_method(dev->handle, "_EJ0"))
630 device_remove_file(&dev->dev, &dev_attr_eject);
632 if (acpi_has_method(dev->handle, "_SUN"))
633 device_remove_file(&dev->dev, &dev_attr_sun);
635 if (acpi_has_method(dev->handle, "_HRV"))
636 device_remove_file(&dev->dev, &dev_attr_hrv);
638 if (dev->pnp.unique_id)
639 device_remove_file(&dev->dev, &dev_attr_uid);
640 if (dev->pnp.type.bus_address)
641 device_remove_file(&dev->dev, &dev_attr_adr);
642 device_remove_file(&dev->dev, &dev_attr_modalias);
643 device_remove_file(&dev->dev, &dev_attr_hid);
644 if (acpi_has_method(dev->handle, "_STA"))
645 device_remove_file(&dev->dev, &dev_attr_status);
647 device_remove_file(&dev->dev, &dev_attr_path);