Subject: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

This add supports for devices like keyboard, backlight, tablet and
accelerometer.

This work is supported by International Syst S/A.

Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
---
MAINTAINERS | 6 +
drivers/platform/x86/Kconfig | 12 +
drivers/platform/x86/Makefile | 1 +
drivers/platform/x86/cmpc_acpi.c | 522 ++++++++++++++++++++++++++++++++++++++
4 files changed, 541 insertions(+), 0 deletions(-)
create mode 100644 drivers/platform/x86/cmpc_acpi.c

diff --git a/MAINTAINERS b/MAINTAINERS
index c450f3a..9e14df1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1368,6 +1368,12 @@ L: [email protected]
S: Supported
F: drivers/scsi/fnic/

+CMPC ACPI DRIVER
+M: Thadeu Lima de Souza Cascardo <[email protected]>
+M: Daniel Oliveira Nascimento <[email protected]>
+S: Supported
+F: drivers/platform/x86/cmpc_acpi.c
+
CODA FILE SYSTEM
M: Jan Harkes <[email protected]>
M: [email protected]
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 55ca39d..2cbc6a6 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -435,4 +435,16 @@ config ACPI_TOSHIBA

If you have a legacy free Toshiba laptop (such as the Libretto L1
series), say Y.
+
+config ACPI_CMPC
+ tristate "CMPC Laptop Extras"
+ depends on X86
+ select INPUT
+ select BACKLIGHT_CLASS_DEVICE
+ default n
+ help
+ Support for Intel Classmate PC ACPI devices, including some
+ keys as input device, backlight device, tablet and accelerometer
+ devices.
+
endif # X86_PLATFORM_DEVICES
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index d1c1621..fc92247 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -21,3 +21,4 @@ obj-$(CONFIG_ACPI_WMI) += wmi.o
obj-$(CONFIG_ACPI_ASUS) += asus_acpi.o
obj-$(CONFIG_TOPSTAR_LAPTOP) += topstar-laptop.o
obj-$(CONFIG_ACPI_TOSHIBA) += toshiba_acpi.o
+obj-$(CONFIG_ACPI_CMPC) += cmpc_acpi.o
diff --git a/drivers/platform/x86/cmpc_acpi.c b/drivers/platform/x86/cmpc_acpi.c
new file mode 100644
index 0000000..c77c855
--- /dev/null
+++ b/drivers/platform/x86/cmpc_acpi.c
@@ -0,0 +1,522 @@
+/*
+ * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/workqueue.h>
+#include <acpi/acpi_drivers.h>
+#include <linux/backlight.h>
+#include <linux/input.h>
+
+MODULE_LICENSE("GPL");
+
+
+/*
+ * Generic input device code.
+ */
+
+typedef void (*input_device_init)(struct input_dev *dev);
+
+static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
+ acpi_notify_handler handler,
+ input_device_init idev_init)
+{
+ struct input_dev *inputdev;
+ acpi_status status;
+ int error;
+ inputdev = input_allocate_device();
+ if (!inputdev) {
+ error = -ENOMEM;
+ goto out;
+ }
+ inputdev->name = name;
+ inputdev->dev.parent = &acpi->dev;
+ idev_init(inputdev);
+ error = input_register_device(inputdev);
+ if (error)
+ goto err_reg;
+ dev_set_drvdata(&acpi->dev, inputdev);
+ status = acpi_install_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
+ handler, inputdev);
+ if (ACPI_FAILURE(status)) {
+ error = -ENODEV;
+ goto err_acpi;
+ }
+ return 0;
+err_acpi:
+ input_unregister_device(inputdev);
+err_reg:
+ input_free_device(inputdev);
+out:
+ return error;
+}
+
+static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi,
+ acpi_notify_handler handler)
+{
+ struct input_dev *inputdev;
+ acpi_status status;
+ status = acpi_remove_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
+ handler);
+ inputdev = dev_get_drvdata(&acpi->dev);
+ input_unregister_device(inputdev);
+ input_free_device(inputdev);
+ return 0;
+}
+
+
+/*
+ * Accelerometer code.
+ */
+
+static acpi_status cmpc_start_accel(acpi_handle handle)
+{
+ union acpi_object param[2];
+ struct acpi_object_list input;
+ acpi_status status;
+ param[0].type = ACPI_TYPE_INTEGER;
+ param[0].integer.value = 0x3;
+ param[1].type = ACPI_TYPE_INTEGER;
+ input.count = 2;
+ input.pointer = param;
+ status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
+ return status;
+}
+
+static acpi_status cmpc_stop_accel(acpi_handle handle)
+{
+ union acpi_object param[2];
+ struct acpi_object_list input;
+ acpi_status status;
+ param[0].type = ACPI_TYPE_INTEGER;
+ param[0].integer.value = 0x4;
+ param[1].type = ACPI_TYPE_INTEGER;
+ input.count = 2;
+ input.pointer = param;
+ status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
+ return status;
+}
+
+static acpi_status cmpc_accel_set_sense(acpi_handle handle, int val)
+{
+ union acpi_object param[2];
+ struct acpi_object_list input;
+ param[0].type = ACPI_TYPE_INTEGER;
+ param[0].integer.value = 0x02;
+ param[1].type = ACPI_TYPE_INTEGER;
+ param[1].integer.value = val;
+ input.count = 2;
+ input.pointer = param;
+ return acpi_evaluate_object(handle, "ACMD", &input, NULL);
+}
+
+static acpi_status cmpc_get_accel(acpi_handle handle,
+ unsigned char *x,
+ unsigned char *y,
+ unsigned char *z)
+{
+ union acpi_object param[2];
+ struct acpi_object_list input;
+ struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
+ unsigned char *locs;
+ acpi_status status;
+ param[0].type = ACPI_TYPE_INTEGER;
+ param[0].integer.value = 0x01;
+ param[1].type = ACPI_TYPE_INTEGER;
+ input.count = 2;
+ input.pointer = param;
+ status = acpi_evaluate_object(handle, "ACMD", &input, &output);
+ if (ACPI_SUCCESS(status)) {
+ union acpi_object *obj;
+ obj = output.pointer;
+ locs = obj->buffer.pointer;
+ *x = locs[0];
+ *y = locs[1];
+ *z = locs[2];
+ kfree(output.pointer);
+ }
+ return status;
+}
+
+static void cmpc_accel_handler(acpi_handle handle, u32 event, void *ctx)
+{
+ struct input_dev *inputdev = ctx;
+ acpi_status status;
+ unsigned char x, y, z;
+ if (event == 0x81) {
+ status = cmpc_get_accel(handle, &x, &y, &z);
+ if (ACPI_SUCCESS(status)) {
+ input_report_abs(inputdev, ABS_X, x);
+ input_report_abs(inputdev, ABS_Y, y);
+ input_report_abs(inputdev, ABS_Z, z);
+ input_sync(inputdev);
+ }
+ }
+}
+
+static ssize_t cmpc_accel_sense_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct acpi_device *acpi;
+ int sense;
+ acpi = to_acpi_device(dev);
+ if (sscanf(buf, "%d", &sense) <= 0)
+ return -EINVAL;
+ cmpc_accel_set_sense(acpi->handle, sense);
+ return strnlen(buf, count);
+}
+
+struct device_attribute cmpc_accel_sense_attr = {
+ .attr = { .name = "sense", .mode = 0220 },
+ .store = cmpc_accel_sense_store
+};
+
+static int cmpc_accel_open(struct input_dev *input)
+{
+ struct acpi_device *acpi;
+ acpi = to_acpi_device(input->dev.parent);
+ if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
+ return 0;
+ return -EIO;
+}
+
+static void cmpc_accel_close(struct input_dev *input)
+{
+ struct acpi_device *acpi;
+ acpi = to_acpi_device(input->dev.parent);
+ cmpc_stop_accel(acpi->handle);
+}
+
+static void cmpc_accel_idev_init(struct input_dev *inputdev)
+{
+ set_bit(EV_ABS, inputdev->evbit);
+ input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
+ input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
+ input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);
+ inputdev->open = cmpc_accel_open;
+ inputdev->close = cmpc_accel_close;
+}
+
+static int cmpc_accel_add(struct acpi_device *acpi)
+{
+ int error;
+ error = device_create_file(&acpi->dev, &cmpc_accel_sense_attr);
+ if (error)
+ return error;
+ return cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
+ cmpc_accel_handler,
+ cmpc_accel_idev_init);
+}
+
+static int cmpc_accel_remove(struct acpi_device *acpi, int type)
+{
+ device_remove_file(&acpi->dev, &cmpc_accel_sense_attr);
+ return cmpc_remove_acpi_notify_device(acpi, cmpc_accel_handler);
+}
+
+static const struct acpi_device_id cmpc_accel_device_ids[] = {
+ {"ACCE0000", 0},
+ {"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, cmpc_accel_device_ids);
+
+static struct acpi_driver cmpc_accel_acpi_driver = {
+ .name = "cmpc_accel",
+ .class = "cmpc_accel",
+ .ids = cmpc_accel_device_ids,
+ .ops = {
+ .add = cmpc_accel_add,
+ .remove = cmpc_accel_remove
+ }
+};
+
+static bool cmpc_accel_driver_registered;
+
+
+/*
+ * Tablet mode code.
+ */
+static acpi_status cmpc_get_tablet(acpi_handle handle,
+ unsigned long long *value)
+{
+ union acpi_object param;
+ struct acpi_object_list input;
+ unsigned long long output;
+ acpi_status status;
+ param.type = ACPI_TYPE_INTEGER;
+ param.integer.value = 0x01;
+ input.count = 1;
+ input.pointer = &param;
+ status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
+ if (ACPI_SUCCESS(status))
+ *value = output;
+ return status;
+}
+
+static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *ctx)
+{
+ unsigned long long val = 0;
+ struct input_dev *inputdev = ctx;
+ if (event == 0x81) {
+ if (ACPI_SUCCESS(cmpc_get_tablet(handle, &val)))
+ input_report_switch(inputdev, SW_TABLET_MODE, !val);
+ }
+}
+
+static void cmpc_tablet_idev_init(struct input_dev *inputdev)
+{
+ set_bit(EV_SW, inputdev->evbit);
+ set_bit(SW_TABLET_MODE, inputdev->swbit);
+}
+
+static int cmpc_tablet_add(struct acpi_device *acpi)
+{
+ return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
+ cmpc_tablet_handler,
+ cmpc_tablet_idev_init);
+}
+
+static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
+{
+ return cmpc_remove_acpi_notify_device(acpi, cmpc_tablet_handler);
+}
+
+static const struct acpi_device_id cmpc_tablet_device_ids[] = {
+ {"TBLT0000", 0},
+ {"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, cmpc_tablet_device_ids);
+
+static struct acpi_driver cmpc_tablet_acpi_driver = {
+ .name = "cmpc_tablet",
+ .class = "cmpc_tablet",
+ .ids = cmpc_tablet_device_ids,
+ .ops = {
+ .add = cmpc_tablet_add,
+ .remove = cmpc_tablet_remove
+ }
+};
+
+static bool cmpc_tablet_driver_registered;
+
+
+/*
+ * Backlight code.
+ */
+
+static acpi_status cmpc_get_brightness(acpi_handle handle,
+ unsigned long long *value)
+{
+ union acpi_object param;
+ struct acpi_object_list input;
+ unsigned long long output;
+ acpi_status status;
+ param.type = ACPI_TYPE_INTEGER;
+ param.integer.value = 0xC0;
+ input.count = 1;
+ input.pointer = &param;
+ status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
+ if (ACPI_SUCCESS(status))
+ *value = output;
+ return status;
+}
+
+static acpi_status cmpc_set_brightness(acpi_handle handle,
+ unsigned long long value)
+{
+ union acpi_object param[2];
+ struct acpi_object_list input;
+ acpi_status status;
+ unsigned long long output;
+ param[0].type = ACPI_TYPE_INTEGER;
+ param[0].integer.value = 0xC0;
+ param[1].type = ACPI_TYPE_INTEGER;
+ param[1].integer.value = value;
+ input.count = 2;
+ input.pointer = param;
+ status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
+ return status;
+}
+
+static int cmpc_bl_get_brightness(struct backlight_device *bd)
+{
+ acpi_status status;
+ acpi_handle handle;
+ unsigned long long brightness;
+ handle = bl_get_data(bd);
+ status = cmpc_get_brightness(handle, &brightness);
+ if (ACPI_SUCCESS(status))
+ return brightness;
+ else
+ return -1;
+}
+
+static int cmpc_bl_update_status(struct backlight_device *bd)
+{
+ acpi_status status;
+ acpi_handle handle;
+ handle = bl_get_data(bd);
+ status = cmpc_set_brightness(handle, bd->props.brightness);
+ if (ACPI_SUCCESS(status))
+ return 0;
+ else
+ return -1;
+}
+
+static struct backlight_ops cmpc_bl_ops = {
+ .get_brightness = cmpc_bl_get_brightness,
+ .update_status = cmpc_bl_update_status
+};
+
+static int cmpc_bl_add(struct acpi_device *acpi)
+{
+ struct backlight_device *bd;
+ bd = backlight_device_register("cmpc_bl", &acpi->dev,
+ acpi->handle, &cmpc_bl_ops);
+ bd->props.max_brightness = 7;
+ dev_set_drvdata(&acpi->dev, bd);
+ return 0;
+}
+
+static int cmpc_bl_remove(struct acpi_device *acpi, int type)
+{
+ struct backlight_device *bd;
+ bd = dev_get_drvdata(&acpi->dev);
+ backlight_device_unregister(bd);
+ return 0;
+}
+
+static const struct acpi_device_id cmpc_device_ids[] = {
+ {"IPML200", 0},
+ {"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);
+
+static struct acpi_driver cmpc_bl_acpi_driver = {
+ .name = "cmpc",
+ .class = "cmpc",
+ .ids = cmpc_device_ids,
+ .ops = {
+ .add = cmpc_bl_add,
+ .remove = cmpc_bl_remove
+ }
+};
+
+static bool cmpc_bl_driver_registered;
+
+
+/*
+ * Extra keys code.
+ */
+static int cmpc_keys_codes[] = {
+ KEY_UNKNOWN,
+ KEY_WLAN,
+ KEY_SWITCHVIDEOMODE,
+ KEY_BRIGHTNESSDOWN,
+ KEY_BRIGHTNESSUP,
+ KEY_VENDOR,
+ KEY_MAX
+};
+
+static void cmpc_keys_handler(acpi_handle handle, u32 event, void *ctx)
+{
+ struct input_dev *inputdev;
+ int code = KEY_MAX;
+ if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
+ code = cmpc_keys_codes[event & 0x0F];
+ inputdev = ctx;
+ input_report_key(inputdev, code, !(event & 0x10));
+}
+
+static void cmpc_keys_idev_init(struct input_dev *inputdev)
+{
+ int i;
+ set_bit(EV_KEY, inputdev->evbit);
+ for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
+ set_bit(cmpc_keys_codes[i], inputdev->keybit);
+}
+
+static int cmpc_keys_add(struct acpi_device *acpi)
+{
+ return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
+ cmpc_keys_handler,
+ cmpc_keys_idev_init);
+}
+
+static int cmpc_keys_remove(struct acpi_device *acpi, int type)
+{
+ return cmpc_remove_acpi_notify_device(acpi, cmpc_keys_handler);
+}
+
+static const struct acpi_device_id cmpc_keys_device_ids[] = {
+ {"FnBT0000", 0},
+ {"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, cmpc_keys_device_ids);
+
+static struct acpi_driver cmpc_keys_acpi_driver = {
+ .name = "cmpc_keys",
+ .class = "cmpc_keys",
+ .ids = cmpc_keys_device_ids,
+ .ops = {
+ .add = cmpc_keys_add,
+ .remove = cmpc_keys_remove
+ }
+};
+
+static bool cmpc_keys_driver_registered;
+
+
+/*
+ * General init/exit code.
+ */
+
+static int cmpc_init(void)
+{
+ int result;
+ result = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
+ cmpc_keys_driver_registered = !!result;
+ result = acpi_bus_register_driver(&cmpc_bl_acpi_driver);
+ cmpc_bl_driver_registered = !!result;
+ result = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
+ cmpc_tablet_driver_registered = !!result;
+ result = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
+ cmpc_accel_driver_registered = !!result;
+ /*
+ * Not every CMPC has every ACPI device supported here. So always return
+ * success.
+ */
+ return 0;
+}
+
+static void cmpc_exit(void)
+{
+ if (cmpc_accel_driver_registered)
+ acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
+ if (cmpc_tablet_driver_registered)
+ acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
+ if (cmpc_bl_driver_registered)
+ acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
+ if (cmpc_keys_driver_registered)
+ acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
+}
+
+module_init(cmpc_init);
+module_exit(cmpc_exit);
--
1.6.3.3


2009-09-29 09:20:44

by Alan Jenkins

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On 9/29/09, Thadeu Lima de Souza Cascardo <[email protected]> wrote:
> This add supports for devices like keyboard, backlight, tablet and
> accelerometer.
>
> This work is supported by International Syst S/A.
>
> Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>

Hi!

In general this driver was a pleasure to read. It looks like you have
a nice and clean ACPI interface to work with. I haven't seen anything
quite like the cmpc_{add,remove}_acpi_notify_device() functions
before, but they make a lot of sense here.

But I do have a few comments :-).

The "-laptop" naming convention is more informative than "-acpi".
Perhaps this driver should be called "classmate-laptop". I would keep
the cmpc_ prefix in the source code though.

More comments inline.

> +/*
> + * Generic input device code.
> + */
> +
> +typedef void (*input_device_init)(struct input_dev *dev);
> +
> +static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char
> *name,
> + acpi_notify_handler handler,
> + input_device_init idev_init)
> +{
> + struct input_dev *inputdev;
> + acpi_status status;
> + int error;
> + inputdev = input_allocate_device();
> + if (!inputdev) {
> + error = -ENOMEM;
> + goto out;
> + }
> + inputdev->name = name;
> + inputdev->dev.parent = &acpi->dev;
> + idev_init(inputdev);
> + error = input_register_device(inputdev);
> + if (error)
> + goto err_reg;
> + dev_set_drvdata(&acpi->dev, inputdev);
> + status = acpi_install_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> + handler, inputdev);
> + if (ACPI_FAILURE(status)) {
> + error = -ENODEV;
> + goto err_acpi;
> + }
> + return 0;
> +err_acpi:
> + input_unregister_device(inputdev);
> +err_reg:
> + input_free_device(inputdev);
> +out:
> + return error;
> +}
> +
> +static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi,
> + acpi_notify_handler handler)
> +{
> + struct input_dev *inputdev;
> + acpi_status status;
> + status = acpi_remove_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> + handler);
> + inputdev = dev_get_drvdata(&acpi->dev);
> + input_unregister_device(inputdev);
> + input_free_device(inputdev);

Dmitry the input maintainer says "input_free_device() should not be
called after input_unregister_device()." (This also applies to the
error handling in add_acpi_notify_device()).

