drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / acpi / device_sysfs.c
blob78c2653bf0206922d28b9d7453c7e4220a5ef710
1 /*
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>
27 #include "internal.h"
29 static ssize_t acpi_object_path(acpi_handle handle, char *buf)
31 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
32 int result;
34 result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path);
35 if (result)
36 return result;
38 result = sprintf(buf, "%s\n", (char *)path.pointer);
39 kfree(path.pointer);
40 return result;
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 dn->handle ? acpi_object_path(dn->handle, buf) : 0;
58 DATA_NODE_ATTR(path);
60 static struct attribute *acpi_data_node_default_attrs[] = {
61 &data_node_path.attr,
62 NULL
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;
99 if (list_empty(list))
100 return;
102 list_for_each_entry(dn, list, sibling) {
103 int ret;
105 init_completion(&dn->kobj_done);
106 ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype,
107 kobj, "%s", dn->name);
108 if (!ret)
109 acpi_expose_nondev_subnodes(&dn->kobj, &dn->data);
110 else if (dn->handle)
111 acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret);
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))
121 return;
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,
143 int size)
145 int len;
146 int count;
147 struct acpi_hardware_id *id;
149 /* Avoid unnecessarily loading modules for non present devices. */
150 if (!acpi_device_is_present(acpi_dev))
151 return 0;
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
156 * device's list.
158 count = 0;
159 list_for_each_entry(id, &acpi_dev->pnp.ids, list)
160 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
161 count++;
163 if (!count)
164 return 0;
166 len = snprintf(modalias, size, "acpi:");
167 if (len <= 0)
168 return len;
170 size -= len;
172 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
173 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
174 continue;
176 count = snprintf(&modalias[len], size, "%s:", id->id);
177 if (count < 0)
178 return -EINVAL;
180 if (count >= size)
181 return -ENOMEM;
183 len += count;
184 size -= count;
186 modalias[len] = '\0';
187 return len;
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
198 * ACPI/PNP IDs.
200 static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
201 int size)
203 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
204 const union acpi_object *of_compatible, *obj;
205 acpi_status status;
206 int len, count;
207 int i, nval;
208 char *c;
210 status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
211 if (ACPI_FAILURE(status))
212 return -ENODEV;
214 /* DT strings are all in lower case */
215 for (c = buf.pointer; *c != '\0'; c++)
216 *c = tolower(*c);
218 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
219 ACPI_FREE(buf.pointer);
221 if (len <= 0)
222 return len;
224 of_compatible = acpi_dev->data.of_compatible;
225 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
226 nval = of_compatible->package.count;
227 obj = of_compatible->package.elements;
228 } else { /* Must be ACPI_TYPE_STRING. */
229 nval = 1;
230 obj = of_compatible;
232 for (i = 0; i < nval; i++, obj++) {
233 count = snprintf(&modalias[len], size, "C%s",
234 obj->string.pointer);
235 if (count < 0)
236 return -EINVAL;
238 if (count >= size)
239 return -ENOMEM;
241 len += count;
242 size -= count;
244 modalias[len] = '\0';
245 return len;
248 int __acpi_device_uevent_modalias(struct acpi_device *adev,
249 struct kobj_uevent_env *env)
251 int len;
253 if (!adev)
254 return -ENODEV;
256 if (list_empty(&adev->pnp.ids))
257 return 0;
259 if (add_uevent_var(env, "MODALIAS="))
260 return -ENOMEM;
262 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
263 sizeof(env->buf) - env->buflen);
264 if (len < 0)
265 return len;
267 env->buflen += len;
268 if (!adev->data.of_compatible)
269 return 0;
271 if (len > 0 && add_uevent_var(env, "MODALIAS="))
272 return -ENOMEM;
274 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
275 sizeof(env->buf) - env->buflen);
276 if (len < 0)
277 return len;
279 env->buflen += len;
281 return 0;
285 * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
287 * Create the uevent modalias field for ACPI-enumerated devices.
289 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
290 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
292 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
294 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
296 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
298 static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
300 int len, count;
302 if (!adev)
303 return -ENODEV;
305 if (list_empty(&adev->pnp.ids))
306 return 0;
308 len = create_pnp_modalias(adev, buf, size - 1);
309 if (len < 0) {
310 return len;
311 } else if (len > 0) {
312 buf[len++] = '\n';
313 size -= len;
315 if (!adev->data.of_compatible)
316 return len;
318 count = create_of_modalias(adev, buf + len, size - 1);
319 if (count < 0) {
320 return count;
321 } else if (count > 0) {
322 len += count;
323 buf[len++] = '\n';
326 return len;
330 * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
332 * Create the modalias sysfs attribute for ACPI-enumerated devices.
334 * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
335 * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
337 int acpi_device_modalias(struct device *dev, char *buf, int size)
339 return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
341 EXPORT_SYMBOL_GPL(acpi_device_modalias);
343 static ssize_t
344 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
346 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
348 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
350 static ssize_t real_power_state_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
353 struct acpi_device *adev = to_acpi_device(dev);
354 int state;
355 int ret;
357 ret = acpi_device_get_power(adev, &state);
358 if (ret)
359 return ret;
361 return sprintf(buf, "%s\n", acpi_power_state_string(state));
364 static DEVICE_ATTR_RO(real_power_state);
366 static ssize_t power_state_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 struct acpi_device *adev = to_acpi_device(dev);
371 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
374 static DEVICE_ATTR_RO(power_state);
376 static ssize_t
377 acpi_eject_store(struct device *d, struct device_attribute *attr,
378 const char *buf, size_t count)
380 struct acpi_device *acpi_device = to_acpi_device(d);
381 acpi_object_type not_used;
382 acpi_status status;
384 if (!count || buf[0] != '1')
385 return -EINVAL;
387 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
388 && !acpi_device->driver)
389 return -ENODEV;
391 status = acpi_get_type(acpi_device->handle, &not_used);
392 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
393 return -ENODEV;
395 get_device(&acpi_device->dev);
396 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
397 if (ACPI_SUCCESS(status))
398 return count;
400 put_device(&acpi_device->dev);
401 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
402 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
403 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
406 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
408 static ssize_t
409 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf)
411 struct acpi_device *acpi_dev = to_acpi_device(dev);
413 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
415 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
417 static ssize_t acpi_device_uid_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
420 struct acpi_device *acpi_dev = to_acpi_device(dev);
422 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
424 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
426 static ssize_t acpi_device_adr_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 struct acpi_device *acpi_dev = to_acpi_device(dev);
431 if (acpi_dev->pnp.bus_address > U32_MAX)
432 return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
433 else
434 return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
436 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
438 static ssize_t acpi_device_path_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct acpi_device *acpi_dev = to_acpi_device(dev);
443 return acpi_object_path(acpi_dev->handle, buf);
445 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
447 /* sysfs file that shows description text from the ACPI _STR method */
448 static ssize_t description_show(struct device *dev,
449 struct device_attribute *attr,
450 char *buf) {
451 struct acpi_device *acpi_dev = to_acpi_device(dev);
452 int result;
454 if (acpi_dev->pnp.str_obj == NULL)
455 return 0;
458 * The _STR object contains a Unicode identifier for a device.
459 * We need to convert to utf-8 so it can be displayed.
461 result = utf16s_to_utf8s(
462 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
463 acpi_dev->pnp.str_obj->buffer.length,
464 UTF16_LITTLE_ENDIAN, buf,
465 PAGE_SIZE);
467 buf[result++] = '\n';
469 return result;
471 static DEVICE_ATTR_RO(description);
473 static ssize_t
474 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
475 char *buf) {
476 struct acpi_device *acpi_dev = to_acpi_device(dev);
477 acpi_status status;
478 unsigned long long sun;
480 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
481 if (ACPI_FAILURE(status))
482 return -EIO;
484 return sprintf(buf, "%llu\n", sun);
486 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
488 static ssize_t
489 acpi_device_hrv_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 hrv;
495 status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
496 if (ACPI_FAILURE(status))
497 return -EIO;
499 return sprintf(buf, "%llu\n", hrv);
501 static DEVICE_ATTR(hrv, 0444, acpi_device_hrv_show, NULL);
503 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
504 char *buf) {
505 struct acpi_device *acpi_dev = to_acpi_device(dev);
506 acpi_status status;
507 unsigned long long sta;
509 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
510 if (ACPI_FAILURE(status))
511 return -EIO;
513 return sprintf(buf, "%llu\n", sta);
515 static DEVICE_ATTR_RO(status);
518 * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
519 * @dev: ACPI device object.
521 int acpi_device_setup_files(struct acpi_device *dev)
523 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
524 acpi_status status;
525 int result = 0;
528 * Devices gotten from FADT don't have a "path" attribute
530 if (dev->handle) {
531 result = device_create_file(&dev->dev, &dev_attr_path);
532 if (result)
533 goto end;
536 if (!list_empty(&dev->pnp.ids)) {
537 result = device_create_file(&dev->dev, &dev_attr_hid);
538 if (result)
539 goto end;
541 result = device_create_file(&dev->dev, &dev_attr_modalias);
542 if (result)
543 goto end;
547 * If device has _STR, 'description' file is created
549 if (acpi_has_method(dev->handle, "_STR")) {
550 status = acpi_evaluate_object(dev->handle, "_STR",
551 NULL, &buffer);
552 if (ACPI_FAILURE(status))
553 buffer.pointer = NULL;
554 dev->pnp.str_obj = buffer.pointer;
555 result = device_create_file(&dev->dev, &dev_attr_description);
556 if (result)
557 goto end;
560 if (dev->pnp.type.bus_address)
561 result = device_create_file(&dev->dev, &dev_attr_adr);
562 if (dev->pnp.unique_id)
563 result = device_create_file(&dev->dev, &dev_attr_uid);
565 if (acpi_has_method(dev->handle, "_SUN")) {
566 result = device_create_file(&dev->dev, &dev_attr_sun);
567 if (result)
568 goto end;
571 if (acpi_has_method(dev->handle, "_HRV")) {
572 result = device_create_file(&dev->dev, &dev_attr_hrv);
573 if (result)
574 goto end;
577 if (acpi_has_method(dev->handle, "_STA")) {
578 result = device_create_file(&dev->dev, &dev_attr_status);
579 if (result)
580 goto end;
584 * If device has _EJ0, 'eject' file is created that is used to trigger
585 * hot-removal function from userland.
587 if (acpi_has_method(dev->handle, "_EJ0")) {
588 result = device_create_file(&dev->dev, &dev_attr_eject);
589 if (result)
590 return result;
593 if (dev->flags.power_manageable) {
594 result = device_create_file(&dev->dev, &dev_attr_power_state);
595 if (result)
596 return result;
598 if (dev->power.flags.power_resources)
599 result = device_create_file(&dev->dev,
600 &dev_attr_real_power_state);
603 acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
605 end:
606 return result;
610 * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
611 * @dev: ACPI device object.
613 void acpi_device_remove_files(struct acpi_device *dev)
615 acpi_hide_nondev_subnodes(&dev->data);
617 if (dev->flags.power_manageable) {
618 device_remove_file(&dev->dev, &dev_attr_power_state);
619 if (dev->power.flags.power_resources)
620 device_remove_file(&dev->dev,
621 &dev_attr_real_power_state);
625 * If device has _STR, remove 'description' file
627 if (acpi_has_method(dev->handle, "_STR")) {
628 kfree(dev->pnp.str_obj);
629 device_remove_file(&dev->dev, &dev_attr_description);
632 * If device has _EJ0, remove 'eject' file.
634 if (acpi_has_method(dev->handle, "_EJ0"))
635 device_remove_file(&dev->dev, &dev_attr_eject);
637 if (acpi_has_method(dev->handle, "_SUN"))
638 device_remove_file(&dev->dev, &dev_attr_sun);
640 if (acpi_has_method(dev->handle, "_HRV"))
641 device_remove_file(&dev->dev, &dev_attr_hrv);
643 if (dev->pnp.unique_id)
644 device_remove_file(&dev->dev, &dev_attr_uid);
645 if (dev->pnp.type.bus_address)
646 device_remove_file(&dev->dev, &dev_attr_adr);
647 device_remove_file(&dev->dev, &dev_attr_modalias);
648 device_remove_file(&dev->dev, &dev_attr_hid);
649 if (acpi_has_method(dev->handle, "_STA"))
650 device_remove_file(&dev->dev, &dev_attr_status);
651 if (dev->handle)
652 device_remove_file(&dev->dev, &dev_attr_path);