device_sysfs.c 14 KB

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