0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/acpi.h>
0015 #include <linux/device.h>
0016 #include <linux/export.h>
0017 #include <linux/nls.h>
0018
0019 #include "internal.h"
0020
0021 static ssize_t acpi_object_path(acpi_handle handle, char *buf)
0022 {
0023 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
0024 int result;
0025
0026 result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path);
0027 if (result)
0028 return result;
0029
0030 result = sprintf(buf, "%s\n", (char *)path.pointer);
0031 kfree(path.pointer);
0032 return result;
0033 }
0034
0035 struct acpi_data_node_attr {
0036 struct attribute attr;
0037 ssize_t (*show)(struct acpi_data_node *, char *);
0038 ssize_t (*store)(struct acpi_data_node *, const char *, size_t count);
0039 };
0040
0041 #define DATA_NODE_ATTR(_name) \
0042 static struct acpi_data_node_attr data_node_##_name = \
0043 __ATTR(_name, 0444, data_node_show_##_name, NULL)
0044
0045 static ssize_t data_node_show_path(struct acpi_data_node *dn, char *buf)
0046 {
0047 return dn->handle ? acpi_object_path(dn->handle, buf) : 0;
0048 }
0049
0050 DATA_NODE_ATTR(path);
0051
0052 static struct attribute *acpi_data_node_default_attrs[] = {
0053 &data_node_path.attr,
0054 NULL
0055 };
0056 ATTRIBUTE_GROUPS(acpi_data_node_default);
0057
0058 #define to_data_node(k) container_of(k, struct acpi_data_node, kobj)
0059 #define to_attr(a) container_of(a, struct acpi_data_node_attr, attr)
0060
0061 static ssize_t acpi_data_node_attr_show(struct kobject *kobj,
0062 struct attribute *attr, char *buf)
0063 {
0064 struct acpi_data_node *dn = to_data_node(kobj);
0065 struct acpi_data_node_attr *dn_attr = to_attr(attr);
0066
0067 return dn_attr->show ? dn_attr->show(dn, buf) : -ENXIO;
0068 }
0069
0070 static const struct sysfs_ops acpi_data_node_sysfs_ops = {
0071 .show = acpi_data_node_attr_show,
0072 };
0073
0074 static void acpi_data_node_release(struct kobject *kobj)
0075 {
0076 struct acpi_data_node *dn = to_data_node(kobj);
0077
0078 complete(&dn->kobj_done);
0079 }
0080
0081 static struct kobj_type acpi_data_node_ktype = {
0082 .sysfs_ops = &acpi_data_node_sysfs_ops,
0083 .default_groups = acpi_data_node_default_groups,
0084 .release = acpi_data_node_release,
0085 };
0086
0087 static void acpi_expose_nondev_subnodes(struct kobject *kobj,
0088 struct acpi_device_data *data)
0089 {
0090 struct list_head *list = &data->subnodes;
0091 struct acpi_data_node *dn;
0092
0093 if (list_empty(list))
0094 return;
0095
0096 list_for_each_entry(dn, list, sibling) {
0097 int ret;
0098
0099 init_completion(&dn->kobj_done);
0100 ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype,
0101 kobj, "%s", dn->name);
0102 if (!ret)
0103 acpi_expose_nondev_subnodes(&dn->kobj, &dn->data);
0104 else if (dn->handle)
0105 acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret);
0106 }
0107 }
0108
0109 static void acpi_hide_nondev_subnodes(struct acpi_device_data *data)
0110 {
0111 struct list_head *list = &data->subnodes;
0112 struct acpi_data_node *dn;
0113
0114 if (list_empty(list))
0115 return;
0116
0117 list_for_each_entry_reverse(dn, list, sibling) {
0118 acpi_hide_nondev_subnodes(&dn->data);
0119 kobject_put(&dn->kobj);
0120 }
0121 }
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias,
0137 int size)
0138 {
0139 int len;
0140 int count;
0141 struct acpi_hardware_id *id;
0142
0143
0144 if (!acpi_device_is_present(acpi_dev))
0145 return 0;
0146
0147
0148
0149
0150
0151
0152 count = 0;
0153 list_for_each_entry(id, &acpi_dev->pnp.ids, list)
0154 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
0155 count++;
0156
0157 if (!count)
0158 return 0;
0159
0160 len = snprintf(modalias, size, "acpi:");
0161 if (len <= 0)
0162 return len;
0163
0164 size -= len;
0165
0166 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
0167 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
0168 continue;
0169
0170 count = snprintf(&modalias[len], size, "%s:", id->id);
0171 if (count < 0)
0172 return -EINVAL;
0173
0174 if (count >= size)
0175 return -ENOMEM;
0176
0177 len += count;
0178 size -= count;
0179 }
0180 modalias[len] = '\0';
0181 return len;
0182 }
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
0195 int size)
0196 {
0197 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
0198 const union acpi_object *of_compatible, *obj;
0199 acpi_status status;
0200 int len, count;
0201 int i, nval;
0202 char *c;
0203
0204 status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
0205 if (ACPI_FAILURE(status))
0206 return -ENODEV;
0207
0208
0209 for (c = buf.pointer; *c != '\0'; c++)
0210 *c = tolower(*c);
0211
0212 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
0213 ACPI_FREE(buf.pointer);
0214
0215 if (len <= 0)
0216 return len;
0217
0218 of_compatible = acpi_dev->data.of_compatible;
0219 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
0220 nval = of_compatible->package.count;
0221 obj = of_compatible->package.elements;
0222 } else {
0223 nval = 1;
0224 obj = of_compatible;
0225 }
0226 for (i = 0; i < nval; i++, obj++) {
0227 count = snprintf(&modalias[len], size, "C%s",
0228 obj->string.pointer);
0229 if (count < 0)
0230 return -EINVAL;
0231
0232 if (count >= size)
0233 return -ENOMEM;
0234
0235 len += count;
0236 size -= count;
0237 }
0238 modalias[len] = '\0';
0239 return len;
0240 }
0241
0242 int __acpi_device_uevent_modalias(struct acpi_device *adev,
0243 struct kobj_uevent_env *env)
0244 {
0245 int len;
0246
0247 if (!adev)
0248 return -ENODEV;
0249
0250 if (list_empty(&adev->pnp.ids))
0251 return 0;
0252
0253 if (add_uevent_var(env, "MODALIAS="))
0254 return -ENOMEM;
0255
0256 if (adev->data.of_compatible)
0257 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
0258 sizeof(env->buf) - env->buflen);
0259 else
0260 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
0261 sizeof(env->buf) - env->buflen);
0262 if (len < 0)
0263 return len;
0264
0265 env->buflen += len;
0266
0267 return 0;
0268 }
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
0281 {
0282 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
0283 }
0284 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
0285
0286 static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
0287 {
0288 int len, count;
0289
0290 if (!adev)
0291 return -ENODEV;
0292
0293 if (list_empty(&adev->pnp.ids))
0294 return 0;
0295
0296 len = create_pnp_modalias(adev, buf, size - 1);
0297 if (len < 0) {
0298 return len;
0299 } else if (len > 0) {
0300 buf[len++] = '\n';
0301 size -= len;
0302 }
0303 if (!adev->data.of_compatible)
0304 return len;
0305
0306 count = create_of_modalias(adev, buf + len, size - 1);
0307 if (count < 0) {
0308 return count;
0309 } else if (count > 0) {
0310 len += count;
0311 buf[len++] = '\n';
0312 }
0313
0314 return len;
0315 }
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328 int acpi_device_modalias(struct device *dev, char *buf, int size)
0329 {
0330 return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
0331 }
0332 EXPORT_SYMBOL_GPL(acpi_device_modalias);
0333
0334 static ssize_t
0335 modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
0336 {
0337 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
0338 }
0339 static DEVICE_ATTR_RO(modalias);
0340
0341 static ssize_t real_power_state_show(struct device *dev,
0342 struct device_attribute *attr, char *buf)
0343 {
0344 struct acpi_device *adev = to_acpi_device(dev);
0345 int state;
0346 int ret;
0347
0348 ret = acpi_device_get_power(adev, &state);
0349 if (ret)
0350 return ret;
0351
0352 return sprintf(buf, "%s\n", acpi_power_state_string(state));
0353 }
0354
0355 static DEVICE_ATTR_RO(real_power_state);
0356
0357 static ssize_t power_state_show(struct device *dev,
0358 struct device_attribute *attr, char *buf)
0359 {
0360 struct acpi_device *adev = to_acpi_device(dev);
0361
0362 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
0363 }
0364
0365 static DEVICE_ATTR_RO(power_state);
0366
0367 static ssize_t
0368 eject_store(struct device *d, struct device_attribute *attr,
0369 const char *buf, size_t count)
0370 {
0371 struct acpi_device *acpi_device = to_acpi_device(d);
0372 acpi_object_type not_used;
0373 acpi_status status;
0374
0375 if (!count || buf[0] != '1')
0376 return -EINVAL;
0377
0378 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
0379 && !d->driver)
0380 return -ENODEV;
0381
0382 status = acpi_get_type(acpi_device->handle, ¬_used);
0383 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
0384 return -ENODEV;
0385
0386 acpi_dev_get(acpi_device);
0387 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
0388 if (ACPI_SUCCESS(status))
0389 return count;
0390
0391 acpi_dev_put(acpi_device);
0392 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
0393 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
0394 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
0395 }
0396
0397 static DEVICE_ATTR_WO(eject);
0398
0399 static ssize_t
0400 hid_show(struct device *dev, struct device_attribute *attr, char *buf)
0401 {
0402 struct acpi_device *acpi_dev = to_acpi_device(dev);
0403
0404 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
0405 }
0406 static DEVICE_ATTR_RO(hid);
0407
0408 static ssize_t uid_show(struct device *dev,
0409 struct device_attribute *attr, char *buf)
0410 {
0411 struct acpi_device *acpi_dev = to_acpi_device(dev);
0412
0413 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
0414 }
0415 static DEVICE_ATTR_RO(uid);
0416
0417 static ssize_t adr_show(struct device *dev,
0418 struct device_attribute *attr, char *buf)
0419 {
0420 struct acpi_device *acpi_dev = to_acpi_device(dev);
0421
0422 if (acpi_dev->pnp.bus_address > U32_MAX)
0423 return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
0424 else
0425 return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
0426 }
0427 static DEVICE_ATTR_RO(adr);
0428
0429 static ssize_t path_show(struct device *dev,
0430 struct device_attribute *attr, char *buf)
0431 {
0432 struct acpi_device *acpi_dev = to_acpi_device(dev);
0433
0434 return acpi_object_path(acpi_dev->handle, buf);
0435 }
0436 static DEVICE_ATTR_RO(path);
0437
0438
0439 static ssize_t description_show(struct device *dev,
0440 struct device_attribute *attr,
0441 char *buf)
0442 {
0443 struct acpi_device *acpi_dev = to_acpi_device(dev);
0444 int result;
0445
0446 if (acpi_dev->pnp.str_obj == NULL)
0447 return 0;
0448
0449
0450
0451
0452
0453 result = utf16s_to_utf8s(
0454 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
0455 acpi_dev->pnp.str_obj->buffer.length,
0456 UTF16_LITTLE_ENDIAN, buf,
0457 PAGE_SIZE - 1);
0458
0459 buf[result++] = '\n';
0460
0461 return result;
0462 }
0463 static DEVICE_ATTR_RO(description);
0464
0465 static ssize_t
0466 sun_show(struct device *dev, struct device_attribute *attr,
0467 char *buf)
0468 {
0469 struct acpi_device *acpi_dev = to_acpi_device(dev);
0470 acpi_status status;
0471 unsigned long long sun;
0472
0473 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
0474 if (ACPI_FAILURE(status))
0475 return -EIO;
0476
0477 return sprintf(buf, "%llu\n", sun);
0478 }
0479 static DEVICE_ATTR_RO(sun);
0480
0481 static ssize_t
0482 hrv_show(struct device *dev, struct device_attribute *attr,
0483 char *buf)
0484 {
0485 struct acpi_device *acpi_dev = to_acpi_device(dev);
0486 acpi_status status;
0487 unsigned long long hrv;
0488
0489 status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
0490 if (ACPI_FAILURE(status))
0491 return -EIO;
0492
0493 return sprintf(buf, "%llu\n", hrv);
0494 }
0495 static DEVICE_ATTR_RO(hrv);
0496
0497 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
0498 char *buf)
0499 {
0500 struct acpi_device *acpi_dev = to_acpi_device(dev);
0501 acpi_status status;
0502 unsigned long long sta;
0503
0504 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
0505 if (ACPI_FAILURE(status))
0506 return -EIO;
0507
0508 return sprintf(buf, "%llu\n", sta);
0509 }
0510 static DEVICE_ATTR_RO(status);
0511
0512
0513
0514
0515
0516 int acpi_device_setup_files(struct acpi_device *dev)
0517 {
0518 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
0519 acpi_status status;
0520 int result = 0;
0521
0522
0523
0524
0525 if (dev->handle) {
0526 result = device_create_file(&dev->dev, &dev_attr_path);
0527 if (result)
0528 goto end;
0529 }
0530
0531 if (!list_empty(&dev->pnp.ids)) {
0532 result = device_create_file(&dev->dev, &dev_attr_hid);
0533 if (result)
0534 goto end;
0535
0536 result = device_create_file(&dev->dev, &dev_attr_modalias);
0537 if (result)
0538 goto end;
0539 }
0540
0541
0542
0543
0544 if (acpi_has_method(dev->handle, "_STR")) {
0545 status = acpi_evaluate_object(dev->handle, "_STR",
0546 NULL, &buffer);
0547 if (ACPI_FAILURE(status))
0548 buffer.pointer = NULL;
0549 dev->pnp.str_obj = buffer.pointer;
0550 result = device_create_file(&dev->dev, &dev_attr_description);
0551 if (result)
0552 goto end;
0553 }
0554
0555 if (dev->pnp.type.bus_address)
0556 result = device_create_file(&dev->dev, &dev_attr_adr);
0557 if (dev->pnp.unique_id)
0558 result = device_create_file(&dev->dev, &dev_attr_uid);
0559
0560 if (acpi_has_method(dev->handle, "_SUN")) {
0561 result = device_create_file(&dev->dev, &dev_attr_sun);
0562 if (result)
0563 goto end;
0564 }
0565
0566 if (acpi_has_method(dev->handle, "_HRV")) {
0567 result = device_create_file(&dev->dev, &dev_attr_hrv);
0568 if (result)
0569 goto end;
0570 }
0571
0572 if (acpi_has_method(dev->handle, "_STA")) {
0573 result = device_create_file(&dev->dev, &dev_attr_status);
0574 if (result)
0575 goto end;
0576 }
0577
0578
0579
0580
0581
0582 if (acpi_has_method(dev->handle, "_EJ0")) {
0583 result = device_create_file(&dev->dev, &dev_attr_eject);
0584 if (result)
0585 return result;
0586 }
0587
0588 if (dev->flags.power_manageable) {
0589 result = device_create_file(&dev->dev, &dev_attr_power_state);
0590 if (result)
0591 return result;
0592
0593 if (dev->power.flags.power_resources)
0594 result = device_create_file(&dev->dev,
0595 &dev_attr_real_power_state);
0596 }
0597
0598 acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
0599
0600 end:
0601 return result;
0602 }
0603
0604
0605
0606
0607
0608 void acpi_device_remove_files(struct acpi_device *dev)
0609 {
0610 acpi_hide_nondev_subnodes(&dev->data);
0611
0612 if (dev->flags.power_manageable) {
0613 device_remove_file(&dev->dev, &dev_attr_power_state);
0614 if (dev->power.flags.power_resources)
0615 device_remove_file(&dev->dev,
0616 &dev_attr_real_power_state);
0617 }
0618
0619
0620
0621
0622 if (acpi_has_method(dev->handle, "_STR")) {
0623 kfree(dev->pnp.str_obj);
0624 device_remove_file(&dev->dev, &dev_attr_description);
0625 }
0626
0627
0628
0629 if (acpi_has_method(dev->handle, "_EJ0"))
0630 device_remove_file(&dev->dev, &dev_attr_eject);
0631
0632 if (acpi_has_method(dev->handle, "_SUN"))
0633 device_remove_file(&dev->dev, &dev_attr_sun);
0634
0635 if (acpi_has_method(dev->handle, "_HRV"))
0636 device_remove_file(&dev->dev, &dev_attr_hrv);
0637
0638 if (dev->pnp.unique_id)
0639 device_remove_file(&dev->dev, &dev_attr_uid);
0640 if (dev->pnp.type.bus_address)
0641 device_remove_file(&dev->dev, &dev_attr_adr);
0642 device_remove_file(&dev->dev, &dev_attr_modalias);
0643 device_remove_file(&dev->dev, &dev_attr_hid);
0644 if (acpi_has_method(dev->handle, "_STA"))
0645 device_remove_file(&dev->dev, &dev_attr_status);
0646 if (dev->handle)
0647 device_remove_file(&dev->dev, &dev_attr_path);
0648 }