]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/platform/x86/toshiba_haps.c
toshiba_haps: Replace sscanf with kstrtoint
[mirror_ubuntu-artful-kernel.git] / drivers / platform / x86 / toshiba_haps.c
CommitLineData
23d0ba0c
AA
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);
a8820037
AA
81 int level;
82 int ret;
23d0ba0c 83
a8820037
AA
84 ret = kstrtoint(buf, 0, &level);
85 if (ret)
86 return ret;
87 /*
88 * Check for supported levels, which can be:
23d0ba0c
AA
89 * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High
90 */
a8820037
AA
91 if (level < 0 || level > 3)
92 return -EINVAL;
93
94 /* Set the sensor level */
23d0ba0c
AA
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}
103
104static ssize_t reset_protection_store(struct device *dev,
105 struct device_attribute *attr,
106 const char *buf, size_t count)
107{
108 struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
a8820037
AA
109 int reset;
110 int ret;
23d0ba0c 111
a8820037
AA
112 ret = kstrtoint(buf, 0, &reset);
113 if (ret)
114 return ret;
115 /* The only accepted value is 1 */
116 if (reset != 1)
23d0ba0c
AA
117 return -EINVAL;
118
119 /* Reset the protection interface */
120 ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
121 if (ret != 0)
122 return ret;
123
124 return count;
125}
126
127static DEVICE_ATTR(protection_level, S_IRUGO | S_IWUSR,
128 protection_level_show, protection_level_store);
129static DEVICE_ATTR(reset_protection, S_IWUSR, NULL, reset_protection_store);
130
131static struct attribute *haps_attributes[] = {
132 &dev_attr_protection_level.attr,
133 &dev_attr_reset_protection.attr,
134 NULL,
135};
136
137static struct attribute_group haps_attr_group = {
138 .attrs = haps_attributes,
139};
140
141/*
142 * ACPI stuff
143 */
144static void toshiba_haps_notify(struct acpi_device *device, u32 event)
145{
146 pr_info("Received event: 0x%x", event);
147
148 acpi_bus_generate_netlink_event(device->pnp.device_class,
149 dev_name(&device->dev),
150 event, 0);
151}
152
153static int toshiba_haps_remove(struct acpi_device *device)
154{
155 sysfs_remove_group(&device->dev.kobj, &haps_attr_group);
156
157 if (toshiba_haps)
158 toshiba_haps = NULL;
159
160 return 0;
161}
162
163/* Helper function */
164static int toshiba_haps_available(acpi_handle handle)
165{
166 acpi_status status;
167 u64 hdd_present;
168
169 /*
170 * A non existent device as well as having (only)
171 * Solid State Drives can cause the call to fail.
172 */
173 status = acpi_evaluate_integer(handle, "_STA", NULL,
174 &hdd_present);
175 if (ACPI_FAILURE(status) || !hdd_present) {
176 pr_info("HDD protection not available or using SSD\n");
177 return 0;
178 }
179
180 return 1;
181}
182
183static int toshiba_haps_add(struct acpi_device *acpi_dev)
184{
185 struct toshiba_haps_dev *haps;
186 int ret;
187
188 if (toshiba_haps)
189 return -EBUSY;
190
191 if (!toshiba_haps_available(acpi_dev->handle))
192 return -ENODEV;
193
194 pr_info("Toshiba HDD Active Protection Sensor device\n");
195
196 haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL);
197 if (!haps)
198 return -ENOMEM;
199
200 haps->acpi_dev = acpi_dev;
201 haps->protection_level = 2;
202 acpi_dev->driver_data = haps;
203 dev_set_drvdata(&acpi_dev->dev, haps);
204
205 /* Set the protection level, currently at level 2 (Medium) */
206 ret = toshiba_haps_protection_level(acpi_dev->handle, 2);
207 if (ret != 0)
208 return ret;
209
210 ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group);
211 if (ret)
212 return ret;
213
214 toshiba_haps = haps;
215
216 return 0;
217}
218
219#ifdef CONFIG_PM_SLEEP
220static int toshiba_haps_suspend(struct device *device)
221{
222 struct toshiba_haps_dev *haps;
223 int ret;
224
225 haps = acpi_driver_data(to_acpi_device(device));
226
227 /* Deactivate the protection on suspend */
228 ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0);
229
230 return ret;
231}
232
233static int toshiba_haps_resume(struct device *device)
234{
235 struct toshiba_haps_dev *haps;
236 int ret;
237
238 haps = acpi_driver_data(to_acpi_device(device));
239
240 /* Set the stored protection level */
241 ret = toshiba_haps_protection_level(haps->acpi_dev->handle,
242 haps->protection_level);
243
244 /* Reset the protection on resume */
245 ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
246 if (ret != 0)
247 return ret;
248
249 return ret;
250}
251#endif
252
253static SIMPLE_DEV_PM_OPS(toshiba_haps_pm,
254 toshiba_haps_suspend, toshiba_haps_resume);
255
256static const struct acpi_device_id haps_device_ids[] = {
257 {"TOS620A", 0},
258 {"", 0},
259};
260MODULE_DEVICE_TABLE(acpi, haps_device_ids);
261
262static struct acpi_driver toshiba_haps_driver = {
263 .name = "Toshiba HAPS",
264 .owner = THIS_MODULE,
265 .ids = haps_device_ids,
266 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
267 .ops = {
268 .add = toshiba_haps_add,
269 .remove = toshiba_haps_remove,
270 .notify = toshiba_haps_notify,
271 },
272 .drv.pm = &toshiba_haps_pm,
273};
274
275module_acpi_driver(toshiba_haps_driver);