Merge branch 'acpi-misc'
[linux-2.6-microblaze.git] / drivers / acpi / device_sysfs.c
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
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
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
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
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
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  */
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 <= 0)
161                 return len;
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  */
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 <= 0)
215                 return len;
216
217         of_compatible = acpi_dev->data.of_compatible;
218         if (of_compatible->type == ACPI_TYPE_PACKAGE) {
219                 nval = of_compatible->package.count;
220                 obj = of_compatible->package.elements;
221         } else { /* Must be ACPI_TYPE_STRING. */
222                 nval = 1;
223                 obj = of_compatible;
224         }
225         for (i = 0; i < nval; i++, obj++) {
226                 count = snprintf(&modalias[len], size, "C%s",
227                                  obj->string.pointer);
228                 if (count < 0)
229                         return -EINVAL;
230
231                 if (count >= size)
232                         return -ENOMEM;
233
234                 len += count;
235                 size -= count;
236         }
237         modalias[len] = '\0';
238         return len;
239 }
240
241 int __acpi_device_uevent_modalias(struct acpi_device *adev,
242                                   struct kobj_uevent_env *env)
243 {
244         int len;
245
246         if (!adev)
247                 return -ENODEV;
248
249         if (list_empty(&adev->pnp.ids))
250                 return 0;
251
252         if (add_uevent_var(env, "MODALIAS="))
253                 return -ENOMEM;
254
255         if (adev->data.of_compatible)
256                 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
257                                          sizeof(env->buf) - env->buflen);
258         else
259                 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
260                                           sizeof(env->buf) - env->buflen);
261         if (len < 0)
262                 return len;
263
264         env->buflen += len;
265
266         return 0;
267 }
268
269 /**
270  * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
271  *
272  * Create the uevent modalias field for ACPI-enumerated devices.
273  *
274  * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
275  * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
276  */
277 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
278 {
279         return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
280 }
281 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
282
283 static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
284 {
285         int len, count;
286
287         if (!adev)
288                 return -ENODEV;
289
290         if (list_empty(&adev->pnp.ids))
291                 return 0;
292
293         len = create_pnp_modalias(adev, buf, size - 1);
294         if (len < 0) {
295                 return len;
296         } else if (len > 0) {
297                 buf[len++] = '\n';
298                 size -= len;
299         }
300         if (!adev->data.of_compatible)
301                 return len;
302
303         count = create_of_modalias(adev, buf + len, size - 1);
304         if (count < 0) {
305                 return count;
306         } else if (count > 0) {
307                 len += count;
308                 buf[len++] = '\n';
309         }
310
311         return len;
312 }
313
314 /**
315  * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
316  *
317  * Create the modalias sysfs attribute for ACPI-enumerated devices.
318  *
319  * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
320  * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
321  */
322 int acpi_device_modalias(struct device *dev, char *buf, int size)
323 {
324         return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
325 }
326 EXPORT_SYMBOL_GPL(acpi_device_modalias);
327
328 static ssize_t
329 modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
330 {
331         return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
332 }
333 static DEVICE_ATTR_RO(modalias);
334
335 static ssize_t real_power_state_show(struct device *dev,
336                                      struct device_attribute *attr, char *buf)
337 {
338         struct acpi_device *adev = to_acpi_device(dev);
339         int state;
340         int ret;
341
342         ret = acpi_device_get_power(adev, &state);
343         if (ret)
344                 return ret;
345
346         return sprintf(buf, "%s\n", acpi_power_state_string(state));
347 }
348
349 static DEVICE_ATTR_RO(real_power_state);
350
351 static ssize_t power_state_show(struct device *dev,
352                                 struct device_attribute *attr, char *buf)
353 {
354         struct acpi_device *adev = to_acpi_device(dev);
355
356         return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
357 }
358
359 static DEVICE_ATTR_RO(power_state);
360
361 static ssize_t
362 eject_store(struct device *d, struct device_attribute *attr,
363             const char *buf, size_t count)
364 {
365         struct acpi_device *acpi_device = to_acpi_device(d);
366         acpi_object_type not_used;
367         acpi_status status;
368
369         if (!count || buf[0] != '1')
370                 return -EINVAL;
371
372         if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
373             && !acpi_device->driver)
374                 return -ENODEV;
375
376         status = acpi_get_type(acpi_device->handle, &not_used);
377         if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
378                 return -ENODEV;
379
380         acpi_dev_get(acpi_device);
381         status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
382         if (ACPI_SUCCESS(status))
383                 return count;
384
385         acpi_dev_put(acpi_device);
386         acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
387                           ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
388         return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
389 }
390
391 static DEVICE_ATTR_WO(eject);
392
393 static ssize_t
394 hid_show(struct device *dev, struct device_attribute *attr, char *buf)
395 {
396         struct acpi_device *acpi_dev = to_acpi_device(dev);
397
398         return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
399 }
400 static DEVICE_ATTR_RO(hid);
401
402 static ssize_t uid_show(struct device *dev,
403                         struct device_attribute *attr, char *buf)
404 {
405         struct acpi_device *acpi_dev = to_acpi_device(dev);
406
407         return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
408 }
409 static DEVICE_ATTR_RO(uid);
410
411 static ssize_t adr_show(struct device *dev,
412                         struct device_attribute *attr, char *buf)
413 {
414         struct acpi_device *acpi_dev = to_acpi_device(dev);
415
416         if (acpi_dev->pnp.bus_address > U32_MAX)
417                 return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
418         else
419                 return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
420 }
421 static DEVICE_ATTR_RO(adr);
422
423 static ssize_t path_show(struct device *dev,
424                          struct device_attribute *attr, char *buf)
425 {
426         struct acpi_device *acpi_dev = to_acpi_device(dev);
427
428         return acpi_object_path(acpi_dev->handle, buf);
429 }
430 static DEVICE_ATTR_RO(path);
431
432 /* sysfs file that shows description text from the ACPI _STR method */
433 static ssize_t description_show(struct device *dev,
434                                 struct device_attribute *attr,
435                                 char *buf)
436 {
437         struct acpi_device *acpi_dev = to_acpi_device(dev);
438         int result;
439
440         if (acpi_dev->pnp.str_obj == NULL)
441                 return 0;
442
443         /*
444          * The _STR object contains a Unicode identifier for a device.
445          * We need to convert to utf-8 so it can be displayed.
446          */
447         result = utf16s_to_utf8s(
448                 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
449                 acpi_dev->pnp.str_obj->buffer.length,
450                 UTF16_LITTLE_ENDIAN, buf,
451                 PAGE_SIZE);
452
453         buf[result++] = '\n';
454
455         return result;
456 }
457 static DEVICE_ATTR_RO(description);
458
459 static ssize_t
460 sun_show(struct device *dev, struct device_attribute *attr,
461          char *buf)
462 {
463         struct acpi_device *acpi_dev = to_acpi_device(dev);
464         acpi_status status;
465         unsigned long long sun;
466
467         status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
468         if (ACPI_FAILURE(status))
469                 return -EIO;
470
471         return sprintf(buf, "%llu\n", sun);
472 }
473 static DEVICE_ATTR_RO(sun);
474
475 static ssize_t
476 hrv_show(struct device *dev, struct device_attribute *attr,
477          char *buf)
478 {
479         struct acpi_device *acpi_dev = to_acpi_device(dev);
480         acpi_status status;
481         unsigned long long hrv;
482
483         status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
484         if (ACPI_FAILURE(status))
485                 return -EIO;
486
487         return sprintf(buf, "%llu\n", hrv);
488 }
489 static DEVICE_ATTR_RO(hrv);
490
491 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
492                                 char *buf)
493 {
494         struct acpi_device *acpi_dev = to_acpi_device(dev);
495         acpi_status status;
496         unsigned long long sta;
497
498         status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
499         if (ACPI_FAILURE(status))
500                 return -EIO;
501
502         return sprintf(buf, "%llu\n", sta);
503 }
504 static DEVICE_ATTR_RO(status);
505
506 /**
507  * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
508  * @dev: ACPI device object.
509  */
510 int acpi_device_setup_files(struct acpi_device *dev)
511 {
512         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
513         acpi_status status;
514         int result = 0;
515
516         /*
517          * Devices gotten from FADT don't have a "path" attribute
518          */
519         if (dev->handle) {
520                 result = device_create_file(&dev->dev, &dev_attr_path);
521                 if (result)
522                         goto end;
523         }
524
525         if (!list_empty(&dev->pnp.ids)) {
526                 result = device_create_file(&dev->dev, &dev_attr_hid);
527                 if (result)
528                         goto end;
529
530                 result = device_create_file(&dev->dev, &dev_attr_modalias);
531                 if (result)
532                         goto end;
533         }
534
535         /*
536          * If device has _STR, 'description' file is created
537          */
538         if (acpi_has_method(dev->handle, "_STR")) {
539                 status = acpi_evaluate_object(dev->handle, "_STR",
540                                         NULL, &buffer);
541                 if (ACPI_FAILURE(status))
542                         buffer.pointer = NULL;
543                 dev->pnp.str_obj = buffer.pointer;
544                 result = device_create_file(&dev->dev, &dev_attr_description);
545                 if (result)
546                         goto end;
547         }
548
549         if (dev->pnp.type.bus_address)
550                 result = device_create_file(&dev->dev, &dev_attr_adr);
551         if (dev->pnp.unique_id)
552                 result = device_create_file(&dev->dev, &dev_attr_uid);
553
554         if (acpi_has_method(dev->handle, "_SUN")) {
555                 result = device_create_file(&dev->dev, &dev_attr_sun);
556                 if (result)
557                         goto end;
558         }
559
560         if (acpi_has_method(dev->handle, "_HRV")) {
561                 result = device_create_file(&dev->dev, &dev_attr_hrv);
562                 if (result)
563                         goto end;
564         }
565
566         if (acpi_has_method(dev->handle, "_STA")) {
567                 result = device_create_file(&dev->dev, &dev_attr_status);
568                 if (result)
569                         goto end;
570         }
571
572         /*
573          * If device has _EJ0, 'eject' file is created that is used to trigger
574          * hot-removal function from userland.
575          */
576         if (acpi_has_method(dev->handle, "_EJ0")) {
577                 result = device_create_file(&dev->dev, &dev_attr_eject);
578                 if (result)
579                         return result;
580         }
581
582         if (dev->flags.power_manageable) {
583                 result = device_create_file(&dev->dev, &dev_attr_power_state);
584                 if (result)
585                         return result;
586
587                 if (dev->power.flags.power_resources)
588                         result = device_create_file(&dev->dev,
589                                                     &dev_attr_real_power_state);
590         }
591
592         acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
593
594 end:
595         return result;
596 }
597
598 /**
599  * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
600  * @dev: ACPI device object.
601  */
602 void acpi_device_remove_files(struct acpi_device *dev)
603 {
604         acpi_hide_nondev_subnodes(&dev->data);
605
606         if (dev->flags.power_manageable) {
607                 device_remove_file(&dev->dev, &dev_attr_power_state);
608                 if (dev->power.flags.power_resources)
609                         device_remove_file(&dev->dev,
610                                            &dev_attr_real_power_state);
611         }
612
613         /*
614          * If device has _STR, remove 'description' file
615          */
616         if (acpi_has_method(dev->handle, "_STR")) {
617                 kfree(dev->pnp.str_obj);
618                 device_remove_file(&dev->dev, &dev_attr_description);
619         }
620         /*
621          * If device has _EJ0, remove 'eject' file.
622          */
623         if (acpi_has_method(dev->handle, "_EJ0"))
624                 device_remove_file(&dev->dev, &dev_attr_eject);
625
626         if (acpi_has_method(dev->handle, "_SUN"))
627                 device_remove_file(&dev->dev, &dev_attr_sun);
628
629         if (acpi_has_method(dev->handle, "_HRV"))
630                 device_remove_file(&dev->dev, &dev_attr_hrv);
631
632         if (dev->pnp.unique_id)
633                 device_remove_file(&dev->dev, &dev_attr_uid);
634         if (dev->pnp.type.bus_address)
635                 device_remove_file(&dev->dev, &dev_attr_adr);
636         device_remove_file(&dev->dev, &dev_attr_modalias);
637         device_remove_file(&dev->dev, &dev_attr_hid);
638         if (acpi_has_method(dev->handle, "_STA"))
639                 device_remove_file(&dev->dev, &dev_attr_status);
640         if (dev->handle)
641                 device_remove_file(&dev->dev, &dev_attr_path);
642 }