2 * drivers/acpi/device_sysfs.c - ACPI device sysfs attributes and modalias.
4 * Copyright (C) 2015, Intel Corp.
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/acpi.h>
23 #include <linux/device.h>
24 #include <linux/export.h>
25 #include <linux/nls.h>
29 static ssize_t
acpi_object_path(acpi_handle handle
, char *buf
)
31 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
34 result
= acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &path
);
38 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
43 struct acpi_data_node_attr
{
44 struct attribute attr
;
45 ssize_t (*show
)(struct acpi_data_node
*, char *);
46 ssize_t (*store
)(struct acpi_data_node
*, const char *, size_t count
);
49 #define DATA_NODE_ATTR(_name) \
50 static struct acpi_data_node_attr data_node_##_name = \
51 __ATTR(_name, 0444, data_node_show_##_name, NULL)
53 static ssize_t
data_node_show_path(struct acpi_data_node
*dn
, char *buf
)
55 return acpi_object_path(dn
->handle
, buf
);
60 static struct attribute
*acpi_data_node_default_attrs
[] = {
65 #define to_data_node(k) container_of(k, struct acpi_data_node, kobj)
66 #define to_attr(a) container_of(a, struct acpi_data_node_attr, attr)
68 static ssize_t
acpi_data_node_attr_show(struct kobject
*kobj
,
69 struct attribute
*attr
, char *buf
)
71 struct acpi_data_node
*dn
= to_data_node(kobj
);
72 struct acpi_data_node_attr
*dn_attr
= to_attr(attr
);
74 return dn_attr
->show
? dn_attr
->show(dn
, buf
) : -ENXIO
;
77 static const struct sysfs_ops acpi_data_node_sysfs_ops
= {
78 .show
= acpi_data_node_attr_show
,
81 static void acpi_data_node_release(struct kobject
*kobj
)
83 struct acpi_data_node
*dn
= to_data_node(kobj
);
84 complete(&dn
->kobj_done
);
87 static struct kobj_type acpi_data_node_ktype
= {
88 .sysfs_ops
= &acpi_data_node_sysfs_ops
,
89 .default_attrs
= acpi_data_node_default_attrs
,
90 .release
= acpi_data_node_release
,
93 static void acpi_expose_nondev_subnodes(struct kobject
*kobj
,
94 struct acpi_device_data
*data
)
96 struct list_head
*list
= &data
->subnodes
;
97 struct acpi_data_node
*dn
;
102 list_for_each_entry(dn
, list
, sibling
) {
105 init_completion(&dn
->kobj_done
);
106 ret
= kobject_init_and_add(&dn
->kobj
, &acpi_data_node_ktype
,
107 kobj
, "%s", dn
->name
);
109 acpi_handle_err(dn
->handle
, "Failed to expose (%d)\n", ret
);
111 acpi_expose_nondev_subnodes(&dn
->kobj
, &dn
->data
);
115 static void acpi_hide_nondev_subnodes(struct acpi_device_data
*data
)
117 struct list_head
*list
= &data
->subnodes
;
118 struct acpi_data_node
*dn
;
120 if (list_empty(list
))
123 list_for_each_entry_reverse(dn
, list
, sibling
) {
124 acpi_hide_nondev_subnodes(&dn
->data
);
125 kobject_put(&dn
->kobj
);
130 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
131 * @acpi_dev: ACPI device object.
132 * @modalias: Buffer to print into.
133 * @size: Size of the buffer.
135 * Creates hid/cid(s) string needed for modalias and uevent
136 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
137 * char *modalias: "acpi:IBM0001:ACPI0001"
138 * Return: 0: no _HID and no _CID
139 * -EINVAL: output error
140 * -ENOMEM: output is truncated
142 static int create_pnp_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
147 struct acpi_hardware_id
*id
;
149 /* Avoid unnecessarily loading modules for non present devices. */
150 if (!acpi_device_is_present(acpi_dev
))
154 * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
155 * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
159 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
)
160 if (strcmp(id
->id
, ACPI_DT_NAMESPACE_HID
))
166 len
= snprintf(modalias
, size
, "acpi:");
172 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
) {
173 if (!strcmp(id
->id
, ACPI_DT_NAMESPACE_HID
))
176 count
= snprintf(&modalias
[len
], size
, "%s:", id
->id
);
186 modalias
[len
] = '\0';
191 * create_of_modalias - Creates DT compatible string for modalias and uevent
192 * @acpi_dev: ACPI device object.
193 * @modalias: Buffer to print into.
194 * @size: Size of the buffer.
196 * Expose DT compatible modalias as of:NnameTCcompatible. This function should
197 * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
200 static int create_of_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
203 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
204 const union acpi_object
*of_compatible
, *obj
;
209 acpi_get_name(acpi_dev
->handle
, ACPI_SINGLE_NAME
, &buf
);
210 /* DT strings are all in lower case */
211 for (c
= buf
.pointer
; *c
!= '\0'; c
++)
214 len
= snprintf(modalias
, size
, "of:N%sT", (char *)buf
.pointer
);
215 ACPI_FREE(buf
.pointer
);
220 of_compatible
= acpi_dev
->data
.of_compatible
;
221 if (of_compatible
->type
== ACPI_TYPE_PACKAGE
) {
222 nval
= of_compatible
->package
.count
;
223 obj
= of_compatible
->package
.elements
;
224 } else { /* Must be ACPI_TYPE_STRING. */
228 for (i
= 0; i
< nval
; i
++, obj
++) {
229 count
= snprintf(&modalias
[len
], size
, "C%s",
230 obj
->string
.pointer
);
240 modalias
[len
] = '\0';
244 int __acpi_device_uevent_modalias(struct acpi_device
*adev
,
245 struct kobj_uevent_env
*env
)
252 if (list_empty(&adev
->pnp
.ids
))
255 if (add_uevent_var(env
, "MODALIAS="))
258 len
= create_pnp_modalias(adev
, &env
->buf
[env
->buflen
- 1],
259 sizeof(env
->buf
) - env
->buflen
);
264 if (!adev
->data
.of_compatible
)
267 if (len
> 0 && add_uevent_var(env
, "MODALIAS="))
270 len
= create_of_modalias(adev
, &env
->buf
[env
->buflen
- 1],
271 sizeof(env
->buf
) - env
->buflen
);
281 * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
283 * Create the uevent modalias field for ACPI-enumerated devices.
285 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
286 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
288 int acpi_device_uevent_modalias(struct device
*dev
, struct kobj_uevent_env
*env
)
290 return __acpi_device_uevent_modalias(acpi_companion_match(dev
), env
);
292 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias
);
294 static int __acpi_device_modalias(struct acpi_device
*adev
, char *buf
, int size
)
301 if (list_empty(&adev
->pnp
.ids
))
304 len
= create_pnp_modalias(adev
, buf
, size
- 1);
307 } else if (len
> 0) {
311 if (!adev
->data
.of_compatible
)
314 count
= create_of_modalias(adev
, buf
+ len
, size
- 1);
317 } else if (count
> 0) {
326 * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
328 * Create the modalias sysfs attribute for ACPI-enumerated devices.
330 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
331 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
333 int acpi_device_modalias(struct device
*dev
, char *buf
, int size
)
335 return __acpi_device_modalias(acpi_companion_match(dev
), buf
, size
);
337 EXPORT_SYMBOL_GPL(acpi_device_modalias
);
340 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
341 return __acpi_device_modalias(to_acpi_device(dev
), buf
, 1024);
343 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
345 static ssize_t
real_power_state_show(struct device
*dev
,
346 struct device_attribute
*attr
, char *buf
)
348 struct acpi_device
*adev
= to_acpi_device(dev
);
352 ret
= acpi_device_get_power(adev
, &state
);
356 return sprintf(buf
, "%s\n", acpi_power_state_string(state
));
359 static DEVICE_ATTR(real_power_state
, 0444, real_power_state_show
, NULL
);
361 static ssize_t
power_state_show(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 struct acpi_device
*adev
= to_acpi_device(dev
);
366 return sprintf(buf
, "%s\n", acpi_power_state_string(adev
->power
.state
));
369 static DEVICE_ATTR(power_state
, 0444, power_state_show
, NULL
);
372 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
373 const char *buf
, size_t count
)
375 struct acpi_device
*acpi_device
= to_acpi_device(d
);
376 acpi_object_type not_used
;
379 if (!count
|| buf
[0] != '1')
382 if ((!acpi_device
->handler
|| !acpi_device
->handler
->hotplug
.enabled
)
383 && !acpi_device
->driver
)
386 status
= acpi_get_type(acpi_device
->handle
, ¬_used
);
387 if (ACPI_FAILURE(status
) || !acpi_device
->flags
.ejectable
)
390 get_device(&acpi_device
->dev
);
391 status
= acpi_hotplug_schedule(acpi_device
, ACPI_OST_EC_OSPM_EJECT
);
392 if (ACPI_SUCCESS(status
))
395 put_device(&acpi_device
->dev
);
396 acpi_evaluate_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
397 ACPI_OST_SC_NON_SPECIFIC_FAILURE
, NULL
);
398 return status
== AE_NO_MEMORY
? -ENOMEM
: -EAGAIN
;
401 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
404 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
405 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
407 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
409 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
411 static ssize_t
acpi_device_uid_show(struct device
*dev
,
412 struct device_attribute
*attr
, char *buf
)
414 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
416 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.unique_id
);
418 static DEVICE_ATTR(uid
, 0444, acpi_device_uid_show
, NULL
);
420 static ssize_t
acpi_device_adr_show(struct device
*dev
,
421 struct device_attribute
*attr
, char *buf
)
423 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
425 return sprintf(buf
, "0x%08x\n",
426 (unsigned int)(acpi_dev
->pnp
.bus_address
));
428 static DEVICE_ATTR(adr
, 0444, acpi_device_adr_show
, NULL
);
430 static ssize_t
acpi_device_path_show(struct device
*dev
,
431 struct device_attribute
*attr
, char *buf
)
433 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
435 return acpi_object_path(acpi_dev
->handle
, buf
);
437 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
439 /* sysfs file that shows description text from the ACPI _STR method */
440 static ssize_t
description_show(struct device
*dev
,
441 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(description
, 0444, description_show
, NULL
);
466 acpi_device_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(sun
, 0444, acpi_device_sun_show
, NULL
);
480 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
482 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
484 unsigned long long sta
;
486 status
= acpi_evaluate_integer(acpi_dev
->handle
, "_STA", NULL
, &sta
);
487 if (ACPI_FAILURE(status
))
490 return sprintf(buf
, "%llu\n", sta
);
492 static DEVICE_ATTR_RO(status
);
495 * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
496 * @dev: ACPI device object.
498 int acpi_device_setup_files(struct acpi_device
*dev
)
500 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
505 * Devices gotten from FADT don't have a "path" attribute
508 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
513 if (!list_empty(&dev
->pnp
.ids
)) {
514 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
518 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
524 * If device has _STR, 'description' file is created
526 if (acpi_has_method(dev
->handle
, "_STR")) {
527 status
= acpi_evaluate_object(dev
->handle
, "_STR",
529 if (ACPI_FAILURE(status
))
530 buffer
.pointer
= NULL
;
531 dev
->pnp
.str_obj
= buffer
.pointer
;
532 result
= device_create_file(&dev
->dev
, &dev_attr_description
);
537 if (dev
->pnp
.type
.bus_address
)
538 result
= device_create_file(&dev
->dev
, &dev_attr_adr
);
539 if (dev
->pnp
.unique_id
)
540 result
= device_create_file(&dev
->dev
, &dev_attr_uid
);
542 if (acpi_has_method(dev
->handle
, "_SUN")) {
543 result
= device_create_file(&dev
->dev
, &dev_attr_sun
);
548 if (acpi_has_method(dev
->handle
, "_STA")) {
549 result
= device_create_file(&dev
->dev
, &dev_attr_status
);
555 * If device has _EJ0, 'eject' file is created that is used to trigger
556 * hot-removal function from userland.
558 if (acpi_has_method(dev
->handle
, "_EJ0")) {
559 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
564 if (dev
->flags
.power_manageable
) {
565 result
= device_create_file(&dev
->dev
, &dev_attr_power_state
);
569 if (dev
->power
.flags
.power_resources
)
570 result
= device_create_file(&dev
->dev
,
571 &dev_attr_real_power_state
);
574 acpi_expose_nondev_subnodes(&dev
->dev
.kobj
, &dev
->data
);
581 * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
582 * @dev: ACPI device object.
584 void acpi_device_remove_files(struct acpi_device
*dev
)
586 acpi_hide_nondev_subnodes(&dev
->data
);
588 if (dev
->flags
.power_manageable
) {
589 device_remove_file(&dev
->dev
, &dev_attr_power_state
);
590 if (dev
->power
.flags
.power_resources
)
591 device_remove_file(&dev
->dev
,
592 &dev_attr_real_power_state
);
596 * If device has _STR, remove 'description' file
598 if (acpi_has_method(dev
->handle
, "_STR")) {
599 kfree(dev
->pnp
.str_obj
);
600 device_remove_file(&dev
->dev
, &dev_attr_description
);
603 * If device has _EJ0, remove 'eject' file.
605 if (acpi_has_method(dev
->handle
, "_EJ0"))
606 device_remove_file(&dev
->dev
, &dev_attr_eject
);
608 if (acpi_has_method(dev
->handle
, "_SUN"))
609 device_remove_file(&dev
->dev
, &dev_attr_sun
);
611 if (dev
->pnp
.unique_id
)
612 device_remove_file(&dev
->dev
, &dev_attr_uid
);
613 if (dev
->pnp
.type
.bus_address
)
614 device_remove_file(&dev
->dev
, &dev_attr_adr
);
615 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
616 device_remove_file(&dev
->dev
, &dev_attr_hid
);
617 if (acpi_has_method(dev
->handle
, "_STA"))
618 device_remove_file(&dev
->dev
, &dev_attr_status
);
620 device_remove_file(&dev
->dev
, &dev_attr_path
);