Loading...
1/*
2 * OLPC XO-1.5 ebook switch driver
3 * (based on generic ACPI button driver)
4 *
5 * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
6 * Copyright (C) 2010 One Laptop per Child
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
20#include <linux/input.h>
21#include <linux/acpi.h>
22
23#define MODULE_NAME "xo15-ebook"
24
25#define XO15_EBOOK_CLASS MODULE_NAME
26#define XO15_EBOOK_TYPE_UNKNOWN 0x00
27#define XO15_EBOOK_NOTIFY_STATUS 0x80
28
29#define XO15_EBOOK_SUBCLASS "ebook"
30#define XO15_EBOOK_HID "XO15EBK"
31#define XO15_EBOOK_DEVICE_NAME "EBook Switch"
32
33ACPI_MODULE_NAME(MODULE_NAME);
34
35MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
36MODULE_LICENSE("GPL");
37
38static const struct acpi_device_id ebook_device_ids[] = {
39 { XO15_EBOOK_HID, 0 },
40 { "", 0 },
41};
42MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
43
44struct ebook_switch {
45 struct input_dev *input;
46 char phys[32]; /* for input device */
47};
48
49static int ebook_send_state(struct acpi_device *device)
50{
51 struct ebook_switch *button = acpi_driver_data(device);
52 unsigned long long state;
53 acpi_status status;
54
55 status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
56 if (ACPI_FAILURE(status))
57 return -EIO;
58
59 /* input layer checks if event is redundant */
60 input_report_switch(button->input, SW_TABLET_MODE, !state);
61 input_sync(button->input);
62 return 0;
63}
64
65static void ebook_switch_notify(struct acpi_device *device, u32 event)
66{
67 switch (event) {
68 case ACPI_FIXED_HARDWARE_EVENT:
69 case XO15_EBOOK_NOTIFY_STATUS:
70 ebook_send_state(device);
71 break;
72 default:
73 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
74 "Unsupported event [0x%x]\n", event));
75 break;
76 }
77}
78
79#ifdef CONFIG_PM_SLEEP
80static int ebook_switch_resume(struct device *dev)
81{
82 return ebook_send_state(to_acpi_device(dev));
83}
84#endif
85
86static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
87
88static int ebook_switch_add(struct acpi_device *device)
89{
90 struct ebook_switch *button;
91 struct input_dev *input;
92 const char *hid = acpi_device_hid(device);
93 char *name, *class;
94 int error;
95
96 button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
97 if (!button)
98 return -ENOMEM;
99
100 device->driver_data = button;
101
102 button->input = input = input_allocate_device();
103 if (!input) {
104 error = -ENOMEM;
105 goto err_free_button;
106 }
107
108 name = acpi_device_name(device);
109 class = acpi_device_class(device);
110
111 if (strcmp(hid, XO15_EBOOK_HID)) {
112 pr_err("Unsupported hid [%s]\n", hid);
113 error = -ENODEV;
114 goto err_free_input;
115 }
116
117 strcpy(name, XO15_EBOOK_DEVICE_NAME);
118 sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
119
120 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
121
122 input->name = name;
123 input->phys = button->phys;
124 input->id.bustype = BUS_HOST;
125 input->dev.parent = &device->dev;
126
127 input->evbit[0] = BIT_MASK(EV_SW);
128 set_bit(SW_TABLET_MODE, input->swbit);
129
130 error = input_register_device(input);
131 if (error)
132 goto err_free_input;
133
134 ebook_send_state(device);
135
136 if (device->wakeup.flags.valid) {
137 /* Button's GPE is run-wake GPE */
138 acpi_enable_gpe(device->wakeup.gpe_device,
139 device->wakeup.gpe_number);
140 device_set_wakeup_enable(&device->dev, true);
141 }
142
143 return 0;
144
145 err_free_input:
146 input_free_device(input);
147 err_free_button:
148 kfree(button);
149 return error;
150}
151
152static int ebook_switch_remove(struct acpi_device *device)
153{
154 struct ebook_switch *button = acpi_driver_data(device);
155
156 input_unregister_device(button->input);
157 kfree(button);
158 return 0;
159}
160
161static struct acpi_driver xo15_ebook_driver = {
162 .name = MODULE_NAME,
163 .class = XO15_EBOOK_CLASS,
164 .ids = ebook_device_ids,
165 .ops = {
166 .add = ebook_switch_add,
167 .remove = ebook_switch_remove,
168 .notify = ebook_switch_notify,
169 },
170 .drv.pm = &ebook_switch_pm,
171};
172module_acpi_driver(xo15_ebook_driver);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OLPC XO-1.5 ebook switch driver
4 * (based on generic ACPI button driver)
5 *
6 * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
7 * Copyright (C) 2010 One Laptop per Child
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/types.h>
16#include <linux/input.h>
17#include <linux/acpi.h>
18
19#define MODULE_NAME "xo15-ebook"
20
21#define XO15_EBOOK_CLASS MODULE_NAME
22#define XO15_EBOOK_TYPE_UNKNOWN 0x00
23#define XO15_EBOOK_NOTIFY_STATUS 0x80
24
25#define XO15_EBOOK_SUBCLASS "ebook"
26#define XO15_EBOOK_HID "XO15EBK"
27#define XO15_EBOOK_DEVICE_NAME "EBook Switch"
28
29ACPI_MODULE_NAME(MODULE_NAME);
30
31MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
32MODULE_LICENSE("GPL");
33
34static const struct acpi_device_id ebook_device_ids[] = {
35 { XO15_EBOOK_HID, 0 },
36 { "", 0 },
37};
38MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
39
40struct ebook_switch {
41 struct input_dev *input;
42 char phys[32]; /* for input device */
43};
44
45static int ebook_send_state(struct acpi_device *device)
46{
47 struct ebook_switch *button = acpi_driver_data(device);
48 unsigned long long state;
49 acpi_status status;
50
51 status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
52 if (ACPI_FAILURE(status))
53 return -EIO;
54
55 /* input layer checks if event is redundant */
56 input_report_switch(button->input, SW_TABLET_MODE, !state);
57 input_sync(button->input);
58 return 0;
59}
60
61static void ebook_switch_notify(struct acpi_device *device, u32 event)
62{
63 switch (event) {
64 case ACPI_FIXED_HARDWARE_EVENT:
65 case XO15_EBOOK_NOTIFY_STATUS:
66 ebook_send_state(device);
67 break;
68 default:
69 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
70 "Unsupported event [0x%x]\n", event));
71 break;
72 }
73}
74
75#ifdef CONFIG_PM_SLEEP
76static int ebook_switch_resume(struct device *dev)
77{
78 return ebook_send_state(to_acpi_device(dev));
79}
80#endif
81
82static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
83
84static int ebook_switch_add(struct acpi_device *device)
85{
86 struct ebook_switch *button;
87 struct input_dev *input;
88 const char *hid = acpi_device_hid(device);
89 char *name, *class;
90 int error;
91
92 button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
93 if (!button)
94 return -ENOMEM;
95
96 device->driver_data = button;
97
98 button->input = input = input_allocate_device();
99 if (!input) {
100 error = -ENOMEM;
101 goto err_free_button;
102 }
103
104 name = acpi_device_name(device);
105 class = acpi_device_class(device);
106
107 if (strcmp(hid, XO15_EBOOK_HID)) {
108 pr_err("Unsupported hid [%s]\n", hid);
109 error = -ENODEV;
110 goto err_free_input;
111 }
112
113 strcpy(name, XO15_EBOOK_DEVICE_NAME);
114 sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
115
116 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
117
118 input->name = name;
119 input->phys = button->phys;
120 input->id.bustype = BUS_HOST;
121 input->dev.parent = &device->dev;
122
123 input->evbit[0] = BIT_MASK(EV_SW);
124 set_bit(SW_TABLET_MODE, input->swbit);
125
126 error = input_register_device(input);
127 if (error)
128 goto err_free_input;
129
130 ebook_send_state(device);
131
132 if (device->wakeup.flags.valid) {
133 /* Button's GPE is run-wake GPE */
134 acpi_enable_gpe(device->wakeup.gpe_device,
135 device->wakeup.gpe_number);
136 device_set_wakeup_enable(&device->dev, true);
137 }
138
139 return 0;
140
141 err_free_input:
142 input_free_device(input);
143 err_free_button:
144 kfree(button);
145 return error;
146}
147
148static int ebook_switch_remove(struct acpi_device *device)
149{
150 struct ebook_switch *button = acpi_driver_data(device);
151
152 input_unregister_device(button->input);
153 kfree(button);
154 return 0;
155}
156
157static struct acpi_driver xo15_ebook_driver = {
158 .name = MODULE_NAME,
159 .class = XO15_EBOOK_CLASS,
160 .ids = ebook_device_ids,
161 .ops = {
162 .add = ebook_switch_add,
163 .remove = ebook_switch_remove,
164 .notify = ebook_switch_notify,
165 },
166 .drv.pm = &ebook_switch_pm,
167};
168module_acpi_driver(xo15_ebook_driver);