toshiba_haps.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_debug("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_debug("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, &hdd_present);
  139. if (ACPI_FAILURE(status)) {
  140. pr_err("ACPI call to query HDD protection failed\n");
  141. return 0;
  142. }
  143. if (!hdd_present) {
  144. pr_info("HDD protection not available or using SSD\n");
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. static int toshiba_haps_add(struct acpi_device *acpi_dev)
  150. {
  151. struct toshiba_haps_dev *haps;
  152. int ret;
  153. if (toshiba_haps)
  154. return -EBUSY;
  155. if (!toshiba_haps_available(acpi_dev->handle))
  156. return -ENODEV;
  157. pr_info("Toshiba HDD Active Protection Sensor device\n");
  158. haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL);
  159. if (!haps)
  160. return -ENOMEM;
  161. haps->acpi_dev = acpi_dev;
  162. haps->protection_level = 2;
  163. acpi_dev->driver_data = haps;
  164. dev_set_drvdata(&acpi_dev->dev, haps);
  165. /* Set the protection level, currently at level 2 (Medium) */
  166. ret = toshiba_haps_protection_level(acpi_dev->handle, 2);
  167. if (ret != 0)
  168. return ret;
  169. ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group);
  170. if (ret)
  171. return ret;
  172. toshiba_haps = haps;
  173. return 0;
  174. }
  175. #ifdef CONFIG_PM_SLEEP
  176. static int toshiba_haps_suspend(struct device *device)
  177. {
  178. struct toshiba_haps_dev *haps;
  179. int ret;
  180. haps = acpi_driver_data(to_acpi_device(device));
  181. /* Deactivate the protection on suspend */
  182. ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0);
  183. return ret;
  184. }
  185. static int toshiba_haps_resume(struct device *device)
  186. {
  187. struct toshiba_haps_dev *haps;
  188. int ret;
  189. haps = acpi_driver_data(to_acpi_device(device));
  190. /* Set the stored protection level */
  191. ret = toshiba_haps_protection_level(haps->acpi_dev->handle,
  192. haps->protection_level);
  193. /* Reset the protection on resume */
  194. ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
  195. if (ret != 0)
  196. return ret;
  197. return ret;
  198. }
  199. #endif
  200. static SIMPLE_DEV_PM_OPS(toshiba_haps_pm,
  201. toshiba_haps_suspend, toshiba_haps_resume);
  202. static const struct acpi_device_id haps_device_ids[] = {
  203. {"TOS620A", 0},
  204. {"", 0},
  205. };
  206. MODULE_DEVICE_TABLE(acpi, haps_device_ids);
  207. static struct acpi_driver toshiba_haps_driver = {
  208. .name = "Toshiba HAPS",
  209. .owner = THIS_MODULE,
  210. .ids = haps_device_ids,
  211. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  212. .ops = {
  213. .add = toshiba_haps_add,
  214. .remove = toshiba_haps_remove,
  215. .notify = toshiba_haps_notify,
  216. },
  217. .drv.pm = &toshiba_haps_pm,
  218. };
  219. module_acpi_driver(toshiba_haps_driver);