xref: /linux/drivers/platform/x86/huawei-wmi.c (revision 5b05bb3f6c5716fab6911e12d60dd1f43ad9806a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Huawei WMI laptop extras driver
4  *
5  *  Copyright (C) 2018	      Ayman Bagabas <ayman.bagabas@gmail.com>
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/cleanup.h>
10 #include <linux/debugfs.h>
11 #include <linux/delay.h>
12 #include <linux/dmi.h>
13 #include <linux/input.h>
14 #include <linux/input/sparse-keymap.h>
15 #include <linux/leds.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/sysfs.h>
21 #include <linux/wmi.h>
22 #include <acpi/battery.h>
23 
24 /*
25  * Huawei WMI GUIDs
26  */
27 #define HWMI_METHOD_GUID "ABBC0F5B-8EA1-11D1-A000-C90629100000"
28 #define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000"
29 
30 /* Legacy GUIDs */
31 #define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100"
32 #define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100"
33 
34 /* HWMI commands */
35 
36 enum {
37 	BATTERY_THRESH_GET		= 0x00001103, /* \GBTT */
38 	BATTERY_THRESH_SET		= 0x00001003, /* \SBTT */
39 	FN_LOCK_GET			= 0x00000604, /* \GFRS */
40 	FN_LOCK_SET			= 0x00000704, /* \SFRS */
41 	MICMUTE_LED_SET			= 0x00000b04, /* \SMLS */
42 };
43 
44 union hwmi_arg {
45 	u64 cmd;
46 	u8 args[8];
47 };
48 
49 struct quirk_entry {
50 	bool battery_reset;
51 	bool ec_micmute;
52 	bool report_brightness;
53 };
54 
55 static struct quirk_entry *quirks;
56 
57 struct huawei_wmi_debug {
58 	struct dentry *root;
59 	u64 arg;
60 };
61 
62 struct huawei_wmi {
63 	bool battery_available;
64 	bool fn_lock_available;
65 
66 	struct huawei_wmi_debug debug;
67 	struct led_classdev cdev;
68 	struct device *dev;
69 
70 	struct mutex wmi_lock;
71 };
72 
73 static struct huawei_wmi *huawei_wmi;
74 
75 static const struct key_entry huawei_wmi_keymap[] = {
76 	{ KE_KEY,    0x281, { KEY_BRIGHTNESSDOWN } },
77 	{ KE_KEY,    0x282, { KEY_BRIGHTNESSUP } },
78 	{ KE_KEY,    0x284, { KEY_MUTE } },
79 	{ KE_KEY,    0x285, { KEY_VOLUMEDOWN } },
80 	{ KE_KEY,    0x286, { KEY_VOLUMEUP } },
81 	{ KE_KEY,    0x287, { KEY_MICMUTE } },
82 	{ KE_KEY,    0x289, { KEY_WLAN } },
83 	// Huawei |M| key
84 	{ KE_KEY,    0x28a, { KEY_CONFIG } },
85 	// HONOR YOYO key
86 	{ KE_KEY,    0x28b, { KEY_NOTIFICATION_CENTER } },
87 	// HONOR print screen
88 	{ KE_KEY,    0x28e, { KEY_PRINT } },
89 	// Keyboard backlit
90 	{ KE_IGNORE, 0x293, { KEY_KBDILLUMTOGGLE } },
91 	{ KE_IGNORE, 0x294, { KEY_KBDILLUMUP } },
92 	{ KE_IGNORE, 0x295, { KEY_KBDILLUMUP } },
93 	// Ignore Ambient Light Sensoring
94 	{ KE_KEY,    0x2c1, { KEY_RESERVED } },
95 	{ KE_END,	 0 }
96 };
97 
98 static int battery_reset = -1;
99 static int report_brightness = -1;
100 
101 module_param(battery_reset, bint, 0444);
102 MODULE_PARM_DESC(battery_reset,
103 		"Reset battery charge values to (0-0) before disabling it using (0-100)");
104 module_param(report_brightness, bint, 0444);
105 MODULE_PARM_DESC(report_brightness,
106 		"Report brightness keys.");
107 
108 /* Quirks */
109 
dmi_matched(const struct dmi_system_id * dmi)110 static int __init dmi_matched(const struct dmi_system_id *dmi)
111 {
112 	quirks = dmi->driver_data;
113 	return 1;
114 }
115 
116 static struct quirk_entry quirk_unknown = {
117 };
118 
119 static struct quirk_entry quirk_battery_reset = {
120 	.battery_reset = true,
121 };
122 
123 static struct quirk_entry quirk_matebook_x = {
124 	.ec_micmute = true,
125 	.report_brightness = true,
126 };
127 
128 static const struct dmi_system_id huawei_quirks[] = {
129 	{
130 		.callback = dmi_matched,
131 		.ident = "Huawei MACH-WX9",
132 		.matches = {
133 			DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
134 			DMI_MATCH(DMI_PRODUCT_NAME, "MACH-WX9"),
135 		},
136 		.driver_data = &quirk_battery_reset
137 	},
138 	{
139 		.callback = dmi_matched,
140 		.ident = "Huawei MateBook X",
141 		.matches = {
142 			DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
143 			DMI_MATCH(DMI_PRODUCT_NAME, "HUAWEI MateBook X")
144 		},
145 		.driver_data = &quirk_matebook_x
146 	},
147 	{  }
148 };
149 
150 /* Utils */
151 
huawei_wmi_call(struct huawei_wmi * huawei,struct acpi_buffer * in,struct acpi_buffer * out)152 static int huawei_wmi_call(struct huawei_wmi *huawei,
153 			   struct acpi_buffer *in, struct acpi_buffer *out)
154 {
155 	acpi_status status;
156 
157 	mutex_lock(&huawei->wmi_lock);
158 	status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out);
159 	mutex_unlock(&huawei->wmi_lock);
160 	if (ACPI_FAILURE(status)) {
161 		dev_err(huawei->dev, "Failed to evaluate wmi method\n");
162 		return -ENODEV;
163 	}
164 
165 	return 0;
166 }
167 
168 /* HWMI takes a 64 bit input and returns either a package with 2 buffers, one of
169  * 4 bytes and the other of 256 bytes, or one buffer of size 0x104 (260) bytes.
170  * The first 4 bytes are ignored, we ignore the first 4 bytes buffer if we got a
171  * package, or skip the first 4 if a buffer of 0x104 is used. The first byte of
172  * the remaining 0x100 sized buffer has the return status of every call. In case
173  * the return status is non-zero, we return -ENODEV but still copy the returned
174  * buffer to the given buffer parameter (buf).
175  */
huawei_wmi_cmd(u64 arg,u8 * buf,size_t buflen)176 static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
177 {
178 	struct huawei_wmi *huawei = huawei_wmi;
179 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
180 	struct acpi_buffer in;
181 	union acpi_object *obj;
182 	size_t len;
183 	int err, i;
184 
185 	in.length = sizeof(arg);
186 	in.pointer = &arg;
187 
188 	/* Some models require calling HWMI twice to execute a command. We evaluate
189 	 * HWMI and if we get a non-zero return status we evaluate it again.
190 	 */
191 	for (i = 0; i < 2; i++) {
192 		err = huawei_wmi_call(huawei, &in, &out);
193 		if (err)
194 			goto fail_cmd;
195 
196 		obj = out.pointer;
197 		if (!obj) {
198 			err = -EIO;
199 			goto fail_cmd;
200 		}
201 
202 		switch (obj->type) {
203 		/* Models that implement both "legacy" and HWMI tend to return a 0x104
204 		 * sized buffer instead of a package of 0x4 and 0x100 buffers.
205 		 */
206 		case ACPI_TYPE_BUFFER:
207 			if (obj->buffer.length == 0x104) {
208 				// Skip the first 4 bytes.
209 				obj->buffer.pointer += 4;
210 				len = 0x100;
211 			} else {
212 				dev_err(huawei->dev, "Bad buffer length, got %d\n", obj->buffer.length);
213 				err = -EIO;
214 				goto fail_cmd;
215 			}
216 
217 			break;
218 		/* HWMI returns a package with 2 buffer elements, one of 4 bytes and the
219 		 * other is 256 bytes.
220 		 */
221 		case ACPI_TYPE_PACKAGE:
222 			if (obj->package.count != 2) {
223 				dev_err(huawei->dev, "Bad package count, got %d\n", obj->package.count);
224 				err = -EIO;
225 				goto fail_cmd;
226 			}
227 
228 			obj = &obj->package.elements[1];
229 			if (obj->type != ACPI_TYPE_BUFFER) {
230 				dev_err(huawei->dev, "Bad package element type, got %d\n", obj->type);
231 				err = -EIO;
232 				goto fail_cmd;
233 			}
234 			len = obj->buffer.length;
235 
236 			break;
237 		/* Shouldn't get here! */
238 		default:
239 			dev_err(huawei->dev, "Unexpected obj type, got: %d\n", obj->type);
240 			err = -EIO;
241 			goto fail_cmd;
242 		}
243 
244 		if (!*obj->buffer.pointer)
245 			break;
246 	}
247 
248 	err = (*obj->buffer.pointer) ? -ENODEV : 0;
249 
250 	if (buf) {
251 		len = min(buflen, len);
252 		memcpy(buf, obj->buffer.pointer, len);
253 	}
254 
255 fail_cmd:
256 	kfree(out.pointer);
257 	return err;
258 }
259 
260 /* LEDs */
261 
huawei_wmi_micmute_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)262 static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
263 		enum led_brightness brightness)
264 {
265 	/* This is a workaround until the "legacy" interface is implemented. */
266 	if (quirks && quirks->ec_micmute) {
267 		char *acpi_method;
268 		acpi_handle handle;
269 		acpi_status status;
270 		union acpi_object args[3];
271 		struct acpi_object_list arg_list = {
272 			.pointer = args,
273 			.count = ARRAY_SIZE(args),
274 		};
275 
276 		handle = ec_get_handle();
277 		if (!handle)
278 			return -ENODEV;
279 
280 		args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER;
281 		args[1].integer.value = 0x04;
282 
283 		if (acpi_has_method(handle, "SPIN")) {
284 			acpi_method = "SPIN";
285 			args[0].integer.value = 0;
286 			args[2].integer.value = brightness ? 1 : 0;
287 		} else if (acpi_has_method(handle, "WPIN")) {
288 			acpi_method = "WPIN";
289 			args[0].integer.value = 1;
290 			args[2].integer.value = brightness ? 0 : 1;
291 		} else {
292 			return -ENODEV;
293 		}
294 
295 		status = acpi_evaluate_object(handle, acpi_method, &arg_list, NULL);
296 		if (ACPI_FAILURE(status))
297 			return -ENODEV;
298 
299 		return 0;
300 	} else {
301 		union hwmi_arg arg;
302 
303 		arg.cmd = MICMUTE_LED_SET;
304 		arg.args[2] = brightness;
305 
306 		return huawei_wmi_cmd(arg.cmd, NULL, 0);
307 	}
308 }
309 
huawei_wmi_leds_setup(struct device * dev)310 static void huawei_wmi_leds_setup(struct device *dev)
311 {
312 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
313 
314 	huawei->cdev.name = "platform::micmute";
315 	huawei->cdev.max_brightness = 1;
316 	huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set;
317 	huawei->cdev.default_trigger = "audio-micmute";
318 	huawei->cdev.dev = dev;
319 	huawei->cdev.flags = LED_CORE_SUSPENDRESUME;
320 
321 	devm_led_classdev_register(dev, &huawei->cdev);
322 }
323 
324 /* Battery protection */
325 
huawei_wmi_battery_get(int * start,int * end)326 static int huawei_wmi_battery_get(int *start, int *end)
327 {
328 	u8 ret[0x100];
329 	int err, i;
330 
331 	err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, sizeof(ret));
332 	if (err)
333 		return err;
334 
335 	/* Find the last two non-zero values. Return status is ignored. */
336 	i = ARRAY_SIZE(ret) - 1;
337 	do {
338 		if (start)
339 			*start = ret[i-1];
340 		if (end)
341 			*end = ret[i];
342 	} while (i > 2 && !ret[i--]);
343 
344 	return 0;
345 }
346 
huawei_wmi_battery_set(int start,int end)347 static int huawei_wmi_battery_set(int start, int end)
348 {
349 	union hwmi_arg arg;
350 	int err;
351 
352 	if (start < 0 || end < 0 || start > 100 || end > 100)
353 		return -EINVAL;
354 
355 	arg.cmd = BATTERY_THRESH_SET;
356 	arg.args[2] = start;
357 	arg.args[3] = end;
358 
359 	/* This is an edge case were some models turn battery protection
360 	 * off without changing their thresholds values. We clear the
361 	 * values before turning off protection. Sometimes we need a sleep delay to
362 	 * make sure these values make their way to EC memory.
363 	 */
364 	if (quirks && quirks->battery_reset && start == 0 && end == 100) {
365 		err = huawei_wmi_battery_set(0, 0);
366 		if (err)
367 			return err;
368 
369 		msleep(1000);
370 	}
371 
372 	err = huawei_wmi_cmd(arg.cmd, NULL, 0);
373 
374 	return err;
375 }
376 
charge_control_start_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)377 static ssize_t charge_control_start_threshold_show(struct device *dev,
378 		struct device_attribute *attr,
379 		char *buf)
380 {
381 	int err, start;
382 
383 	err = huawei_wmi_battery_get(&start, NULL);
384 	if (err)
385 		return err;
386 
387 	return sysfs_emit(buf, "%d\n", start);
388 }
389 
charge_control_end_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)390 static ssize_t charge_control_end_threshold_show(struct device *dev,
391 		struct device_attribute *attr,
392 		char *buf)
393 {
394 	int err, end;
395 
396 	err = huawei_wmi_battery_get(NULL, &end);
397 	if (err)
398 		return err;
399 
400 	return sysfs_emit(buf, "%d\n", end);
401 }
402 
charge_control_thresholds_show(struct device * dev,struct device_attribute * attr,char * buf)403 static ssize_t charge_control_thresholds_show(struct device *dev,
404 		struct device_attribute *attr,
405 		char *buf)
406 {
407 	int err, start, end;
408 
409 	err = huawei_wmi_battery_get(&start, &end);
410 	if (err)
411 		return err;
412 
413 	return sysfs_emit(buf, "%d %d\n", start, end);
414 }
415 
charge_control_start_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)416 static ssize_t charge_control_start_threshold_store(struct device *dev,
417 		struct device_attribute *attr,
418 		const char *buf, size_t size)
419 {
420 	int err, start, end;
421 
422 	err = huawei_wmi_battery_get(NULL, &end);
423 	if (err)
424 		return err;
425 
426 	if (sscanf(buf, "%d", &start) != 1)
427 		return -EINVAL;
428 
429 	err = huawei_wmi_battery_set(start, end);
430 	if (err)
431 		return err;
432 
433 	return size;
434 }
435 
charge_control_end_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)436 static ssize_t charge_control_end_threshold_store(struct device *dev,
437 		struct device_attribute *attr,
438 		const char *buf, size_t size)
439 {
440 	int err, start, end;
441 
442 	err = huawei_wmi_battery_get(&start, NULL);
443 	if (err)
444 		return err;
445 
446 	if (sscanf(buf, "%d", &end) != 1)
447 		return -EINVAL;
448 
449 	err = huawei_wmi_battery_set(start, end);
450 	if (err)
451 		return err;
452 
453 	return size;
454 }
455 
charge_control_thresholds_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)456 static ssize_t charge_control_thresholds_store(struct device *dev,
457 		struct device_attribute *attr,
458 		const char *buf, size_t size)
459 {
460 	int err, start, end;
461 
462 	if (sscanf(buf, "%d %d", &start, &end) != 2)
463 		return -EINVAL;
464 
465 	err = huawei_wmi_battery_set(start, end);
466 	if (err)
467 		return err;
468 
469 	return size;
470 }
471 
472 static DEVICE_ATTR_RW(charge_control_start_threshold);
473 static DEVICE_ATTR_RW(charge_control_end_threshold);
474 static DEVICE_ATTR_RW(charge_control_thresholds);
475 
huawei_wmi_battery_add(struct power_supply * battery,struct acpi_battery_hook * hook)476 static int huawei_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
477 {
478 	int err = 0;
479 
480 	err = device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
481 	if (err)
482 		return err;
483 
484 	err = device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
485 	if (err)
486 		device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
487 
488 	return err;
489 }
490 
huawei_wmi_battery_remove(struct power_supply * battery,struct acpi_battery_hook * hook)491 static int huawei_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
492 {
493 	device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold);
494 	device_remove_file(&battery->dev, &dev_attr_charge_control_end_threshold);
495 
496 	return 0;
497 }
498 
499 static struct acpi_battery_hook huawei_wmi_battery_hook = {
500 	.add_battery = huawei_wmi_battery_add,
501 	.remove_battery = huawei_wmi_battery_remove,
502 	.name = "Huawei Battery Extension"
503 };
504 
huawei_wmi_battery_setup(struct device * dev)505 static void huawei_wmi_battery_setup(struct device *dev)
506 {
507 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
508 
509 	huawei->battery_available = true;
510 	if (huawei_wmi_battery_get(NULL, NULL)) {
511 		huawei->battery_available = false;
512 		return;
513 	}
514 
515 	battery_hook_register(&huawei_wmi_battery_hook);
516 	device_create_file(dev, &dev_attr_charge_control_thresholds);
517 }
518 
huawei_wmi_battery_exit(struct device * dev)519 static void huawei_wmi_battery_exit(struct device *dev)
520 {
521 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
522 
523 	if (huawei->battery_available) {
524 		battery_hook_unregister(&huawei_wmi_battery_hook);
525 		device_remove_file(dev, &dev_attr_charge_control_thresholds);
526 	}
527 }
528 
529 /* Fn lock */
530 
531 /* GFRS byte[1] / SFRS byte[2] (FRSR) fn-lock state values */
532 #define FN_LOCK_ACPI_OFF	1
533 #define FN_LOCK_ACPI_ON		2
534 #define FN_LOCK_ACPI_STAT_OK	0
535 
536 /*
537  * Newer Huawei models (e.g. HUAWEI FLMH-XX / MateBook 14 2024) use direct
538  * ACPI methods \GFRS / \SFRS (Get/Set Fn key Reversal Status) to control
539  * Fn-lock via EC registers 0x6B (read) and 0x6C (write).
540  *
541  * GFRS response buffer layout:
542  *   byte[0] = STAT (FN_LOCK_ACPI_STAT_OK = success)
543  *   byte[1] = FN_LOCK_ACPI_OFF (fn-lock off) or FN_LOCK_ACPI_ON (fn-lock on)
544  *
545  * SFRS argument layout (CreateByteField(Arg0, 0x02, FRSR)):
546  *   Value is read from byte[2] of the integer argument, so it must be
547  *   passed as (value << 16):
548  *   (FN_LOCK_ACPI_OFF << 16) = fn-lock off (writes 0x55 to EC 0x6C)
549  *   (FN_LOCK_ACPI_ON  << 16) = fn-lock on  (writes 0x5A to EC 0x6C)
550  */
551 
huawei_acpi_fn_lock_get(int * on)552 static int huawei_acpi_fn_lock_get(int *on)
553 {
554 	union acpi_object acpi_arg;
555 	struct acpi_object_list arg_list = { .count = 1, .pointer = &acpi_arg };
556 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
557 	acpi_status status;
558 
559 	acpi_arg.type = ACPI_TYPE_INTEGER;
560 	acpi_arg.integer.value = 0;
561 
562 	status = acpi_evaluate_object(NULL, "\\GFRS", &arg_list, &output);
563 	if (ACPI_FAILURE(status))
564 		return -EIO;
565 
566 	union acpi_object *obj __free(kfree) = output.pointer;
567 	if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length < 2)
568 		return -ENODATA;
569 
570 	/* byte[0] = STAT, byte[1] = fn-lock state */
571 	if (obj->buffer.pointer[0] != FN_LOCK_ACPI_STAT_OK)
572 		return -EIO;
573 
574 	switch (obj->buffer.pointer[1]) {
575 	case FN_LOCK_ACPI_OFF:
576 		if (on)
577 			*on = 0;
578 		break;
579 	case FN_LOCK_ACPI_ON:
580 		if (on)
581 			*on = 1;
582 		break;
583 	default:
584 		return -ENODATA;
585 	}
586 
587 	return 0;
588 }
589 
huawei_acpi_fn_lock_set(int on)590 static int huawei_acpi_fn_lock_set(int on)
591 {
592 	union acpi_object acpi_arg;
593 	struct acpi_object_list arg_list = { .count = 1, .pointer = &acpi_arg };
594 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
595 	acpi_status status;
596 
597 	/*
598 	 * SFRS reads byte[2] of its argument via CreateByteField(Arg0, 0x02).
599 	 * on=0 → FRSR=FN_LOCK_ACPI_OFF → EC gets 0x55 (fn-lock off)
600 	 * on=1 → FRSR=FN_LOCK_ACPI_ON  → EC gets 0x5A (fn-lock on)
601 	 */
602 	acpi_arg.type = ACPI_TYPE_INTEGER;
603 	acpi_arg.integer.value = (on ? FN_LOCK_ACPI_ON : FN_LOCK_ACPI_OFF) << 16;
604 
605 	status = acpi_evaluate_object(NULL, "\\SFRS", &arg_list, &output);
606 	if (ACPI_FAILURE(status))
607 		return -EIO;
608 
609 	union acpi_object *obj __free(kfree) = output.pointer;
610 	if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length < 1)
611 		return -ENODATA;
612 
613 	if (obj->buffer.pointer[0] != FN_LOCK_ACPI_STAT_OK)
614 		return -EIO;
615 
616 	return 0;
617 }
618 
huawei_wmi_fn_lock_get(int * on)619 static int huawei_wmi_fn_lock_get(int *on)
620 {
621 	u8 ret[0x100] = { 0 };
622 	int err, i;
623 
624 	/* Newer models: use direct ACPI \GFRS method */
625 	if (acpi_has_method(NULL, "\\GFRS"))
626 		return huawei_acpi_fn_lock_get(on);
627 
628 	/* WMI fallback */
629 	err = huawei_wmi_cmd(FN_LOCK_GET, ret, 0x100);
630 	if (err)
631 		return err;
632 
633 	/* Find the first non-zero value. Return status is ignored. */
634 	i = 1;
635 	do {
636 		if (on)
637 			*on = ret[i] - 1; // -1 undefined, 0 off, 1 on.
638 	} while (i < 0xff && !ret[i++]);
639 
640 	return 0;
641 }
642 
huawei_wmi_fn_lock_set(int on)643 static int huawei_wmi_fn_lock_set(int on)
644 {
645 	union hwmi_arg arg;
646 
647 	/* Newer models: use direct ACPI \SFRS method */
648 	if (acpi_has_method(NULL, "\\SFRS"))
649 		return huawei_acpi_fn_lock_set(on);
650 
651 	/* WMI fallback */
652 	arg.cmd = FN_LOCK_SET;
653 	arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
654 
655 	return huawei_wmi_cmd(arg.cmd, NULL, 0);
656 }
657 
fn_lock_state_show(struct device * dev,struct device_attribute * attr,char * buf)658 static ssize_t fn_lock_state_show(struct device *dev,
659 		struct device_attribute *attr,
660 		char *buf)
661 {
662 	int err, on;
663 
664 	err = huawei_wmi_fn_lock_get(&on);
665 	if (err)
666 		return err;
667 
668 	return sysfs_emit(buf, "%d\n", on);
669 }
670 
fn_lock_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)671 static ssize_t fn_lock_state_store(struct device *dev,
672 		struct device_attribute *attr,
673 		const char *buf, size_t size)
674 {
675 	int on, err;
676 
677 	if (kstrtoint(buf, 10, &on) ||
678 			on < 0 || on > 1)
679 		return -EINVAL;
680 
681 	err = huawei_wmi_fn_lock_set(on);
682 	if (err)
683 		return err;
684 
685 	return size;
686 }
687 
688 static DEVICE_ATTR_RW(fn_lock_state);
689 
huawei_wmi_fn_lock_setup(struct device * dev)690 static void huawei_wmi_fn_lock_setup(struct device *dev)
691 {
692 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
693 
694 	huawei->fn_lock_available = true;
695 	if (huawei_wmi_fn_lock_get(NULL)) {
696 		huawei->fn_lock_available = false;
697 		return;
698 	}
699 
700 	device_create_file(dev, &dev_attr_fn_lock_state);
701 }
702 
huawei_wmi_fn_lock_exit(struct device * dev)703 static void huawei_wmi_fn_lock_exit(struct device *dev)
704 {
705 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
706 
707 	if (huawei->fn_lock_available)
708 		device_remove_file(dev, &dev_attr_fn_lock_state);
709 }
710 
711 /* debugfs */
712 
huawei_wmi_debugfs_call_dump(struct seq_file * m,void * data,union acpi_object * obj)713 static void huawei_wmi_debugfs_call_dump(struct seq_file *m, void *data,
714 		union acpi_object *obj)
715 {
716 	struct huawei_wmi *huawei = m->private;
717 	int i;
718 
719 	switch (obj->type) {
720 	case ACPI_TYPE_INTEGER:
721 		seq_printf(m, "0x%llx", obj->integer.value);
722 		break;
723 	case ACPI_TYPE_STRING:
724 		seq_printf(m, "\"%.*s\"", obj->string.length, obj->string.pointer);
725 		break;
726 	case ACPI_TYPE_BUFFER:
727 		seq_puts(m, "{");
728 		for (i = 0; i < obj->buffer.length; i++) {
729 			seq_printf(m, "0x%02x", obj->buffer.pointer[i]);
730 			if (i < obj->buffer.length - 1)
731 				seq_puts(m, ",");
732 		}
733 		seq_puts(m, "}");
734 		break;
735 	case ACPI_TYPE_PACKAGE:
736 		seq_puts(m, "[");
737 		for (i = 0; i < obj->package.count; i++) {
738 			huawei_wmi_debugfs_call_dump(m, huawei, &obj->package.elements[i]);
739 			if (i < obj->package.count - 1)
740 				seq_puts(m, ",");
741 		}
742 		seq_puts(m, "]");
743 		break;
744 	default:
745 		dev_err(huawei->dev, "Unexpected obj type, got %d\n", obj->type);
746 		return;
747 	}
748 }
749 
huawei_wmi_debugfs_call_show(struct seq_file * m,void * data)750 static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data)
751 {
752 	struct huawei_wmi *huawei = m->private;
753 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
754 	struct acpi_buffer in;
755 	union acpi_object *obj;
756 	int err;
757 
758 	in.length = sizeof(u64);
759 	in.pointer = &huawei->debug.arg;
760 
761 	err = huawei_wmi_call(huawei, &in, &out);
762 	if (err)
763 		return err;
764 
765 	obj = out.pointer;
766 	if (!obj) {
767 		err = -EIO;
768 		goto fail_debugfs_call;
769 	}
770 
771 	huawei_wmi_debugfs_call_dump(m, huawei, obj);
772 
773 fail_debugfs_call:
774 	kfree(out.pointer);
775 	return err;
776 }
777 
778 DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call);
779 
huawei_wmi_debugfs_setup(struct device * dev)780 static void huawei_wmi_debugfs_setup(struct device *dev)
781 {
782 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
783 
784 	huawei->debug.root = debugfs_create_dir("huawei-wmi", NULL);
785 
786 	debugfs_create_x64("arg", 0644, huawei->debug.root,
787 		&huawei->debug.arg);
788 	debugfs_create_file("call", 0400,
789 		huawei->debug.root, huawei, &huawei_wmi_debugfs_call_fops);
790 }
791 
huawei_wmi_debugfs_exit(struct device * dev)792 static void huawei_wmi_debugfs_exit(struct device *dev)
793 {
794 	struct huawei_wmi *huawei = dev_get_drvdata(dev);
795 
796 	debugfs_remove_recursive(huawei->debug.root);
797 }
798 
799 /* Input */
800 
huawei_wmi_process_key(struct input_dev * idev,int code)801 static void huawei_wmi_process_key(struct input_dev *idev, int code)
802 {
803 	const struct key_entry *key;
804 
805 	/*
806 	 * WMI0 uses code 0x80 to indicate a hotkey event.
807 	 * The actual key is fetched from the method WQ00
808 	 * using WMI0_EXPENSIVE_GUID.
809 	 */
810 	if (code == 0x80) {
811 		struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
812 		union acpi_object *obj;
813 		acpi_status status;
814 
815 		status = wmi_query_block(WMI0_EXPENSIVE_GUID, 0, &response);
816 		if (ACPI_FAILURE(status))
817 			return;
818 
819 		obj = (union acpi_object *)response.pointer;
820 		if (obj && obj->type == ACPI_TYPE_INTEGER)
821 			code = obj->integer.value;
822 
823 		kfree(response.pointer);
824 	}
825 
826 	key = sparse_keymap_entry_from_scancode(idev, code);
827 	if (!key) {
828 		dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
829 		return;
830 	}
831 
832 	if (quirks && !quirks->report_brightness &&
833 			(key->sw.code == KEY_BRIGHTNESSDOWN ||
834 			key->sw.code == KEY_BRIGHTNESSUP))
835 		return;
836 
837 	sparse_keymap_report_entry(idev, key, 1, true);
838 }
839 
huawei_wmi_input_notify(union acpi_object * obj,void * context)840 static void huawei_wmi_input_notify(union acpi_object *obj, void *context)
841 {
842 	struct input_dev *idev = (struct input_dev *)context;
843 
844 	if (obj && obj->type == ACPI_TYPE_INTEGER)
845 		huawei_wmi_process_key(idev, obj->integer.value);
846 	else
847 		dev_err(&idev->dev, "Bad response type\n");
848 }
849 
huawei_wmi_input_setup(struct device * dev,const char * guid)850 static int huawei_wmi_input_setup(struct device *dev, const char *guid)
851 {
852 	struct input_dev *idev;
853 	acpi_status status;
854 	int err;
855 
856 	idev = devm_input_allocate_device(dev);
857 	if (!idev)
858 		return -ENOMEM;
859 
860 	idev->name = "Huawei WMI hotkeys";
861 	idev->phys = "wmi/input0";
862 	idev->id.bustype = BUS_HOST;
863 	idev->dev.parent = dev;
864 
865 	err = sparse_keymap_setup(idev, huawei_wmi_keymap, NULL);
866 	if (err)
867 		return err;
868 
869 	err = input_register_device(idev);
870 	if (err)
871 		return err;
872 
873 	status = wmi_install_notify_handler(guid, huawei_wmi_input_notify, idev);
874 	if (ACPI_FAILURE(status))
875 		return -EIO;
876 
877 	return 0;
878 }
879 
huawei_wmi_input_exit(struct device * dev,const char * guid)880 static void huawei_wmi_input_exit(struct device *dev, const char *guid)
881 {
882 	wmi_remove_notify_handler(guid);
883 }
884 
885 /* Huawei driver */
886 
887 static const struct wmi_device_id huawei_wmi_events_id_table[] = {
888 	{ .guid_string = WMI0_EVENT_GUID },
889 	{ .guid_string = HWMI_EVENT_GUID },
890 	{  }
891 };
892 
huawei_wmi_probe(struct platform_device * pdev)893 static int huawei_wmi_probe(struct platform_device *pdev)
894 {
895 	const struct wmi_device_id *guid = huawei_wmi_events_id_table;
896 	int err;
897 
898 	platform_set_drvdata(pdev, huawei_wmi);
899 	huawei_wmi->dev = &pdev->dev;
900 
901 	while (*guid->guid_string) {
902 		if (wmi_has_guid(guid->guid_string)) {
903 			err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string);
904 			if (err) {
905 				dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string);
906 				return err;
907 			}
908 		}
909 
910 		guid++;
911 	}
912 
913 	if (wmi_has_guid(HWMI_METHOD_GUID)) {
914 		mutex_init(&huawei_wmi->wmi_lock);
915 
916 		huawei_wmi_leds_setup(&pdev->dev);
917 		huawei_wmi_fn_lock_setup(&pdev->dev);
918 		huawei_wmi_battery_setup(&pdev->dev);
919 		huawei_wmi_debugfs_setup(&pdev->dev);
920 	}
921 
922 	return 0;
923 }
924 
huawei_wmi_remove(struct platform_device * pdev)925 static void huawei_wmi_remove(struct platform_device *pdev)
926 {
927 	const struct wmi_device_id *guid = huawei_wmi_events_id_table;
928 
929 	while (*guid->guid_string) {
930 		if (wmi_has_guid(guid->guid_string))
931 			huawei_wmi_input_exit(&pdev->dev, guid->guid_string);
932 
933 		guid++;
934 	}
935 
936 	if (wmi_has_guid(HWMI_METHOD_GUID)) {
937 		huawei_wmi_debugfs_exit(&pdev->dev);
938 		huawei_wmi_battery_exit(&pdev->dev);
939 		huawei_wmi_fn_lock_exit(&pdev->dev);
940 	}
941 }
942 
943 static struct platform_driver huawei_wmi_driver = {
944 	.driver = {
945 		.name = "huawei-wmi",
946 	},
947 	.probe = huawei_wmi_probe,
948 	.remove = huawei_wmi_remove,
949 };
950 
huawei_wmi_init(void)951 static __init int huawei_wmi_init(void)
952 {
953 	struct platform_device *pdev;
954 	int err;
955 
956 	huawei_wmi = kzalloc_obj(struct huawei_wmi);
957 	if (!huawei_wmi)
958 		return -ENOMEM;
959 
960 	quirks = &quirk_unknown;
961 	dmi_check_system(huawei_quirks);
962 	if (battery_reset != -1)
963 		quirks->battery_reset = battery_reset;
964 	if (report_brightness != -1)
965 		quirks->report_brightness = report_brightness;
966 
967 	err = platform_driver_register(&huawei_wmi_driver);
968 	if (err)
969 		goto pdrv_err;
970 
971 	pdev = platform_device_register_simple("huawei-wmi", PLATFORM_DEVID_NONE, NULL, 0);
972 	if (IS_ERR(pdev)) {
973 		err = PTR_ERR(pdev);
974 		goto pdev_err;
975 	}
976 
977 	return 0;
978 
979 pdev_err:
980 	platform_driver_unregister(&huawei_wmi_driver);
981 pdrv_err:
982 	kfree(huawei_wmi);
983 	return err;
984 }
985 
huawei_wmi_exit(void)986 static __exit void huawei_wmi_exit(void)
987 {
988 	struct platform_device *pdev = to_platform_device(huawei_wmi->dev);
989 
990 	platform_device_unregister(pdev);
991 	platform_driver_unregister(&huawei_wmi_driver);
992 
993 	kfree(huawei_wmi);
994 }
995 
996 module_init(huawei_wmi_init);
997 module_exit(huawei_wmi_exit);
998 
999 MODULE_ALIAS("wmi:"HWMI_METHOD_GUID);
1000 MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table);
1001 MODULE_AUTHOR("Ayman Bagabas <ayman.bagabas@gmail.com>");
1002 MODULE_DESCRIPTION("Huawei WMI laptop extras driver");
1003 MODULE_LICENSE("GPL v2");
1004