* [PATCH] OLPC XO-1.5 ebook switch driver
@ 2010-12-29 18:58 Daniel Drake
2011-01-07 1:34 ` Dmitry Torokhov
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Drake @ 2010-12-29 18:58 UTC (permalink / raw)
To: mjg; +Cc: platform-driver-x86, pgf
From: Paul Fox <pgf@laptop.org>
The OLPC XO-1.5 has an ebook switch, triggered when the laptop
screen is rotated then folding down, converting the device into ebook
form.
This switch is exposed through ACPI. Add a driver that exposes it
to userspace as an input device and sysfs "state" attribute.
Signed-off-by: Daniel Drake <dsd@laptop.org>
---
drivers/platform/x86/Kconfig | 9 ++
drivers/platform/x86/Makefile | 1 +
drivers/platform/x86/xo15-ebook.c | 216 +++++++++++++++++++++++++++++++++++++
3 files changed, 226 insertions(+), 0 deletions(-)
create mode 100644 drivers/platform/x86/xo15-ebook.c
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index faec777..43dd19d 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -639,4 +639,13 @@ config XO1_RFKILL
Support for enabling/disabling the WLAN interface on the OLPC XO-1
laptop.
+config XO15_EBOOK
+ tristate "OLPC XO-1.5 ebook switch"
+ depends on ACPI && INPUT
+ ---help---
+ Support for the ebook switch on the OLPC XO-1.5 laptop.
+
+ This switch is triggered as the screen is rotated and folded down to
+ convert the device into ebook form.
+
endif # X86_PLATFORM_DEVICES
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 9950ccc..7273a89 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -33,3 +33,4 @@ obj-$(CONFIG_INTEL_IPS) += intel_ips.o
obj-$(CONFIG_GPIO_INTEL_PMIC) += intel_pmic_gpio.o
obj-$(CONFIG_XO1_RFKILL) += xo1-rfkill.o
obj-$(CONFIG_IBM_RTL) += ibm_rtl.o
+obj-$(CONFIG_XO15_EBOOK) += xo15-ebook.o
diff --git a/drivers/platform/x86/xo15-ebook.c b/drivers/platform/x86/xo15-ebook.c
new file mode 100644
index 0000000..1c7f770
--- /dev/null
+++ b/drivers/platform/x86/xo15-ebook.c
@@ -0,0 +1,216 @@
+/*
+ * OLPC XO-1.5 ebook switch driver
+ * (based on generic ACPI button driver)
+ *
+ * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
+ * Copyright (C) 2010 One Laptop per Child
+ *
+ * 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.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/types.h>
+#include <linux/input.h>
+#include <acpi/acpi_bus.h>
+#include <acpi/acpi_drivers.h>
+
+#define MODULE_NAME "xo15-ebook"
+#define PREFIX MODULE_NAME ": "
+
+#define XO15_EBOOK_CLASS MODULE_NAME
+#define XO15_EBOOK_TYPE_UNKNOWN 0x00
+#define XO15_EBOOK_NOTIFY_STATUS 0x80
+
+#define XO15_EBOOK_SUBCLASS "ebook"
+#define XO15_EBOOK_HID "XO15EBK"
+#define XO15_EBOOK_DEVICE_NAME "EBook Switch"
+
+ACPI_MODULE_NAME(MODULE_NAME);
+
+MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
+MODULE_LICENSE("GPL");
+
+static const struct acpi_device_id ebook_device_ids[] = {
+ { XO15_EBOOK_HID, 0 },
+ { "", 0 },
+};
+MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
+
+struct ebook_switch {
+ struct input_dev *input;
+ char phys[32]; /* for input device */
+};
+
+/* --------------------------------------------------------------------------
+ /sys Interface
+ -------------------------------------------------------------------------- */
+
+static ssize_t ebook_show_state(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct acpi_device *acpi_dev = to_acpi_device(dev);
+ const char *state_txt = "unsupported";
+ unsigned long long state;
+ acpi_status status;
+
+ status = acpi_evaluate_integer(acpi_dev->handle, "EBK", NULL, &state);
+ if (!ACPI_FAILURE(status))
+ state_txt = state ? "open" : "closed";
+
+ return snprintf(buf, PAGE_SIZE, "%s\n", state_txt);
+}
+static DEVICE_ATTR(state, S_IRUGO, ebook_show_state, NULL);
+
+/* --------------------------------------------------------------------------
+ Driver Interface
+ -------------------------------------------------------------------------- */
+static int ebook_send_state(struct acpi_device *device)
+{
+ struct ebook_switch *button = acpi_driver_data(device);
+ unsigned long long state;
+ acpi_status status;
+
+ status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
+ if (ACPI_FAILURE(status))
+ return -ENODEV;
+
+ /* input layer checks if event is redundant */
+ input_report_switch(button->input, SW_TABLET_MODE, !state);
+ input_sync(button->input);
+ return 0;
+}
+
+static void ebook_switch_notify(struct acpi_device *device, u32 event)
+{
+ struct ebook_switch *button = acpi_driver_data(device);
+ struct input_dev *input;
+
+ switch (event) {
+ case ACPI_FIXED_HARDWARE_EVENT:
+ case XO15_EBOOK_NOTIFY_STATUS:
+ input = button->input;
+ ebook_send_state(device);
+ break;
+ default:
+ ACPI_DEBUG_PRINT((ACPI_DB_INFO,
+ "Unsupported event [0x%x]\n", event));
+ break;
+ }
+}
+
+static int ebook_switch_resume(struct acpi_device *device)
+{
+ return ebook_send_state(device);
+}
+
+static int ebook_switch_add(struct acpi_device *device)
+{
+ struct ebook_switch *button;
+ struct input_dev *input;
+ const char *hid = acpi_device_hid(device);
+ char *name, *class;
+ int error;
+
+ button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
+ if (!button)
+ return -ENOMEM;
+
+ device->driver_data = button;
+
+ button->input = input = input_allocate_device();
+ if (!input) {
+ error = -ENOMEM;
+ goto err_free_button;
+ }
+
+ name = acpi_device_name(device);
+ class = acpi_device_class(device);
+
+ if (strcmp(hid, XO15_EBOOK_HID)) {
+ printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
+ error = -ENODEV;
+ goto err_free_input;
+ }
+
+ strcpy(name, XO15_EBOOK_DEVICE_NAME);
+ sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
+
+ error = device_create_file(&device->dev, &dev_attr_state);
+ if (error)
+ goto err_free_input;
+
+ snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
+
+ input->name = name;
+ input->phys = button->phys;
+ input->id.bustype = BUS_HOST;
+ input->dev.parent = &device->dev;
+
+ input->evbit[0] = BIT_MASK(EV_SW);
+ set_bit(SW_TABLET_MODE, input->swbit);
+
+ error = input_register_device(input);
+ if (error)
+ goto err_remove_fs;
+
+ ebook_send_state(device);
+
+ if (device->wakeup.flags.valid) {
+ /* Button's GPE is run-wake GPE */
+ acpi_enable_gpe(device->wakeup.gpe_device,
+ device->wakeup.gpe_number);
+ device->wakeup.run_wake_count++;
+ device->wakeup.state.enabled = 1;
+ }
+
+ printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
+ return 0;
+
+ err_remove_fs:
+ device_remove_file(&device->dev, &dev_attr_state);
+ err_free_input:
+ input_free_device(input);
+ err_free_button:
+ kfree(button);
+ return error;
+}
+
+static int ebook_switch_remove(struct acpi_device *device, int type)
+{
+ struct ebook_switch *button = acpi_driver_data(device);
+
+ device_remove_file(&device->dev, &dev_attr_state);
+ input_unregister_device(button->input);
+ kfree(button);
+ return 0;
+}
+
+static struct acpi_driver xo15_ebook_driver = {
+ .name = MODULE_NAME,
+ .class = XO15_EBOOK_CLASS,
+ .ids = ebook_device_ids,
+ .ops = {
+ .add = ebook_switch_add,
+ .resume = ebook_switch_resume,
+ .remove = ebook_switch_remove,
+ .notify = ebook_switch_notify,
+ },
+};
+
+static int __init xo15_ebook_init(void)
+{
+ return acpi_bus_register_driver(&xo15_ebook_driver);
+}
+
+static void __exit xo15_ebook_exit(void)
+{
+ acpi_bus_unregister_driver(&xo15_ebook_driver);
+}
+
+module_init(xo15_ebook_init);
+module_exit(xo15_ebook_exit);
--
1.7.3.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] OLPC XO-1.5 ebook switch driver
2010-12-29 18:58 [PATCH] OLPC XO-1.5 ebook switch driver Daniel Drake
@ 2011-01-07 1:34 ` Dmitry Torokhov
2011-01-11 14:55 ` Daniel Drake
0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Torokhov @ 2011-01-07 1:34 UTC (permalink / raw)
To: Daniel Drake; +Cc: mjg, platform-driver-x86, pgf
Hi Daniel,
On Wed, Dec 29, 2010 at 06:58:29PM +0000, Daniel Drake wrote:
> From: Paul Fox <pgf@laptop.org>
>
> The OLPC XO-1.5 has an ebook switch, triggered when the laptop
> screen is rotated then folding down, converting the device into ebook
> form.
>
> This switch is exposed through ACPI. Add a driver that exposes it
> to userspace as an input device and sysfs "state" attribute.
>
> Signed-off-by: Daniel Drake <dsd@laptop.org>
> ---
> drivers/platform/x86/Kconfig | 9 ++
> drivers/platform/x86/Makefile | 1 +
> drivers/platform/x86/xo15-ebook.c | 216 +++++++++++++++++++++++++++++++++++++
> 3 files changed, 226 insertions(+), 0 deletions(-)
> create mode 100644 drivers/platform/x86/xo15-ebook.c
>
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index faec777..43dd19d 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -639,4 +639,13 @@ config XO1_RFKILL
> Support for enabling/disabling the WLAN interface on the OLPC XO-1
> laptop.
>
> +config XO15_EBOOK
> + tristate "OLPC XO-1.5 ebook switch"
> + depends on ACPI && INPUT
> + ---help---
> + Support for the ebook switch on the OLPC XO-1.5 laptop.
> +
> + This switch is triggered as the screen is rotated and folded down to
> + convert the device into ebook form.
> +
> endif # X86_PLATFORM_DEVICES
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 9950ccc..7273a89 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -33,3 +33,4 @@ obj-$(CONFIG_INTEL_IPS) += intel_ips.o
> obj-$(CONFIG_GPIO_INTEL_PMIC) += intel_pmic_gpio.o
> obj-$(CONFIG_XO1_RFKILL) += xo1-rfkill.o
> obj-$(CONFIG_IBM_RTL) += ibm_rtl.o
> +obj-$(CONFIG_XO15_EBOOK) += xo15-ebook.o
> diff --git a/drivers/platform/x86/xo15-ebook.c b/drivers/platform/x86/xo15-ebook.c
> new file mode 100644
> index 0000000..1c7f770
> --- /dev/null
> +++ b/drivers/platform/x86/xo15-ebook.c
> @@ -0,0 +1,216 @@
> +/*
> + * OLPC XO-1.5 ebook switch driver
> + * (based on generic ACPI button driver)
> + *
> + * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
> + * Copyright (C) 2010 One Laptop per Child
> + *
> + * 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.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/init.h>
> +#include <linux/types.h>
> +#include <linux/input.h>
> +#include <acpi/acpi_bus.h>
> +#include <acpi/acpi_drivers.h>
> +
> +#define MODULE_NAME "xo15-ebook"
> +#define PREFIX MODULE_NAME ": "
> +
> +#define XO15_EBOOK_CLASS MODULE_NAME
> +#define XO15_EBOOK_TYPE_UNKNOWN 0x00
> +#define XO15_EBOOK_NOTIFY_STATUS 0x80
> +
> +#define XO15_EBOOK_SUBCLASS "ebook"
> +#define XO15_EBOOK_HID "XO15EBK"
> +#define XO15_EBOOK_DEVICE_NAME "EBook Switch"
> +
> +ACPI_MODULE_NAME(MODULE_NAME);
> +
> +MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
> +MODULE_LICENSE("GPL");
> +
> +static const struct acpi_device_id ebook_device_ids[] = {
> + { XO15_EBOOK_HID, 0 },
> + { "", 0 },
> +};
> +MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
> +
> +struct ebook_switch {
> + struct input_dev *input;
> + char phys[32]; /* for input device */
> +};
> +
> +/* --------------------------------------------------------------------------
> + /sys Interface
> + -------------------------------------------------------------------------- */
> +
> +static ssize_t ebook_show_state(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct acpi_device *acpi_dev = to_acpi_device(dev);
> + const char *state_txt = "unsupported";
> + unsigned long long state;
> + acpi_status status;
> +
> + status = acpi_evaluate_integer(acpi_dev->handle, "EBK", NULL, &state);
> + if (!ACPI_FAILURE(status))
> + state_txt = state ? "open" : "closed";
> +
> + return snprintf(buf, PAGE_SIZE, "%s\n", state_txt);
> +}
> +static DEVICE_ATTR(state, S_IRUGO, ebook_show_state, NULL);
No, instead of adding yet another kernel attribute just use ioctl to get
current switch state. Or write a general purpose utility to query
switch/key state and contribute it to consoletools project - many people
have asked fr it ;)
> +
> +/* --------------------------------------------------------------------------
> + Driver Interface
> + -------------------------------------------------------------------------- */
> +static int ebook_send_state(struct acpi_device *device)
> +{
> + struct ebook_switch *button = acpi_driver_data(device);
> + unsigned long long state;
> + acpi_status status;
> +
> + status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
> + if (ACPI_FAILURE(status))
> + return -ENODEV;
ENODEV is really weird here, EIO?
> +
> + /* input layer checks if event is redundant */
> + input_report_switch(button->input, SW_TABLET_MODE, !state);
> + input_sync(button->input);
> + return 0;
> +}
> +
> +static void ebook_switch_notify(struct acpi_device *device, u32 event)
> +{
> + struct ebook_switch *button = acpi_driver_data(device);
> + struct input_dev *input;
> +
> + switch (event) {
> + case ACPI_FIXED_HARDWARE_EVENT:
> + case XO15_EBOOK_NOTIFY_STATUS:
> + input = button->input;
What is the purpose of this assignment?
> + ebook_send_state(device);
> + break;
> + default:
> + ACPI_DEBUG_PRINT((ACPI_DB_INFO,
> + "Unsupported event [0x%x]\n", event));
> + break;
> + }
> +}
> +
> +static int ebook_switch_resume(struct acpi_device *device)
> +{
> + return ebook_send_state(device);
> +}
> +
> +static int ebook_switch_add(struct acpi_device *device)
> +{
> + struct ebook_switch *button;
> + struct input_dev *input;
> + const char *hid = acpi_device_hid(device);
> + char *name, *class;
> + int error;
> +
> + button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
> + if (!button)
> + return -ENOMEM;
> +
> + device->driver_data = button;
> +
> + button->input = input = input_allocate_device();
> + if (!input) {
> + error = -ENOMEM;
> + goto err_free_button;
> + }
> +
> + name = acpi_device_name(device);
> + class = acpi_device_class(device);
> +
> + if (strcmp(hid, XO15_EBOOK_HID)) {
> + printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
> + error = -ENODEV;
> + goto err_free_input;
> + }
> +
> + strcpy(name, XO15_EBOOK_DEVICE_NAME);
> + sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
> +
> + error = device_create_file(&device->dev, &dev_attr_state);
> + if (error)
> + goto err_free_input;
> +
> + snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
> +
> + input->name = name;
> + input->phys = button->phys;
> + input->id.bustype = BUS_HOST;
> + input->dev.parent = &device->dev;
> +
> + input->evbit[0] = BIT_MASK(EV_SW);
> + set_bit(SW_TABLET_MODE, input->swbit);
> +
> + error = input_register_device(input);
> + if (error)
> + goto err_remove_fs;
> +
> + ebook_send_state(device);
> +
> + if (device->wakeup.flags.valid) {
> + /* Button's GPE is run-wake GPE */
> + acpi_enable_gpe(device->wakeup.gpe_device,
> + device->wakeup.gpe_number);
> + device->wakeup.run_wake_count++;
> + device->wakeup.state.enabled = 1;
> + }
> +
> + printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
You should already have output from input core, let's not clutter dmesg
any further.
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] OLPC XO-1.5 ebook switch driver
2011-01-07 1:34 ` Dmitry Torokhov
@ 2011-01-11 14:55 ` Daniel Drake
0 siblings, 0 replies; 3+ messages in thread
From: Daniel Drake @ 2011-01-11 14:55 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: mjg, platform-driver-x86, pgf
[-- Attachment #1: Type: text/plain, Size: 545 bytes --]
On Thu, 2011-01-06 at 17:34 -0800, Dmitry Torokhov wrote:
> No, instead of adding yet another kernel attribute just use ioctl to get
> current switch state. Or write a general purpose utility to query
> switch/key state and contribute it to consoletools project - many people
> have asked fr it ;)
Thanks for the review.
Did you mean consoletools as in http://lct.sourceforge.net/ ?
It looks rather dead.
Perhaps util-linux-ng would be a more appropriate target?
I'm attaching the general purpose utility for your comments.
Thanks,
Daniel
[-- Attachment #2: Type: text/x-csrc, Size: 2836 bytes --]
/*
* evstate: query evdev key/led/sw/snd state
* Returns exit code 1 if the state bit is set (key pressed, LED on, etc.),
* and 0 if the state bit is unset.
*
* Copyright (C) 2011 One Laptop per Child
*
* 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.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/input.h>
#define BITS_PER_LONG (sizeof(long) * 8)
static int test_bit(unsigned int nr, void *addr)
{
return ((1UL << (nr % BITS_PER_LONG)) &
(((unsigned long *) addr)[nr / BITS_PER_LONG])) != 0;
}
static void __attribute__((noreturn)) usage(void)
{
printf("Usage:\n"
" %s <device> <mode> <key>\n"
"Valid modes: key, led, snd, sw\n",
program_invocation_short_name);
exit(2);
}
static const struct mode {
const char *name;
int max;
int rq;
} requests[] = {
{ "key", KEY_MAX, EVIOCGKEY(KEY_MAX) },
{ "led", LED_MAX, EVIOCGLED(LED_MAX) },
{ "snd", SND_MAX, EVIOCGSND(SND_MAX) },
{ "sw", SW_MAX, EVIOCGSW(SW_MAX) },
};
static const struct mode *find_mode(const char *name)
{
int i;
for (i = 0; i < sizeof(requests) / sizeof(*requests); i++) {
const struct mode *mode = &requests[i];
if (strcmp(mode->name, name) == 0)
return mode;
}
return NULL;
}
static int query_state(const char *device, long int keyno,
const struct mode *mode)
{
uint8_t state[(mode->max / 8) + 1];
int fd;
int r;
if (keyno < 0 || keyno > mode->max) {
fprintf(stderr, "Unrecognised key %d\n", keyno);
exit(3);
}
fd = open(device, O_RDONLY);
if (fd == -1) {
perror("open");
exit(3);
}
memset(state, 0, sizeof(state));
r = ioctl(fd, mode->rq, state);
close(fd);
if (r == -1) {
perror("ioctl");
exit(3);
}
return test_bit(keyno, state);
}
int main(int argc, char **argv)
{
const struct mode *mode;
long int keyno;
if (argc != 4)
usage();
mode = find_mode(argv[2]);
if (!mode) {
fprintf(stderr, "Unrecognised mode.\n");
usage();
}
keyno = strtol(argv[3], NULL, 10);
return query_state(argv[1], keyno, mode);
}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-01-11 14:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-29 18:58 [PATCH] OLPC XO-1.5 ebook switch driver Daniel Drake
2011-01-07 1:34 ` Dmitry Torokhov
2011-01-11 14:55 ` Daniel Drake
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.