device_sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. #include <linux/acpi.h>
  14. #include <linux/device.h>
  15. #include <linux/export.h>
  16. #include <linux/nls.h>
  17. #include "internal.h"
  18. static ssize_t acpi_object_path(acpi_handle handle, char *buf)
  19. {
  20. struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
  21. int result;
  22. result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path);
  23. if (result)
  24. return result;
  25. result = sysfs_emit(buf, "%s\n", (char *)path.pointer);
  26. kfree(path.pointer);
  27. return result;
  28. }
  29. struct acpi_data_node_attr {
  30. struct attribute attr;
  31. ssize_t (*show)(struct acpi_data_node *, char *);
  32. ssize_t (*store)(struct acpi_data_node *, const char *, size_t count);
  33. };
  34. #define DATA_NODE_ATTR(_name) \
  35. static struct acpi_data_node_attr data_node_##_name = \
  36. __ATTR(_name, 0444, data_node_show_##_name, NULL)
  37. static ssize_t data_node_show_path(struct acpi_data_node *dn, char *buf)
  38. {
  39. return dn->handle ? acpi_object_path(dn->handle, buf) : 0;
  40. }
  41. DATA_NODE_ATTR(path);
  42. static struct attribute *acpi_data_node_default_attrs[] = {
  43. &data_node_path.attr,
  44. NULL
  45. };
  46. ATTRIBUTE_GROUPS(acpi_data_node_default);
  47. #define to_data_node(k) container_of(k, struct acpi_data_node, kobj)
  48. #define to_attr(a) container_of(a, struct acpi_data_node_attr, attr)
  49. static ssize_t acpi_data_node_attr_show(struct kobject *kobj,
  50. struct attribute *attr, char *buf)
  51. {
  52. struct acpi_data_node *dn = to_data_node(kobj);
  53. struct acpi_data_node_attr *dn_attr = to_attr(attr);
  54. return dn_attr->show ? dn_attr->show(dn, buf) : -ENXIO;
  55. }
  56. static const struct sysfs_ops acpi_data_node_sysfs_ops = {
  57. .show = acpi_data_node_attr_show,
  58. };
  59. static void acpi_data_node_release(struct kobject *kobj)
  60. {
  61. struct acpi_data_node *dn = to_data_node(kobj);
  62. complete(&dn->kobj_done);
  63. }
  64. static const struct kobj_type acpi_data_node_ktype = {
  65. .sysfs_ops = &acpi_data_node_sysfs_ops,
  66. .default_groups = acpi_data_node_default_groups,
  67. .release = acpi_data_node_release,
  68. };
  69. static void acpi_expose_nondev_subnodes(struct kobject *kobj,
  70. struct acpi_device_data *data)
  71. {
  72. struct list_head *list = &data->subnodes;
  73. struct acpi_data_node *dn;
  74. if (list_empty(list))
  75. return;
  76. list_for_each_entry(dn, list, sibling) {
  77. int ret;
  78. init_completion(&dn->kobj_done);
  79. ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype,
  80. kobj, "%s", dn->name);
  81. if (!ret)
  82. acpi_expose_nondev_subnodes(&dn->kobj, &dn->data);
  83. else if (dn->handle)
  84. acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret);
  85. }
  86. }
  87. static void acpi_hide_nondev_subnodes(struct acpi_device_data *data)
  88. {
  89. struct list_head *list = &data->subnodes;
  90. struct acpi_data_node *dn;
  91. if (list_empty(list))
  92. return;
  93. list_for_each_entry_reverse(dn, list, sibling) {
  94. acpi_hide_nondev_subnodes(&dn->data);
  95. kobject_put(&dn->kobj);
  96. }
  97. }
  98. /**
  99. * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
  100. * @acpi_dev: ACPI device object.
  101. * @modalias: Buffer to print into.
  102. * @size: Size of the buffer.
  103. *
  104. * Creates hid/cid(s) string needed for modalias and uevent
  105. * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
  106. * char *modalias: "acpi:IBM0001:ACPI0001"
  107. * Return: 0: no _HID and no _CID
  108. * -EINVAL: output error
  109. * -ENOMEM: output is truncated
  110. */
  111. static int create_pnp_modalias(const struct acpi_device *acpi_dev, char *modalias,
  112. int size)
  113. {
  114. int len;
  115. int count;
  116. struct acpi_hardware_id *id;
  117. /* Avoid unnecessarily loading modules for non present devices. */
  118. if (!acpi_device_is_present(acpi_dev))
  119. return 0;
  120. /*
  121. * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
  122. * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
  123. * device's list.
  124. */
  125. count = 0;
  126. list_for_each_entry(id, &acpi_dev->pnp.ids, list)
  127. if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
  128. count++;
  129. if (!count)
  130. return 0;
  131. len = snprintf(modalias, size, "acpi:");
  132. if (len >= size)
  133. return -ENOMEM;
  134. size -= len;
  135. list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
  136. if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
  137. continue;
  138. count = snprintf(&modalias[len], size, "%s:", id->id);
  139. if (count >= size)
  140. return -ENOMEM;
  141. len += count;
  142. size -= count;
  143. }
  144. return len;
  145. }
  146. /**
  147. * create_of_modalias - Creates DT compatible string for modalias and uevent
  148. * @acpi_dev: ACPI device object.
  149. * @modalias: Buffer to print into.
  150. * @size: Size of the buffer.
  151. *
  152. * Expose DT compatible modalias as of:NnameTCcompatible. This function should
  153. * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
  154. * ACPI/PNP IDs.
  155. */
  156. static int create_of_modalias(const struct acpi_device *acpi_dev, char *modalias,
  157. int size)
  158. {
  159. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  160. const union acpi_object *of_compatible, *obj;
  161. acpi_status status;
  162. int len, count;
  163. int i, nval;
  164. char *c;
  165. status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
  166. if (ACPI_FAILURE(status))
  167. return -ENODEV;
  168. /* DT strings are all in lower case */
  169. for (c = buf.pointer; *c != '\0'; c++)
  170. *c = tolower(*c);
  171. len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
  172. ACPI_FREE(buf.pointer);
  173. if (len >= size)
  174. return -ENOMEM;
  175. size -= len;
  176. of_compatible = acpi_dev->data.of_compatible;
  177. if (of_compatible->type == ACPI_TYPE_PACKAGE) {
  178. nval = of_compatible->package.count;
  179. obj = of_compatible->package.elements;
  180. } else { /* Must be ACPI_TYPE_STRING. */
  181. nval = 1;
  182. obj = of_compatible;
  183. }
  184. for (i = 0; i < nval; i++, obj++) {
  185. count = snprintf(&modalias[len], size, "C%s",
  186. obj->string.pointer);
  187. if (count >= size)
  188. return -ENOMEM;
  189. len += count;
  190. size -= count;
  191. }
  192. return len;
  193. }
  194. int __acpi_device_uevent_modalias(const struct acpi_device *adev,
  195. struct kobj_uevent_env *env)
  196. {
  197. int len;
  198. if (!adev)
  199. return -ENODEV;
  200. if (list_empty(&adev->pnp.ids))
  201. return 0;
  202. if (add_uevent_var(env, "MODALIAS="))
  203. return -ENOMEM;
  204. if (adev->data.of_compatible)
  205. len = create_of_modalias(adev, &env->buf[env->buflen - 1],
  206. sizeof(env->buf) - env->buflen);
  207. else
  208. len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
  209. sizeof(env->buf) - env->buflen);
  210. if (len < 0)
  211. return len;
  212. env->buflen += len;
  213. return 0;
  214. }
  215. /**
  216. * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
  217. * @dev: Struct device to get ACPI device node.
  218. * @env: Environment variables of the kobject uevent.
  219. *
  220. * Create the uevent modalias field for ACPI-enumerated devices.
  221. *
  222. * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
  223. * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
  224. */
  225. int acpi_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *env)
  226. {
  227. return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
  228. }
  229. EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
  230. static int __acpi_device_modalias(const struct acpi_device *adev, char *buf, int size)
  231. {
  232. int len, count;
  233. if (!adev)
  234. return -ENODEV;
  235. if (list_empty(&adev->pnp.ids))
  236. return 0;
  237. len = create_pnp_modalias(adev, buf, size - 1);
  238. if (len < 0) {
  239. return len;
  240. } else if (len > 0) {
  241. buf[len++] = '\n';
  242. size -= len;
  243. }
  244. if (!adev->data.of_compatible)
  245. return len;
  246. count = create_of_modalias(adev, buf + len, size - 1);
  247. if (count < 0) {
  248. return count;
  249. } else if (count > 0) {
  250. len += count;
  251. buf[len++] = '\n';
  252. }
  253. return len;
  254. }
  255. /**
  256. * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
  257. * @dev: Struct device to get ACPI device node.
  258. * @buf: The buffer to save pnp_modalias and of_modalias.
  259. * @size: Size of buffer.
  260. *
  261. * Create the modalias sysfs attribute for ACPI-enumerated devices.
  262. *
  263. * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
  264. * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
  265. */
  266. int acpi_device_modalias(struct device *dev, char *buf, int size)
  267. {
  268. return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
  269. }
  270. EXPORT_SYMBOL_GPL(acpi_device_modalias);
  271. static ssize_t
  272. modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  273. {
  274. return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
  275. }
  276. static DEVICE_ATTR_RO(modalias);
  277. static ssize_t real_power_state_show(struct device *dev,
  278. struct device_attribute *attr, char *buf)
  279. {
  280. struct acpi_device *adev = to_acpi_device(dev);
  281. int state;
  282. int ret;
  283. ret = acpi_device_get_power(adev, &state);
  284. if (ret)
  285. return ret;
  286. return sysfs_emit(buf, "%s\n", acpi_power_state_string(state));
  287. }
  288. static DEVICE_ATTR_RO(real_power_state);
  289. static ssize_t power_state_show(struct device *dev,
  290. struct device_attribute *attr, char *buf)
  291. {
  292. struct acpi_device *adev = to_acpi_device(dev);
  293. return sysfs_emit(buf, "%s\n", acpi_power_state_string(adev->power.state));
  294. }
  295. static DEVICE_ATTR_RO(power_state);
  296. static ssize_t
  297. eject_store(struct device *d, struct device_attribute *attr,
  298. const char *buf, size_t count)
  299. {
  300. struct acpi_device *acpi_device = to_acpi_device(d);
  301. acpi_object_type not_used;
  302. acpi_status status;
  303. if (!count || buf[0] != '1')
  304. return -EINVAL;
  305. if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
  306. && !d->driver)
  307. return -ENODEV;
  308. status = acpi_get_type(acpi_device->handle, &not_used);
  309. if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
  310. return -ENODEV;
  311. acpi_dev_get(acpi_device);
  312. status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
  313. if (ACPI_SUCCESS(status))
  314. return count;
  315. acpi_dev_put(acpi_device);
  316. acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
  317. ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
  318. return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
  319. }
  320. static DEVICE_ATTR_WO(eject);
  321. static ssize_t
  322. hid_show(struct device *dev, struct device_attribute *attr, char *buf)
  323. {
  324. struct acpi_device *acpi_dev = to_acpi_device(dev);
  325. return sysfs_emit(buf, "%s\n", acpi_device_hid(acpi_dev));
  326. }
  327. static DEVICE_ATTR_RO(hid);
  328. static ssize_t cid_show(struct device *dev, struct device_attribute *attr,
  329. char *buf)
  330. {
  331. struct acpi_device *acpi_dev = to_acpi_device(dev);
  332. struct acpi_device_info *info = NULL;
  333. ssize_t len = 0;
  334. acpi_get_object_info(acpi_dev->handle, &info);
  335. if (!info)
  336. return 0;
  337. if (info->valid & ACPI_VALID_CID) {
  338. struct acpi_pnp_device_id_list *cid_list = &info->compatible_id_list;
  339. int i;
  340. for (i = 0; i < cid_list->count - 1; i++)
  341. len += sysfs_emit_at(buf, len, "%s,", cid_list->ids[i].string);
  342. len += sysfs_emit_at(buf, len, "%s\n", cid_list->ids[i].string);
  343. }
  344. kfree(info);
  345. return len;
  346. }
  347. static DEVICE_ATTR_RO(cid);
  348. static ssize_t uid_show(struct device *dev,
  349. struct device_attribute *attr, char *buf)
  350. {
  351. struct acpi_device *acpi_dev = to_acpi_device(dev);
  352. return sysfs_emit(buf, "%s\n", acpi_device_uid(acpi_dev));
  353. }
  354. static DEVICE_ATTR_RO(uid);
  355. static ssize_t adr_show(struct device *dev,
  356. struct device_attribute *attr, char *buf)
  357. {
  358. struct acpi_device *acpi_dev = to_acpi_device(dev);
  359. if (acpi_dev->pnp.bus_address > U32_MAX)
  360. return sysfs_emit(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
  361. else
  362. return sysfs_emit(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
  363. }
  364. static DEVICE_ATTR_RO(adr);
  365. static ssize_t path_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. struct acpi_device *acpi_dev = to_acpi_device(dev);
  369. return acpi_object_path(acpi_dev->handle, buf);
  370. }
  371. static DEVICE_ATTR_RO(path);
  372. /* sysfs file that shows description text from the ACPI _STR method */
  373. static ssize_t description_show(struct device *dev,
  374. struct device_attribute *attr,
  375. char *buf)
  376. {
  377. struct acpi_device *acpi_dev = to_acpi_device(dev);
  378. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  379. union acpi_object *str_obj;
  380. acpi_status status;
  381. int result;
  382. status = acpi_evaluate_object_typed(acpi_dev->handle, "_STR",
  383. NULL, &buffer,
  384. ACPI_TYPE_BUFFER);
  385. if (ACPI_FAILURE(status))
  386. return -EIO;
  387. str_obj = buffer.pointer;
  388. /*
  389. * The _STR object contains a Unicode identifier for a device.
  390. * We need to convert to utf-8 so it can be displayed.
  391. */
  392. result = utf16s_to_utf8s(
  393. (wchar_t *)str_obj->buffer.pointer,
  394. str_obj->buffer.length,
  395. UTF16_LITTLE_ENDIAN, buf,
  396. PAGE_SIZE - 1);
  397. buf[result++] = '\n';
  398. ACPI_FREE(str_obj);
  399. return result;
  400. }
  401. static DEVICE_ATTR_RO(description);
  402. static ssize_t
  403. sun_show(struct device *dev, struct device_attribute *attr,
  404. char *buf)
  405. {
  406. struct acpi_device *acpi_dev = to_acpi_device(dev);
  407. acpi_status status;
  408. unsigned long long sun;
  409. status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
  410. if (ACPI_FAILURE(status))
  411. return -EIO;
  412. return sysfs_emit(buf, "%llu\n", sun);
  413. }
  414. static DEVICE_ATTR_RO(sun);
  415. static ssize_t
  416. hrv_show(struct device *dev, struct device_attribute *attr,
  417. char *buf)
  418. {
  419. struct acpi_device *acpi_dev = to_acpi_device(dev);
  420. acpi_status status;
  421. unsigned long long hrv;
  422. status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
  423. if (ACPI_FAILURE(status))
  424. return -EIO;
  425. return sysfs_emit(buf, "%llu\n", hrv);
  426. }
  427. static DEVICE_ATTR_RO(hrv);
  428. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  429. char *buf)
  430. {
  431. struct acpi_device *acpi_dev = to_acpi_device(dev);
  432. acpi_status status;
  433. unsigned long long sta;
  434. status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
  435. if (ACPI_FAILURE(status))
  436. return -EIO;
  437. return sysfs_emit(buf, "%llu\n", sta);
  438. }
  439. static DEVICE_ATTR_RO(status);
  440. static struct attribute *acpi_attrs[] = {
  441. &dev_attr_path.attr,
  442. &dev_attr_hid.attr,
  443. &dev_attr_cid.attr,
  444. &dev_attr_modalias.attr,
  445. &dev_attr_description.attr,
  446. &dev_attr_adr.attr,
  447. &dev_attr_uid.attr,
  448. &dev_attr_sun.attr,
  449. &dev_attr_hrv.attr,
  450. &dev_attr_status.attr,
  451. &dev_attr_eject.attr,
  452. &dev_attr_power_state.attr,
  453. &dev_attr_real_power_state.attr,
  454. NULL
  455. };
  456. static bool acpi_show_attr(struct acpi_device *dev, const struct device_attribute *attr)
  457. {
  458. /*
  459. * Devices gotten from FADT don't have a "path" attribute
  460. */
  461. if (attr == &dev_attr_path)
  462. return dev->handle;
  463. if (attr == &dev_attr_hid || attr == &dev_attr_modalias)
  464. return !list_empty(&dev->pnp.ids);
  465. if (attr == &dev_attr_description)
  466. return acpi_has_method(dev->handle, "_STR");
  467. if (attr == &dev_attr_adr)
  468. return dev->pnp.type.bus_address;
  469. if (attr == &dev_attr_uid)
  470. return acpi_device_uid(dev);
  471. if (attr == &dev_attr_sun)
  472. return acpi_has_method(dev->handle, "_SUN");
  473. if (attr == &dev_attr_hrv)
  474. return acpi_has_method(dev->handle, "_HRV");
  475. if (attr == &dev_attr_status)
  476. return acpi_has_method(dev->handle, "_STA");
  477. if (attr == &dev_attr_cid)
  478. return acpi_has_method(dev->handle, "_CID");
  479. /*
  480. * If device has _EJ0, 'eject' file is created that is used to trigger
  481. * hot-removal function from userland.
  482. */
  483. if (attr == &dev_attr_eject)
  484. return acpi_has_method(dev->handle, "_EJ0");
  485. if (attr == &dev_attr_power_state)
  486. return dev->flags.power_manageable;
  487. if (attr == &dev_attr_real_power_state)
  488. return dev->flags.power_manageable && dev->power.flags.power_resources;
  489. dev_warn_once(&dev->dev, "Unexpected attribute: %s\n", attr->attr.name);
  490. return false;
  491. }
  492. static umode_t acpi_attr_is_visible(struct kobject *kobj,
  493. struct attribute *attr,
  494. int attrno)
  495. {
  496. struct acpi_device *dev = to_acpi_device(kobj_to_dev(kobj));
  497. if (acpi_show_attr(dev, container_of(attr, struct device_attribute, attr)))
  498. return attr->mode;
  499. else
  500. return 0;
  501. }
  502. static const struct attribute_group acpi_group = {
  503. .attrs = acpi_attrs,
  504. .is_visible = acpi_attr_is_visible,
  505. };
  506. const struct attribute_group *acpi_groups[] = {
  507. &acpi_group,
  508. NULL
  509. };
  510. /**
  511. * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
  512. * @dev: ACPI device object.
  513. */
  514. void acpi_device_setup_files(struct acpi_device *dev)
  515. {
  516. acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
  517. }
  518. /**
  519. * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
  520. * @dev: ACPI device object.
  521. */
  522. void acpi_device_remove_files(struct acpi_device *dev)
  523. {
  524. acpi_hide_nondev_subnodes(&dev->data);
  525. }