> + return 0;
> +}
> +
> +
> +/*
> + * Accelerometer code.
> + */
> +
> +static acpi_status cmpc_start_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x3;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}
> +
> +static acpi_status cmpc_stop_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x4;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}
> +
> +static acpi_status cmpc_accel_set_sense(acpi_handle handle, int val)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x02;
> + param[1].type = ACPI_TYPE_INTEGER;
> + param[1].integer.value = val;
> + input.count = 2;
> + input.pointer = param;
> + return acpi_evaluate_object(handle, "ACMD", &input, NULL);
> +}
> +
> +static acpi_status cmpc_get_accel(acpi_handle handle,
> + unsigned char *x,
> + unsigned char *y,
> + unsigned char *z)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
> + unsigned char *locs;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x01;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, &output);
> + if (ACPI_SUCCESS(status)) {
> + union acpi_object *obj;
> + obj = output.pointer;
> + locs = obj->buffer.pointer;
> + *x = locs[0];
> + *y = locs[1];
> + *z = locs[2];
> + kfree(output.pointer);
> + }
> + return status;
> +}
> +
> +static void cmpc_accel_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + struct input_dev *inputdev = ctx;
> + acpi_status status;
> + unsigned char x, y, z;
> + if (event == 0x81) {
> + status = cmpc_get_accel(handle, &x, &y, &z);
> + if (ACPI_SUCCESS(status)) {
> + input_report_abs(inputdev, ABS_X, x);
> + input_report_abs(inputdev, ABS_Y, y);
> + input_report_abs(inputdev, ABS_Z, z);
> + input_sync(inputdev);
> + }
> + }
> +}
> +
> +static ssize_t cmpc_accel_sense_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct acpi_device *acpi;
> + int sense;
> + acpi = to_acpi_device(dev);
> + if (sscanf(buf, "%d", &sense) <= 0)
> + return -EINVAL;
> + cmpc_accel_set_sense(acpi->handle, sense);
> + return strnlen(buf, count);
> +}
> +
> +struct device_attribute cmpc_accel_sense_attr = {
> + .attr = { .name = "sense", .mode = 0220 },
> + .store = cmpc_accel_sense_store
> +};

What does this do? What range of values can it take? Is it a common
attribute for input devices, or does it need specific documentation?

Would it make sense for the driver to initialize it to a default
value, so that we would always know what the current value is, and
provide a .show() method as well?

