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