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.
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[] = {
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)
60 static ssize_t acpi_data_node_attr_show(struct kobject *kobj,
61 struct attribute *attr, char *buf)
63 struct acpi_data_node *dn = to_data_node(kobj);
64 struct acpi_data_node_attr *dn_attr = to_attr(attr);
66 return dn_attr->show ? dn_attr->show(dn, buf) : -ENXIO;
69 static const struct sysfs_ops acpi_data_node_sysfs_ops = {
70 .show = acpi_data_node_attr_show,
73 static void acpi_data_node_release(struct kobject *kobj)
75 struct acpi_data_node *dn = to_data_node(kobj);
77 complete(&dn->kobj_done);
80 static struct kobj_type acpi_data_node_ktype = {
81 .sysfs_ops = &acpi_data_node_sysfs_ops,
82 .default_attrs = acpi_data_node_default_attrs,
83 .release = acpi_data_node_release,
86 static void acpi_expose_nondev_subnodes(struct kobject *kobj,
87 struct acpi_device_data *data)
89 struct list_head *list = &data->subnodes;
90 struct acpi_data_node *dn;
95 list_for_each_entry(dn, list, sibling) {
98 init_completion(&dn->kobj_done);
99 ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype,
100 kobj, "%s", dn->name);
102 acpi_expose_nondev_subnodes(&dn->kobj, &dn->data);
104 acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret);
108 static void acpi_hide_nondev_subnodes(struct acpi_device_data *data)
110 struct list_head *list = &data->subnodes;
111 struct acpi_data_node *dn;
113 if (list_empty(list))
116 list_for_each_entry_reverse(dn, list, sibling) {
117 acpi_hide_nondev_subnodes(&dn->data);
118 kobject_put(&dn->kobj);
123 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
124 * @acpi_dev: ACPI device object.
125 * @modalias: Buffer to print into.
126 * @size: Size of the buffer.
128 * Creates hid/cid(s) string needed for modalias and uevent
129 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
130 * char *modalias: "acpi:IBM0001:ACPI0001"
131 * Return: 0: no _HID and no _CID
132 * -EINVAL: output error
133 * -ENOMEM: output is truncated
135 static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias,
140 struct acpi_hardware_id *id;
142 /* Avoid unnecessarily loading modules for non present devices. */
143 if (!acpi_device_is_present(acpi_dev))
147 * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
148 * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
152 list_for_each_entry(id, &acpi_dev->pnp.ids, list)
153 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
159 len = snprintf(modalias, size, "acpi:");
165 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
166 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
169 count = snprintf(&modalias[len], size, "%s:", id->id);
179 modalias[len] = '\0';
184 * create_of_modalias - Creates DT compatible string for modalias and uevent
185 * @acpi_dev: ACPI device object.
186 * @modalias: Buffer to print into.
187 * @size: Size of the buffer.
189 * Expose DT compatible modalias as of:NnameTCcompatible. This function should
190 * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
193 static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
196 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
197 const union acpi_object *of_compatible, *obj;
203 status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
204 if (ACPI_FAILURE(status))
207 /* DT strings are all in lower case */
208 for (c = buf.pointer; *c != '\0'; c++)
211 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
212 ACPI_FREE(buf.pointer);
217 of_compatible = acpi_dev->data.of_compatible;
218 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
219 nval = of_compatible->package.count;
220 obj = of_compatible->package.elements;
221 } else { /* Must be ACPI_TYPE_STRING. */
225 for (i = 0; i < nval; i++, obj++) {
226 count = snprintf(&modalias[len], size, "C%s",
227 obj->string.pointer);
237 modalias[len] = '\0';
241 int __acpi_device_uevent_modalias(struct acpi_device *adev,
242 struct kobj_uevent_env *env)
249 if (list_empty(&adev->pnp.ids))
252 if (add_uevent_var(env, "MODALIAS="))
255 if (adev->data.of_compatible)
256 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
257 sizeof(env->buf) - env->buflen);
259 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
260 sizeof(env->buf) - env->buflen);
270 * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
271 * @dev: Struct device to get ACPI device node.
272 * @env: Environment variables of the kobject uevent.
274 * Create the uevent modalias field for ACPI-enumerated devices.
276 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
277 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
279 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
281 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
283 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
285 static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
292 if (list_empty(&adev->pnp.ids))
295 len = create_pnp_modalias(adev, buf, size - 1);
298 } else if (len > 0) {
302 if (!adev->data.of_compatible)
305 count = create_of_modalias(adev, buf + len, size - 1);
308 } else if (count > 0) {
317 * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
318 * @dev: Struct device to get ACPI device node.
319 * @buf: The buffer to save pnp_modalias and of_modalias.
320 * @size: Size of buffer.
322 * Create the modalias sysfs attribute for ACPI-enumerated devices.
324 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
325 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
327 int acpi_device_modalias(struct device *dev, char *buf, int size)
329 return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
331 EXPORT_SYMBOL_GPL(acpi_device_modalias);
334 modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
336 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
338 static DEVICE_ATTR_RO(modalias);
340 static ssize_t real_power_state_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
343 struct acpi_device *adev = to_acpi_device(dev);
347 ret = acpi_device_get_power(adev, &state);
351 return sprintf(buf, "%s\n", acpi_power_state_string(state));
354 static DEVICE_ATTR_RO(real_power_state);
356 static ssize_t power_state_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
359 struct acpi_device *adev = to_acpi_device(dev);
361 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
364 static DEVICE_ATTR_RO(power_state);
367 eject_store(struct device *d, struct device_attribute *attr,
368 const char *buf, size_t count)
370 struct acpi_device *acpi_device = to_acpi_device(d);
371 acpi_object_type not_used;
374 if (!count || buf[0] != '1')
377 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
378 && !acpi_device->driver)
381 status = acpi_get_type(acpi_device->handle, ¬_used);
382 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
385 acpi_dev_get(acpi_device);
386 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
387 if (ACPI_SUCCESS(status))
390 acpi_dev_put(acpi_device);
391 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
392 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
393 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
396 static DEVICE_ATTR_WO(eject);
399 hid_show(struct device *dev, struct device_attribute *attr, char *buf)
401 struct acpi_device *acpi_dev = to_acpi_device(dev);
403 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
405 static DEVICE_ATTR_RO(hid);
407 static ssize_t uid_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 struct acpi_device *acpi_dev = to_acpi_device(dev);
412 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
414 static DEVICE_ATTR_RO(uid);
416 static ssize_t adr_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct acpi_device *acpi_dev = to_acpi_device(dev);
421 if (acpi_dev->pnp.bus_address > U32_MAX)
422 return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
424 return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
426 static DEVICE_ATTR_RO(adr);
428 static ssize_t path_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct acpi_device *acpi_dev = to_acpi_device(dev);
433 return acpi_object_path(acpi_dev->handle, buf);
435 static DEVICE_ATTR_RO(path);
437 /* sysfs file that shows description text from the ACPI _STR method */
438 static ssize_t description_show(struct device *dev,
439 struct device_attribute *attr,
442 struct acpi_device *acpi_dev = to_acpi_device(dev);
445 if (acpi_dev->pnp.str_obj == NULL)
449 * The _STR object contains a Unicode identifier for a device.
450 * We need to convert to utf-8 so it can be displayed.
452 result = utf16s_to_utf8s(
453 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
454 acpi_dev->pnp.str_obj->buffer.length,
455 UTF16_LITTLE_ENDIAN, buf,
458 buf[result++] = '\n';
462 static DEVICE_ATTR_RO(description);
465 sun_show(struct device *dev, struct device_attribute *attr,
468 struct acpi_device *acpi_dev = to_acpi_device(dev);
470 unsigned long long sun;
472 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
473 if (ACPI_FAILURE(status))
476 return sprintf(buf, "%llu\n", sun);
478 static DEVICE_ATTR_RO(sun);
481 hrv_show(struct device *dev, struct device_attribute *attr,
484 struct acpi_device *acpi_dev = to_acpi_device(dev);
486 unsigned long long hrv;
488 status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
489 if (ACPI_FAILURE(status))
492 return sprintf(buf, "%llu\n", hrv);
494 static DEVICE_ATTR_RO(hrv);
496 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
499 struct acpi_device *acpi_dev = to_acpi_device(dev);
501 unsigned long long sta;
503 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
504 if (ACPI_FAILURE(status))
507 return sprintf(buf, "%llu\n", sta);
509 static DEVICE_ATTR_RO(status);
512 * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
513 * @dev: ACPI device object.
515 int acpi_device_setup_files(struct acpi_device *dev)
517 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
522 * Devices gotten from FADT don't have a "path" attribute
525 result = device_create_file(&dev->dev, &dev_attr_path);
530 if (!list_empty(&dev->pnp.ids)) {
531 result = device_create_file(&dev->dev, &dev_attr_hid);
535 result = device_create_file(&dev->dev, &dev_attr_modalias);
541 * If device has _STR, 'description' file is created
543 if (acpi_has_method(dev->handle, "_STR")) {
544 status = acpi_evaluate_object(dev->handle, "_STR",
546 if (ACPI_FAILURE(status))
547 buffer.pointer = NULL;
548 dev->pnp.str_obj = buffer.pointer;
549 result = device_create_file(&dev->dev, &dev_attr_description);
554 if (dev->pnp.type.bus_address)
555 result = device_create_file(&dev->dev, &dev_attr_adr);
556 if (dev->pnp.unique_id)
557 result = device_create_file(&dev->dev, &dev_attr_uid);
559 if (acpi_has_method(dev->handle, "_SUN")) {
560 result = device_create_file(&dev->dev, &dev_attr_sun);
565 if (acpi_has_method(dev->handle, "_HRV")) {
566 result = device_create_file(&dev->dev, &dev_attr_hrv);
571 if (acpi_has_method(dev->handle, "_STA")) {
572 result = device_create_file(&dev->dev, &dev_attr_status);
578 * If device has _EJ0, 'eject' file is created that is used to trigger
579 * hot-removal function from userland.
581 if (acpi_has_method(dev->handle, "_EJ0")) {
582 result = device_create_file(&dev->dev, &dev_attr_eject);
587 if (dev->flags.power_manageable) {
588 result = device_create_file(&dev->dev, &dev_attr_power_state);
592 if (dev->power.flags.power_resources)
593 result = device_create_file(&dev->dev,
594 &dev_attr_real_power_state);
597 acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
604 * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
605 * @dev: ACPI device object.
607 void acpi_device_remove_files(struct acpi_device *dev)
609 acpi_hide_nondev_subnodes(&dev->data);
611 if (dev->flags.power_manageable) {
612 device_remove_file(&dev->dev, &dev_attr_power_state);
613 if (dev->power.flags.power_resources)
614 device_remove_file(&dev->dev,
615 &dev_attr_real_power_state);
619 * If device has _STR, remove 'description' file
621 if (acpi_has_method(dev->handle, "_STR")) {
622 kfree(dev->pnp.str_obj);
623 device_remove_file(&dev->dev, &dev_attr_description);
626 * If device has _EJ0, remove 'eject' file.
628 if (acpi_has_method(dev->handle, "_EJ0"))
629 device_remove_file(&dev->dev, &dev_attr_eject);
631 if (acpi_has_method(dev->handle, "_SUN"))
632 device_remove_file(&dev->dev, &dev_attr_sun);
634 if (acpi_has_method(dev->handle, "_HRV"))
635 device_remove_file(&dev->dev, &dev_attr_hrv);
637 if (dev->pnp.unique_id)
638 device_remove_file(&dev->dev, &dev_attr_uid);
639 if (dev->pnp.type.bus_address)
640 device_remove_file(&dev->dev, &dev_attr_adr);
641 device_remove_file(&dev->dev, &dev_attr_modalias);
642 device_remove_file(&dev->dev, &dev_attr_hid);
643 if (acpi_has_method(dev->handle, "_STA"))
644 device_remove_file(&dev->dev, &dev_attr_status);
646 device_remove_file(&dev->dev, &dev_attr_path);