> +
> +static int cmpc_accel_open(struct input_dev *input)
> +{
> + struct acpi_device *acpi;
> + acpi = to_acpi_device(input->dev.parent);
> + if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
> + return 0;
> + return -EIO;
> +}
> +
> +static void cmpc_accel_close(struct input_dev *input)
> +{
> + struct acpi_device *acpi;
> + acpi = to_acpi_device(input->dev.parent);
> + cmpc_stop_accel(acpi->handle);
> +}
> +
> +static void cmpc_accel_idev_init(struct input_dev *inputdev)
> +{
> + set_bit(EV_ABS, inputdev->evbit);
> + input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
> + input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
> + input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);
> + inputdev->open = cmpc_accel_open;
> + inputdev->close = cmpc_accel_close;
> +}
> +
> +static int cmpc_accel_add(struct acpi_device *acpi)
> +{
> + int error;
> + error = device_create_file(&acpi->dev, &cmpc_accel_sense_attr);
> + if (error)
> + return error;
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
> + cmpc_accel_handler,
> + cmpc_accel_idev_init);
> +}
> +
> +static int cmpc_accel_remove(struct acpi_device *acpi, int type)
> +{
> + device_remove_file(&acpi->dev, &cmpc_accel_sense_attr);
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_accel_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_accel_device_ids[] = {
> + {"ACCE0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_accel_device_ids);
> +
> +static struct acpi_driver cmpc_accel_acpi_driver = {
> + .name = "cmpc_accel",
> + .class = "cmpc_accel",
> + .ids = cmpc_accel_device_ids,
> + .ops = {
> + .add = cmpc_accel_add,
> + .remove = cmpc_accel_remove
> + }

Could you please set

.owner = THIS_MODULE,

on all the acpi drivers? It will provide data in
/sys/module/cmpc_acpi/drivers and
/sys/bus/acpi/drivers/<driver>/module. It seems to be forgotten
knowledge, but I'm trying to revive it :-).

> +};
> +
> +static bool cmpc_accel_driver_registered;
> +
> +
> +/*
> + * Tablet mode code.
> + */
> +static acpi_status cmpc_get_tablet(acpi_handle handle,
> + unsigned long long *value)
> +{
> + union acpi_object param;
> + struct acpi_object_list input;
> + unsigned long long output;
> + acpi_status status;
> + param.type = ACPI_TYPE_INTEGER;
> + param.integer.value = 0x01;
> + input.count = 1;
> + input.pointer = &param;
> + status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
> + if (ACPI_SUCCESS(status))
> + *value = output;
> + return status;
> +}
> +
> +static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + unsigned long long val = 0;
> + struct input_dev *inputdev = ctx;
> + if (event == 0x81) {
> + if (ACPI_SUCCESS(cmpc_get_tablet(handle, &val)))
> + input_report_switch(inputdev, SW_TABLET_MODE, !val);
> + }
> +}
> +
> +static void cmpc_tablet_idev_init(struct input_dev *inputdev)
> +{
> + set_bit(EV_SW, inputdev->evbit);
> + set_bit(SW_TABLET_MODE, inputdev->swbit);

Don't you need to initialize the switch value here?

Also, have you tested this with changing the switch value while
suspended? I _think_ you need to update the switch state on resume.
This is purely from looking at other acpi drivers and their evolution;
I don't have any practical experience with input drivers.

> +}
> +
> +static int cmpc_tablet_add(struct acpi_device *acpi)
> +{
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
> + cmpc_tablet_handler,
> + cmpc_tablet_idev_init);
> +}
> +
> +static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
> +{
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_tablet_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_tablet_device_ids[] = {
> + {"TBLT0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_tablet_device_ids);
> +
> +static struct acpi_driver cmpc_tablet_acpi_driver = {
> + .name = "cmpc_tablet",
> + .class = "cmpc_tablet",
> + .ids = cmpc_tablet_device_ids,
> + .ops = {
> + .add = cmpc_tablet_add,
> + .remove = cmpc_tablet_remove
> + }
> +};
> +
> +static bool cmpc_tablet_driver_registered;
> +
> +
> +/*
> + * Backlight code.
> + */
> +
> +static acpi_status cmpc_get_brightness(acpi_handle handle,
> + unsigned long long *value)
> +{
> + union acpi_object param;
> + struct acpi_object_list input;
> + unsigned long long output;
> + acpi_status status;
> + param.type = ACPI_TYPE_INTEGER;
> + param.integer.value = 0xC0;
> + input.count = 1;
> + input.pointer = &param;
> + status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
> + if (ACPI_SUCCESS(status))
> + *value = output;
> + return status;
> +}
> +
> +static acpi_status cmpc_set_brightness(acpi_handle handle,
> + unsigned long long value)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + unsigned long long output;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0xC0;
> + param[1].type = ACPI_TYPE_INTEGER;
> + param[1].integer.value = value;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
> + return status;
> +}
> +
> +static int cmpc_bl_get_brightness(struct backlight_device *bd)
> +{
> + acpi_status status;
> + acpi_handle handle;
> + unsigned long long brightness;
> + handle = bl_get_data(bd);
> + status = cmpc_get_brightness(handle, &brightness);
> + if (ACPI_SUCCESS(status))
> + return brightness;
> + else
> + return -1;
> +}
> +
> +static int cmpc_bl_update_status(struct backlight_device *bd)
> +{
> + acpi_status status;
> + acpi_handle handle;
> + handle = bl_get_data(bd);
> + status = cmpc_set_brightness(handle, bd->props.brightness);
> + if (ACPI_SUCCESS(status))
> + return 0;
> + else
> + return -1;
> +}
> +
> +static struct backlight_ops cmpc_bl_ops = {
> + .get_brightness = cmpc_bl_get_brightness,
> + .update_status = cmpc_bl_update_status
> +};
> +
> +static int cmpc_bl_add(struct acpi_device *acpi)
> +{
> + struct backlight_device *bd;
> + bd = backlight_device_register("cmpc_bl", &acpi->dev,
> + acpi->handle, &cmpc_bl_ops);
> + bd->props.max_brightness = 7;
> + dev_set_drvdata(&acpi->dev, bd);
> + return 0;
> +}
> +
> +static int cmpc_bl_remove(struct acpi_device *acpi, int type)
> +{
> + struct backlight_device *bd;
> + bd = dev_get_drvdata(&acpi->dev);
> + backlight_device_unregister(bd);
> + return 0;
> +}
> +
> +static const struct acpi_device_id cmpc_device_ids[] = {
> + {"IPML200", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);
> +
> +static struct acpi_driver cmpc_bl_acpi_driver = {
> + .name = "cmpc",
> + .class = "cmpc",
> + .ids = cmpc_device_ids,
> + .ops = {
> + .add = cmpc_bl_add,
> + .remove = cmpc_bl_remove
> + }
> +};
> +
> +static bool cmpc_bl_driver_registered;
> +
> +
> +/*
> + * Extra keys code.
> + */
> +static int cmpc_keys_codes[] = {
> + KEY_UNKNOWN,
> + KEY_WLAN,
> + KEY_SWITCHVIDEOMODE,

> + KEY_BRIGHTNESSDOWN,
> + KEY_BRIGHTNESSUP,

Please confirm that the brightness keys do not directly affect the
backlight, and these key events are a signal for userspace to adjust
the backlight itself.

If they _do_ affect the backlight, you should call the newly added
backlight_force_update() when they are pressed, and it will generate a
uevent on the backlight device. (I guess you will have to add an ugly
global variable for the backlight device, sorry about that). In this
case you should ideally avoid generating _input_ events for brightness
keys.

Currently userspace is expected to handle annoying devices which
generate KEY_BRIGHTNESS* while directly changing the brightness. But
requires a racy hack, so you should definitely try to avoid this for
new devices. It will encourage userspace to implement support for the
backlight device uevents :-).

> + KEY_VENDOR,
> + KEY_MAX
> +};
> +
> +static void cmpc_keys_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + struct input_dev *inputdev;
> + int code = KEY_MAX;
> + if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
> + code = cmpc_keys_codes[event & 0x0F];
> + inputdev = ctx;
> + input_report_key(inputdev, code, !(event & 0x10));
> +}
> +
> +static void cmpc_keys_idev_init(struct input_dev *inputdev)
> +{
> + int i;
> + set_bit(EV_KEY, inputdev->evbit);
> + for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
> + set_bit(cmpc_keys_codes[i], inputdev->keybit);
> +}
> +
> +static int cmpc_keys_add(struct acpi_device *acpi)
> +{
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
> + cmpc_keys_handler,
> + cmpc_keys_idev_init);
> +}
> +
> +static int cmpc_keys_remove(struct acpi_device *acpi, int type)
> +{
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_keys_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_keys_device_ids[] = {
> + {"FnBT0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_keys_device_ids);
> +
> +static struct acpi_driver cmpc_keys_acpi_driver = {
> + .name = "cmpc_keys",
> + .class = "cmpc_keys",
> + .ids = cmpc_keys_device_ids,
> + .ops = {
> + .add = cmpc_keys_add,
> + .remove = cmpc_keys_remove
> + }
> +};
> +
> +static bool cmpc_keys_driver_registered;
> +
> +
> +/*
> + * General init/exit code.
> + */
> +
> +static int cmpc_init(void)
> +{
> + int result;
> + result = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
> + cmpc_keys_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_bl_acpi_driver);
> + cmpc_bl_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
> + cmpc_tablet_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
> + cmpc_accel_driver_registered = !!result;

These _registered flags are not necessary. Registration doesn't fail
if the hardware doesn't exist. It only fails if acpi=off, or on
-ENOMEM. It would be reasonable to fail loading the module if one of
the drivers fails to register.

> + /*
> + * Not every CMPC has every ACPI device supported here. So always return
> + * success.
> + */
> + return 0;
> +}
> +
> +static void cmpc_exit(void)
> +{
> + if (cmpc_accel_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
> + if (cmpc_tablet_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
> + if (cmpc_bl_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
> + if (cmpc_keys_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
> +}
> +
> +module_init(cmpc_init);
> +module_exit(cmpc_exit);
> --
> 1.6.3.3
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Tue, Sep 29, 2009 at 10:20:44AM +0100, Alan Jenkins wrote:
> On 9/29/09, Thadeu Lima de Souza Cascardo <[email protected]> wrote:
> > This add supports for devices like keyboard, backlight, tablet and
> > accelerometer.
> >
> > This work is supported by International Syst S/A.
> >
> > Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
>
> Hi!
>
> In general this driver was a pleasure to read. It looks like you have
> a nice and clean ACPI interface to work with. I haven't seen anything
> quite like the cmpc_{add,remove}_acpi_notify_device() functions
> before, but they make a lot of sense here.
>
> But I do have a few comments :-).
>
> The "-laptop" naming convention is more informative than "-acpi".
> Perhaps this driver should be called "classmate-laptop". I would keep
> the cmpc_ prefix in the source code though.
>
> More comments inline.
>

Hello, Allan.

Thanks for the feedback. I am considering an investigation whether we
have lots of other ACPI input devices that could share some code like
{add,remove}_acpi_notify_device. Regarding the driver naming, I will
send a second version with it and other modifications considering your
feedback and that of other people too.

I will also ask for some explicit feedback and add linux-input and
Dmitry to the loop.

> > +/*
> > + * Generic input device code.
> > + */
> > +
> > +typedef void (*input_device_init)(struct input_dev *dev);
> > +
> > +static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char
> > *name,
> > + acpi_notify_handler handler,
> > + input_device_init idev_init)
> > +{
> > + struct input_dev *inputdev;
> > + acpi_status status;
> > + int error;
> > + inputdev = input_allocate_device();
> > + if (!inputdev) {
> > + error = -ENOMEM;
> > + goto out;
> > + }
> > + inputdev->name = name;
> > + inputdev->dev.parent = &acpi->dev;
> > + idev_init(inputdev);
> > + error = input_register_device(inputdev);
> > + if (error)
> > + goto err_reg;
> > + dev_set_drvdata(&acpi->dev, inputdev);
> > + status = acpi_install_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> > + handler, inputdev);
> > + if (ACPI_FAILURE(status)) {
> > + error = -ENODEV;
> > + goto err_acpi;
> > + }
> > + return 0;
> > +err_acpi:
> > + input_unregister_device(inputdev);
> > +err_reg:
> > + input_free_device(inputdev);
> > +out:
> > + return error;
> > +}
> > +
> > +static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi,
> > + acpi_notify_handler handler)
> > +{
> > + struct input_dev *inputdev;
> > + acpi_status status;
> > + status = acpi_remove_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> > + handler);
> > + inputdev = dev_get_drvdata(&acpi->dev);
> > + input_unregister_device(inputdev);
> > + input_free_device(inputdev);
>
> Dmitry the input maintainer says "input_free_device() should not be
> called after input_unregister_device()." (This also applies to the
> error handling in add_acpi_notify_device()).
>

That's right. My mistake here. And I've hit this mistake before.
input_free_device calls put_device (through input_put_device) and
input_unregister_device calls device_unregister, which also calls
put_device.

> > + return 0;
> > +}
> > +
> > +
> > +/*
> > + * Accelerometer code.
> > + */
> > +
> > +static acpi_status cmpc_start_accel(acpi_handle handle)
> > +{
> > + union acpi_object param[2];
> > + struct acpi_object_list input;
> > + acpi_status status;
> > + param[0].type = ACPI_TYPE_INTEGER;
> > + param[0].integer.value = 0x3;
> > + param[1].type = ACPI_TYPE_INTEGER;
> > + input.count = 2;
> > + input.pointer = param;
> > + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> > + return status;
> > +}
> > +
> > +static acpi_status cmpc_stop_accel(acpi_handle handle)
> > +{
> > + union acpi_object param[2];
> > + struct acpi_object_list input;
> > + acpi_status status;
> > + param[0].type = ACPI_TYPE_INTEGER;
> > + param[0].integer.value = 0x4;
> > + param[1].type = ACPI_TYPE_INTEGER;
> > + input.count = 2;
> > + input.pointer = param;
> > + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> > + return status;
> > +}
> > +
> > +static acpi_status cmpc_accel_set_sense(acpi_handle handle, int val)
> > +{
> > + union acpi_object param[2];
> > + struct acpi_object_list input;
> > + param[0].type = ACPI_TYPE_INTEGER;
> > + param[0].integer.value = 0x02;
> > + param[1].type = ACPI_TYPE_INTEGER;
> > + param[1].integer.value = val;
> > + input.count = 2;
> > + input.pointer = param;
> > + return acpi_evaluate_object(handle, "ACMD", &input, NULL);
> > +}
> > +
> > +static acpi_status cmpc_get_accel(acpi_handle handle,
> > + unsigned char *x,
> > + unsigned char *y,
> > + unsigned char *z)
> > +{
> > + union acpi_object param[2];
> > + struct acpi_object_list input;
> > + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
> > + unsigned char *locs;
> > + acpi_status status;
> > + param[0].type = ACPI_TYPE_INTEGER;
> > + param[0].integer.value = 0x01;
> > + param[1].type = ACPI_TYPE_INTEGER;
> > + input.count = 2;
> > + input.pointer = param;
> > + status = acpi_evaluate_object(handle, "ACMD", &input, &output);
> > + if (ACPI_SUCCESS(status)) {
> > + union acpi_object *obj;
> > + obj = output.pointer;
> > + locs = obj->buffer.pointer;
> > + *x = locs[0];
> > + *y = locs[1];
> > + *z = locs[2];
> > + kfree(output.pointer);
> > + }
> > + return status;
> > +}
> > +
> > +static void cmpc_accel_handler(acpi_handle handle, u32 event, void *ctx)
> > +{
> > + struct input_dev *inputdev = ctx;
> > + acpi_status status;
> > + unsigned char x, y, z;
> > + if (event == 0x81) {
> > + status = cmpc_get_accel(handle, &x, &y, &z);
> > + if (ACPI_SUCCESS(status)) {
> > + input_report_abs(inputdev, ABS_X, x);
> > + input_report_abs(inputdev, ABS_Y, y);
> > + input_report_abs(inputdev, ABS_Z, z);
> > + input_sync(inputdev);
> > + }
> > + }
> > +}
> > +
> > +static ssize_t cmpc_accel_sense_store(struct device *dev,
> > + struct device_attribute *attr,
> > + const char *buf, size_t count)
> > +{
> > + struct acpi_device *acpi;
> > + int sense;
> > + acpi = to_acpi_device(dev);
> > + if (sscanf(buf, "%d", &sense) <= 0)
> > + return -EINVAL;
> > + cmpc_accel_set_sense(acpi->handle, sense);
> > + return strnlen(buf, count);
> > +}
> > +
> > +struct device_attribute cmpc_accel_sense_attr = {
> > + .attr = { .name = "sense", .mode = 0220 },
> > + .store = cmpc_accel_sense_store
> > +};
>
> What does this do? What range of values can it take? Is it a common
> attribute for input devices, or does it need specific documentation?
>
> Would it make sense for the driver to initialize it to a default
> value, so that we would always know what the current value is, and
> provide a .show() method as well?
>

This changes the accelerometer device sensitivity. I will take a look at
the ACPI tables to get a range. If very much sensitive, the device will
generate too much ACPI notifications, even when the classmate PC is no a
table and there seems to be no movement. If not very much sensitive, you
must throw it spinning into the air to get anything. :-)

