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 18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 20 #include <linux/kernel.h> 21 #include <linux/module.h> 22 #include <linux/init.h> 23 #include <linux/types.h> 24 #include <linux/acpi.h> 25 26 MODULE_AUTHOR("Azael Avalos <coproscefalo@gmail.com>"); 27 MODULE_DESCRIPTION("Toshiba HDD Active Protection Sensor"); 28 MODULE_LICENSE("GPL"); 29 30 struct toshiba_haps_dev { 31 struct acpi_device *acpi_dev; 32 33 int protection_level; 34 }; 35 36 static struct toshiba_haps_dev *toshiba_haps; 37 38 /* HAPS functions */ 39 static int toshiba_haps_reset_protection(acpi_handle handle) 40 { 41 acpi_status status; 42 43 status = acpi_evaluate_object(handle, "RSSS", NULL, NULL); 44 if (ACPI_FAILURE(status)) { 45 pr_err("Unable to reset the HDD protection\n"); 46 return -EIO; 47 } 48 49 return 0; 50 } 51 52 static int toshiba_haps_protection_level(acpi_handle handle, int level) 53 { 54 acpi_status status; 55 56 status = acpi_execute_simple_method(handle, "PTLV", level); 57 if (ACPI_FAILURE(status)) { 58 pr_err("Error while setting the protection level\n"); 59 return -EIO; 60 } 61 62 pr_info("HDD protection level set to: %d\n", level); 63 64 return 0; 65 } 66 67 /* sysfs files */ 68 static ssize_t protection_level_show(struct device *dev, 69 struct device_attribute *attr, char *buf) 70 { 71 struct toshiba_haps_dev *haps = dev_get_drvdata(dev); 72 73 return sprintf(buf, "%i\n", haps->protection_level); 74 } 75 76 static ssize_t protection_level_store(struct device *dev, 77 struct device_attribute *attr, 78 const char *buf, size_t count) 79 { 80 struct toshiba_haps_dev *haps = dev_get_drvdata(dev); 81 int level, ret; 82 83 if (sscanf(buf, "%d", &level) != 1 || level < 0 || level > 3) 84 return -EINVAL; 85 86 /* Set the sensor level. 87 * Acceptable levels are: 88 * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High 89 */ 90 ret = toshiba_haps_protection_level(haps->acpi_dev->handle, level); 91 if (ret != 0) 92 return ret; 93 94 haps->protection_level = level; 95 96 return count; 97 } 98 99 static ssize_t reset_protection_store(struct device *dev, 100 struct device_attribute *attr, 101 const char *buf, size_t count) 102 { 103 struct toshiba_haps_dev *haps = dev_get_drvdata(dev); 104 int reset, ret; 105 106 if (sscanf(buf, "%d", &reset) != 1 || reset != 1) 107 return -EINVAL; 108 109 /* Reset the protection interface */ 110 ret = toshiba_haps_reset_protection(haps->acpi_dev->handle); 111 if (ret != 0) 112 return ret; 113 114 return count; 115 } 116 117 static DEVICE_ATTR(protection_level, S_IRUGO | S_IWUSR, 118 protection_level_show, protection_level_store); 119 static DEVICE_ATTR(reset_protection, S_IWUSR, NULL, reset_protection_store); 120 121 static struct attribute *haps_attributes[] = { 122 &dev_attr_protection_level.attr, 123 &dev_attr_reset_protection.attr, 124 NULL, 125 }; 126 127 static struct attribute_group haps_attr_group = { 128 .attrs = haps_attributes, 129 }; 130 131 /* 132 * ACPI stuff 133 */ 134 static void toshiba_haps_notify(struct acpi_device *device, u32 event) 135 { 136 pr_info("Received event: 0x%x", event); 137 138 acpi_bus_generate_netlink_event(device->pnp.device_class, 139 dev_name(&device->dev), 140 event, 0); 141 } 142 143 static int toshiba_haps_remove(struct acpi_device *device) 144 { 145 sysfs_remove_group(&device->dev.kobj, &haps_attr_group); 146 147 if (toshiba_haps) 148 toshiba_haps = NULL; 149 150 return 0; 151 } 152 153 /* Helper function */ 154 static int toshiba_haps_available(acpi_handle handle) 155 { 156 acpi_status status; 157 u64 hdd_present; 158 159 /* 160 * A non existent device as well as having (only) 161 * Solid State Drives can cause the call to fail. 162 */ 163 status = acpi_evaluate_integer(handle, "_STA", NULL, 164 &hdd_present); 165 if (ACPI_FAILURE(status) || !hdd_present) { 166 pr_info("HDD protection not available or using SSD\n"); 167 return 0; 168 } 169 170 return 1; 171 } 172 173 static int toshiba_haps_add(struct acpi_device *acpi_dev) 174 { 175 struct toshiba_haps_dev *haps; 176 int ret; 177 178 if (toshiba_haps) 179 return -EBUSY; 180 181 if (!toshiba_haps_available(acpi_dev->handle)) 182 return -ENODEV; 183 184 pr_info("Toshiba HDD Active Protection Sensor device\n"); 185 186 haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL); 187 if (!haps) 188 return -ENOMEM; 189 190 haps->acpi_dev = acpi_dev; 191 haps->protection_level = 2; 192 acpi_dev->driver_data = haps; 193 dev_set_drvdata(&acpi_dev->dev, haps); 194 195 /* Set the protection level, currently at level 2 (Medium) */ 196 ret = toshiba_haps_protection_level(acpi_dev->handle, 2); 197 if (ret != 0) 198 return ret; 199 200 ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group); 201 if (ret) 202 return ret; 203 204 toshiba_haps = haps; 205 206 return 0; 207 } 208 209 #ifdef CONFIG_PM_SLEEP 210 static int toshiba_haps_suspend(struct device *device) 211 { 212 struct toshiba_haps_dev *haps; 213 int ret; 214 215 haps = acpi_driver_data(to_acpi_device(device)); 216 217 /* Deactivate the protection on suspend */ 218 ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0); 219 220 return ret; 221 } 222 223 static int toshiba_haps_resume(struct device *device) 224 { 225 struct toshiba_haps_dev *haps; 226 int ret; 227 228 haps = acpi_driver_data(to_acpi_device(device)); 229 230 /* Set the stored protection level */ 231 ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 232 haps->protection_level); 233 234 /* Reset the protection on resume */ 235 ret = toshiba_haps_reset_protection(haps->acpi_dev->handle); 236 if (ret != 0) 237 return ret; 238 239 return ret; 240 } 241 #endif 242 243 static SIMPLE_DEV_PM_OPS(toshiba_haps_pm, 244 toshiba_haps_suspend, toshiba_haps_resume); 245 246 static const struct acpi_device_id haps_device_ids[] = { 247 {"TOS620A", 0}, 248 {"", 0}, 249 }; 250 MODULE_DEVICE_TABLE(acpi, haps_device_ids); 251 252 static struct acpi_driver toshiba_haps_driver = { 253 .name = "Toshiba HAPS", 254 .owner = THIS_MODULE, 255 .ids = haps_device_ids, 256 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, 257 .ops = { 258 .add = toshiba_haps_add, 259 .remove = toshiba_haps_remove, 260 .notify = toshiba_haps_notify, 261 }, 262 .drv.pm = &toshiba_haps_pm, 263 }; 264 265 module_acpi_driver(toshiba_haps_driver); 266