1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
223d0ba0cSAzael Avalos /*
323d0ba0cSAzael Avalos * Toshiba HDD Active Protection Sensor (HAPS) driver
423d0ba0cSAzael Avalos *
523d0ba0cSAzael Avalos * Copyright (C) 2014 Azael Avalos <coproscefalo@gmail.com>
623d0ba0cSAzael Avalos */
723d0ba0cSAzael Avalos
823d0ba0cSAzael Avalos #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
923d0ba0cSAzael Avalos
1023d0ba0cSAzael Avalos #include <linux/kernel.h>
1123d0ba0cSAzael Avalos #include <linux/module.h>
1223d0ba0cSAzael Avalos #include <linux/init.h>
1323d0ba0cSAzael Avalos #include <linux/types.h>
1423d0ba0cSAzael Avalos #include <linux/acpi.h>
1523d0ba0cSAzael Avalos
1623d0ba0cSAzael Avalos MODULE_AUTHOR("Azael Avalos <coproscefalo@gmail.com>");
1723d0ba0cSAzael Avalos MODULE_DESCRIPTION("Toshiba HDD Active Protection Sensor");
1823d0ba0cSAzael Avalos MODULE_LICENSE("GPL");
1923d0ba0cSAzael Avalos
2023d0ba0cSAzael Avalos struct toshiba_haps_dev {
2123d0ba0cSAzael Avalos struct acpi_device *acpi_dev;
2223d0ba0cSAzael Avalos
2323d0ba0cSAzael Avalos int protection_level;
2423d0ba0cSAzael Avalos };
2523d0ba0cSAzael Avalos
2623d0ba0cSAzael Avalos static struct toshiba_haps_dev *toshiba_haps;
2723d0ba0cSAzael Avalos
2823d0ba0cSAzael Avalos /* HAPS functions */
toshiba_haps_reset_protection(acpi_handle handle)2923d0ba0cSAzael Avalos static int toshiba_haps_reset_protection(acpi_handle handle)
3023d0ba0cSAzael Avalos {
3123d0ba0cSAzael Avalos acpi_status status;
3223d0ba0cSAzael Avalos
3323d0ba0cSAzael Avalos status = acpi_evaluate_object(handle, "RSSS", NULL, NULL);
3423d0ba0cSAzael Avalos if (ACPI_FAILURE(status)) {
3523d0ba0cSAzael Avalos pr_err("Unable to reset the HDD protection\n");
3623d0ba0cSAzael Avalos return -EIO;
3723d0ba0cSAzael Avalos }
3823d0ba0cSAzael Avalos
3923d0ba0cSAzael Avalos return 0;
4023d0ba0cSAzael Avalos }
4123d0ba0cSAzael Avalos
toshiba_haps_protection_level(acpi_handle handle,int level)4223d0ba0cSAzael Avalos static int toshiba_haps_protection_level(acpi_handle handle, int level)
4323d0ba0cSAzael Avalos {
4423d0ba0cSAzael Avalos acpi_status status;
4523d0ba0cSAzael Avalos
4623d0ba0cSAzael Avalos status = acpi_execute_simple_method(handle, "PTLV", level);
4723d0ba0cSAzael Avalos if (ACPI_FAILURE(status)) {
4823d0ba0cSAzael Avalos pr_err("Error while setting the protection level\n");
4923d0ba0cSAzael Avalos return -EIO;
5023d0ba0cSAzael Avalos }
5123d0ba0cSAzael Avalos
52baae5f91SAzael Avalos pr_debug("HDD protection level set to: %d\n", level);
5323d0ba0cSAzael Avalos
5423d0ba0cSAzael Avalos return 0;
5523d0ba0cSAzael Avalos }
5623d0ba0cSAzael Avalos
5723d0ba0cSAzael Avalos /* sysfs files */
protection_level_show(struct device * dev,struct device_attribute * attr,char * buf)5823d0ba0cSAzael Avalos static ssize_t protection_level_show(struct device *dev,
5923d0ba0cSAzael Avalos struct device_attribute *attr, char *buf)
6023d0ba0cSAzael Avalos {
6123d0ba0cSAzael Avalos struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
6223d0ba0cSAzael Avalos
6323d0ba0cSAzael Avalos return sprintf(buf, "%i\n", haps->protection_level);
6423d0ba0cSAzael Avalos }
6523d0ba0cSAzael Avalos
protection_level_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)6623d0ba0cSAzael Avalos static ssize_t protection_level_store(struct device *dev,
6723d0ba0cSAzael Avalos struct device_attribute *attr,
6823d0ba0cSAzael Avalos const char *buf, size_t count)
6923d0ba0cSAzael Avalos {
7023d0ba0cSAzael Avalos struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
71a8820037SAzael Avalos int level;
72a8820037SAzael Avalos int ret;
7323d0ba0cSAzael Avalos
74a8820037SAzael Avalos ret = kstrtoint(buf, 0, &level);
75a8820037SAzael Avalos if (ret)
76a8820037SAzael Avalos return ret;
77a8820037SAzael Avalos /*
78a8820037SAzael Avalos * Check for supported levels, which can be:
7923d0ba0cSAzael Avalos * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High
8023d0ba0cSAzael Avalos */
81a8820037SAzael Avalos if (level < 0 || level > 3)
82a8820037SAzael Avalos return -EINVAL;
83a8820037SAzael Avalos
84a8820037SAzael Avalos /* Set the sensor level */
8523d0ba0cSAzael Avalos ret = toshiba_haps_protection_level(haps->acpi_dev->handle, level);
8623d0ba0cSAzael Avalos if (ret != 0)
8723d0ba0cSAzael Avalos return ret;
8823d0ba0cSAzael Avalos
8923d0ba0cSAzael Avalos haps->protection_level = level;
9023d0ba0cSAzael Avalos
9123d0ba0cSAzael Avalos return count;
9223d0ba0cSAzael Avalos }
937dd62c01SAzael Avalos static DEVICE_ATTR_RW(protection_level);
9423d0ba0cSAzael Avalos
reset_protection_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)9523d0ba0cSAzael Avalos static ssize_t reset_protection_store(struct device *dev,
9623d0ba0cSAzael Avalos struct device_attribute *attr,
9723d0ba0cSAzael Avalos const char *buf, size_t count)
9823d0ba0cSAzael Avalos {
9923d0ba0cSAzael Avalos struct toshiba_haps_dev *haps = dev_get_drvdata(dev);
100a8820037SAzael Avalos int reset;
101a8820037SAzael Avalos int ret;
10223d0ba0cSAzael Avalos
103a8820037SAzael Avalos ret = kstrtoint(buf, 0, &reset);
104a8820037SAzael Avalos if (ret)
105a8820037SAzael Avalos return ret;
106a8820037SAzael Avalos /* The only accepted value is 1 */
107a8820037SAzael Avalos if (reset != 1)
10823d0ba0cSAzael Avalos return -EINVAL;
10923d0ba0cSAzael Avalos
11023d0ba0cSAzael Avalos /* Reset the protection interface */
11123d0ba0cSAzael Avalos ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
11223d0ba0cSAzael Avalos if (ret != 0)
11323d0ba0cSAzael Avalos return ret;
11423d0ba0cSAzael Avalos
11523d0ba0cSAzael Avalos return count;
11623d0ba0cSAzael Avalos }
1177dd62c01SAzael Avalos static DEVICE_ATTR_WO(reset_protection);
11823d0ba0cSAzael Avalos
11923d0ba0cSAzael Avalos static struct attribute *haps_attributes[] = {
12023d0ba0cSAzael Avalos &dev_attr_protection_level.attr,
12123d0ba0cSAzael Avalos &dev_attr_reset_protection.attr,
12223d0ba0cSAzael Avalos NULL,
12323d0ba0cSAzael Avalos };
12423d0ba0cSAzael Avalos
12536a1f2c6SArvind Yadav static const struct attribute_group haps_attr_group = {
12623d0ba0cSAzael Avalos .attrs = haps_attributes,
12723d0ba0cSAzael Avalos };
12823d0ba0cSAzael Avalos
12923d0ba0cSAzael Avalos /*
13023d0ba0cSAzael Avalos * ACPI stuff
13123d0ba0cSAzael Avalos */
toshiba_haps_notify(struct acpi_device * device,u32 event)13223d0ba0cSAzael Avalos static void toshiba_haps_notify(struct acpi_device *device, u32 event)
13323d0ba0cSAzael Avalos {
1347dc4a18dSHans de Goede pr_debug("Received event: 0x%x\n", event);
13523d0ba0cSAzael Avalos
13623d0ba0cSAzael Avalos acpi_bus_generate_netlink_event(device->pnp.device_class,
13723d0ba0cSAzael Avalos dev_name(&device->dev),
13823d0ba0cSAzael Avalos event, 0);
13923d0ba0cSAzael Avalos }
14023d0ba0cSAzael Avalos
toshiba_haps_remove(struct acpi_device * device)141*6c0eb5baSDawei Li static void toshiba_haps_remove(struct acpi_device *device)
14223d0ba0cSAzael Avalos {
14323d0ba0cSAzael Avalos sysfs_remove_group(&device->dev.kobj, &haps_attr_group);
14423d0ba0cSAzael Avalos
14523d0ba0cSAzael Avalos if (toshiba_haps)
14623d0ba0cSAzael Avalos toshiba_haps = NULL;
14723d0ba0cSAzael Avalos }
14823d0ba0cSAzael Avalos
14923d0ba0cSAzael Avalos /* Helper function */
toshiba_haps_available(acpi_handle handle)15023d0ba0cSAzael Avalos static int toshiba_haps_available(acpi_handle handle)
15123d0ba0cSAzael Avalos {
15223d0ba0cSAzael Avalos acpi_status status;
15323d0ba0cSAzael Avalos u64 hdd_present;
15423d0ba0cSAzael Avalos
15523d0ba0cSAzael Avalos /*
15623d0ba0cSAzael Avalos * A non existent device as well as having (only)
15723d0ba0cSAzael Avalos * Solid State Drives can cause the call to fail.
15823d0ba0cSAzael Avalos */
15955455449SAzael Avalos status = acpi_evaluate_integer(handle, "_STA", NULL, &hdd_present);
16055455449SAzael Avalos if (ACPI_FAILURE(status)) {
16155455449SAzael Avalos pr_err("ACPI call to query HDD protection failed\n");
16255455449SAzael Avalos return 0;
16355455449SAzael Avalos }
16455455449SAzael Avalos
16555455449SAzael Avalos if (!hdd_present) {
16623d0ba0cSAzael Avalos pr_info("HDD protection not available or using SSD\n");
16723d0ba0cSAzael Avalos return 0;
16823d0ba0cSAzael Avalos }
16923d0ba0cSAzael Avalos
17023d0ba0cSAzael Avalos return 1;
17123d0ba0cSAzael Avalos }
17223d0ba0cSAzael Avalos
toshiba_haps_add(struct acpi_device * acpi_dev)17323d0ba0cSAzael Avalos static int toshiba_haps_add(struct acpi_device *acpi_dev)
17423d0ba0cSAzael Avalos {
17523d0ba0cSAzael Avalos struct toshiba_haps_dev *haps;
17623d0ba0cSAzael Avalos int ret;
17723d0ba0cSAzael Avalos
17823d0ba0cSAzael Avalos if (toshiba_haps)
17923d0ba0cSAzael Avalos return -EBUSY;
18023d0ba0cSAzael Avalos
18123d0ba0cSAzael Avalos if (!toshiba_haps_available(acpi_dev->handle))
18223d0ba0cSAzael Avalos return -ENODEV;
18323d0ba0cSAzael Avalos
18423d0ba0cSAzael Avalos pr_info("Toshiba HDD Active Protection Sensor device\n");
18523d0ba0cSAzael Avalos
18623d0ba0cSAzael Avalos haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL);
18723d0ba0cSAzael Avalos if (!haps)
18823d0ba0cSAzael Avalos return -ENOMEM;
18923d0ba0cSAzael Avalos
19023d0ba0cSAzael Avalos haps->acpi_dev = acpi_dev;
19123d0ba0cSAzael Avalos haps->protection_level = 2;
19223d0ba0cSAzael Avalos acpi_dev->driver_data = haps;
19323d0ba0cSAzael Avalos dev_set_drvdata(&acpi_dev->dev, haps);
19423d0ba0cSAzael Avalos
19523d0ba0cSAzael Avalos /* Set the protection level, currently at level 2 (Medium) */
19623d0ba0cSAzael Avalos ret = toshiba_haps_protection_level(acpi_dev->handle, 2);
19723d0ba0cSAzael Avalos if (ret != 0)
19823d0ba0cSAzael Avalos return ret;
19923d0ba0cSAzael Avalos
20023d0ba0cSAzael Avalos ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group);
20123d0ba0cSAzael Avalos if (ret)
20223d0ba0cSAzael Avalos return ret;
20323d0ba0cSAzael Avalos
20423d0ba0cSAzael Avalos toshiba_haps = haps;
20523d0ba0cSAzael Avalos
20623d0ba0cSAzael Avalos return 0;
20723d0ba0cSAzael Avalos }
20823d0ba0cSAzael Avalos
20923d0ba0cSAzael Avalos #ifdef CONFIG_PM_SLEEP
toshiba_haps_suspend(struct device * device)21023d0ba0cSAzael Avalos static int toshiba_haps_suspend(struct device *device)
21123d0ba0cSAzael Avalos {
21223d0ba0cSAzael Avalos struct toshiba_haps_dev *haps;
21323d0ba0cSAzael Avalos int ret;
21423d0ba0cSAzael Avalos
21523d0ba0cSAzael Avalos haps = acpi_driver_data(to_acpi_device(device));
21623d0ba0cSAzael Avalos
21723d0ba0cSAzael Avalos /* Deactivate the protection on suspend */
21823d0ba0cSAzael Avalos ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0);
21923d0ba0cSAzael Avalos
22023d0ba0cSAzael Avalos return ret;
22123d0ba0cSAzael Avalos }
22223d0ba0cSAzael Avalos
toshiba_haps_resume(struct device * device)22323d0ba0cSAzael Avalos static int toshiba_haps_resume(struct device *device)
22423d0ba0cSAzael Avalos {
22523d0ba0cSAzael Avalos struct toshiba_haps_dev *haps;
22623d0ba0cSAzael Avalos int ret;
22723d0ba0cSAzael Avalos
22823d0ba0cSAzael Avalos haps = acpi_driver_data(to_acpi_device(device));
22923d0ba0cSAzael Avalos
23023d0ba0cSAzael Avalos /* Set the stored protection level */
23123d0ba0cSAzael Avalos ret = toshiba_haps_protection_level(haps->acpi_dev->handle,
23223d0ba0cSAzael Avalos haps->protection_level);
23323d0ba0cSAzael Avalos
23423d0ba0cSAzael Avalos /* Reset the protection on resume */
23523d0ba0cSAzael Avalos ret = toshiba_haps_reset_protection(haps->acpi_dev->handle);
23623d0ba0cSAzael Avalos if (ret != 0)
23723d0ba0cSAzael Avalos return ret;
23823d0ba0cSAzael Avalos
23923d0ba0cSAzael Avalos return ret;
24023d0ba0cSAzael Avalos }
24123d0ba0cSAzael Avalos #endif
24223d0ba0cSAzael Avalos
24323d0ba0cSAzael Avalos static SIMPLE_DEV_PM_OPS(toshiba_haps_pm,
24423d0ba0cSAzael Avalos toshiba_haps_suspend, toshiba_haps_resume);
24523d0ba0cSAzael Avalos
24623d0ba0cSAzael Avalos static const struct acpi_device_id haps_device_ids[] = {
24723d0ba0cSAzael Avalos {"TOS620A", 0},
24823d0ba0cSAzael Avalos {"", 0},
24923d0ba0cSAzael Avalos };
25023d0ba0cSAzael Avalos MODULE_DEVICE_TABLE(acpi, haps_device_ids);
25123d0ba0cSAzael Avalos
25223d0ba0cSAzael Avalos static struct acpi_driver toshiba_haps_driver = {
25323d0ba0cSAzael Avalos .name = "Toshiba HAPS",
25423d0ba0cSAzael Avalos .ids = haps_device_ids,
25523d0ba0cSAzael Avalos .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
25623d0ba0cSAzael Avalos .ops = {
25723d0ba0cSAzael Avalos .add = toshiba_haps_add,
25823d0ba0cSAzael Avalos .remove = toshiba_haps_remove,
25923d0ba0cSAzael Avalos .notify = toshiba_haps_notify,
26023d0ba0cSAzael Avalos },
26123d0ba0cSAzael Avalos .drv.pm = &toshiba_haps_pm,
26223d0ba0cSAzael Avalos };
26323d0ba0cSAzael Avalos
26423d0ba0cSAzael Avalos module_acpi_driver(toshiba_haps_driver);
265