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