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