device_sysfs.c 16 KB

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