toshiba_haps.c 6.1 KB

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