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