I will pick a default value, although I think we could let it to
userspace, but a sensible default value will not hurt here. As far as I
know, there is no ACPI method to do get_sense, but we can mirror the
last value written and provide a .show.

What do you recommend as a documentation? Something at
Documentation/ABI/testing/, perhaps? I don't know if there are any other
devices with something similar, but I would not be surprised if there
are some of them.

> > +
> > +static int cmpc_accel_open(struct input_dev *input)
> > +{
> > + struct acpi_device *acpi;
> > + acpi = to_acpi_device(input->dev.parent);
> > + if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
> > + return 0;
> > + return -EIO;
> > +}
> > +
> > +static void cmpc_accel_close(struct input_dev *input)
> > +{
> > + struct acpi_device *acpi;
> > + acpi = to_acpi_device(input->dev.parent);
> > + cmpc_stop_accel(acpi->handle);
> > +}
> > +
> > +static void cmpc_accel_idev_init(struct input_dev *inputdev)
> > +{
> > + set_bit(EV_ABS, inputdev->evbit);
> > + input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
> > + input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
> > + input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);

Any hints on how to pick up values for this calibration? Any testing
procedure or anything?

> > + inputdev->open = cmpc_accel_open;
> > + inputdev->close = cmpc_accel_close;
> > +}
> > +
> > +static int cmpc_accel_add(struct acpi_device *acpi)
> > +{
> > + int error;
> > + error = device_create_file(&acpi->dev, &cmpc_accel_sense_attr);
> > + if (error)
> > + return error;
> > + return cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
> > + cmpc_accel_handler,
> > + cmpc_accel_idev_init);
> > +}
> > +
> > +static int cmpc_accel_remove(struct acpi_device *acpi, int type)
> > +{
> > + device_remove_file(&acpi->dev, &cmpc_accel_sense_attr);
> > + return cmpc_remove_acpi_notify_device(acpi, cmpc_accel_handler);
> > +}
> > +
> > +static const struct acpi_device_id cmpc_accel_device_ids[] = {
> > + {"ACCE0000", 0},
> > + {"", 0}
> > +};
> > +MODULE_DEVICE_TABLE(acpi, cmpc_accel_device_ids);
> > +
> > +static struct acpi_driver cmpc_accel_acpi_driver = {
> > + .name = "cmpc_accel",
> > + .class = "cmpc_accel",
> > + .ids = cmpc_accel_device_ids,
> > + .ops = {
> > + .add = cmpc_accel_add,
> > + .remove = cmpc_accel_remove
> > + }
>
> Could you please set
>
> .owner = THIS_MODULE,
>
> on all the acpi drivers? It will provide data in
> /sys/module/cmpc_acpi/drivers and
> /sys/bus/acpi/drivers/<driver>/module. It seems to be forgotten
> knowledge, but I'm trying to revive it :-).
>

Consider it done. Although I think this could be done automatically
someday.

> > +};
> > +
> > +static bool cmpc_accel_driver_registered;
> > +
> > +
> > +/*
> > + * Tablet mode code.
> > + */
> > +static acpi_status cmpc_get_tablet(acpi_handle handle,
> > + unsigned long long *value)
> > +{
> > + union acpi_object param;
> > + struct acpi_object_list input;
> > + unsigned long long output;
> > + acpi_status status;
> > + param.type = ACPI_TYPE_INTEGER;
> > + param.integer.value = 0x01;
> > + input.count = 1;
> > + input.pointer = &param;
> > + status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
> > + if (ACPI_SUCCESS(status))
> > + *value = output;
> > + return status;
> > +}
> > +
> > +static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *ctx)
> > +{
> > + unsigned long long val = 0;
> > + struct input_dev *inputdev = ctx;
> > + if (event == 0x81) {
> > + if (ACPI_SUCCESS(cmpc_get_tablet(handle, &val)))
> > + input_report_switch(inputdev, SW_TABLET_MODE, !val);
> > + }
> > +}
> > +
> > +static void cmpc_tablet_idev_init(struct input_dev *inputdev)
> > +{
> > + set_bit(EV_SW, inputdev->evbit);
> > + set_bit(SW_TABLET_MODE, inputdev->swbit);
>
> Don't you need to initialize the switch value here?
>

No, input_allocate_device does kzalloc. Otherwise, this would apply to
the other bitmaps as well.

> Also, have you tested this with changing the switch value while
> suspended? I _think_ you need to update the switch state on resume.
> This is purely from looking at other acpi drivers and their evolution;
> I don't have any practical experience with input drivers.
>

Interesting point. I will do the testing.

> > +}
> > +
> > +static int cmpc_tablet_add(struct acpi_device *acpi)
> > +{
> > + return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
> > + cmpc_tablet_handler,
> > + cmpc_tablet_idev_init);
> > +}
> > +
> > +static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
> > +{
> > + return cmpc_remove_acpi_notify_device(acpi, cmpc_tablet_handler);
> > +}
> > +
> > +static const struct acpi_device_id cmpc_tablet_device_ids[] = {
> > + {"TBLT0000", 0},
> > + {"", 0}
> > +};
> > +MODULE_DEVICE_TABLE(acpi, cmpc_tablet_device_ids);
> > +
> > +static struct acpi_driver cmpc_tablet_acpi_driver = {
> > + .name = "cmpc_tablet",
> > + .class = "cmpc_tablet",
> > + .ids = cmpc_tablet_device_ids,
> > + .ops = {
> > + .add = cmpc_tablet_add,
> > + .remove = cmpc_tablet_remove
> > + }
> > +};
> > +
> > +static bool cmpc_tablet_driver_registered;
> > +
> > +
> > +/*
> > + * Backlight code.
> > + */
> > +
> > +static acpi_status cmpc_get_brightness(acpi_handle handle,
> > + unsigned long long *value)
> > +{
> > + union acpi_object param;
> > + struct acpi_object_list input;
> > + unsigned long long output;
> > + acpi_status status;
> > + param.type = ACPI_TYPE_INTEGER;
> > + param.integer.value = 0xC0;
> > + input.count = 1;
> > + input.pointer = &param;
> > + status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
> > + if (ACPI_SUCCESS(status))
> > + *value = output;
> > + return status;
> > +}
> > +
> > +static acpi_status cmpc_set_brightness(acpi_handle handle,
> > + unsigned long long value)
> > +{
> > + union acpi_object param[2];
> > + struct acpi_object_list input;
> > + acpi_status status;
> > + unsigned long long output;
> > + param[0].type = ACPI_TYPE_INTEGER;
> > + param[0].integer.value = 0xC0;
> > + param[1].type = ACPI_TYPE_INTEGER;
> > + param[1].integer.value = value;
> > + input.count = 2;
> > + input.pointer = param;
> > + status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
> > + return status;
> > +}
> > +
> > +static int cmpc_bl_get_brightness(struct backlight_device *bd)
> > +{
> > + acpi_status status;
> > + acpi_handle handle;
> > + unsigned long long brightness;
> > + handle = bl_get_data(bd);
> > + status = cmpc_get_brightness(handle, &brightness);
> > + if (ACPI_SUCCESS(status))
> > + return brightness;
> > + else
> > + return -1;
> > +}
> > +
> > +static int cmpc_bl_update_status(struct backlight_device *bd)
> > +{
> > + acpi_status status;
> > + acpi_handle handle;
> > + handle = bl_get_data(bd);
> > + status = cmpc_set_brightness(handle, bd->props.brightness);
> > + if (ACPI_SUCCESS(status))
> > + return 0;
> > + else
> > + return -1;
> > +}
> > +
> > +static struct backlight_ops cmpc_bl_ops = {
> > + .get_brightness = cmpc_bl_get_brightness,
> > + .update_status = cmpc_bl_update_status
> > +};
> > +
> > +static int cmpc_bl_add(struct acpi_device *acpi)
> > +{
> > + struct backlight_device *bd;
> > + bd = backlight_device_register("cmpc_bl", &acpi->dev,
> > + acpi->handle, &cmpc_bl_ops);
> > + bd->props.max_brightness = 7;
> > + dev_set_drvdata(&acpi->dev, bd);
> > + return 0;
> > +}
> > +
> > +static int cmpc_bl_remove(struct acpi_device *acpi, int type)
> > +{
> > + struct backlight_device *bd;
> > + bd = dev_get_drvdata(&acpi->dev);
> > + backlight_device_unregister(bd);
> > + return 0;
> > +}
> > +
> > +static const struct acpi_device_id cmpc_device_ids[] = {
> > + {"IPML200", 0},
> > + {"", 0}
> > +};
> > +MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);
> > +
> > +static struct acpi_driver cmpc_bl_acpi_driver = {
> > + .name = "cmpc",
> > + .class = "cmpc",
> > + .ids = cmpc_device_ids,
> > + .ops = {
> > + .add = cmpc_bl_add,
> > + .remove = cmpc_bl_remove
> > + }
> > +};
> > +
> > +static bool cmpc_bl_driver_registered;
> > +
> > +
> > +/*
> > + * Extra keys code.
> > + */
> > +static int cmpc_keys_codes[] = {
> > + KEY_UNKNOWN,
> > + KEY_WLAN,
> > + KEY_SWITCHVIDEOMODE,
>
> > + KEY_BRIGHTNESSDOWN,
> > + KEY_BRIGHTNESSUP,
>
> Please confirm that the brightness keys do not directly affect the
> backlight, and these key events are a signal for userspace to adjust
> the backlight itself.
>

AFAIK, they don't. We even had to write a simple userspace daemon that
would change the backlight values through sysfs when receiving an input
event.

> If they _do_ affect the backlight, you should call the newly added
> backlight_force_update() when they are pressed, and it will generate a
> uevent on the backlight device. (I guess you will have to add an ugly
> global variable for the backlight device, sorry about that). In this
> case you should ideally avoid generating _input_ events for brightness
> keys.
>
> Currently userspace is expected to handle annoying devices which
> generate KEY_BRIGHTNESS* while directly changing the brightness. But
> requires a racy hack, so you should definitely try to avoid this for
> new devices. It will encourage userspace to implement support for the
> backlight device uevents :-).
>
> > + KEY_VENDOR,

This key is at the side of the LCD panel, not on the keyboard proper and
it has the drawing of a house. Any suggestions besides using KEY_VENDOR?

