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