toshiba_haps.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, ret;
  66. if (sscanf(buf, "%d", &level) != 1 || level < 0 || level > 3)
  67. return -EINVAL;
  68. /* Set the sensor level.
  69. * Acceptable levels are:
  70. * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High
  71. */
  72. ret = toshiba_haps_protection_level(haps->acpi_dev->handle, level);
  73. if (ret != 0)
  74. return ret;
  75. haps->protection_level = level;
  76. return count;
  77. }
  78. static ssize_t reset_protection_store(struct device *dev,
  79. struct device_attribute *attr,
  80. const char *buf, size_t count)
  81. {
  82. struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
  83. int reset, ret;
  84. if (sscanf(buf, "%d", &reset) != 1 || reset != 1)
  85. return -EINVAL;
  86. /* Reset the protection interface */
  87. ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
  88. if (ret != 0)
  89. return ret;
  90. return count;
  91. }
  92. static DEVICE_ATTR(protection_level, S_IRUGO | S_IWUSR,
  93. protection_level_show, protection_level_store);
  94. static DEVICE_ATTR(reset_protection, S_IWUSR, NULL, reset_protection_store);
  95. static struct attribute *haps_attributes[] = {
  96. &dev_attr_protection_level.attr,
  97. &dev_attr_reset_protection.attr,
  98. NULL,
  99. };
  100. static struct attribute_group haps_attr_group = {
  101. .attrs = haps_attributes,
  102. };
  103. /*
  104. * ACPI stuff
  105. */
  106. static void toshiba_haps_notify(struct acpi_device *device, u32 event)
  107. {
  108. pr_info("Received event: 0x%x", event);
  109. acpi_bus_generate_netlink_event(device->pnp.device_class,
  110. dev_name(&device->dev),
  111. event, 0);
  112. }
  113. static int toshiba_haps_remove(struct acpi_device *device)
  114. {
  115. sysfs_remove_group(&device->dev.kobj, &haps_attr_group);
  116. if (toshiba_haps)
  117. toshiba_haps = NULL;
  118. return 0;
  119. }
  120. /* Helper function */
  121. static int toshiba_haps_available(acpi_handle handle)
  122. {
  123. acpi_status status;
  124. u64 hdd_present;
  125. /*
  126. * A non existent device as well as having (only)
  127. * Solid State Drives can cause the call to fail.
  128. */
  129. status = acpi_evaluate_integer(handle, "_STA", NULL,
  130. &hdd_present);
  131. if (ACPI_FAILURE(status) || !hdd_present) {
  132. pr_info("HDD protection not available or using SSD\n");
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. static int toshiba_haps_add(struct acpi_device *acpi_dev)
  138. {
  139. struct toshiba_haps_dev *haps;
  140. int ret;
  141. if (toshiba_haps)
  142. return -EBUSY;
  143. if (!toshiba_haps_available(acpi_dev->handle))
  144. return -ENODEV;
  145. pr_info("Toshiba HDD Active Protection Sensor device\n");
  146. haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL);
  147. if (!haps)
  148. return -ENOMEM;
  149. haps->acpi_dev = acpi_dev;
  150. haps->protection_level = 2;
  151. acpi_dev->driver_data = haps;
  152. dev_set_drvdata(&acpi_dev->dev, haps);
  153. /* Set the protection level, currently at level 2 (Medium) */
  154. ret = toshiba_haps_protection_level(acpi_dev->handle, 2);
  155. if (ret != 0)
  156. return ret;
  157. ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group);
  158. if (ret)
  159. return ret;
  160. toshiba_haps = haps;
  161. return 0;
  162. }
  163. #ifdef CONFIG_PM_SLEEP
  164. static int toshiba_haps_suspend(struct device *device)
  165. {
  166. struct toshiba_haps_dev *haps;
  167. int ret;
  168. haps = acpi_driver_data(to_acpi_device(device));
  169. /* Deactivate the protection on suspend */
  170. ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0);
  171. return ret;
  172. }
  173. static int toshiba_haps_resume(struct device *device)
  174. {
  175. struct toshiba_haps_dev *haps;
  176. int ret;
  177. haps = acpi_driver_data(to_acpi_device(device));
  178. /* Set the stored protection level */
  179. ret = toshiba_haps_protection_level(haps->acpi_dev->handle,
  180. haps->protection_level);
  181. /* Reset the protection on resume */
  182. ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
  183. if (ret != 0)
  184. return ret;
  185. return ret;
  186. }
  187. #endif
  188. static SIMPLE_DEV_PM_OPS(toshiba_haps_pm,
  189. toshiba_haps_suspend, toshiba_haps_resume);
  190. static const struct acpi_device_id haps_device_ids[] = {
  191. {"TOS620A", 0},
  192. {"", 0},
  193. };
  194. MODULE_DEVICE_TABLE(acpi, haps_device_ids);
  195. static struct acpi_driver toshiba_haps_driver = {
  196. .name = "Toshiba HAPS",
  197. .owner = THIS_MODULE,
  198. .ids = haps_device_ids,
  199. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  200. .ops = {
  201. .add = toshiba_haps_add,
  202. .remove = toshiba_haps_remove,
  203. .notify = toshiba_haps_notify,
  204. },
  205. .drv.pm = &toshiba_haps_pm,
  206. };
  207. module_acpi_driver(toshiba_haps_driver);