Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  hp_accel.c - Interface between LIS3LV02DL driver and HP ACPI BIOS
0004  *
0005  *  Copyright (C) 2007-2008 Yan Burman
0006  *  Copyright (C) 2008 Eric Piel
0007  *  Copyright (C) 2008-2009 Pavel Machek
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/init.h>
0014 #include <linux/dmi.h>
0015 #include <linux/module.h>
0016 #include <linux/types.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/delay.h>
0020 #include <linux/wait.h>
0021 #include <linux/poll.h>
0022 #include <linux/freezer.h>
0023 #include <linux/uaccess.h>
0024 #include <linux/leds.h>
0025 #include <linux/atomic.h>
0026 #include <linux/acpi.h>
0027 #include <linux/i8042.h>
0028 #include <linux/serio.h>
0029 #include "../../misc/lis3lv02d/lis3lv02d.h"
0030 
0031 /* Delayed LEDs infrastructure ------------------------------------ */
0032 
0033 /* Special LED class that can defer work */
0034 struct delayed_led_classdev {
0035     struct led_classdev led_classdev;
0036     struct work_struct work;
0037     enum led_brightness new_brightness;
0038 
0039     unsigned int led;       /* For driver */
0040     void (*set_brightness)(struct delayed_led_classdev *data, enum led_brightness value);
0041 };
0042 
0043 static inline void delayed_set_status_worker(struct work_struct *work)
0044 {
0045     struct delayed_led_classdev *data =
0046             container_of(work, struct delayed_led_classdev, work);
0047 
0048     data->set_brightness(data, data->new_brightness);
0049 }
0050 
0051 static inline void delayed_sysfs_set(struct led_classdev *led_cdev,
0052                   enum led_brightness brightness)
0053 {
0054     struct delayed_led_classdev *data = container_of(led_cdev,
0055                  struct delayed_led_classdev, led_classdev);
0056     data->new_brightness = brightness;
0057     schedule_work(&data->work);
0058 }
0059 
0060 /* HP-specific accelerometer driver ------------------------------------ */
0061 
0062 /* e0 25, e0 26, e0 27, e0 28 are scan codes that the accelerometer with acpi id
0063  * HPQ6000 sends through the keyboard bus */
0064 #define ACCEL_1 0x25
0065 #define ACCEL_2 0x26
0066 #define ACCEL_3 0x27
0067 #define ACCEL_4 0x28
0068 
0069 /* For automatic insertion of the module */
0070 static const struct acpi_device_id lis3lv02d_device_ids[] = {
0071     {"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
0072     {"HPQ6000", 0}, /* HP Mobile Data Protection System PNP */
0073     {"HPQ6007", 0}, /* HP Mobile Data Protection System PNP */
0074     {"", 0},
0075 };
0076 MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
0077 
0078 /**
0079  * lis3lv02d_acpi_init - initialize the device for ACPI
0080  * @lis3: pointer to the device struct
0081  *
0082  * Returns 0 on success.
0083  */
0084 static int lis3lv02d_acpi_init(struct lis3lv02d *lis3)
0085 {
0086     return 0;
0087 }
0088 
0089 /**
0090  * lis3lv02d_acpi_read - ACPI ALRD method: read a register
0091  * @lis3: pointer to the device struct
0092  * @reg:    the register to read
0093  * @ret:    result of the operation
0094  *
0095  * Returns 0 on success.
0096  */
0097 static int lis3lv02d_acpi_read(struct lis3lv02d *lis3, int reg, u8 *ret)
0098 {
0099     struct acpi_device *dev = lis3->bus_priv;
0100     union acpi_object arg0 = { ACPI_TYPE_INTEGER };
0101     struct acpi_object_list args = { 1, &arg0 };
0102     unsigned long long lret;
0103     acpi_status status;
0104 
0105     arg0.integer.value = reg;
0106 
0107     status = acpi_evaluate_integer(dev->handle, "ALRD", &args, &lret);
0108     if (ACPI_FAILURE(status))
0109         return -EINVAL;
0110     *ret = lret;
0111     return 0;
0112 }
0113 
0114 /**
0115  * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
0116  * @lis3: pointer to the device struct
0117  * @reg:    the register to write to
0118  * @val:    the value to write
0119  *
0120  * Returns 0 on success.
0121  */
0122 static int lis3lv02d_acpi_write(struct lis3lv02d *lis3, int reg, u8 val)
0123 {
0124     struct acpi_device *dev = lis3->bus_priv;
0125     unsigned long long ret; /* Not used when writting */
0126     union acpi_object in_obj[2];
0127     struct acpi_object_list args = { 2, in_obj };
0128 
0129     in_obj[0].type          = ACPI_TYPE_INTEGER;
0130     in_obj[0].integer.value = reg;
0131     in_obj[1].type          = ACPI_TYPE_INTEGER;
0132     in_obj[1].integer.value = val;
0133 
0134     if (acpi_evaluate_integer(dev->handle, "ALWR", &args, &ret) != AE_OK)
0135         return -EINVAL;
0136 
0137     return 0;
0138 }
0139 
0140 static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
0141 {
0142     lis3_dev.ac = *((union axis_conversion *)dmi->driver_data);
0143     pr_info("hardware type %s found\n", dmi->ident);
0144 
0145     return 1;
0146 }
0147 
0148 /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
0149  * If the value is negative, the opposite of the hw value is used. */
0150 #define DEFINE_CONV(name, x, y, z)                \
0151     static union axis_conversion lis3lv02d_axis_##name = \
0152         { .as_array = { x, y, z } }
0153 DEFINE_CONV(normal, 1, 2, 3);
0154 DEFINE_CONV(y_inverted, 1, -2, 3);
0155 DEFINE_CONV(x_inverted, -1, 2, 3);
0156 DEFINE_CONV(x_inverted_usd, -1, 2, -3);
0157 DEFINE_CONV(z_inverted, 1, 2, -3);
0158 DEFINE_CONV(xy_swap, 2, 1, 3);
0159 DEFINE_CONV(xy_rotated_left, -2, 1, 3);
0160 DEFINE_CONV(xy_rotated_left_usd, -2, 1, -3);
0161 DEFINE_CONV(xy_swap_inverted, -2, -1, 3);
0162 DEFINE_CONV(xy_rotated_right, 2, -1, 3);
0163 DEFINE_CONV(xy_swap_yz_inverted, 2, -1, -3);
0164 
0165 #define AXIS_DMI_MATCH(_ident, _name, _axis) {      \
0166     .ident = _ident,                \
0167     .callback = lis3lv02d_dmi_matched,      \
0168     .matches = {                    \
0169         DMI_MATCH(DMI_PRODUCT_NAME, _name)  \
0170     },                      \
0171     .driver_data = &lis3lv02d_axis_##_axis      \
0172 }
0173 
0174 #define AXIS_DMI_MATCH2(_ident, _class1, _name1,    \
0175                 _class2, _name2,    \
0176                 _axis) {        \
0177     .ident = _ident,                \
0178     .callback = lis3lv02d_dmi_matched,      \
0179     .matches = {                    \
0180         DMI_MATCH(DMI_##_class1, _name1),   \
0181         DMI_MATCH(DMI_##_class2, _name2),   \
0182     },                      \
0183     .driver_data = &lis3lv02d_axis_##_axis      \
0184 }
0185 static const struct dmi_system_id lis3lv02d_dmi_ids[] = {
0186     /* product names are truncated to match all kinds of a same model */
0187     AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
0188     AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
0189     AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
0190     AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
0191     AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
0192     AXIS_DMI_MATCH("NC2710", "HP Compaq 2710", xy_swap),
0193     AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
0194     AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
0195     AXIS_DMI_MATCH("HP2140", "HP 2140", xy_swap_inverted),
0196     AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
0197     AXIS_DMI_MATCH("NC6730b", "HP Compaq 6730b", xy_rotated_left_usd),
0198     AXIS_DMI_MATCH("NC6730s", "HP Compaq 6730s", xy_swap),
0199     AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
0200     AXIS_DMI_MATCH("NC6710x", "HP Compaq 6710", xy_swap_yz_inverted),
0201     AXIS_DMI_MATCH("NC6715x", "HP Compaq 6715", y_inverted),
0202     AXIS_DMI_MATCH("NC693xx", "HP EliteBook 693", xy_rotated_right),
0203     AXIS_DMI_MATCH("NC693xx", "HP EliteBook 853", xy_swap),
0204     AXIS_DMI_MATCH("NC854xx", "HP EliteBook 854", y_inverted),
0205     AXIS_DMI_MATCH("NC273xx", "HP EliteBook 273", y_inverted),
0206     /* Intel-based HP Pavilion dv5 */
0207     AXIS_DMI_MATCH2("HPDV5_I",
0208             PRODUCT_NAME, "HP Pavilion dv5",
0209             BOARD_NAME, "3603",
0210             x_inverted),
0211     /* AMD-based HP Pavilion dv5 */
0212     AXIS_DMI_MATCH2("HPDV5_A",
0213             PRODUCT_NAME, "HP Pavilion dv5",
0214             BOARD_NAME, "3600",
0215             y_inverted),
0216     AXIS_DMI_MATCH("DV7", "HP Pavilion dv7", x_inverted),
0217     AXIS_DMI_MATCH("HP8710", "HP Compaq 8710", y_inverted),
0218     AXIS_DMI_MATCH("HDX18", "HP HDX 18", x_inverted),
0219     AXIS_DMI_MATCH("HPB432x", "HP ProBook 432", xy_rotated_left),
0220     AXIS_DMI_MATCH("HPB440G3", "HP ProBook 440 G3", x_inverted_usd),
0221     AXIS_DMI_MATCH("HPB440G4", "HP ProBook 440 G4", x_inverted),
0222     AXIS_DMI_MATCH("HPB442x", "HP ProBook 442", xy_rotated_left),
0223     AXIS_DMI_MATCH("HPB450G0", "HP ProBook 450 G0", x_inverted),
0224     AXIS_DMI_MATCH("HPB452x", "HP ProBook 452", y_inverted),
0225     AXIS_DMI_MATCH("HPB522x", "HP ProBook 522", xy_swap),
0226     AXIS_DMI_MATCH("HPB532x", "HP ProBook 532", y_inverted),
0227     AXIS_DMI_MATCH("HPB655x", "HP ProBook 655", xy_swap_inverted),
0228     AXIS_DMI_MATCH("Mini510x", "HP Mini 510", xy_rotated_left_usd),
0229     AXIS_DMI_MATCH("HPB63xx", "HP ProBook 63", xy_swap),
0230     AXIS_DMI_MATCH("HPB64xx", "HP ProBook 64", xy_swap),
0231     AXIS_DMI_MATCH("HPB64xx", "HP EliteBook 84", xy_swap),
0232     AXIS_DMI_MATCH("HPB65xx", "HP ProBook 65", x_inverted),
0233     AXIS_DMI_MATCH("HPZBook15", "HP ZBook 15", x_inverted),
0234     AXIS_DMI_MATCH("HPZBook17G5", "HP ZBook 17 G5", x_inverted),
0235     AXIS_DMI_MATCH("HPZBook17", "HP ZBook 17", xy_swap_yz_inverted),
0236     { NULL, }
0237 /* Laptop models without axis info (yet):
0238  * "NC6910" "HP Compaq 6910"
0239  * "NC2400" "HP Compaq nc2400"
0240  * "NX74x0" "HP Compaq nx74"
0241  * "NX6325" "HP Compaq nx6325"
0242  * "NC4400" "HP Compaq nc4400"
0243  */
0244 };
0245 
0246 static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
0247 {
0248     struct acpi_device *dev = lis3_dev.bus_priv;
0249     unsigned long long ret; /* Not used when writing */
0250     union acpi_object in_obj[1];
0251     struct acpi_object_list args = { 1, in_obj };
0252 
0253     in_obj[0].type          = ACPI_TYPE_INTEGER;
0254     in_obj[0].integer.value = !!value;
0255 
0256     acpi_evaluate_integer(dev->handle, "ALED", &args, &ret);
0257 }
0258 
0259 static struct delayed_led_classdev hpled_led = {
0260     .led_classdev = {
0261         .name           = "hp::hddprotect",
0262         .default_trigger    = "none",
0263         .brightness_set     = delayed_sysfs_set,
0264         .flags                  = LED_CORE_SUSPENDRESUME,
0265     },
0266     .set_brightness = hpled_set,
0267 };
0268 
0269 static bool hp_accel_i8042_filter(unsigned char data, unsigned char str,
0270                   struct serio *port)
0271 {
0272     static bool extended;
0273 
0274     if (str & I8042_STR_AUXDATA)
0275         return false;
0276 
0277     if (data == 0xe0) {
0278         extended = true;
0279         return true;
0280     } else if (unlikely(extended)) {
0281         extended = false;
0282 
0283         switch (data) {
0284         case ACCEL_1:
0285         case ACCEL_2:
0286         case ACCEL_3:
0287         case ACCEL_4:
0288             return true;
0289         default:
0290             serio_interrupt(port, 0xe0, 0);
0291             return false;
0292         }
0293     }
0294 
0295     return false;
0296 }
0297 
0298 static int lis3lv02d_probe(struct platform_device *device)
0299 {
0300     int ret;
0301 
0302     lis3_dev.bus_priv = ACPI_COMPANION(&device->dev);
0303     lis3_dev.init = lis3lv02d_acpi_init;
0304     lis3_dev.read = lis3lv02d_acpi_read;
0305     lis3_dev.write = lis3lv02d_acpi_write;
0306 
0307     /* obtain IRQ number of our device from ACPI */
0308     ret = platform_get_irq_optional(device, 0);
0309     if (ret > 0)
0310         lis3_dev.irq = ret;
0311 
0312     /* If possible use a "standard" axes order */
0313     if (lis3_dev.ac.x && lis3_dev.ac.y && lis3_dev.ac.z) {
0314         pr_info("Using custom axes %d,%d,%d\n",
0315             lis3_dev.ac.x, lis3_dev.ac.y, lis3_dev.ac.z);
0316     } else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
0317         pr_info("laptop model unknown, using default axes configuration\n");
0318         lis3_dev.ac = lis3lv02d_axis_normal;
0319     }
0320 
0321     /* call the core layer do its init */
0322     ret = lis3lv02d_init_device(&lis3_dev);
0323     if (ret)
0324         return ret;
0325 
0326     /* filter to remove HPQ6000 accelerometer data
0327      * from keyboard bus stream */
0328     if (strstr(dev_name(&device->dev), "HPQ6000"))
0329         i8042_install_filter(hp_accel_i8042_filter);
0330 
0331     INIT_WORK(&hpled_led.work, delayed_set_status_worker);
0332     ret = led_classdev_register(NULL, &hpled_led.led_classdev);
0333     if (ret) {
0334         i8042_remove_filter(hp_accel_i8042_filter);
0335         lis3lv02d_joystick_disable(&lis3_dev);
0336         lis3lv02d_poweroff(&lis3_dev);
0337         flush_work(&hpled_led.work);
0338         lis3lv02d_remove_fs(&lis3_dev);
0339         return ret;
0340     }
0341 
0342     return ret;
0343 }
0344 
0345 static int lis3lv02d_remove(struct platform_device *device)
0346 {
0347     i8042_remove_filter(hp_accel_i8042_filter);
0348     lis3lv02d_joystick_disable(&lis3_dev);
0349     lis3lv02d_poweroff(&lis3_dev);
0350 
0351     led_classdev_unregister(&hpled_led.led_classdev);
0352     flush_work(&hpled_led.work);
0353 
0354     lis3lv02d_remove_fs(&lis3_dev);
0355     return 0;
0356 }
0357 
0358 static int __maybe_unused lis3lv02d_suspend(struct device *dev)
0359 {
0360     /* make sure the device is off when we suspend */
0361     lis3lv02d_poweroff(&lis3_dev);
0362     return 0;
0363 }
0364 
0365 static int __maybe_unused lis3lv02d_resume(struct device *dev)
0366 {
0367     lis3lv02d_poweron(&lis3_dev);
0368     return 0;
0369 }
0370 
0371 static SIMPLE_DEV_PM_OPS(hp_accel_pm, lis3lv02d_suspend, lis3lv02d_resume);
0372 
0373 /* For the HP MDPS aka 3D Driveguard */
0374 static struct platform_driver lis3lv02d_driver = {
0375     .probe  = lis3lv02d_probe,
0376     .remove = lis3lv02d_remove,
0377     .driver = {
0378         .name   = "hp_accel",
0379         .pm = &hp_accel_pm,
0380         .acpi_match_table = lis3lv02d_device_ids,
0381     },
0382 };
0383 module_platform_driver(lis3lv02d_driver);
0384 
0385 MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
0386 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
0387 MODULE_LICENSE("GPL");