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