Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * power/home/volume button support for
  4 * Microsoft Surface Pro 3/4 tablet.
  5 *
  6 * Copyright (c) 2015 Intel Corporation.
  7 * All rights reserved.
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/init.h>
 13#include <linux/types.h>
 14#include <linux/input.h>
 15#include <linux/acpi.h>
 16#include <acpi/button.h>
 17
 18#define SURFACE_PRO3_BUTTON_HID		"MSHW0028"
 19#define SURFACE_PRO4_BUTTON_HID		"MSHW0040"
 20#define SURFACE_BUTTON_OBJ_NAME		"VGBI"
 21#define SURFACE_BUTTON_DEVICE_NAME	"Surface Pro 3/4 Buttons"
 22
 23#define MSHW0040_DSM_REVISION		0x01
 24#define MSHW0040_DSM_GET_OMPR		0x02	// get OEM Platform Revision
 25static const guid_t MSHW0040_DSM_UUID =
 26	GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
 27		  0x49, 0x80, 0x35);
 28
 29#define SURFACE_BUTTON_NOTIFY_TABLET_MODE	0xc8
 30
 31#define SURFACE_BUTTON_NOTIFY_PRESS_POWER	0xc6
 32#define SURFACE_BUTTON_NOTIFY_RELEASE_POWER	0xc7
 33
 34#define SURFACE_BUTTON_NOTIFY_PRESS_HOME	0xc4
 35#define SURFACE_BUTTON_NOTIFY_RELEASE_HOME	0xc5
 36
 37#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP	0xc0
 38#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP	0xc1
 39
 40#define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN		0xc2
 41#define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN	0xc3
 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		fallthrough;
 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		fallthrough;
 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		fallthrough;
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		fallthrough;
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_dev_event(&device->dev, 0, button->suspended);
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
149/*
150 * Surface Pro 4 and Surface Book 2 / Surface Pro 2017 use the same device
151 * ID (MSHW0040) for the power/volume buttons. Make sure this is the right
152 * device by checking for the _DSM method and OEM Platform Revision.
153 *
154 * Returns true if the driver should bind to this device, i.e. the device is
155 * either MSWH0028 (Pro 3) or MSHW0040 on a Pro 4 or Book 1.
156 */
157static bool surface_button_check_MSHW0040(struct acpi_device *dev)
158{
159	acpi_handle handle = dev->handle;
160	union acpi_object *result;
161	u64 oem_platform_rev = 0;	// valid revisions are nonzero
162
163	// get OEM platform revision
164	result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
165					 MSHW0040_DSM_REVISION,
166					 MSHW0040_DSM_GET_OMPR,
167					 NULL, ACPI_TYPE_INTEGER);
168
169	/*
170	 * If evaluating the _DSM fails, the method is not present. This means
171	 * that we have either MSHW0028 or MSHW0040 on Pro 4 or Book 1, so we
172	 * should use this driver. We use revision 0 indicating it is
173	 * unavailable.
174	 */
175
176	if (result) {
177		oem_platform_rev = result->integer.value;
178		ACPI_FREE(result);
179	}
180
181	dev_dbg(&dev->dev, "OEM Platform Revision %llu\n", oem_platform_rev);
182
183	return oem_platform_rev == 0;
184}
185
186
187static int surface_button_add(struct acpi_device *device)
188{
189	struct surface_button *button;
190	struct input_dev *input;
191	const char *hid = acpi_device_hid(device);
192	char *name;
193	int error;
194
195	if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
196	    strlen(SURFACE_BUTTON_OBJ_NAME)))
197		return -ENODEV;
198
199	if (!surface_button_check_MSHW0040(device))
200		return -ENODEV;
201
202	button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
203	if (!button)
204		return -ENOMEM;
205
206	device->driver_data = button;
207	button->input = input = input_allocate_device();
208	if (!input) {
209		error = -ENOMEM;
210		goto err_free_button;
211	}
212
213	name = acpi_device_name(device);
214	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
215	snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
216
217	input->name = name;
218	input->phys = button->phys;
219	input->id.bustype = BUS_HOST;
220	input->dev.parent = &device->dev;
221	input_set_capability(input, EV_KEY, KEY_POWER);
222	input_set_capability(input, EV_KEY, KEY_LEFTMETA);
223	input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
224	input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
225
226	error = input_register_device(input);
227	if (error)
228		goto err_free_input;
229
230	device_init_wakeup(&device->dev, true);
231	dev_info(&device->dev,
232			"%s [%s]\n", name, acpi_device_bid(device));
233	return 0;
234
235 err_free_input:
236	input_free_device(input);
237 err_free_button:
238	kfree(button);
239	return error;
240}
241
242static void surface_button_remove(struct acpi_device *device)
243{
244	struct surface_button *button = acpi_driver_data(device);
245
246	input_unregister_device(button->input);
247	kfree(button);
248}
249
250static SIMPLE_DEV_PM_OPS(surface_button_pm,
251		surface_button_suspend, surface_button_resume);
252
253static struct acpi_driver surface_button_driver = {
254	.name = "surface_pro3_button",
255	.class = "SurfacePro3",
256	.ids = surface_button_device_ids,
257	.ops = {
258		.add = surface_button_add,
259		.remove = surface_button_remove,
260		.notify = surface_button_notify,
261	},
262	.drv.pm = &surface_button_pm,
263};
264
265module_acpi_driver(surface_button_driver);