device_sysfs.c 16 KB

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