18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Toshiba HDD Active Protection Sensor (HAPS) driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Azael Avalos <coproscefalo@gmail.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/init.h> 138c2ecf20Sopenharmony_ci#include <linux/types.h> 148c2ecf20Sopenharmony_ci#include <linux/acpi.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciMODULE_AUTHOR("Azael Avalos <coproscefalo@gmail.com>"); 178c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Toshiba HDD Active Protection Sensor"); 188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct toshiba_haps_dev { 218c2ecf20Sopenharmony_ci struct acpi_device *acpi_dev; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci int protection_level; 248c2ecf20Sopenharmony_ci}; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic struct toshiba_haps_dev *toshiba_haps; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* HAPS functions */ 298c2ecf20Sopenharmony_cistatic int toshiba_haps_reset_protection(acpi_handle handle) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci acpi_status status; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci status = acpi_evaluate_object(handle, "RSSS", NULL, NULL); 348c2ecf20Sopenharmony_ci if (ACPI_FAILURE(status)) { 358c2ecf20Sopenharmony_ci pr_err("Unable to reset the HDD protection\n"); 368c2ecf20Sopenharmony_ci return -EIO; 378c2ecf20Sopenharmony_ci } 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci return 0; 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic int toshiba_haps_protection_level(acpi_handle handle, int level) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci acpi_status status; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci status = acpi_execute_simple_method(handle, "PTLV", level); 478c2ecf20Sopenharmony_ci if (ACPI_FAILURE(status)) { 488c2ecf20Sopenharmony_ci pr_err("Error while setting the protection level\n"); 498c2ecf20Sopenharmony_ci return -EIO; 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci pr_debug("HDD protection level set to: %d\n", level); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return 0; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* sysfs files */ 588c2ecf20Sopenharmony_cistatic ssize_t protection_level_show(struct device *dev, 598c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci struct toshiba_haps_dev *haps = dev_get_drvdata(dev); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci return sprintf(buf, "%i\n", haps->protection_level); 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic ssize_t protection_level_store(struct device *dev, 678c2ecf20Sopenharmony_ci struct device_attribute *attr, 688c2ecf20Sopenharmony_ci const char *buf, size_t count) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct toshiba_haps_dev *haps = dev_get_drvdata(dev); 718c2ecf20Sopenharmony_ci int level; 728c2ecf20Sopenharmony_ci int ret; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 0, &level); 758c2ecf20Sopenharmony_ci if (ret) 768c2ecf20Sopenharmony_ci return ret; 778c2ecf20Sopenharmony_ci /* 788c2ecf20Sopenharmony_ci * Check for supported levels, which can be: 798c2ecf20Sopenharmony_ci * 0 - Disabled | 1 - Low | 2 - Medium | 3 - High 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_ci if (level < 0 || level > 3) 828c2ecf20Sopenharmony_ci return -EINVAL; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Set the sensor level */ 858c2ecf20Sopenharmony_ci ret = toshiba_haps_protection_level(haps->acpi_dev->handle, level); 868c2ecf20Sopenharmony_ci if (ret != 0) 878c2ecf20Sopenharmony_ci return ret; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci haps->protection_level = level; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci return count; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(protection_level); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic ssize_t reset_protection_store(struct device *dev, 968c2ecf20Sopenharmony_ci struct device_attribute *attr, 978c2ecf20Sopenharmony_ci const char *buf, size_t count) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct toshiba_haps_dev *haps = dev_get_drvdata(dev); 1008c2ecf20Sopenharmony_ci int reset; 1018c2ecf20Sopenharmony_ci int ret; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 0, &reset); 1048c2ecf20Sopenharmony_ci if (ret) 1058c2ecf20Sopenharmony_ci return ret; 1068c2ecf20Sopenharmony_ci /* The only accepted value is 1 */ 1078c2ecf20Sopenharmony_ci if (reset != 1) 1088c2ecf20Sopenharmony_ci return -EINVAL; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* Reset the protection interface */ 1118c2ecf20Sopenharmony_ci ret = toshiba_haps_reset_protection(haps->acpi_dev->handle); 1128c2ecf20Sopenharmony_ci if (ret != 0) 1138c2ecf20Sopenharmony_ci return ret; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci return count; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(reset_protection); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic struct attribute *haps_attributes[] = { 1208c2ecf20Sopenharmony_ci &dev_attr_protection_level.attr, 1218c2ecf20Sopenharmony_ci &dev_attr_reset_protection.attr, 1228c2ecf20Sopenharmony_ci NULL, 1238c2ecf20Sopenharmony_ci}; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic const struct attribute_group haps_attr_group = { 1268c2ecf20Sopenharmony_ci .attrs = haps_attributes, 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* 1308c2ecf20Sopenharmony_ci * ACPI stuff 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_cistatic void toshiba_haps_notify(struct acpi_device *device, u32 event) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci pr_debug("Received event: 0x%x", event); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci acpi_bus_generate_netlink_event(device->pnp.device_class, 1378c2ecf20Sopenharmony_ci dev_name(&device->dev), 1388c2ecf20Sopenharmony_ci event, 0); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic int toshiba_haps_remove(struct acpi_device *device) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci sysfs_remove_group(&device->dev.kobj, &haps_attr_group); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci if (toshiba_haps) 1468c2ecf20Sopenharmony_ci toshiba_haps = NULL; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* Helper function */ 1528c2ecf20Sopenharmony_cistatic int toshiba_haps_available(acpi_handle handle) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci acpi_status status; 1558c2ecf20Sopenharmony_ci u64 hdd_present; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci /* 1588c2ecf20Sopenharmony_ci * A non existent device as well as having (only) 1598c2ecf20Sopenharmony_ci * Solid State Drives can cause the call to fail. 1608c2ecf20Sopenharmony_ci */ 1618c2ecf20Sopenharmony_ci status = acpi_evaluate_integer(handle, "_STA", NULL, &hdd_present); 1628c2ecf20Sopenharmony_ci if (ACPI_FAILURE(status)) { 1638c2ecf20Sopenharmony_ci pr_err("ACPI call to query HDD protection failed\n"); 1648c2ecf20Sopenharmony_ci return 0; 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (!hdd_present) { 1688c2ecf20Sopenharmony_ci pr_info("HDD protection not available or using SSD\n"); 1698c2ecf20Sopenharmony_ci return 0; 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci return 1; 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic int toshiba_haps_add(struct acpi_device *acpi_dev) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct toshiba_haps_dev *haps; 1788c2ecf20Sopenharmony_ci int ret; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci if (toshiba_haps) 1818c2ecf20Sopenharmony_ci return -EBUSY; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci if (!toshiba_haps_available(acpi_dev->handle)) 1848c2ecf20Sopenharmony_ci return -ENODEV; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci pr_info("Toshiba HDD Active Protection Sensor device\n"); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci haps = kzalloc(sizeof(struct toshiba_haps_dev), GFP_KERNEL); 1898c2ecf20Sopenharmony_ci if (!haps) 1908c2ecf20Sopenharmony_ci return -ENOMEM; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci haps->acpi_dev = acpi_dev; 1938c2ecf20Sopenharmony_ci haps->protection_level = 2; 1948c2ecf20Sopenharmony_ci acpi_dev->driver_data = haps; 1958c2ecf20Sopenharmony_ci dev_set_drvdata(&acpi_dev->dev, haps); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci /* Set the protection level, currently at level 2 (Medium) */ 1988c2ecf20Sopenharmony_ci ret = toshiba_haps_protection_level(acpi_dev->handle, 2); 1998c2ecf20Sopenharmony_ci if (ret != 0) 2008c2ecf20Sopenharmony_ci return ret; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci ret = sysfs_create_group(&acpi_dev->dev.kobj, &haps_attr_group); 2038c2ecf20Sopenharmony_ci if (ret) 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci toshiba_haps = haps; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci return 0; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 2128c2ecf20Sopenharmony_cistatic int toshiba_haps_suspend(struct device *device) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci struct toshiba_haps_dev *haps; 2158c2ecf20Sopenharmony_ci int ret; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci haps = acpi_driver_data(to_acpi_device(device)); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci /* Deactivate the protection on suspend */ 2208c2ecf20Sopenharmony_ci ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 0); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci return ret; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_cistatic int toshiba_haps_resume(struct device *device) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci struct toshiba_haps_dev *haps; 2288c2ecf20Sopenharmony_ci int ret; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci haps = acpi_driver_data(to_acpi_device(device)); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* Set the stored protection level */ 2338c2ecf20Sopenharmony_ci ret = toshiba_haps_protection_level(haps->acpi_dev->handle, 2348c2ecf20Sopenharmony_ci haps->protection_level); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* Reset the protection on resume */ 2378c2ecf20Sopenharmony_ci ret = toshiba_haps_reset_protection(haps->acpi_dev->handle); 2388c2ecf20Sopenharmony_ci if (ret != 0) 2398c2ecf20Sopenharmony_ci return ret; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci return ret; 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci#endif 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(toshiba_haps_pm, 2468c2ecf20Sopenharmony_ci toshiba_haps_suspend, toshiba_haps_resume); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic const struct acpi_device_id haps_device_ids[] = { 2498c2ecf20Sopenharmony_ci {"TOS620A", 0}, 2508c2ecf20Sopenharmony_ci {"", 0}, 2518c2ecf20Sopenharmony_ci}; 2528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, haps_device_ids); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic struct acpi_driver toshiba_haps_driver = { 2558c2ecf20Sopenharmony_ci .name = "Toshiba HAPS", 2568c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 2578c2ecf20Sopenharmony_ci .ids = haps_device_ids, 2588c2ecf20Sopenharmony_ci .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, 2598c2ecf20Sopenharmony_ci .ops = { 2608c2ecf20Sopenharmony_ci .add = toshiba_haps_add, 2618c2ecf20Sopenharmony_ci .remove = toshiba_haps_remove, 2628c2ecf20Sopenharmony_ci .notify = toshiba_haps_notify, 2638c2ecf20Sopenharmony_ci }, 2648c2ecf20Sopenharmony_ci .drv.pm = &toshiba_haps_pm, 2658c2ecf20Sopenharmony_ci}; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cimodule_acpi_driver(toshiba_haps_driver); 268