XO-1.5 ebook switch driver - upstream submission

Daniel Drake dsd at laptop.org
Sat Dec 4 10:13:12 EST 2010


Hi Paul,

I've made some small changes to your code for the XO-1.5 ebook switch
driver and am wondering if you have any comments before I submit this
upstream.

 - driver renamed to "xo1p5-ebook"
 - THRM# bit handling removed, since we'll do that in the DSDT
   (pending Mitch's approval)
 - /proc interface removed
 - /sys interface added (much simpler)
 - minor updates for new ACPI API

powerd will need an update for the /sys change. I'll take this on when
the time comes. (all this is framed for post-F14 release)



[PATCH] OLPC XO-1.5 ebook switch driver

From: Paul Fox <pgf at 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 at laptop.org>
---
 drivers/platform/x86/Kconfig       |    9 ++
 drivers/platform/x86/Makefile      |    1 +
 drivers/platform/x86/xo1p5-ebook.c |  229 ++++++++++++++++++++++++++++++++++++
 3 files changed, 239 insertions(+), 0 deletions(-)
 create mode 100644 drivers/platform/x86/xo1p5-ebook.c

diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index faec777..d3893d9 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 XO1P5_EBOOK
+	tristate "OLPC XO-1.5 ebook switch"
+	depends on ACPI
+	---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..20b74be 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_XO1P5_EBOOK)	+= xo1p5-ebook.o
diff --git a/drivers/platform/x86/xo1p5-ebook.c b/drivers/platform/x86/xo1p5-ebook.c
new file mode 100644
index 0000000..f8ba020
--- /dev/null
+++ b/drivers/platform/x86/xo1p5-ebook.c
@@ -0,0 +1,229 @@
+/*
+ *  OLPC XO-1.5 ebook switch driver
+ *  (based on generic ACPI button driver)
+ *
+ *  Copyright (C) 2009 Paul Fox <pgf at 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.
+ *
+ *  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.,
+ *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ */
+
+#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 "xo1p5-ebook"
+#define PREFIX MODULE_NAME ": "
+
+#define XO1P5_EBOOK_CLASS		MODULE_NAME
+#define XO1P5_EBOOK_TYPE_UNKNOWN	0x00
+#define XO1P5_EBOOK_NOTIFY_STATUS	0x80
+
+#define XO1P5_EBOOK_SUBCLASS		"ebook"
+#define XO1P5_EBOOK_HID			"XO15EBK"
+#define XO1P5_EBOOK_DEVICE_NAME		"EBook Switch"
+
+ACPI_MODULE_NAME(MODULE_NAME);
+
+MODULE_AUTHOR("Paul Fox");
+MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
+MODULE_LICENSE("GPL");
+
+static const struct acpi_device_id ebook_device_ids[] = {
+	{ XO1P5_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:
+		event = XO1P5_EBOOK_NOTIFY_STATUS;
+		/* fall through */
+	case XO1P5_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, XO1P5_EBOOK_HID)) {
+		printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
+		error = -ENODEV;
+		goto err_free_input;
+	}
+
+	strcpy(name, XO1P5_EBOOK_DEVICE_NAME);
+	sprintf(class, "%s/%s", XO1P5_EBOOK_CLASS, XO1P5_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 xo1p5_ebook_driver = {
+	.name = MODULE_NAME,
+	.class = XO1P5_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 xo1p5_ebook_init(void)
+{
+	return acpi_bus_register_driver(&xo1p5_ebook_driver);
+}
+
+static void __exit xo1p5_ebook_exit(void)
+{
+	acpi_bus_unregister_driver(&xo1p5_ebook_driver);
+}
+
+module_init(xo1p5_ebook_init);
+module_exit(xo1p5_ebook_exit);
-- 
1.7.3.2




More information about the Devel mailing list