• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Alienware AlienFX control
3  *
4  * Copyright (C) 2014 Dell Inc <mario_limonciello@dell.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/acpi.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/dmi.h>
24 #include <linux/acpi.h>
25 #include <linux/leds.h>
26 
27 #define LEGACY_CONTROL_GUID		"A90597CE-A997-11DA-B012-B622A1EF5492"
28 #define LEGACY_POWER_CONTROL_GUID	"A80593CE-A997-11DA-B012-B622A1EF5492"
29 #define WMAX_CONTROL_GUID		"A70591CE-A997-11DA-B012-B622A1EF5492"
30 
31 #define WMAX_METHOD_HDMI_SOURCE		0x1
32 #define WMAX_METHOD_HDMI_STATUS		0x2
33 #define WMAX_METHOD_BRIGHTNESS		0x3
34 #define WMAX_METHOD_ZONE_CONTROL	0x4
35 #define WMAX_METHOD_HDMI_CABLE		0x5
36 
37 MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
38 MODULE_DESCRIPTION("Alienware special feature control");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
41 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
42 
43 enum INTERFACE_FLAGS {
44 	LEGACY,
45 	WMAX,
46 };
47 
48 enum LEGACY_CONTROL_STATES {
49 	LEGACY_RUNNING = 1,
50 	LEGACY_BOOTING = 0,
51 	LEGACY_SUSPEND = 3,
52 };
53 
54 enum WMAX_CONTROL_STATES {
55 	WMAX_RUNNING = 0xFF,
56 	WMAX_BOOTING = 0,
57 	WMAX_SUSPEND = 3,
58 };
59 
60 struct quirk_entry {
61 	u8 num_zones;
62 	u8 hdmi_mux;
63 };
64 
65 static struct quirk_entry *quirks;
66 
67 static struct quirk_entry quirk_unknown = {
68 	.num_zones = 2,
69 	.hdmi_mux = 0,
70 };
71 
72 static struct quirk_entry quirk_x51_family = {
73 	.num_zones = 3,
74 	.hdmi_mux = 0.
75 };
76 
77 static struct quirk_entry quirk_asm100 = {
78 	.num_zones = 2,
79 	.hdmi_mux = 1,
80 };
81 
dmi_matched(const struct dmi_system_id * dmi)82 static int __init dmi_matched(const struct dmi_system_id *dmi)
83 {
84 	quirks = dmi->driver_data;
85 	return 1;
86 }
87 
88 static const struct dmi_system_id alienware_quirks[] __initconst = {
89 	{
90 	 .callback = dmi_matched,
91 	 .ident = "Alienware X51 R1",
92 	 .matches = {
93 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
94 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
95 		     },
96 	 .driver_data = &quirk_x51_family,
97 	 },
98 	{
99 	 .callback = dmi_matched,
100 	 .ident = "Alienware X51 R2",
101 	 .matches = {
102 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
103 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
104 		     },
105 	 .driver_data = &quirk_x51_family,
106 	 },
107 	{
108 		.callback = dmi_matched,
109 		.ident = "Alienware ASM100",
110 		.matches = {
111 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
112 			DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
113 		},
114 		.driver_data = &quirk_asm100,
115 	},
116 	{}
117 };
118 
119 struct color_platform {
120 	u8 blue;
121 	u8 green;
122 	u8 red;
123 } __packed;
124 
125 struct platform_zone {
126 	u8 location;
127 	struct device_attribute *attr;
128 	struct color_platform colors;
129 };
130 
131 struct wmax_brightness_args {
132 	u32 led_mask;
133 	u32 percentage;
134 };
135 
136 struct hdmi_args {
137 	u8 arg;
138 };
139 
140 struct legacy_led_args {
141 	struct color_platform colors;
142 	u8 brightness;
143 	u8 state;
144 } __packed;
145 
146 struct wmax_led_args {
147 	u32 led_mask;
148 	struct color_platform colors;
149 	u8 state;
150 } __packed;
151 
152 static struct platform_device *platform_device;
153 static struct device_attribute *zone_dev_attrs;
154 static struct attribute **zone_attrs;
155 static struct platform_zone *zone_data;
156 
157 static struct platform_driver platform_driver = {
158 	.driver = {
159 		   .name = "alienware-wmi",
160 		   .owner = THIS_MODULE,
161 		   }
162 };
163 
164 static struct attribute_group zone_attribute_group = {
165 	.name = "rgb_zones",
166 };
167 
168 static u8 interface;
169 static u8 lighting_control_state;
170 static u8 global_brightness;
171 
172 /*
173  * Helpers used for zone control
174 */
parse_rgb(const char * buf,struct platform_zone * zone)175 static int parse_rgb(const char *buf, struct platform_zone *zone)
176 {
177 	long unsigned int rgb;
178 	int ret;
179 	union color_union {
180 		struct color_platform cp;
181 		int package;
182 	} repackager;
183 
184 	ret = kstrtoul(buf, 16, &rgb);
185 	if (ret)
186 		return ret;
187 
188 	/* RGB triplet notation is 24-bit hexadecimal */
189 	if (rgb > 0xFFFFFF)
190 		return -EINVAL;
191 
192 	repackager.package = rgb & 0x0f0f0f0f;
193 	pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
194 		 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
195 	zone->colors = repackager.cp;
196 	return 0;
197 }
198 
match_zone(struct device_attribute * attr)199 static struct platform_zone *match_zone(struct device_attribute *attr)
200 {
201 	int i;
202 	for (i = 0; i < quirks->num_zones; i++) {
203 		if ((struct device_attribute *)zone_data[i].attr == attr) {
204 			pr_debug("alienware-wmi: matched zone location: %d\n",
205 				 zone_data[i].location);
206 			return &zone_data[i];
207 		}
208 	}
209 	return NULL;
210 }
211 
212 /*
213  * Individual RGB zone control
214 */
alienware_update_led(struct platform_zone * zone)215 static int alienware_update_led(struct platform_zone *zone)
216 {
217 	int method_id;
218 	acpi_status status;
219 	char *guid;
220 	struct acpi_buffer input;
221 	struct legacy_led_args legacy_args;
222 	struct wmax_led_args wmax_args;
223 	if (interface == WMAX) {
224 		wmax_args.led_mask = 1 << zone->location;
225 		wmax_args.colors = zone->colors;
226 		wmax_args.state = lighting_control_state;
227 		guid = WMAX_CONTROL_GUID;
228 		method_id = WMAX_METHOD_ZONE_CONTROL;
229 
230 		input.length = (acpi_size) sizeof(wmax_args);
231 		input.pointer = &wmax_args;
232 	} else {
233 		legacy_args.colors = zone->colors;
234 		legacy_args.brightness = global_brightness;
235 		legacy_args.state = 0;
236 		if (lighting_control_state == LEGACY_BOOTING ||
237 		    lighting_control_state == LEGACY_SUSPEND) {
238 			guid = LEGACY_POWER_CONTROL_GUID;
239 			legacy_args.state = lighting_control_state;
240 		} else
241 			guid = LEGACY_CONTROL_GUID;
242 		method_id = zone->location + 1;
243 
244 		input.length = (acpi_size) sizeof(legacy_args);
245 		input.pointer = &legacy_args;
246 	}
247 	pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
248 
249 	status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
250 	if (ACPI_FAILURE(status))
251 		pr_err("alienware-wmi: zone set failure: %u\n", status);
252 	return ACPI_FAILURE(status);
253 }
254 
zone_show(struct device * dev,struct device_attribute * attr,char * buf)255 static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
256 			 char *buf)
257 {
258 	struct platform_zone *target_zone;
259 	target_zone = match_zone(attr);
260 	if (target_zone == NULL)
261 		return sprintf(buf, "red: -1, green: -1, blue: -1\n");
262 	return sprintf(buf, "red: %d, green: %d, blue: %d\n",
263 		       target_zone->colors.red,
264 		       target_zone->colors.green, target_zone->colors.blue);
265 
266 }
267 
zone_set(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)268 static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
269 			const char *buf, size_t count)
270 {
271 	struct platform_zone *target_zone;
272 	int ret;
273 	target_zone = match_zone(attr);
274 	if (target_zone == NULL) {
275 		pr_err("alienware-wmi: invalid target zone\n");
276 		return 1;
277 	}
278 	ret = parse_rgb(buf, target_zone);
279 	if (ret)
280 		return ret;
281 	ret = alienware_update_led(target_zone);
282 	return ret ? ret : count;
283 }
284 
285 /*
286  * LED Brightness (Global)
287 */
wmax_brightness(int brightness)288 static int wmax_brightness(int brightness)
289 {
290 	acpi_status status;
291 	struct acpi_buffer input;
292 	struct wmax_brightness_args args = {
293 		.led_mask = 0xFF,
294 		.percentage = brightness,
295 	};
296 	input.length = (acpi_size) sizeof(args);
297 	input.pointer = &args;
298 	status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
299 				     WMAX_METHOD_BRIGHTNESS, &input, NULL);
300 	if (ACPI_FAILURE(status))
301 		pr_err("alienware-wmi: brightness set failure: %u\n", status);
302 	return ACPI_FAILURE(status);
303 }
304 
global_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)305 static void global_led_set(struct led_classdev *led_cdev,
306 			   enum led_brightness brightness)
307 {
308 	int ret;
309 	global_brightness = brightness;
310 	if (interface == WMAX)
311 		ret = wmax_brightness(brightness);
312 	else
313 		ret = alienware_update_led(&zone_data[0]);
314 	if (ret)
315 		pr_err("LED brightness update failed\n");
316 }
317 
global_led_get(struct led_classdev * led_cdev)318 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
319 {
320 	return global_brightness;
321 }
322 
323 static struct led_classdev global_led = {
324 	.brightness_set = global_led_set,
325 	.brightness_get = global_led_get,
326 	.name = "alienware::global_brightness",
327 };
328 
329 /*
330  * Lighting control state device attribute (Global)
331 */
show_control_state(struct device * dev,struct device_attribute * attr,char * buf)332 static ssize_t show_control_state(struct device *dev,
333 				  struct device_attribute *attr, char *buf)
334 {
335 	if (lighting_control_state == LEGACY_BOOTING)
336 		return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
337 	else if (lighting_control_state == LEGACY_SUSPEND)
338 		return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
339 	return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
340 }
341 
store_control_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)342 static ssize_t store_control_state(struct device *dev,
343 				   struct device_attribute *attr,
344 				   const char *buf, size_t count)
345 {
346 	long unsigned int val;
347 	if (strcmp(buf, "booting\n") == 0)
348 		val = LEGACY_BOOTING;
349 	else if (strcmp(buf, "suspend\n") == 0)
350 		val = LEGACY_SUSPEND;
351 	else if (interface == LEGACY)
352 		val = LEGACY_RUNNING;
353 	else
354 		val = WMAX_RUNNING;
355 	lighting_control_state = val;
356 	pr_debug("alienware-wmi: updated control state to %d\n",
357 		 lighting_control_state);
358 	return count;
359 }
360 
361 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
362 		   store_control_state);
363 
alienware_zone_init(struct platform_device * dev)364 static int alienware_zone_init(struct platform_device *dev)
365 {
366 	int i;
367 	char buffer[10];
368 	char *name;
369 
370 	if (interface == WMAX) {
371 		lighting_control_state = WMAX_RUNNING;
372 	} else if (interface == LEGACY) {
373 		lighting_control_state = LEGACY_RUNNING;
374 	}
375 	global_led.max_brightness = 0x0F;
376 	global_brightness = global_led.max_brightness;
377 
378 	/*
379 	 *      - zone_dev_attrs num_zones + 1 is for individual zones and then
380 	 *        null terminated
381 	 *      - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
382 	 *        the lighting control + null terminated
383 	 *      - zone_data num_zones is for the distinct zones
384 	 */
385 	zone_dev_attrs =
386 	    kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
387 		    GFP_KERNEL);
388 	if (!zone_dev_attrs)
389 		return -ENOMEM;
390 
391 	zone_attrs =
392 	    kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
393 		    GFP_KERNEL);
394 	if (!zone_attrs)
395 		return -ENOMEM;
396 
397 	zone_data =
398 	    kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
399 		    GFP_KERNEL);
400 	if (!zone_data)
401 		return -ENOMEM;
402 
403 	for (i = 0; i < quirks->num_zones; i++) {
404 		sprintf(buffer, "zone%02X", i);
405 		name = kstrdup(buffer, GFP_KERNEL);
406 		if (name == NULL)
407 			return 1;
408 		sysfs_attr_init(&zone_dev_attrs[i].attr);
409 		zone_dev_attrs[i].attr.name = name;
410 		zone_dev_attrs[i].attr.mode = 0644;
411 		zone_dev_attrs[i].show = zone_show;
412 		zone_dev_attrs[i].store = zone_set;
413 		zone_data[i].location = i;
414 		zone_attrs[i] = &zone_dev_attrs[i].attr;
415 		zone_data[i].attr = &zone_dev_attrs[i];
416 	}
417 	zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
418 	zone_attribute_group.attrs = zone_attrs;
419 
420 	led_classdev_register(&dev->dev, &global_led);
421 
422 	return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
423 }
424 
alienware_zone_exit(struct platform_device * dev)425 static void alienware_zone_exit(struct platform_device *dev)
426 {
427 	sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
428 	led_classdev_unregister(&global_led);
429 	if (zone_dev_attrs) {
430 		int i;
431 		for (i = 0; i < quirks->num_zones; i++)
432 			kfree(zone_dev_attrs[i].attr.name);
433 	}
434 	kfree(zone_dev_attrs);
435 	kfree(zone_data);
436 	kfree(zone_attrs);
437 }
438 
439 /*
440 	The HDMI mux sysfs node indicates the status of the HDMI input mux.
441 	It can toggle between standard system GPU output and HDMI input.
442 */
alienware_hdmi_command(struct hdmi_args * in_args,u32 command,int * out_data)443 static acpi_status alienware_hdmi_command(struct hdmi_args *in_args,
444 					  u32 command, int *out_data)
445 {
446 	acpi_status status;
447 	union acpi_object *obj;
448 	struct acpi_buffer input;
449 	struct acpi_buffer output;
450 
451 	input.length = (acpi_size) sizeof(*in_args);
452 	input.pointer = in_args;
453 	if (out_data != NULL) {
454 		output.length = ACPI_ALLOCATE_BUFFER;
455 		output.pointer = NULL;
456 		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
457 					     command, &input, &output);
458 	} else
459 		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
460 					     command, &input, NULL);
461 
462 	if (ACPI_SUCCESS(status) && out_data != NULL) {
463 		obj = (union acpi_object *)output.pointer;
464 		if (obj && obj->type == ACPI_TYPE_INTEGER)
465 			*out_data = (u32) obj->integer.value;
466 	}
467 	return status;
468 
469 }
470 
show_hdmi_cable(struct device * dev,struct device_attribute * attr,char * buf)471 static ssize_t show_hdmi_cable(struct device *dev,
472 			       struct device_attribute *attr, char *buf)
473 {
474 	acpi_status status;
475 	u32 out_data;
476 	struct hdmi_args in_args = {
477 		.arg = 0,
478 	};
479 	status =
480 	    alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_CABLE,
481 				   (u32 *) &out_data);
482 	if (ACPI_SUCCESS(status)) {
483 		if (out_data == 0)
484 			return scnprintf(buf, PAGE_SIZE,
485 					 "[unconnected] connected unknown\n");
486 		else if (out_data == 1)
487 			return scnprintf(buf, PAGE_SIZE,
488 					 "unconnected [connected] unknown\n");
489 	}
490 	pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
491 	return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
492 }
493 
show_hdmi_source(struct device * dev,struct device_attribute * attr,char * buf)494 static ssize_t show_hdmi_source(struct device *dev,
495 				struct device_attribute *attr, char *buf)
496 {
497 	acpi_status status;
498 	u32 out_data;
499 	struct hdmi_args in_args = {
500 		.arg = 0,
501 	};
502 	status =
503 	    alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_STATUS,
504 				   (u32 *) &out_data);
505 
506 	if (ACPI_SUCCESS(status)) {
507 		if (out_data == 1)
508 			return scnprintf(buf, PAGE_SIZE,
509 					 "[input] gpu unknown\n");
510 		else if (out_data == 2)
511 			return scnprintf(buf, PAGE_SIZE,
512 					 "input [gpu] unknown\n");
513 	}
514 	pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
515 	return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
516 }
517 
toggle_hdmi_source(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)518 static ssize_t toggle_hdmi_source(struct device *dev,
519 				  struct device_attribute *attr,
520 				  const char *buf, size_t count)
521 {
522 	acpi_status status;
523 	struct hdmi_args args;
524 	if (strcmp(buf, "gpu\n") == 0)
525 		args.arg = 1;
526 	else if (strcmp(buf, "input\n") == 0)
527 		args.arg = 2;
528 	else
529 		args.arg = 3;
530 	pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
531 
532 	status = alienware_hdmi_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
533 
534 	if (ACPI_FAILURE(status))
535 		pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
536 		       status);
537 	return count;
538 }
539 
540 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
541 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
542 		   toggle_hdmi_source);
543 
544 static struct attribute *hdmi_attrs[] = {
545 	&dev_attr_cable.attr,
546 	&dev_attr_source.attr,
547 	NULL,
548 };
549 
550 static struct attribute_group hdmi_attribute_group = {
551 	.name = "hdmi",
552 	.attrs = hdmi_attrs,
553 };
554 
remove_hdmi(struct platform_device * dev)555 static void remove_hdmi(struct platform_device *dev)
556 {
557 	if (quirks->hdmi_mux > 0)
558 		sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
559 }
560 
create_hdmi(struct platform_device * dev)561 static int create_hdmi(struct platform_device *dev)
562 {
563 	int ret;
564 
565 	ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
566 	if (ret)
567 		goto error_create_hdmi;
568 	return 0;
569 
570 error_create_hdmi:
571 	remove_hdmi(dev);
572 	return ret;
573 }
574 
alienware_wmi_init(void)575 static int __init alienware_wmi_init(void)
576 {
577 	int ret;
578 
579 	if (wmi_has_guid(LEGACY_CONTROL_GUID))
580 		interface = LEGACY;
581 	else if (wmi_has_guid(WMAX_CONTROL_GUID))
582 		interface = WMAX;
583 	else {
584 		pr_warn("alienware-wmi: No known WMI GUID found\n");
585 		return -ENODEV;
586 	}
587 
588 	dmi_check_system(alienware_quirks);
589 	if (quirks == NULL)
590 		quirks = &quirk_unknown;
591 
592 	ret = platform_driver_register(&platform_driver);
593 	if (ret)
594 		goto fail_platform_driver;
595 	platform_device = platform_device_alloc("alienware-wmi", -1);
596 	if (!platform_device) {
597 		ret = -ENOMEM;
598 		goto fail_platform_device1;
599 	}
600 	ret = platform_device_add(platform_device);
601 	if (ret)
602 		goto fail_platform_device2;
603 
604 	if (quirks->hdmi_mux > 0) {
605 		ret = create_hdmi(platform_device);
606 		if (ret)
607 			goto fail_prep_hdmi;
608 	}
609 
610 	ret = alienware_zone_init(platform_device);
611 	if (ret)
612 		goto fail_prep_zones;
613 
614 	return 0;
615 
616 fail_prep_zones:
617 	alienware_zone_exit(platform_device);
618 fail_prep_hdmi:
619 	platform_device_del(platform_device);
620 fail_platform_device2:
621 	platform_device_put(platform_device);
622 fail_platform_device1:
623 	platform_driver_unregister(&platform_driver);
624 fail_platform_driver:
625 	return ret;
626 }
627 
628 module_init(alienware_wmi_init);
629 
alienware_wmi_exit(void)630 static void __exit alienware_wmi_exit(void)
631 {
632 	if (platform_device) {
633 		alienware_zone_exit(platform_device);
634 		remove_hdmi(platform_device);
635 		platform_device_unregister(platform_device);
636 		platform_driver_unregister(&platform_driver);
637 	}
638 }
639 
640 module_exit(alienware_wmi_exit);
641