toshiba_haps.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Toshiba HDD Active Protection Sensor (HAPS) driver
  4. *
  5. * Copyright (C) 2014 Azael Avalos <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/acpi.h>
  13. MODULE_AUTHOR("Azael Avalos <[email protected]>");
  14. MODULE_DESCRIPTION("Toshiba HDD Active Protection Sensor");
  15. MODULE_LICENSE("GPL");
  16. struct toshiba_haps_dev {
  17. struct acpi_device *acpi_dev;
  18. int protection_level;
  19. };
  20. static struct toshiba_haps_dev *toshiba_haps;
  21. /* HAPS functions */
  22. static int toshiba_haps_reset_protection(acpi_handle handle)
  23. {
  24. acpi_status status;
  25. status = acpi_evaluate_object(handle, "RSSS", NULL, NULL);
  26. if (ACPI_FAILURE(status)) {
  27. pr_err("Unable to reset the HDD protection\n");
  28. return -EIO;
  29. }
  30. return 0;
  31. }
  32. static int toshiba_haps_protection_level(acpi_handle handle, int level)
  33. {
  34. acpi_status status;
  35. status = acpi_execute_simple_method(handle, "PTLV", level);
  36. if (ACPI_FAILURE(status)) {
  37. pr_err("Error while setting the protection level\n");
  38. return -EIO;
  39. }
  40. pr_debug("HDD protection level set to: %d\n", level);
  41. return 0;
  42. }
  43. /* sysfs files */
  44. static ssize_t protection_level_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
  48. return sprintf(buf, "%i\n", haps->protection_level);
  49. }
  50. static ssize_t protection_level_store(struct device *dev,
  51. struct device_attribute *attr,
  52. const char *buf, size_t count)
  53. {
  54. struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
  55. int level;
  56. int ret;
  57. ret = kstrtoint(buf, 0, &level);
  58. if (ret)
  59. return ret;
  60. /*
  61. * Check for supported levels, which can be:
  62. * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High
  63. */
  64. if (level < 0 || level > 3)
  65. return -EINVAL;
  66. /* Set the sensor level */
  67. ret = toshiba_haps_protection_level(haps->acpi_dev->handle, level);
  68. if (ret != 0)
  69. return ret;
  70. haps->protection_level = level;
  71. return count;
  72. }
  73. static DEVICE_ATTR_RW(protection_level);
  74. static ssize_t reset_protection_store(struct device *dev,
  75. struct device_attribute *attr,
  76. const char *buf, size_t count)
  77. {
  78. struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
  79. int reset;
  80. int ret;
  81. ret = kstrtoint(buf, 0, &reset);
  82. if (ret)
  83. return ret;
  84. /* The only accepted value is 1 */
  85. if (reset != 1)
  86. return -EINVAL;
  87. /* Reset the protection interface */
  88. ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
  89. if (ret != 0)
  90. return ret;
  91. return count;
  92. }
  93. static DEVICE_ATTR_WO(reset_protection);
  94. static struct attribute *haps_attributes[] = {
  95. &dev_attr_protection_level.attr,
  96. &dev_attr_reset_protection.attr,
  97. NULL,
  98. };
  99. static const struct attribute_group haps_attr_group = {
  100. .attrs = haps_attributes,
  101. };
  102. /*
  103. * ACPI stuff
  104. */
  105. static void toshiba_haps_notify(struct acpi_device *device, u32 event)
  106. {
  107. pr_debug("Received event: 0x%x\n", event);
  108. acpi_bus_generate_netlink_event(device->pnp.device_class,
  109. dev_name(&device->dev),
  110. event, 0);
  111. }
  112. static int toshiba_haps_remove(struct acpi_device *device)
  113. {
  114. sysfs_remove_group(&device->dev.kobj, &haps_attr_group);
  115. if (toshiba_haps)
  116. toshiba_haps = NULL;
  117. return 0;
  118. }
  119. /* Helper function */
  120. static int toshiba_haps_available(acpi_handle handle)
  121. {
  122. acpi_status status;
  123. u64 hdd_present;
  124. /*
  125. * A non existent device as well as having (only)
  126. * Solid State Drives can cause the call to fail.
  127. */
  128. status = acpi_evaluate_integer(handle, "_STA", NULL, &hdd_present);
  129. if (ACPI_FAILURE(status)) {
  130. pr_err("ACPI call to query HDD protection failed\n");
  131. return 0;
  132. }
  133. if (!hdd_present) {
  134. pr_info("HDD protection not available or using SSD\n");
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. static int toshiba_haps_add(struct acpi_device *acpi_dev)
  140. {
  141. struct toshiba_haps_dev *haps;
  142. int ret;
  143. if (toshiba_haps)
  144. return -EBUSY;
  145. if (!toshiba_haps_available(acpi_dev->handle))
  146. return -ENODEV;
  147. pr_info("Toshiba HDD Active Protection Sensor device\n");
  148. haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL);
  149. if (!haps)
  150. return -ENOMEM;
  151. haps->acpi_dev = acpi_dev;
  152. haps->protection_level = 2;
  153. acpi_dev->driver_data = haps;
  154. dev_set_drvdata(&acpi_dev->dev, haps);
  155. /* Set the protection level, currently at level 2 (Medium) */
  156. ret = toshiba_haps_protection_level(acpi_dev->handle, 2);
  157. if (ret != 0)
  158. return ret;
  159. ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group);
  160. if (ret)
  161. return ret;
  162. toshiba_haps = haps;
  163. return 0;
  164. }
  165. #ifdef CONFIG_PM_SLEEP
  166. static int toshiba_haps_suspend(struct device *device)
  167. {
  168. struct toshiba_haps_dev *haps;
  169. int ret;
  170. haps = acpi_driver_data(to_acpi_device(device));
  171. /* Deactivate the protection on suspend */
  172. ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0);
  173. return ret;
  174. }
  175. static int toshiba_haps_resume(struct device *device)
  176. {
  177. struct toshiba_haps_dev *haps;
  178. int ret;
  179. haps = acpi_driver_data(to_acpi_device(device));
  180. /* Set the stored protection level */
  181. ret = toshiba_haps_protection_level(haps->acpi_dev->handle,
  182. haps->protection_level);
  183. /* Reset the protection on resume */
  184. ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
  185. if (ret != 0)
  186. return ret;
  187. return ret;
  188. }
  189. #endif
  190. static SIMPLE_DEV_PM_OPS(toshiba_haps_pm,
  191. toshiba_haps_suspend, toshiba_haps_resume);
  192. static const struct acpi_device_id haps_device_ids[] = {
  193. {"TOS620A", 0},
  194. {"", 0},
  195. };
  196. MODULE_DEVICE_TABLE(acpi, haps_device_ids);
  197. static struct acpi_driver toshiba_haps_driver = {
  198. .name = "Toshiba HAPS",
  199. .owner = THIS_MODULE,
  200. .ids = haps_device_ids,
  201. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  202. .ops = {
  203. .add = toshiba_haps_add,
  204. .remove = toshiba_haps_remove,
  205. .notify = toshiba_haps_notify,
  206. },
  207. .drv.pm = &toshiba_haps_pm,
  208. };
  209. module_acpi_driver(toshiba_haps_driver);