x86/mm: Add TLB purge to free pmd/pte page interfaces
[linux/fpc-iii.git] / drivers / platform / x86 / toshiba_haps.c
blobb3dec521e2b650069dbb54f2f064fbb4e1d1c5db
1 /*
2 * Toshiba HDD Active Protection Sensor (HAPS) driver
4 * Copyright (C) 2014 Azael Avalos <coproscefalo@gmail.com>
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.
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.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
26 MODULE_AUTHOR("Azael Avalos <coproscefalo@gmail.com>");
27 MODULE_DESCRIPTION("Toshiba HDD Active Protection Sensor");
28 MODULE_LICENSE("GPL");
30 struct toshiba_haps_dev {
31 struct acpi_device *acpi_dev;
33 int protection_level;
36 static struct toshiba_haps_dev *toshiba_haps;
38 /* HAPS functions */
39 static int toshiba_haps_reset_protection(acpi_handle handle)
41 acpi_status status;
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;
49 return 0;
52 static int toshiba_haps_protection_level(acpi_handle handle, int level)
54 acpi_status status;
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;
62 pr_debug("HDD protection level set to: %d\n", level);
64 return 0;
67 /* sysfs files */
68 static ssize_t protection_level_show(struct device *dev,
69 struct device_attribute *attr, char *buf)
71 struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
73 return sprintf(buf, "%i\n", haps->protection_level);
76 static ssize_t protection_level_store(struct device *dev,
77 struct device_attribute *attr,
78 const char *buf, size_t count)
80 struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
81 int level;
82 int ret;
84 ret = kstrtoint(buf, 0, &level);
85 if (ret)
86 return ret;
88 * Check for supported levels, which can be:
89 * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High
91 if (level < 0 || level > 3)
92 return -EINVAL;
94 /* Set the sensor level */
95 ret = toshiba_haps_protection_level(haps->acpi_dev->handle, level);
96 if (ret != 0)
97 return ret;
99 haps->protection_level = level;
101 return count;
103 static DEVICE_ATTR_RW(protection_level);
105 static ssize_t reset_protection_store(struct device *dev,
106 struct device_attribute *attr,
107 const char *buf, size_t count)
109 struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
110 int reset;
111 int ret;
113 ret = kstrtoint(buf, 0, &reset);
114 if (ret)
115 return ret;
116 /* The only accepted value is 1 */
117 if (reset != 1)
118 return -EINVAL;
120 /* Reset the protection interface */
121 ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
122 if (ret != 0)
123 return ret;
125 return count;
127 static DEVICE_ATTR_WO(reset_protection);
129 static struct attribute *haps_attributes[] = {
130 &dev_attr_protection_level.attr,
131 &dev_attr_reset_protection.attr,
132 NULL,
135 static struct attribute_group haps_attr_group = {
136 .attrs = haps_attributes,
140 * ACPI stuff
142 static void toshiba_haps_notify(struct acpi_device *device, u32 event)
144 pr_debug("Received event: 0x%x", event);
146 acpi_bus_generate_netlink_event(device->pnp.device_class,
147 dev_name(&device->dev),
148 event, 0);
151 static int toshiba_haps_remove(struct acpi_device *device)
153 sysfs_remove_group(&device->dev.kobj, &haps_attr_group);
155 if (toshiba_haps)
156 toshiba_haps = NULL;
158 return 0;
161 /* Helper function */
162 static int toshiba_haps_available(acpi_handle handle)
164 acpi_status status;
165 u64 hdd_present;
168 * A non existent device as well as having (only)
169 * Solid State Drives can cause the call to fail.
171 status = acpi_evaluate_integer(handle, "_STA", NULL, &hdd_present);
172 if (ACPI_FAILURE(status)) {
173 pr_err("ACPI call to query HDD protection failed\n");
174 return 0;
177 if (!hdd_present) {
178 pr_info("HDD protection not available or using SSD\n");
179 return 0;
182 return 1;
185 static int toshiba_haps_add(struct acpi_device *acpi_dev)
187 struct toshiba_haps_dev *haps;
188 int ret;
190 if (toshiba_haps)
191 return -EBUSY;
193 if (!toshiba_haps_available(acpi_dev->handle))
194 return -ENODEV;
196 pr_info("Toshiba HDD Active Protection Sensor device\n");
198 haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL);
199 if (!haps)
200 return -ENOMEM;
202 haps->acpi_dev = acpi_dev;
203 haps->protection_level = 2;
204 acpi_dev->driver_data = haps;
205 dev_set_drvdata(&acpi_dev->dev, haps);
207 /* Set the protection level, currently at level 2 (Medium) */
208 ret = toshiba_haps_protection_level(acpi_dev->handle, 2);
209 if (ret != 0)
210 return ret;
212 ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group);
213 if (ret)
214 return ret;
216 toshiba_haps = haps;
218 return 0;
221 #ifdef CONFIG_PM_SLEEP
222 static int toshiba_haps_suspend(struct device *device)
224 struct toshiba_haps_dev *haps;
225 int ret;
227 haps = acpi_driver_data(to_acpi_device(device));
229 /* Deactivate the protection on suspend */
230 ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0);
232 return ret;
235 static int toshiba_haps_resume(struct device *device)
237 struct toshiba_haps_dev *haps;
238 int ret;
240 haps = acpi_driver_data(to_acpi_device(device));
242 /* Set the stored protection level */
243 ret = toshiba_haps_protection_level(haps->acpi_dev->handle,
244 haps->protection_level);
246 /* Reset the protection on resume */
247 ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
248 if (ret != 0)
249 return ret;
251 return ret;
253 #endif
255 static SIMPLE_DEV_PM_OPS(toshiba_haps_pm,
256 toshiba_haps_suspend, toshiba_haps_resume);
258 static const struct acpi_device_id haps_device_ids[] = {
259 {"TOS620A", 0},
260 {"", 0},
262 MODULE_DEVICE_TABLE(acpi, haps_device_ids);
264 static struct acpi_driver toshiba_haps_driver = {
265 .name = "Toshiba HAPS",
266 .owner = THIS_MODULE,
267 .ids = haps_device_ids,
268 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
269 .ops = {
270 .add = toshiba_haps_add,
271 .remove = toshiba_haps_remove,
272 .notify = toshiba_haps_notify,
274 .drv.pm = &toshiba_haps_pm,
277 module_acpi_driver(toshiba_haps_driver);