Skip to contentSkip to conversation

OLPC XO-1.5 ebook switch driver

PATCH Superseded Daniel Drake 2 participants 3 messages Dec 29, 2010 – Jan 11, 2011 x86 platform drivers

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 OLPC XO-1.5 ebook switch driver

From: Paul Fox <[email protected]>

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.

---
 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
drivers/platform/x86/Kconfig +9 -0
index faec777..43dd19d 100644
@@ -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
drivers/platform/x86/Makefile +1 -0
index 9950ccc..7273a89 100644
@@ -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
drivers/platform/x86/xo15-ebook.c new file +216 -0

216 lines not on this page · raw message

Signature · 1 line
1.7.3.3
#1 lore raw

Hi Daniel,

Quoted 107 lines · Daniel Drake

From: Paul Fox <[email protected]>

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.

---
 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
drivers/platform/x86/Kconfig +9 -0
index faec777..43dd19d 100644
@@ -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
drivers/platform/x86/Makefile +1 -0
index 9950ccc..7273a89 100644
@@ -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
drivers/platform/x86/xo15-ebook.c new file +57 -0
new file mode 100644
index 0000000..1c7f770
@@ -0,0 +1,216 @@
+/*
+ *  OLPC XO-1.5 ebook switch driver
+ *  (based on generic ACPI button driver)
+ *
+ *  Copyright (C) 2009 Paul Fox <[email protected]>
+ *  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";

drivers/platform/x86/xo15-ebook.c line 67

Quoted 10 lines
drivers/platform/x86/xo15-ebook.c new file +10 -0
+	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 ;)

drivers/platform/x86/xo15-ebook.c line 80

Quoted 13 lines
drivers/platform/x86/xo15-ebook.c new file +13 -0
+
+/* --------------------------------------------------------------------------
+                                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?

Quoted 6 lines
drivers/platform/x86/xo15-ebook.c new file +6 -0
+
+	/* input layer checks if event is redundant */
+	input_report_switch(button->input, SW_TABLET_MODE, !state);
+	input_sync(button->input);
+	return 0;
+}

drivers/platform/x86/xo15-ebook.c line 96

Quoted 10 lines
drivers/platform/x86/xo15-ebook.c new file +10 -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?

Quoted 65 lines
drivers/platform/x86/xo15-ebook.c new file +65 -0
+		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);

drivers/platform/x86/xo15-ebook.c line 171

Quoted 10 lines
drivers/platform/x86/xo15-ebook.c new file +10 -0
+
+	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.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 4 lines · Dmitry Torokhov

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

/*
* 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);
}