Loading...
1/*
2 * power/home/volume button support for
3 * Microsoft Surface Pro 3/4 tablet.
4 *
5 * Copyright (c) 2015 Intel Corporation.
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/types.h>
18#include <linux/input.h>
19#include <linux/acpi.h>
20#include <acpi/button.h>
21
22#define SURFACE_PRO3_BUTTON_HID "MSHW0028"
23#define SURFACE_PRO4_BUTTON_HID "MSHW0040"
24#define SURFACE_BUTTON_OBJ_NAME "VGBI"
25#define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
26
27#define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
28#define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
29
30#define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
31#define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
32
33#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
34#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
35
36#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
37#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
38
39ACPI_MODULE_NAME("surface pro 3 button");
40
41MODULE_AUTHOR("Chen Yu");
42MODULE_DESCRIPTION("Surface Pro3 Button Driver");
43MODULE_LICENSE("GPL v2");
44
45/*
46 * Power button, Home button, Volume buttons support is supposed to
47 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
48 * according to "Windows ACPI Design Guide for SoC Platforms".
49 * However surface pro3 seems not to obey the specs, instead it uses
50 * device VGBI(MSHW0028) for dispatching the events.
51 * We choose acpi_driver rather than platform_driver/i2c_driver because
52 * although VGBI has an i2c resource connected to i2c controller, it
53 * is not embedded in any i2c controller's scope, thus neither platform_device
54 * will be created, nor i2c_client will be enumerated, we have to use
55 * acpi_driver.
56 */
57static const struct acpi_device_id surface_button_device_ids[] = {
58 {SURFACE_PRO3_BUTTON_HID, 0},
59 {SURFACE_PRO4_BUTTON_HID, 0},
60 {"", 0},
61};
62MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
63
64struct surface_button {
65 unsigned int type;
66 struct input_dev *input;
67 char phys[32]; /* for input device */
68 unsigned long pushed;
69 bool suspended;
70};
71
72static void surface_button_notify(struct acpi_device *device, u32 event)
73{
74 struct surface_button *button = acpi_driver_data(device);
75 struct input_dev *input;
76 int key_code = KEY_RESERVED;
77 bool pressed = false;
78
79 switch (event) {
80 /* Power button press,release handle */
81 case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
82 pressed = true;
83 /*fall through*/
84 case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
85 key_code = KEY_POWER;
86 break;
87 /* Home button press,release handle */
88 case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
89 pressed = true;
90 /*fall through*/
91 case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
92 key_code = KEY_LEFTMETA;
93 break;
94 /* Volume up button press,release handle */
95 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
96 pressed = true;
97 /*fall through*/
98 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
99 key_code = KEY_VOLUMEUP;
100 break;
101 /* Volume down button press,release handle */
102 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
103 pressed = true;
104 /*fall through*/
105 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
106 key_code = KEY_VOLUMEDOWN;
107 break;
108 default:
109 dev_info_ratelimited(&device->dev,
110 "Unsupported event [0x%x]\n", event);
111 break;
112 }
113 input = button->input;
114 if (key_code == KEY_RESERVED)
115 return;
116 if (pressed)
117 pm_wakeup_event(&device->dev, 0);
118 if (button->suspended)
119 return;
120 input_report_key(input, key_code, pressed?1:0);
121 input_sync(input);
122}
123
124#ifdef CONFIG_PM_SLEEP
125static int surface_button_suspend(struct device *dev)
126{
127 struct acpi_device *device = to_acpi_device(dev);
128 struct surface_button *button = acpi_driver_data(device);
129
130 button->suspended = true;
131 return 0;
132}
133
134static int surface_button_resume(struct device *dev)
135{
136 struct acpi_device *device = to_acpi_device(dev);
137 struct surface_button *button = acpi_driver_data(device);
138
139 button->suspended = false;
140 return 0;
141}
142#endif
143
144static int surface_button_add(struct acpi_device *device)
145{
146 struct surface_button *button;
147 struct input_dev *input;
148 const char *hid = acpi_device_hid(device);
149 char *name;
150 int error;
151
152 if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
153 strlen(SURFACE_BUTTON_OBJ_NAME)))
154 return -ENODEV;
155
156 button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
157 if (!button)
158 return -ENOMEM;
159
160 device->driver_data = button;
161 button->input = input = input_allocate_device();
162 if (!input) {
163 error = -ENOMEM;
164 goto err_free_button;
165 }
166
167 name = acpi_device_name(device);
168 strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
169 snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
170
171 input->name = name;
172 input->phys = button->phys;
173 input->id.bustype = BUS_HOST;
174 input->dev.parent = &device->dev;
175 input_set_capability(input, EV_KEY, KEY_POWER);
176 input_set_capability(input, EV_KEY, KEY_LEFTMETA);
177 input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
178 input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
179
180 error = input_register_device(input);
181 if (error)
182 goto err_free_input;
183 dev_info(&device->dev,
184 "%s [%s]\n", name, acpi_device_bid(device));
185 return 0;
186
187 err_free_input:
188 input_free_device(input);
189 err_free_button:
190 kfree(button);
191 return error;
192}
193
194static int surface_button_remove(struct acpi_device *device)
195{
196 struct surface_button *button = acpi_driver_data(device);
197
198 input_unregister_device(button->input);
199 kfree(button);
200 return 0;
201}
202
203static SIMPLE_DEV_PM_OPS(surface_button_pm,
204 surface_button_suspend, surface_button_resume);
205
206static struct acpi_driver surface_button_driver = {
207 .name = "surface_pro3_button",
208 .class = "SurfacePro3",
209 .ids = surface_button_device_ids,
210 .ops = {
211 .add = surface_button_add,
212 .remove = surface_button_remove,
213 .notify = surface_button_notify,
214 },
215 .drv.pm = &surface_button_pm,
216};
217
218module_acpi_driver(surface_button_driver);
1/*
2 * power/home/volume button support for
3 * Microsoft Surface Pro 3/4 tablet.
4 *
5 * Copyright (c) 2015 Intel Corporation.
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/types.h>
18#include <linux/input.h>
19#include <linux/acpi.h>
20#include <acpi/button.h>
21
22#define SURFACE_PRO3_BUTTON_HID "MSHW0028"
23#define SURFACE_PRO4_BUTTON_HID "MSHW0040"
24#define SURFACE_BUTTON_OBJ_NAME "VGBI"
25#define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
26
27#define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
28
29#define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
30#define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
31
32#define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
33#define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
34
35#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
36#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
37
38#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
39#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
40
41ACPI_MODULE_NAME("surface pro 3 button");
42
43MODULE_AUTHOR("Chen Yu");
44MODULE_DESCRIPTION("Surface Pro3 Button Driver");
45MODULE_LICENSE("GPL v2");
46
47/*
48 * Power button, Home button, Volume buttons support is supposed to
49 * be covered by drivers/input/misc/soc_button_array.c, which is implemented
50 * according to "Windows ACPI Design Guide for SoC Platforms".
51 * However surface pro3 seems not to obey the specs, instead it uses
52 * device VGBI(MSHW0028) for dispatching the events.
53 * We choose acpi_driver rather than platform_driver/i2c_driver because
54 * although VGBI has an i2c resource connected to i2c controller, it
55 * is not embedded in any i2c controller's scope, thus neither platform_device
56 * will be created, nor i2c_client will be enumerated, we have to use
57 * acpi_driver.
58 */
59static const struct acpi_device_id surface_button_device_ids[] = {
60 {SURFACE_PRO3_BUTTON_HID, 0},
61 {SURFACE_PRO4_BUTTON_HID, 0},
62 {"", 0},
63};
64MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
65
66struct surface_button {
67 unsigned int type;
68 struct input_dev *input;
69 char phys[32]; /* for input device */
70 unsigned long pushed;
71 bool suspended;
72};
73
74static void surface_button_notify(struct acpi_device *device, u32 event)
75{
76 struct surface_button *button = acpi_driver_data(device);
77 struct input_dev *input;
78 int key_code = KEY_RESERVED;
79 bool pressed = false;
80
81 switch (event) {
82 /* Power button press,release handle */
83 case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
84 pressed = true;
85 /*fall through*/
86 case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
87 key_code = KEY_POWER;
88 break;
89 /* Home button press,release handle */
90 case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
91 pressed = true;
92 /*fall through*/
93 case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
94 key_code = KEY_LEFTMETA;
95 break;
96 /* Volume up button press,release handle */
97 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
98 pressed = true;
99 /*fall through*/
100 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
101 key_code = KEY_VOLUMEUP;
102 break;
103 /* Volume down button press,release handle */
104 case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
105 pressed = true;
106 /*fall through*/
107 case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
108 key_code = KEY_VOLUMEDOWN;
109 break;
110 case SURFACE_BUTTON_NOTIFY_TABLET_MODE:
111 dev_warn_once(&device->dev, "Tablet mode is not supported\n");
112 break;
113 default:
114 dev_info_ratelimited(&device->dev,
115 "Unsupported event [0x%x]\n", event);
116 break;
117 }
118 input = button->input;
119 if (key_code == KEY_RESERVED)
120 return;
121 if (pressed)
122 pm_wakeup_event(&device->dev, 0);
123 if (button->suspended)
124 return;
125 input_report_key(input, key_code, pressed?1:0);
126 input_sync(input);
127}
128
129#ifdef CONFIG_PM_SLEEP
130static int surface_button_suspend(struct device *dev)
131{
132 struct acpi_device *device = to_acpi_device(dev);
133 struct surface_button *button = acpi_driver_data(device);
134
135 button->suspended = true;
136 return 0;
137}
138
139static int surface_button_resume(struct device *dev)
140{
141 struct acpi_device *device = to_acpi_device(dev);
142 struct surface_button *button = acpi_driver_data(device);
143
144 button->suspended = false;
145 return 0;
146}
147#endif
148
149static int surface_button_add(struct acpi_device *device)
150{
151 struct surface_button *button;
152 struct input_dev *input;
153 const char *hid = acpi_device_hid(device);
154 char *name;
155 int error;
156
157 if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
158 strlen(SURFACE_BUTTON_OBJ_NAME)))
159 return -ENODEV;
160
161 button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
162 if (!button)
163 return -ENOMEM;
164
165 device->driver_data = button;
166 button->input = input = input_allocate_device();
167 if (!input) {
168 error = -ENOMEM;
169 goto err_free_button;
170 }
171
172 name = acpi_device_name(device);
173 strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
174 snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
175
176 input->name = name;
177 input->phys = button->phys;
178 input->id.bustype = BUS_HOST;
179 input->dev.parent = &device->dev;
180 input_set_capability(input, EV_KEY, KEY_POWER);
181 input_set_capability(input, EV_KEY, KEY_LEFTMETA);
182 input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
183 input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
184
185 error = input_register_device(input);
186 if (error)
187 goto err_free_input;
188 dev_info(&device->dev,
189 "%s [%s]\n", name, acpi_device_bid(device));
190 return 0;
191
192 err_free_input:
193 input_free_device(input);
194 err_free_button:
195 kfree(button);
196 return error;
197}
198
199static int surface_button_remove(struct acpi_device *device)
200{
201 struct surface_button *button = acpi_driver_data(device);
202
203 input_unregister_device(button->input);
204 kfree(button);
205 return 0;
206}
207
208static SIMPLE_DEV_PM_OPS(surface_button_pm,
209 surface_button_suspend, surface_button_resume);
210
211static struct acpi_driver surface_button_driver = {
212 .name = "surface_pro3_button",
213 .class = "SurfacePro3",
214 .ids = surface_button_device_ids,
215 .ops = {
216 .add = surface_button_add,
217 .remove = surface_button_remove,
218 .notify = surface_button_notify,
219 },
220 .drv.pm = &surface_button_pm,
221};
222
223module_acpi_driver(surface_button_driver);