> > + KEY_MAX
> > +};
> > +
> > +static void cmpc_keys_handler(acpi_handle handle, u32 event, void *ctx)
> > +{
> > + struct input_dev *inputdev;
> > + int code = KEY_MAX;
> > + if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
> > + code = cmpc_keys_codes[event & 0x0F];
> > + inputdev = ctx;
> > + input_report_key(inputdev, code, !(event & 0x10));
> > +}
> > +
> > +static void cmpc_keys_idev_init(struct input_dev *inputdev)
> > +{
> > + int i;
> > + set_bit(EV_KEY, inputdev->evbit);
> > + for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
> > + set_bit(cmpc_keys_codes[i], inputdev->keybit);
> > +}
> > +
> > +static int cmpc_keys_add(struct acpi_device *acpi)
> > +{
> > + return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
> > + cmpc_keys_handler,
> > + cmpc_keys_idev_init);
> > +}
> > +
> > +static int cmpc_keys_remove(struct acpi_device *acpi, int type)
> > +{
> > + return cmpc_remove_acpi_notify_device(acpi, cmpc_keys_handler);
> > +}
> > +
> > +static const struct acpi_device_id cmpc_keys_device_ids[] = {
> > + {"FnBT0000", 0},
> > + {"", 0}
> > +};
> > +MODULE_DEVICE_TABLE(acpi, cmpc_keys_device_ids);
> > +
> > +static struct acpi_driver cmpc_keys_acpi_driver = {
> > + .name = "cmpc_keys",
> > + .class = "cmpc_keys",
> > + .ids = cmpc_keys_device_ids,
> > + .ops = {
> > + .add = cmpc_keys_add,
> > + .remove = cmpc_keys_remove
> > + }
> > +};
> > +
> > +static bool cmpc_keys_driver_registered;
> > +
> > +
> > +/*
> > + * General init/exit code.
> > + */
> > +
> > +static int cmpc_init(void)
> > +{
> > + int result;
> > + result = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
> > + cmpc_keys_driver_registered = !!result;
> > + result = acpi_bus_register_driver(&cmpc_bl_acpi_driver);
> > + cmpc_bl_driver_registered = !!result;
> > + result = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
> > + cmpc_tablet_driver_registered = !!result;
> > + result = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
> > + cmpc_accel_driver_registered = !!result;
>
> These _registered flags are not necessary. Registration doesn't fail
> if the hardware doesn't exist. It only fails if acpi=off, or on
> -ENOMEM. It would be reasonable to fail loading the module if one of
> the drivers fails to register.
>

OK. I had some doubts when writing it this way. I will rewrite it.

> > + /*
> > + * Not every CMPC has every ACPI device supported here. So always return
> > + * success.
> > + */
> > + return 0;
> > +}
> > +
> > +static void cmpc_exit(void)
> > +{
> > + if (cmpc_accel_driver_registered)
> > + acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
> > + if (cmpc_tablet_driver_registered)
> > + acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
> > + if (cmpc_bl_driver_registered)
> > + acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
> > + if (cmpc_keys_driver_registered)
> > + acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
> > +}
> > +
> > +module_init(cmpc_init);
> > +module_exit(cmpc_exit);
> > --
> > 1.6.3.3

Regards,
Thadeu Cascardo.


Attachments:
(No filename) (19.21 kB)
signature.asc (197.00 B)
Digital signature
Download all attachments

2009-09-29 16:12:16

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Monday 28 September 2009 07:38:00 pm Thadeu Lima de Souza Cascardo wrote:
> This add supports for devices like keyboard, backlight, tablet and
> accelerometer.
>
> This work is supported by International Syst S/A.

You should supply .notify() methods in the acpi_driver structs --
then you can get rid of cmpc_add_acpi_notify_device() (or maybe just
have it do the input_allocate_device()).

Please insert a blank line between the automatic variable list
and the first line of executable code.

Bjorn

> Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
> ---
> MAINTAINERS | 6 +
> drivers/platform/x86/Kconfig | 12 +
> drivers/platform/x86/Makefile | 1 +
> drivers/platform/x86/cmpc_acpi.c | 522 ++++++++++++++++++++++++++++++++++++++
> 4 files changed, 541 insertions(+), 0 deletions(-)
> create mode 100644 drivers/platform/x86/cmpc_acpi.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index c450f3a..9e14df1 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -1368,6 +1368,12 @@ L: [email protected]
> S: Supported
> F: drivers/scsi/fnic/
>
> +CMPC ACPI DRIVER
> +M: Thadeu Lima de Souza Cascardo <[email protected]>
> +M: Daniel Oliveira Nascimento <[email protected]>
> +S: Supported
> +F: drivers/platform/x86/cmpc_acpi.c
> +
> CODA FILE SYSTEM
> M: Jan Harkes <[email protected]>
> M: [email protected]
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 55ca39d..2cbc6a6 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -435,4 +435,16 @@ config ACPI_TOSHIBA
>
> If you have a legacy free Toshiba laptop (such as the Libretto L1
> series), say Y.
> +
> +config ACPI_CMPC
> + tristate "CMPC Laptop Extras"
> + depends on X86
> + select INPUT
> + select BACKLIGHT_CLASS_DEVICE
> + default n
> + help
> + Support for Intel Classmate PC ACPI devices, including some
> + keys as input device, backlight device, tablet and accelerometer
> + devices.
> +
> endif # X86_PLATFORM_DEVICES
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index d1c1621..fc92247 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -21,3 +21,4 @@ obj-$(CONFIG_ACPI_WMI) += wmi.o
> obj-$(CONFIG_ACPI_ASUS) += asus_acpi.o
> obj-$(CONFIG_TOPSTAR_LAPTOP) += topstar-laptop.o
> obj-$(CONFIG_ACPI_TOSHIBA) += toshiba_acpi.o
> +obj-$(CONFIG_ACPI_CMPC) += cmpc_acpi.o
> diff --git a/drivers/platform/x86/cmpc_acpi.c b/drivers/platform/x86/cmpc_acpi.c
> new file mode 100644
> index 0000000..c77c855
> --- /dev/null
> +++ b/drivers/platform/x86/cmpc_acpi.c
> @@ -0,0 +1,522 @@
> +/*
> + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <[email protected]>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/workqueue.h>
> +#include <acpi/acpi_drivers.h>
> +#include <linux/backlight.h>
> +#include <linux/input.h>
> +
> +MODULE_LICENSE("GPL");
> +
> +
> +/*
> + * Generic input device code.
> + */
> +
> +typedef void (*input_device_init)(struct input_dev *dev);
> +
> +static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
> + acpi_notify_handler handler,
> + input_device_init idev_init)
> +{
> + struct input_dev *inputdev;
> + acpi_status status;
> + int error;
> + inputdev = input_allocate_device();
> + if (!inputdev) {
> + error = -ENOMEM;
> + goto out;
> + }
> + inputdev->name = name;
> + inputdev->dev.parent = &acpi->dev;
> + idev_init(inputdev);
> + error = input_register_device(inputdev);
> + if (error)
> + goto err_reg;
> + dev_set_drvdata(&acpi->dev, inputdev);
> + status = acpi_install_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> + handler, inputdev);
> + if (ACPI_FAILURE(status)) {
> + error = -ENODEV;
> + goto err_acpi;
> + }
> + return 0;
> +err_acpi:
> + input_unregister_device(inputdev);
> +err_reg:
> + input_free_device(inputdev);
> +out:
> + return error;
> +}
> +
> +static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi,
> + acpi_notify_handler handler)
> +{
> + struct input_dev *inputdev;
> + acpi_status status;
> + status = acpi_remove_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> + handler);
> + inputdev = dev_get_drvdata(&acpi->dev);
> + input_unregister_device(inputdev);
> + input_free_device(inputdev);
> + return 0;
> +}
> +
> +
> +/*
> + * Accelerometer code.
> + */
> +
> +static acpi_status cmpc_start_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x3;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}
> +
> +static acpi_status cmpc_stop_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x4;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}
> +
> +static acpi_status cmpc_accel_set_sense(acpi_handle handle, int val)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x02;
> + param[1].type = ACPI_TYPE_INTEGER;
> + param[1].integer.value = val;
> + input.count = 2;
> + input.pointer = param;
> + return acpi_evaluate_object(handle, "ACMD", &input, NULL);
> +}
> +
> +static acpi_status cmpc_get_accel(acpi_handle handle,
> + unsigned char *x,
> + unsigned char *y,
> + unsigned char *z)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
> + unsigned char *locs;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x01;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, &output);
> + if (ACPI_SUCCESS(status)) {
> + union acpi_object *obj;
> + obj = output.pointer;
> + locs = obj->buffer.pointer;
> + *x = locs[0];
> + *y = locs[1];
> + *z = locs[2];
> + kfree(output.pointer);
> + }
> + return status;
> +}
> +
> +static void cmpc_accel_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + struct input_dev *inputdev = ctx;
> + acpi_status status;
> + unsigned char x, y, z;
> + if (event == 0x81) {
> + status = cmpc_get_accel(handle, &x, &y, &z);
> + if (ACPI_SUCCESS(status)) {
> + input_report_abs(inputdev, ABS_X, x);
> + input_report_abs(inputdev, ABS_Y, y);
> + input_report_abs(inputdev, ABS_Z, z);
> + input_sync(inputdev);
> + }
> + }
> +}
> +
> +static ssize_t cmpc_accel_sense_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct acpi_device *acpi;
> + int sense;
> + acpi = to_acpi_device(dev);
> + if (sscanf(buf, "%d", &sense) <= 0)
> + return -EINVAL;
> + cmpc_accel_set_sense(acpi->handle, sense);
> + return strnlen(buf, count);
> +}
> +
> +struct device_attribute cmpc_accel_sense_attr = {
> + .attr = { .name = "sense", .mode = 0220 },
> + .store = cmpc_accel_sense_store
> +};
> +
> +static int cmpc_accel_open(struct input_dev *input)
> +{
> + struct acpi_device *acpi;
> + acpi = to_acpi_device(input->dev.parent);
> + if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
> + return 0;
> + return -EIO;
> +}
> +
> +static void cmpc_accel_close(struct input_dev *input)
> +{
> + struct acpi_device *acpi;
> + acpi = to_acpi_device(input->dev.parent);
> + cmpc_stop_accel(acpi->handle);
> +}
> +
> +static void cmpc_accel_idev_init(struct input_dev *inputdev)
> +{
> + set_bit(EV_ABS, inputdev->evbit);
> + input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
> + input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
> + input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);
> + inputdev->open = cmpc_accel_open;
> + inputdev->close = cmpc_accel_close;
> +}
> +
> +static int cmpc_accel_add(struct acpi_device *acpi)
> +{
> + int error;
> + error = device_create_file(&acpi->dev, &cmpc_accel_sense_attr);
> + if (error)
> + return error;
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
> + cmpc_accel_handler,
> + cmpc_accel_idev_init);
> +}
> +
> +static int cmpc_accel_remove(struct acpi_device *acpi, int type)
> +{
> + device_remove_file(&acpi->dev, &cmpc_accel_sense_attr);
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_accel_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_accel_device_ids[] = {
> + {"ACCE0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_accel_device_ids);
> +
> +static struct acpi_driver cmpc_accel_acpi_driver = {
> + .name = "cmpc_accel",
> + .class = "cmpc_accel",
> + .ids = cmpc_accel_device_ids,
> + .ops = {
> + .add = cmpc_accel_add,
> + .remove = cmpc_accel_remove
> + }
> +};
> +
> +static bool cmpc_accel_driver_registered;
> +
> +
> +/*
> + * Tablet mode code.
> + */
> +static acpi_status cmpc_get_tablet(acpi_handle handle,
> + unsigned long long *value)
> +{
> + union acpi_object param;
> + struct acpi_object_list input;
> + unsigned long long output;
> + acpi_status status;
> + param.type = ACPI_TYPE_INTEGER;
> + param.integer.value = 0x01;
> + input.count = 1;
> + input.pointer = &param;
> + status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
> + if (ACPI_SUCCESS(status))
> + *value = output;
> + return status;
> +}
> +
> +static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + unsigned long long val = 0;
> + struct input_dev *inputdev = ctx;
> + if (event == 0x81) {
> + if (ACPI_SUCCESS(cmpc_get_tablet(handle, &val)))
> + input_report_switch(inputdev, SW_TABLET_MODE, !val);
> + }
> +}
> +
> +static void cmpc_tablet_idev_init(struct input_dev *inputdev)
> +{
> + set_bit(EV_SW, inputdev->evbit);
> + set_bit(SW_TABLET_MODE, inputdev->swbit);
> +}
> +
> +static int cmpc_tablet_add(struct acpi_device *acpi)
> +{
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
> + cmpc_tablet_handler,
> + cmpc_tablet_idev_init);
> +}
> +
> +static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
> +{
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_tablet_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_tablet_device_ids[] = {
> + {"TBLT0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_tablet_device_ids);
> +
> +static struct acpi_driver cmpc_tablet_acpi_driver = {
> + .name = "cmpc_tablet",
> + .class = "cmpc_tablet",
> + .ids = cmpc_tablet_device_ids,
> + .ops = {
> + .add = cmpc_tablet_add,
> + .remove = cmpc_tablet_remove
> + }
> +};
> +
> +static bool cmpc_tablet_driver_registered;
> +
> +
> +/*
> + * Backlight code.
> + */
> +
> +static acpi_status cmpc_get_brightness(acpi_handle handle,
> + unsigned long long *value)
> +{
> + union acpi_object param;
> + struct acpi_object_list input;
> + unsigned long long output;
> + acpi_status status;
> + param.type = ACPI_TYPE_INTEGER;
> + param.integer.value = 0xC0;
> + input.count = 1;
> + input.pointer = &param;
> + status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
> + if (ACPI_SUCCESS(status))
> + *value = output;
> + return status;
> +}
> +
> +static acpi_status cmpc_set_brightness(acpi_handle handle,
> + unsigned long long value)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + unsigned long long output;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0xC0;
> + param[1].type = ACPI_TYPE_INTEGER;
> + param[1].integer.value = value;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
> + return status;
> +}
> +
> +static int cmpc_bl_get_brightness(struct backlight_device *bd)
> +{
> + acpi_status status;
> + acpi_handle handle;
> + unsigned long long brightness;
> + handle = bl_get_data(bd);
> + status = cmpc_get_brightness(handle, &brightness);
> + if (ACPI_SUCCESS(status))
> + return brightness;
> + else
> + return -1;
> +}
> +
> +static int cmpc_bl_update_status(struct backlight_device *bd)
> +{
> + acpi_status status;
> + acpi_handle handle;
> + handle = bl_get_data(bd);
> + status = cmpc_set_brightness(handle, bd->props.brightness);
> + if (ACPI_SUCCESS(status))
> + return 0;
> + else
> + return -1;
> +}
> +
> +static struct backlight_ops cmpc_bl_ops = {
> + .get_brightness = cmpc_bl_get_brightness,
> + .update_status = cmpc_bl_update_status
> +};
> +
> +static int cmpc_bl_add(struct acpi_device *acpi)
> +{
> + struct backlight_device *bd;
> + bd = backlight_device_register("cmpc_bl", &acpi->dev,
> + acpi->handle, &cmpc_bl_ops);
> + bd->props.max_brightness = 7;
> + dev_set_drvdata(&acpi->dev, bd);
> + return 0;
> +}
> +
> +static int cmpc_bl_remove(struct acpi_device *acpi, int type)
> +{
> + struct backlight_device *bd;
> + bd = dev_get_drvdata(&acpi->dev);
> + backlight_device_unregister(bd);
> + return 0;
> +}
> +
> +static const struct acpi_device_id cmpc_device_ids[] = {
> + {"IPML200", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);
> +
> +static struct acpi_driver cmpc_bl_acpi_driver = {
> + .name = "cmpc",
> + .class = "cmpc",
> + .ids = cmpc_device_ids,
> + .ops = {
> + .add = cmpc_bl_add,
> + .remove = cmpc_bl_remove
> + }
> +};
> +
> +static bool cmpc_bl_driver_registered;
> +
> +
> +/*
> + * Extra keys code.
> + */
> +static int cmpc_keys_codes[] = {
> + KEY_UNKNOWN,
> + KEY_WLAN,
> + KEY_SWITCHVIDEOMODE,
> + KEY_BRIGHTNESSDOWN,
> + KEY_BRIGHTNESSUP,
> + KEY_VENDOR,
> + KEY_MAX
> +};
> +
> +static void cmpc_keys_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + struct input_dev *inputdev;
> + int code = KEY_MAX;
> + if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
> + code = cmpc_keys_codes[event & 0x0F];
> + inputdev = ctx;
> + input_report_key(inputdev, code, !(event & 0x10));
> +}
> +
> +static void cmpc_keys_idev_init(struct input_dev *inputdev)
> +{
> + int i;
> + set_bit(EV_KEY, inputdev->evbit);
> + for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
> + set_bit(cmpc_keys_codes[i], inputdev->keybit);
> +}
> +
> +static int cmpc_keys_add(struct acpi_device *acpi)
> +{
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
> + cmpc_keys_handler,
> + cmpc_keys_idev_init);
> +}
> +
> +static int cmpc_keys_remove(struct acpi_device *acpi, int type)
> +{
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_keys_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_keys_device_ids[] = {
> + {"FnBT0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_keys_device_ids);
> +
> +static struct acpi_driver cmpc_keys_acpi_driver = {
> + .name = "cmpc_keys",
> + .class = "cmpc_keys",
> + .ids = cmpc_keys_device_ids,
> + .ops = {
> + .add = cmpc_keys_add,
> + .remove = cmpc_keys_remove
> + }
> +};
> +
> +static bool cmpc_keys_driver_registered;
> +
> +
> +/*
> + * General init/exit code.
> + */
> +
> +static int cmpc_init(void)
> +{
> + int result;
> + result = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
> + cmpc_keys_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_bl_acpi_driver);
> + cmpc_bl_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
> + cmpc_tablet_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
> + cmpc_accel_driver_registered = !!result;
> + /*
> + * Not every CMPC has every ACPI device supported here. So always return
> + * success.
> + */
> + return 0;
> +}
> +
> +static void cmpc_exit(void)
> +{
> + if (cmpc_accel_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
> + if (cmpc_tablet_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
> + if (cmpc_bl_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
> + if (cmpc_keys_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
> +}
> +
> +module_init(cmpc_init);
> +module_exit(cmpc_exit);

2009-09-29 16:49:28

by Alan Jenkins

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On 9/29/09, Thadeu Lima de Souza Cascardo <[email protected]> wrote:
> On Tue, Sep 29, 2009 at 10:20:44AM +0100, Alan Jenkins wrote:
> Hello, Allan.
>
> Thanks for the feedback. I am considering an investigation whether we
> have lots of other ACPI input devices that could share some code like
> {add,remove}_acpi_notify_device. Regarding the driver naming, I will
> send a second version with it and other modifications considering your
> feedback and that of other people too.
>
> I will also ask for some explicit feedback and add linux-input and
> Dmitry to the loop.
>

I'll leave the input questions for the list. I don't have any
suggestions about your choice of keycode for the house key, or how to
calibrate accelerometers.

>> > +struct device_attribute cmpc_accel_sense_attr = {
>> > + .attr = { .name = "sense", .mode = 0220 },
>> > + .store = cmpc_accel_sense_store
>> > +};

> This changes the accelerometer device sensitivity. I will take a look at
> the ACPI tables to get a range. If very much sensitive, the device will
> generate too much ACPI notifications, even when the classmate PC is no a
> table and there seems to be no movement. If not very much sensitive, you
> must throw it spinning into the air to get anything. :-)
>
> I will pick a default value, although I think we could let it to
> userspace, but a sensible default value will not hurt here. As far as I
> know, there is no ACPI method to do get_sense, but we can mirror the
> last value written and provide a .show.
>
> What do you recommend as a documentation? Something at
> Documentation/ABI/testing/, perhaps? I don't know if there are any other
> devices with something similar, but I would not be surprised if there
> are some of them.

Documentation/ABI seems reasonable; "sysfs-platform-eeepc-laptop" was
accepted there.

You could try (either in place of documentation, or in addition to)
making the interface self-explanatory. Call the attribute
"sensitivity", and add a "max_sensitivity" attribute. It would also
make the interface more generic.

>> > +static void cmpc_tablet_idev_init(struct input_dev *inputdev)
>> > +{
>> > + set_bit(EV_SW, inputdev->evbit);
>> > + set_bit(SW_TABLET_MODE, inputdev->swbit);
>>
>> Don't you need to initialize the switch value here?
>>
>
> No, input_allocate_device does kzalloc. Otherwise, this would apply to
> the other bitmaps as well.

The other bitmaps aren't for switches though. Here's what prompted
this comment - a snippet from the old (2.6.29) version of
Documentation/rfkill.txt:

"2. Input device switches (sources of EV_SW events) DO store their current state
(so you *must* initialize it by issuing a gratuitous input layer event on
driver start-up and also when resuming from sleep), and that state CAN be
queried from userspace through IOCTLs."

>> Also, have you tested this with changing the switch value while
>> suspended? I _think_ you need to update the switch state on resume.
>> This is purely from looking at other acpi drivers and their evolution;
>> I don't have any practical experience with input drivers.
>>
>
> Interesting point. I will do the testing.

Thanks
Alan

2009-09-29 20:05:25

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Mon, 28 Sep 2009 22:38:00 -0300
Thadeu Lima de Souza Cascardo <[email protected]> wrote:

> This add supports for devices like keyboard, backlight, tablet and
> accelerometer.
>
> This work is supported by International Syst S/A.
>

I need to have a big whine about the coding style.

> +static acpi_status cmpc_start_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x3;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}

To the jaded kernel developer's eye, this is quite hard to read. Every
function here squishes the declarations of the locals up against the
start of the code. It's a small thing, but this:

static acpi_status cmpc_start_accel(acpi_handle handle)
{
union acpi_object param[2];
struct acpi_object_list input;
acpi_status status;

param[0].type = ACPI_TYPE_INTEGER;
param[0].integer.value = 0x3;
param[1].type = ACPI_TYPE_INTEGER;
input.count = 2;
input.pointer = param;
status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
return status;
}

puts a smile on our faces.

>
> ...
>
> +static ssize_t cmpc_accel_sense_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct acpi_device *acpi;
> + int sense;
> + acpi = to_acpi_device(dev);
> + if (sscanf(buf, "%d", &sense) <= 0)
> + return -EINVAL;
> + cmpc_accel_set_sense(acpi->handle, sense);
> + return strnlen(buf, count);
> +}

This will treat input of the form "42foo" as a valid decimal number.
That's a bit sloppy. Can we use strict_strtoul() here?

Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Tue, Sep 29, 2009 at 01:05:22PM -0700, Andrew Morton wrote:
> On Mon, 28 Sep 2009 22:38:00 -0300
> Thadeu Lima de Souza Cascardo <[email protected]> wrote:
>
> > This add supports for devices like keyboard, backlight, tablet and
> > accelerometer.
> >
> > This work is supported by International Syst S/A.
> >
>
> I need to have a big whine about the coding style.
>
> > +static acpi_status cmpc_start_accel(acpi_handle handle)
> > +{
> > + union acpi_object param[2];
> > + struct acpi_object_list input;
> > + acpi_status status;
> > + param[0].type = ACPI_TYPE_INTEGER;
> > + param[0].integer.value = 0x3;
> > + param[1].type = ACPI_TYPE_INTEGER;
> > + input.count = 2;
> > + input.pointer = param;
> > + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> > + return status;
> > +}
>
> To the jaded kernel developer's eye, this is quite hard to read. Every
> function here squishes the declarations of the locals up against the
> start of the code. It's a small thing, but this:
>
> static acpi_status cmpc_start_accel(acpi_handle handle)
> {
> union acpi_object param[2];
> struct acpi_object_list input;
> acpi_status status;
>
> param[0].type = ACPI_TYPE_INTEGER;
> param[0].integer.value = 0x3;
> param[1].type = ACPI_TYPE_INTEGER;
> input.count = 2;
> input.pointer = param;
> status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> return status;
> }
>
> puts a smile on our faces.
>

Will put this into consideration on the second version. Thanks, Andrew.

> >
> > ...
> >
> > +static ssize_t cmpc_accel_sense_store(struct device *dev,
> > + struct device_attribute *attr,
> > + const char *buf, size_t count)
> > +{
> > + struct acpi_device *acpi;
> > + int sense;
> > + acpi = to_acpi_device(dev);
> > + if (sscanf(buf, "%d", &sense) <= 0)
> > + return -EINVAL;
> > + cmpc_accel_set_sense(acpi->handle, sense);
> > + return strnlen(buf, count);
> > +}
>
> This will treat input of the form "42foo" as a valid decimal number.
> That's a bit sloppy. Can we use strict_strtoul() here?
>

Sure! The second version is coming soon. Thanks again.

Best regards,
Cascardo.


Attachments:
(No filename) (2.10 kB)
signature.asc (197.00 B)
Digital signature
Download all attachments

2009-09-29 20:17:50

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Tue, 2009-09-29 at 13:05 -0700, Andrew Morton wrote:
> static acpi_status cmpc_start_accel(acpi_handle handle)
> {
> union acpi_object param[2];
> struct acpi_object_list input;
> acpi_status status;
>
> param[0].type = ACPI_TYPE_INTEGER;
> param[0].integer.value = 0x3;
> param[1].type = ACPI_TYPE_INTEGER;
> input.count = 2;
> input.pointer = param;
> status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> return status;
> }
>
> puts a smile on our faces.

Much better, but what appears to be incomplete initialization
of stack variables is also a bit offputting.

cheers, Joe

2009-09-30 15:59:16

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Monday 28 September 2009 07:38:00 pm Thadeu Lima de Souza Cascardo wrote:
> This add supports for devices like keyboard, backlight, tablet and
> accelerometer.

Something about the PNP IDs bothered me yesterday, but I couldn't
figure out what. You're using:

ACCE0000
TBLT0000
IPML200
FnBT0000

Normally these OEM-specific ACPI drivers claim PNP IDs like ATK0100,
TOS6200, FUJ02B1, etc., that are obviously OEM-specific.

But the ones you're claiming seem pretty generic, like things
we could imagine appearing on machines other than the Classmate.

So I guess my question is, "Are these really generic? If so, should
this be split into separate, non-Classmate named drivers, such as
'accelerometer', 'tablet', etc.?"

Bjorn

>
> Signed-off-by: Thadeu Lima de Souza Cascardo <[email protected]>
> ---
> MAINTAINERS | 6 +
> drivers/platform/x86/Kconfig | 12 +
> drivers/platform/x86/Makefile | 1 +
> drivers/platform/x86/cmpc_acpi.c | 522 ++++++++++++++++++++++++++++++++++++++
> 4 files changed, 541 insertions(+), 0 deletions(-)
> create mode 100644 drivers/platform/x86/cmpc_acpi.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index c450f3a..9e14df1 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -1368,6 +1368,12 @@ L: [email protected]
> S: Supported
> F: drivers/scsi/fnic/
>
> +CMPC ACPI DRIVER
> +M: Thadeu Lima de Souza Cascardo <[email protected]>
> +M: Daniel Oliveira Nascimento <[email protected]>
> +S: Supported
> +F: drivers/platform/x86/cmpc_acpi.c
> +
> CODA FILE SYSTEM
> M: Jan Harkes <[email protected]>
> M: [email protected]
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 55ca39d..2cbc6a6 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -435,4 +435,16 @@ config ACPI_TOSHIBA
>
> If you have a legacy free Toshiba laptop (such as the Libretto L1
> series), say Y.
> +
> +config ACPI_CMPC
> + tristate "CMPC Laptop Extras"
> + depends on X86
> + select INPUT
> + select BACKLIGHT_CLASS_DEVICE
> + default n
> + help
> + Support for Intel Classmate PC ACPI devices, including some
> + keys as input device, backlight device, tablet and accelerometer
> + devices.
> +
> endif # X86_PLATFORM_DEVICES
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index d1c1621..fc92247 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -21,3 +21,4 @@ obj-$(CONFIG_ACPI_WMI) += wmi.o
> obj-$(CONFIG_ACPI_ASUS) += asus_acpi.o
> obj-$(CONFIG_TOPSTAR_LAPTOP) += topstar-laptop.o
> obj-$(CONFIG_ACPI_TOSHIBA) += toshiba_acpi.o
> +obj-$(CONFIG_ACPI_CMPC) += cmpc_acpi.o
> diff --git a/drivers/platform/x86/cmpc_acpi.c b/drivers/platform/x86/cmpc_acpi.c
> new file mode 100644
> index 0000000..c77c855
> --- /dev/null
> +++ b/drivers/platform/x86/cmpc_acpi.c
> @@ -0,0 +1,522 @@
> +/*
> + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <[email protected]>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/workqueue.h>
> +#include <acpi/acpi_drivers.h>
> +#include <linux/backlight.h>
> +#include <linux/input.h>
> +
> +MODULE_LICENSE("GPL");
> +
> +
> +/*
> + * Generic input device code.
> + */
> +
> +typedef void (*input_device_init)(struct input_dev *dev);
> +
> +static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
> + acpi_notify_handler handler,
> + input_device_init idev_init)
> +{
> + struct input_dev *inputdev;
> + acpi_status status;
> + int error;
> + inputdev = input_allocate_device();
> + if (!inputdev) {
> + error = -ENOMEM;
> + goto out;
> + }
> + inputdev->name = name;
> + inputdev->dev.parent = &acpi->dev;
> + idev_init(inputdev);
> + error = input_register_device(inputdev);
> + if (error)
> + goto err_reg;
> + dev_set_drvdata(&acpi->dev, inputdev);
> + status = acpi_install_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> + handler, inputdev);
> + if (ACPI_FAILURE(status)) {
> + error = -ENODEV;
> + goto err_acpi;
> + }
> + return 0;
> +err_acpi:
> + input_unregister_device(inputdev);
> +err_reg:
> + input_free_device(inputdev);
> +out:
> + return error;
> +}
> +
> +static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi,
> + acpi_notify_handler handler)
> +{
> + struct input_dev *inputdev;
> + acpi_status status;
> + status = acpi_remove_notify_handler(acpi->handle, ACPI_DEVICE_NOTIFY,
> + handler);
> + inputdev = dev_get_drvdata(&acpi->dev);
> + input_unregister_device(inputdev);
> + input_free_device(inputdev);
> + return 0;
> +}
> +
> +
> +/*
> + * Accelerometer code.
> + */
> +
> +static acpi_status cmpc_start_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x3;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}
> +
> +static acpi_status cmpc_stop_accel(acpi_handle handle)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x4;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
> + return status;
> +}
> +
> +static acpi_status cmpc_accel_set_sense(acpi_handle handle, int val)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x02;
> + param[1].type = ACPI_TYPE_INTEGER;
> + param[1].integer.value = val;
> + input.count = 2;
> + input.pointer = param;
> + return acpi_evaluate_object(handle, "ACMD", &input, NULL);
> +}
> +
> +static acpi_status cmpc_get_accel(acpi_handle handle,
> + unsigned char *x,
> + unsigned char *y,
> + unsigned char *z)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
> + unsigned char *locs;
> + acpi_status status;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0x01;
> + param[1].type = ACPI_TYPE_INTEGER;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_object(handle, "ACMD", &input, &output);
> + if (ACPI_SUCCESS(status)) {
> + union acpi_object *obj;
> + obj = output.pointer;
> + locs = obj->buffer.pointer;
> + *x = locs[0];
> + *y = locs[1];
> + *z = locs[2];
> + kfree(output.pointer);
> + }
> + return status;
> +}
> +
> +static void cmpc_accel_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + struct input_dev *inputdev = ctx;
> + acpi_status status;
> + unsigned char x, y, z;
> + if (event == 0x81) {
> + status = cmpc_get_accel(handle, &x, &y, &z);
> + if (ACPI_SUCCESS(status)) {
> + input_report_abs(inputdev, ABS_X, x);
> + input_report_abs(inputdev, ABS_Y, y);
> + input_report_abs(inputdev, ABS_Z, z);
> + input_sync(inputdev);
> + }
> + }
> +}
> +
> +static ssize_t cmpc_accel_sense_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct acpi_device *acpi;
> + int sense;
> + acpi = to_acpi_device(dev);
> + if (sscanf(buf, "%d", &sense) <= 0)
> + return -EINVAL;
> + cmpc_accel_set_sense(acpi->handle, sense);
> + return strnlen(buf, count);
> +}
> +
> +struct device_attribute cmpc_accel_sense_attr = {
> + .attr = { .name = "sense", .mode = 0220 },
> + .store = cmpc_accel_sense_store
> +};
> +
> +static int cmpc_accel_open(struct input_dev *input)
> +{
> + struct acpi_device *acpi;
> + acpi = to_acpi_device(input->dev.parent);
> + if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
> + return 0;
> + return -EIO;
> +}
> +
> +static void cmpc_accel_close(struct input_dev *input)
> +{
> + struct acpi_device *acpi;
> + acpi = to_acpi_device(input->dev.parent);
> + cmpc_stop_accel(acpi->handle);
> +}
> +
> +static void cmpc_accel_idev_init(struct input_dev *inputdev)
> +{
> + set_bit(EV_ABS, inputdev->evbit);
> + input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
> + input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
> + input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);
> + inputdev->open = cmpc_accel_open;
> + inputdev->close = cmpc_accel_close;
> +}
> +
> +static int cmpc_accel_add(struct acpi_device *acpi)
> +{
> + int error;
> + error = device_create_file(&acpi->dev, &cmpc_accel_sense_attr);
> + if (error)
> + return error;
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
> + cmpc_accel_handler,
> + cmpc_accel_idev_init);
> +}
> +
> +static int cmpc_accel_remove(struct acpi_device *acpi, int type)
> +{
> + device_remove_file(&acpi->dev, &cmpc_accel_sense_attr);
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_accel_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_accel_device_ids[] = {
> + {"ACCE0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_accel_device_ids);
> +
> +static struct acpi_driver cmpc_accel_acpi_driver = {
> + .name = "cmpc_accel",
> + .class = "cmpc_accel",
> + .ids = cmpc_accel_device_ids,
> + .ops = {
> + .add = cmpc_accel_add,
> + .remove = cmpc_accel_remove
> + }
> +};
> +
> +static bool cmpc_accel_driver_registered;
> +
> +
> +/*
> + * Tablet mode code.
> + */
> +static acpi_status cmpc_get_tablet(acpi_handle handle,
> + unsigned long long *value)
> +{
> + union acpi_object param;
> + struct acpi_object_list input;
> + unsigned long long output;
> + acpi_status status;
> + param.type = ACPI_TYPE_INTEGER;
> + param.integer.value = 0x01;
> + input.count = 1;
> + input.pointer = &param;
> + status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
> + if (ACPI_SUCCESS(status))
> + *value = output;
> + return status;
> +}
> +
> +static void cmpc_tablet_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + unsigned long long val = 0;
> + struct input_dev *inputdev = ctx;
> + if (event == 0x81) {
> + if (ACPI_SUCCESS(cmpc_get_tablet(handle, &val)))
> + input_report_switch(inputdev, SW_TABLET_MODE, !val);
> + }
> +}
> +
> +static void cmpc_tablet_idev_init(struct input_dev *inputdev)
> +{
> + set_bit(EV_SW, inputdev->evbit);
> + set_bit(SW_TABLET_MODE, inputdev->swbit);
> +}
> +
> +static int cmpc_tablet_add(struct acpi_device *acpi)
> +{
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
> + cmpc_tablet_handler,
> + cmpc_tablet_idev_init);
> +}
> +
> +static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
> +{
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_tablet_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_tablet_device_ids[] = {
> + {"TBLT0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_tablet_device_ids);
> +
> +static struct acpi_driver cmpc_tablet_acpi_driver = {
> + .name = "cmpc_tablet",
> + .class = "cmpc_tablet",
> + .ids = cmpc_tablet_device_ids,
> + .ops = {
> + .add = cmpc_tablet_add,
> + .remove = cmpc_tablet_remove
> + }
> +};
> +
> +static bool cmpc_tablet_driver_registered;
> +
> +
> +/*
> + * Backlight code.
> + */
> +
> +static acpi_status cmpc_get_brightness(acpi_handle handle,
> + unsigned long long *value)
> +{
> + union acpi_object param;
> + struct acpi_object_list input;
> + unsigned long long output;
> + acpi_status status;
> + param.type = ACPI_TYPE_INTEGER;
> + param.integer.value = 0xC0;
> + input.count = 1;
> + input.pointer = &param;
> + status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
> + if (ACPI_SUCCESS(status))
> + *value = output;
> + return status;
> +}
> +
> +static acpi_status cmpc_set_brightness(acpi_handle handle,
> + unsigned long long value)
> +{
> + union acpi_object param[2];
> + struct acpi_object_list input;
> + acpi_status status;
> + unsigned long long output;
> + param[0].type = ACPI_TYPE_INTEGER;
> + param[0].integer.value = 0xC0;
> + param[1].type = ACPI_TYPE_INTEGER;
> + param[1].integer.value = value;
> + input.count = 2;
> + input.pointer = param;
> + status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
> + return status;
> +}
> +
> +static int cmpc_bl_get_brightness(struct backlight_device *bd)
> +{
> + acpi_status status;
> + acpi_handle handle;
> + unsigned long long brightness;
> + handle = bl_get_data(bd);
> + status = cmpc_get_brightness(handle, &brightness);
> + if (ACPI_SUCCESS(status))
> + return brightness;
> + else
> + return -1;
> +}
> +
> +static int cmpc_bl_update_status(struct backlight_device *bd)
> +{
> + acpi_status status;
> + acpi_handle handle;
> + handle = bl_get_data(bd);
> + status = cmpc_set_brightness(handle, bd->props.brightness);
> + if (ACPI_SUCCESS(status))
> + return 0;
> + else
> + return -1;
> +}
> +
> +static struct backlight_ops cmpc_bl_ops = {
> + .get_brightness = cmpc_bl_get_brightness,
> + .update_status = cmpc_bl_update_status
> +};
> +
> +static int cmpc_bl_add(struct acpi_device *acpi)
> +{
> + struct backlight_device *bd;
> + bd = backlight_device_register("cmpc_bl", &acpi->dev,
> + acpi->handle, &cmpc_bl_ops);
> + bd->props.max_brightness = 7;
> + dev_set_drvdata(&acpi->dev, bd);
> + return 0;
> +}
> +
> +static int cmpc_bl_remove(struct acpi_device *acpi, int type)
> +{
> + struct backlight_device *bd;
> + bd = dev_get_drvdata(&acpi->dev);
> + backlight_device_unregister(bd);
> + return 0;
> +}
> +
> +static const struct acpi_device_id cmpc_device_ids[] = {
> + {"IPML200", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);
> +
> +static struct acpi_driver cmpc_bl_acpi_driver = {
> + .name = "cmpc",
> + .class = "cmpc",
> + .ids = cmpc_device_ids,
> + .ops = {
> + .add = cmpc_bl_add,
> + .remove = cmpc_bl_remove
> + }
> +};
> +
> +static bool cmpc_bl_driver_registered;
> +
> +
> +/*
> + * Extra keys code.
> + */
> +static int cmpc_keys_codes[] = {
> + KEY_UNKNOWN,
> + KEY_WLAN,
> + KEY_SWITCHVIDEOMODE,
> + KEY_BRIGHTNESSDOWN,
> + KEY_BRIGHTNESSUP,
> + KEY_VENDOR,
> + KEY_MAX
> +};
> +
> +static void cmpc_keys_handler(acpi_handle handle, u32 event, void *ctx)
> +{
> + struct input_dev *inputdev;
> + int code = KEY_MAX;
> + if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
> + code = cmpc_keys_codes[event & 0x0F];
> + inputdev = ctx;
> + input_report_key(inputdev, code, !(event & 0x10));
> +}
> +
> +static void cmpc_keys_idev_init(struct input_dev *inputdev)
> +{
> + int i;
> + set_bit(EV_KEY, inputdev->evbit);
> + for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
> + set_bit(cmpc_keys_codes[i], inputdev->keybit);
> +}
> +
> +static int cmpc_keys_add(struct acpi_device *acpi)
> +{
> + return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
> + cmpc_keys_handler,
> + cmpc_keys_idev_init);
> +}
> +
> +static int cmpc_keys_remove(struct acpi_device *acpi, int type)
> +{
> + return cmpc_remove_acpi_notify_device(acpi, cmpc_keys_handler);
> +}
> +
> +static const struct acpi_device_id cmpc_keys_device_ids[] = {
> + {"FnBT0000", 0},
> + {"", 0}
> +};
> +MODULE_DEVICE_TABLE(acpi, cmpc_keys_device_ids);
> +
> +static struct acpi_driver cmpc_keys_acpi_driver = {
> + .name = "cmpc_keys",
> + .class = "cmpc_keys",
> + .ids = cmpc_keys_device_ids,
> + .ops = {
> + .add = cmpc_keys_add,
> + .remove = cmpc_keys_remove
> + }
> +};
> +
> +static bool cmpc_keys_driver_registered;
> +
> +
> +/*
> + * General init/exit code.
> + */
> +
> +static int cmpc_init(void)
> +{
> + int result;
> + result = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
> + cmpc_keys_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_bl_acpi_driver);
> + cmpc_bl_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
> + cmpc_tablet_driver_registered = !!result;
> + result = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
> + cmpc_accel_driver_registered = !!result;
> + /*
> + * Not every CMPC has every ACPI device supported here. So always return
> + * success.
> + */
> + return 0;
> +}
> +
> +static void cmpc_exit(void)
> +{
> + if (cmpc_accel_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
> + if (cmpc_tablet_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
> + if (cmpc_bl_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
> + if (cmpc_keys_driver_registered)
> + acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
> +}
> +
> +module_init(cmpc_init);
> +module_exit(cmpc_exit);

Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Wed, Sep 30, 2009 at 09:50:38AM -0600, Bjorn Helgaas wrote:
> On Monday 28 September 2009 07:38:00 pm Thadeu Lima de Souza Cascardo wrote:
> > This add supports for devices like keyboard, backlight, tablet and
> > accelerometer.
>
> Something about the PNP IDs bothered me yesterday, but I couldn't
> figure out what. You're using:
>
> ACCE0000
> TBLT0000
> IPML200
> FnBT0000
>
> Normally these OEM-specific ACPI drivers claim PNP IDs like ATK0100,
> TOS6200, FUJ02B1, etc., that are obviously OEM-specific.
>
> But the ones you're claiming seem pretty generic, like things
> we could imagine appearing on machines other than the Classmate.
>
> So I guess my question is, "Are these really generic? If so, should
> this be split into separate, non-Classmate named drivers, such as
> 'accelerometer', 'tablet', etc.?"
>

Although I agree that {ATK,TOS,FUJ,IBM}* give pretty much a certainty
these are OEM-specific, I think we'd need some evidence that
{ACCE,TBLT,IPML,FnBT}* are generic enough to grant them a non-classmate
driver.

What I mean is that we should push this driver forward and turn these
into a generic driver if we get some evidence these are not
Classmate-specific. And that will happen when we hit some other system
with these devices.

And what would be the consequences to this decision? Renaming the
driver, changing the config location and documentation. Splitting them.
I can't think of anything else. If splitting them would be interesting
by now, we can do it.

That's my opinion. Please, tell me what you think of it.

> Bjorn
>

Thanks for the comments and best regards,
Cascardo.


Attachments:
(No filename) (1.59 kB)
signature.asc (197.00 B)
Digital signature
Download all attachments

2009-09-30 18:27:50

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Wednesday 30 September 2009 11:51:43 am Thadeu Lima de Souza Cascardo wrote:
> On Wed, Sep 30, 2009 at 09:50:38AM -0600, Bjorn Helgaas wrote:
> > On Monday 28 September 2009 07:38:00 pm Thadeu Lima de Souza Cascardo wrote:
> > > This add supports for devices like keyboard, backlight, tablet and
> > > accelerometer.
> >
> > Something about the PNP IDs bothered me yesterday, but I couldn't
> > figure out what. You're using:
> >
> > ACCE0000
> > TBLT0000
> > IPML200
> > FnBT0000
> >
> > Normally these OEM-specific ACPI drivers claim PNP IDs like ATK0100,
> > TOS6200, FUJ02B1, etc., that are obviously OEM-specific.
> >
> > But the ones you're claiming seem pretty generic, like things
> > we could imagine appearing on machines other than the Classmate.
> >
> > So I guess my question is, "Are these really generic? If so, should
> > this be split into separate, non-Classmate named drivers, such as
> > 'accelerometer', 'tablet', etc.?"
> >
>
> Although I agree that {ATK,TOS,FUJ,IBM}* give pretty much a certainty
> these are OEM-specific, I think we'd need some evidence that
> {ACCE,TBLT,IPML,FnBT}* are generic enough to grant them a non-classmate
> driver.
>
> What I mean is that we should push this driver forward and turn these
> into a generic driver if we get some evidence these are not
> Classmate-specific. And that will happen when we hit some other system
> with these devices.

Yep, I agree with you that it's not worth splitting things up until
we find these devices on more systems. Just out of curiosity, where
did you find specs for these devices? If there's a public spec, it'd
be nice to have a pointer to it in the driver.

Bjorn

Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Wed, Sep 30, 2009 at 12:27:48PM -0600, Bjorn Helgaas wrote:
> On Wednesday 30 September 2009 11:51:43 am Thadeu Lima de Souza Cascardo wrote:
> > On Wed, Sep 30, 2009 at 09:50:38AM -0600, Bjorn Helgaas wrote:
> > > On Monday 28 September 2009 07:38:00 pm Thadeu Lima de Souza Cascardo wrote:
> > > > This add supports for devices like keyboard, backlight, tablet and
> > > > accelerometer.
> > >
> > > Something about the PNP IDs bothered me yesterday, but I couldn't
> > > figure out what. You're using:
> > >
> > > ACCE0000
> > > TBLT0000
> > > IPML200
> > > FnBT0000
> > >
> > > Normally these OEM-specific ACPI drivers claim PNP IDs like ATK0100,
> > > TOS6200, FUJ02B1, etc., that are obviously OEM-specific.
> > >
> > > But the ones you're claiming seem pretty generic, like things
> > > we could imagine appearing on machines other than the Classmate.
> > >
> > > So I guess my question is, "Are these really generic? If so, should
> > > this be split into separate, non-Classmate named drivers, such as
> > > 'accelerometer', 'tablet', etc.?"
> > >
> >
> > Although I agree that {ATK,TOS,FUJ,IBM}* give pretty much a certainty
> > these are OEM-specific, I think we'd need some evidence that
> > {ACCE,TBLT,IPML,FnBT}* are generic enough to grant them a non-classmate
> > driver.
> >
> > What I mean is that we should push this driver forward and turn these
> > into a generic driver if we get some evidence these are not
> > Classmate-specific. And that will happen when we hit some other system
> > with these devices.
>
> Yep, I agree with you that it's not worth splitting things up until
> we find these devices on more systems. Just out of curiosity, where
> did you find specs for these devices? If there's a public spec, it'd
> be nice to have a pointer to it in the driver.
>
> Bjorn

There was no public spec. I used as reference some source code released
as GPL. This implementation used proc files as user interface. Other
references are the dumped tables, which I am using to implement what is
very likely a rfkill device. We are still doing some tests before adding
it to the driver.

Regards,
Cascardo.


Attachments:
(No filename) (2.10 kB)
signature.asc (197.00 B)
Digital signature
Download all attachments

2009-10-01 16:39:11

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH] cmpc_acpi: Added support for Classmate PC ACPI devices.

On Tue, Sep 29, 2009 at 05:41:42PM +0100, Alan Jenkins wrote:
> On 9/29/09, Thadeu Lima de Souza Cascardo <[email protected]> wrote:
>
> >> > +static void cmpc_tablet_idev_init(struct input_dev *inputdev)
> >> > +{
> >> > + set_bit(EV_SW, inputdev->evbit);
> >> > + set_bit(SW_TABLET_MODE, inputdev->swbit);
> >>
> >> Don't you need to initialize the switch value here?
> >>
> >
> > No, input_allocate_device does kzalloc. Otherwise, this would apply to
> > the other bitmaps as well.
>
> The other bitmaps aren't for switches though. Here's what prompted
> this comment - a snippet from the old (2.6.29) version of
> Documentation/rfkill.txt:
>
> "2. Input device switches (sources of EV_SW events) DO store their current state
> (so you *must* initialize it by issuing a gratuitous input layer event on
> driver start-up and also when resuming from sleep), and that state CAN be
> queried from userspace through IOCTLs."
>

There is a mixup betweeb capabilieies and state bitmasks. In init you
set up the capabilitis bits and you don't need to clear bitmaps. This is
true for everything - keys/buttons, switches, relatixe/absolute axis,
everything.

Now switches do naturally have a certain state. Unlke keys (which are
assumed to be normally in released state) switch can be on or off. So
the driver has (if it can) query the switch state (hardware state) and
issue appropriate input_event() to bring internal state of the device in
sync with the state of the hardware. The same on resume - the switch may
be altered while the box was asleep to upon resume we need to do the
same.

--
Dmitry