device_sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 <[email protected]>
  7. * Author: Rafael J. Wysocki <[email protected]>
  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 = sprintf(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 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(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 < 0)
  140. return -EINVAL;
  141. if (count >= size)
  142. return -ENOMEM;
  143. len += count;
  144. size -= count;
  145. }
  146. modalias[len] = '\0';
  147. return len;
  148. }
  149. /**
  150. * create_of_modalias - Creates DT compatible string for modalias and uevent
  151. * @acpi_dev: ACPI device object.
  152. * @modalias: Buffer to print into.
  153. * @size: Size of the buffer.
  154. *
  155. * Expose DT compatible modalias as of:NnameTCcompatible. This function should
  156. * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
  157. * ACPI/PNP IDs.
  158. */
  159. static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
  160. int size)
  161. {
  162. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  163. const union acpi_object *of_compatible, *obj;
  164. acpi_status status;
  165. int len, count;
  166. int i, nval;
  167. char *c;
  168. status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
  169. if (ACPI_FAILURE(status))
  170. return -ENODEV;
  171. /* DT strings are all in lower case */
  172. for (c = buf.pointer; *c != '\0'; c++)
  173. *c = tolower(*c);
  174. len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
  175. ACPI_FREE(buf.pointer);
  176. if (len >= size)
  177. return -ENOMEM;
  178. size -= len;
  179. of_compatible = acpi_dev->data.of_compatible;
  180. if (of_compatible->type == ACPI_TYPE_PACKAGE) {
  181. nval = of_compatible->package.count;
  182. obj = of_compatible->package.elements;
  183. } else { /* Must be ACPI_TYPE_STRING. */
  184. nval = 1;
  185. obj = of_compatible;
  186. }
  187. for (i = 0; i < nval; i++, obj++) {
  188. count = snprintf(&modalias[len], size, "C%s",
  189. obj->string.pointer);
  190. if (count < 0)
  191. return -EINVAL;
  192. if (count >= size)
  193. return -ENOMEM;
  194. len += count;
  195. size -= count;
  196. }
  197. modalias[len] = '\0';
  198. return len;
  199. }
  200. int __acpi_device_uevent_modalias(struct acpi_device *adev,
  201. struct kobj_uevent_env *env)
  202. {
  203. int len;
  204. if (!adev)
  205. return -ENODEV;
  206. if (list_empty(&adev->pnp.ids))
  207. return 0;
  208. if (add_uevent_var(env, "MODALIAS="))
  209. return -ENOMEM;
  210. if (adev->data.of_compatible)
  211. len = create_of_modalias(adev, &env->buf[env->buflen - 1],
  212. sizeof(env->buf) - env->buflen);
  213. else
  214. len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
  215. sizeof(env->buf) - env->buflen);
  216. if (len < 0)
  217. return len;
  218. env->buflen += len;
  219. return 0;
  220. }
  221. /**
  222. * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
  223. * @dev: Struct device to get ACPI device node.
  224. * @env: Environment variables of the kobject uevent.
  225. *
  226. * Create the uevent modalias field for ACPI-enumerated devices.
  227. *
  228. * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
  229. * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
  230. */
  231. int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  232. {
  233. return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
  234. }
  235. EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
  236. static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
  237. {
  238. int len, count;
  239. if (!adev)
  240. return -ENODEV;
  241. if (list_empty(&adev->pnp.ids))
  242. return 0;
  243. len = create_pnp_modalias(adev, buf, size - 1);
  244. if (len < 0) {
  245. return len;
  246. } else if (len > 0) {
  247. buf[len++] = '\n';
  248. size -= len;
  249. }
  250. if (!adev->data.of_compatible)
  251. return len;
  252. count = create_of_modalias(adev, buf + len, size - 1);
  253. if (count < 0) {
  254. return count;
  255. } else if (count > 0) {
  256. len += count;
  257. buf[len++] = '\n';
  258. }
  259. return len;
  260. }
  261. /**
  262. * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
  263. * @dev: Struct device to get ACPI device node.
  264. * @buf: The buffer to save pnp_modalias and of_modalias.
  265. * @size: Size of buffer.
  266. *
  267. * Create the modalias sysfs attribute for ACPI-enumerated devices.
  268. *
  269. * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
  270. * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
  271. */
  272. int acpi_device_modalias(struct device *dev, char *buf, int size)
  273. {
  274. return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
  275. }
  276. EXPORT_SYMBOL_GPL(acpi_device_modalias);
  277. static ssize_t
  278. modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  279. {
  280. return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
  281. }
  282. static DEVICE_ATTR_RO(modalias);
  283. static ssize_t real_power_state_show(struct device *dev,
  284. struct device_attribute *attr, char *buf)
  285. {
  286. struct acpi_device *adev = to_acpi_device(dev);
  287. int state;
  288. int ret;
  289. ret = acpi_device_get_power(adev, &state);
  290. if (ret)
  291. return ret;
  292. return sprintf(buf, "%s\n", acpi_power_state_string(state));
  293. }
  294. static DEVICE_ATTR_RO(real_power_state);
  295. static ssize_t power_state_show(struct device *dev,
  296. struct device_attribute *attr, char *buf)
  297. {
  298. struct acpi_device *adev = to_acpi_device(dev);
  299. return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
  300. }
  301. static DEVICE_ATTR_RO(power_state);
  302. static ssize_t
  303. eject_store(struct device *d, struct device_attribute *attr,
  304. const char *buf, size_t count)
  305. {
  306. struct acpi_device *acpi_device = to_acpi_device(d);
  307. acpi_object_type not_used;
  308. acpi_status status;
  309. if (!count || buf[0] != '1')
  310. return -EINVAL;
  311. if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
  312. && !d->driver)
  313. return -ENODEV;
  314. status = acpi_get_type(acpi_device->handle, &not_used);
  315. if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
  316. return -ENODEV;
  317. acpi_dev_get(acpi_device);
  318. status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
  319. if (ACPI_SUCCESS(status))
  320. return count;
  321. acpi_dev_put(acpi_device);
  322. acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
  323. ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
  324. return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
  325. }
  326. static DEVICE_ATTR_WO(eject);
  327. static ssize_t
  328. hid_show(struct device *dev, struct device_attribute *attr, char *buf)
  329. {
  330. struct acpi_device *acpi_dev = to_acpi_device(dev);
  331. return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
  332. }
  333. static DEVICE_ATTR_RO(hid);
  334. static ssize_t uid_show(struct device *dev,
  335. struct device_attribute *attr, char *buf)
  336. {
  337. struct acpi_device *acpi_dev = to_acpi_device(dev);
  338. return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
  339. }
  340. static DEVICE_ATTR_RO(uid);
  341. static ssize_t adr_show(struct device *dev,
  342. struct device_attribute *attr, char *buf)
  343. {
  344. struct acpi_device *acpi_dev = to_acpi_device(dev);
  345. if (acpi_dev->pnp.bus_address > U32_MAX)
  346. return sprintf(buf, "0x%016llx\n", acpi_dev->pnp.bus_address);
  347. else
  348. return sprintf(buf, "0x%08llx\n", acpi_dev->pnp.bus_address);
  349. }
  350. static DEVICE_ATTR_RO(adr);
  351. static ssize_t path_show(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. struct acpi_device *acpi_dev = to_acpi_device(dev);
  355. return acpi_object_path(acpi_dev->handle, buf);
  356. }
  357. static DEVICE_ATTR_RO(path);
  358. /* sysfs file that shows description text from the ACPI _STR method */
  359. static ssize_t description_show(struct device *dev,
  360. struct device_attribute *attr,
  361. char *buf)
  362. {
  363. struct acpi_device *acpi_dev = to_acpi_device(dev);
  364. int result;
  365. if (acpi_dev->pnp.str_obj == NULL)
  366. return 0;
  367. /*
  368. * The _STR object contains a Unicode identifier for a device.
  369. * We need to convert to utf-8 so it can be displayed.
  370. */
  371. result = utf16s_to_utf8s(
  372. (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
  373. acpi_dev->pnp.str_obj->buffer.length,
  374. UTF16_LITTLE_ENDIAN, buf,
  375. PAGE_SIZE - 1);
  376. buf[result++] = '\n';
  377. return result;
  378. }
  379. static DEVICE_ATTR_RO(description);
  380. static ssize_t
  381. sun_show(struct device *dev, struct device_attribute *attr,
  382. char *buf)
  383. {
  384. struct acpi_device *acpi_dev = to_acpi_device(dev);
  385. acpi_status status;
  386. unsigned long long sun;
  387. status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
  388. if (ACPI_FAILURE(status))
  389. return -EIO;
  390. return sprintf(buf, "%llu\n", sun);
  391. }
  392. static DEVICE_ATTR_RO(sun);
  393. static ssize_t
  394. hrv_show(struct device *dev, struct device_attribute *attr,
  395. char *buf)
  396. {
  397. struct acpi_device *acpi_dev = to_acpi_device(dev);
  398. acpi_status status;
  399. unsigned long long hrv;
  400. status = acpi_evaluate_integer(acpi_dev->handle, "_HRV", NULL, &hrv);
  401. if (ACPI_FAILURE(status))
  402. return -EIO;
  403. return sprintf(buf, "%llu\n", hrv);
  404. }
  405. static DEVICE_ATTR_RO(hrv);
  406. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  407. char *buf)
  408. {
  409. struct acpi_device *acpi_dev = to_acpi_device(dev);
  410. acpi_status status;
  411. unsigned long long sta;
  412. status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
  413. if (ACPI_FAILURE(status))
  414. return -EIO;
  415. return sprintf(buf, "%llu\n", sta);
  416. }
  417. static DEVICE_ATTR_RO(status);
  418. /**
  419. * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
  420. * @dev: ACPI device object.
  421. */
  422. int acpi_device_setup_files(struct acpi_device *dev)
  423. {
  424. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  425. acpi_status status;
  426. int result = 0;
  427. /*
  428. * Devices gotten from FADT don't have a "path" attribute
  429. */
  430. if (dev->handle) {
  431. result = device_create_file(&dev->dev, &dev_attr_path);
  432. if (result)
  433. goto end;
  434. }
  435. if (!list_empty(&dev->pnp.ids)) {
  436. result = device_create_file(&dev->dev, &dev_attr_hid);
  437. if (result)
  438. goto end;
  439. result = device_create_file(&dev->dev, &dev_attr_modalias);
  440. if (result)
  441. goto end;
  442. }
  443. /*
  444. * If device has _STR, 'description' file is created
  445. */
  446. if (acpi_has_method(dev->handle, "_STR")) {
  447. status = acpi_evaluate_object(dev->handle, "_STR",
  448. NULL, &buffer);
  449. if (ACPI_FAILURE(status))
  450. buffer.pointer = NULL;
  451. dev->pnp.str_obj = buffer.pointer;
  452. result = device_create_file(&dev->dev, &dev_attr_description);
  453. if (result)
  454. goto end;
  455. }
  456. if (dev->pnp.type.bus_address)
  457. result = device_create_file(&dev->dev, &dev_attr_adr);
  458. if (dev->pnp.unique_id)
  459. result = device_create_file(&dev->dev, &dev_attr_uid);
  460. if (acpi_has_method(dev->handle, "_SUN")) {
  461. result = device_create_file(&dev->dev, &dev_attr_sun);
  462. if (result)
  463. goto end;
  464. }
  465. if (acpi_has_method(dev->handle, "_HRV")) {
  466. result = device_create_file(&dev->dev, &dev_attr_hrv);
  467. if (result)
  468. goto end;
  469. }
  470. if (acpi_has_method(dev->handle, "_STA")) {
  471. result = device_create_file(&dev->dev, &dev_attr_status);
  472. if (result)
  473. goto end;
  474. }
  475. /*
  476. * If device has _EJ0, 'eject' file is created that is used to trigger
  477. * hot-removal function from userland.
  478. */
  479. if (acpi_has_method(dev->handle, "_EJ0")) {
  480. result = device_create_file(&dev->dev, &dev_attr_eject);
  481. if (result)
  482. return result;
  483. }
  484. if (dev->flags.power_manageable) {
  485. result = device_create_file(&dev->dev, &dev_attr_power_state);
  486. if (result)
  487. return result;
  488. if (dev->power.flags.power_resources)
  489. result = device_create_file(&dev->dev,
  490. &dev_attr_real_power_state);
  491. }
  492. acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
  493. end:
  494. return result;
  495. }
  496. /**
  497. * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
  498. * @dev: ACPI device object.
  499. */
  500. void acpi_device_remove_files(struct acpi_device *dev)
  501. {
  502. acpi_hide_nondev_subnodes(&dev->data);
  503. if (dev->flags.power_manageable) {
  504. device_remove_file(&dev->dev, &dev_attr_power_state);
  505. if (dev->power.flags.power_resources)
  506. device_remove_file(&dev->dev,
  507. &dev_attr_real_power_state);
  508. }
  509. /*
  510. * If device has _STR, remove 'description' file
  511. */
  512. if (acpi_has_method(dev->handle, "_STR")) {
  513. kfree(dev->pnp.str_obj);
  514. device_remove_file(&dev->dev, &dev_attr_description);
  515. }
  516. /*
  517. * If device has _EJ0, remove 'eject' file.
  518. */
  519. if (acpi_has_method(dev->handle, "_EJ0"))
  520. device_remove_file(&dev->dev, &dev_attr_eject);
  521. if (acpi_has_method(dev->handle, "_SUN"))
  522. device_remove_file(&dev->dev, &dev_attr_sun);
  523. if (acpi_has_method(dev->handle, "_HRV"))
  524. device_remove_file(&dev->dev, &dev_attr_hrv);
  525. if (dev->pnp.unique_id)
  526. device_remove_file(&dev->dev, &dev_attr_uid);
  527. if (dev->pnp.type.bus_address)
  528. device_remove_file(&dev->dev, &dev_attr_adr);
  529. device_remove_file(&dev->dev, &dev_attr_modalias);
  530. device_remove_file(&dev->dev, &dev_attr_hid);
  531. if (acpi_has_method(dev->handle, "_STA"))
  532. device_remove_file(&dev->dev, &dev_attr_status);
  533. if (dev->handle)
  534. device_remove_file(&dev->dev, &dev_attr_path);
  535. }