2 * Alienware AlienFX control
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.
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.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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"
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
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);
43 enum INTERFACE_FLAGS {
48 enum LEGACY_CONTROL_STATES {
54 enum WMAX_CONTROL_STATES {
64 static struct quirk_entry *quirks;
66 static struct quirk_entry quirk_unknown = {
70 static struct quirk_entry quirk_x51_family = {
74 static int dmi_matched(const struct dmi_system_id *dmi)
76 quirks = dmi->driver_data;
80 static struct dmi_system_id alienware_quirks[] = {
82 .callback = dmi_matched,
83 .ident = "Alienware X51 R1",
85 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
86 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
88 .driver_data = &quirk_x51_family,
91 .callback = dmi_matched,
92 .ident = "Alienware X51 R2",
94 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
95 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
97 .driver_data = &quirk_x51_family,
102 struct color_platform {
108 struct platform_zone {
110 struct device_attribute *attr;
111 struct color_platform colors;
114 struct wmax_brightness_args {
123 struct legacy_led_args {
124 struct color_platform colors;
129 struct wmax_led_args {
131 struct color_platform colors;
135 static struct platform_device *platform_device;
136 static struct device_attribute *zone_dev_attrs;
137 static struct attribute **zone_attrs;
138 static struct platform_zone *zone_data;
140 static struct platform_driver platform_driver = {
142 .name = "alienware-wmi",
143 .owner = THIS_MODULE,
147 static struct attribute_group zone_attribute_group = {
152 static u8 lighting_control_state;
153 static u8 global_brightness;
156 * Helpers used for zone control
158 static int parse_rgb(const char *buf, struct platform_zone *zone)
160 long unsigned int rgb;
163 struct color_platform cp;
167 ret = kstrtoul(buf, 16, &rgb);
171 /* RGB triplet notation is 24-bit hexadecimal */
175 repackager.package = rgb & 0x0f0f0f0f;
176 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
177 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
178 zone->colors = repackager.cp;
182 static struct platform_zone *match_zone(struct device_attribute *attr)
185 for (i = 0; i < quirks->num_zones; i++) {
186 if ((struct device_attribute *)zone_data[i].attr == attr) {
187 pr_debug("alienware-wmi: matched zone location: %d\n",
188 zone_data[i].location);
189 return &zone_data[i];
196 * Individual RGB zone control
198 static int alienware_update_led(struct platform_zone *zone)
203 struct acpi_buffer input;
204 struct legacy_led_args legacy_args;
205 struct wmax_led_args wmax_args;
206 if (interface == WMAX) {
207 wmax_args.led_mask = 1 << zone->location;
208 wmax_args.colors = zone->colors;
209 wmax_args.state = lighting_control_state;
210 guid = WMAX_CONTROL_GUID;
211 method_id = WMAX_METHOD_ZONE_CONTROL;
213 input.length = (acpi_size) sizeof(wmax_args);
214 input.pointer = &wmax_args;
216 legacy_args.colors = zone->colors;
217 legacy_args.brightness = global_brightness;
218 legacy_args.state = 0;
219 if (lighting_control_state == LEGACY_BOOTING ||
220 lighting_control_state == LEGACY_SUSPEND) {
221 guid = LEGACY_POWER_CONTROL_GUID;
222 legacy_args.state = lighting_control_state;
224 guid = LEGACY_CONTROL_GUID;
225 method_id = zone->location + 1;
227 input.length = (acpi_size) sizeof(legacy_args);
228 input.pointer = &legacy_args;
230 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
232 status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
233 if (ACPI_FAILURE(status))
234 pr_err("alienware-wmi: zone set failure: %u\n", status);
235 return ACPI_FAILURE(status);
238 static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
241 struct platform_zone *target_zone;
242 target_zone = match_zone(attr);
243 if (target_zone == NULL)
244 return sprintf(buf, "red: -1, green: -1, blue: -1\n");
245 return sprintf(buf, "red: %d, green: %d, blue: %d\n",
246 target_zone->colors.red,
247 target_zone->colors.green, target_zone->colors.blue);
251 static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
252 const char *buf, size_t count)
254 struct platform_zone *target_zone;
256 target_zone = match_zone(attr);
257 if (target_zone == NULL) {
258 pr_err("alienware-wmi: invalid target zone\n");
261 ret = parse_rgb(buf, target_zone);
264 ret = alienware_update_led(target_zone);
265 return ret ? ret : count;
269 * LED Brightness (Global)
271 static int wmax_brightness(int brightness)
274 struct acpi_buffer input;
275 struct wmax_brightness_args args = {
277 .percentage = brightness,
279 input.length = (acpi_size) sizeof(args);
280 input.pointer = &args;
281 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
282 WMAX_METHOD_BRIGHTNESS, &input, NULL);
283 if (ACPI_FAILURE(status))
284 pr_err("alienware-wmi: brightness set failure: %u\n", status);
285 return ACPI_FAILURE(status);
288 static void global_led_set(struct led_classdev *led_cdev,
289 enum led_brightness brightness)
292 global_brightness = brightness;
293 if (interface == WMAX)
294 ret = wmax_brightness(brightness);
296 ret = alienware_update_led(&zone_data[0]);
298 pr_err("LED brightness update failed\n");
301 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
303 return global_brightness;
306 static struct led_classdev global_led = {
307 .brightness_set = global_led_set,
308 .brightness_get = global_led_get,
309 .name = "alienware::global_brightness",
313 * Lighting control state device attribute (Global)
315 static ssize_t show_control_state(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 if (lighting_control_state == LEGACY_BOOTING)
319 return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
320 else if (lighting_control_state == LEGACY_SUSPEND)
321 return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
322 return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
325 static ssize_t store_control_state(struct device *dev,
326 struct device_attribute *attr,
327 const char *buf, size_t count)
329 long unsigned int val;
330 if (strcmp(buf, "booting\n") == 0)
331 val = LEGACY_BOOTING;
332 else if (strcmp(buf, "suspend\n") == 0)
333 val = LEGACY_SUSPEND;
334 else if (interface == LEGACY)
335 val = LEGACY_RUNNING;
338 lighting_control_state = val;
339 pr_debug("alienware-wmi: updated control state to %d\n",
340 lighting_control_state);
344 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
345 store_control_state);
347 static int alienware_zone_init(struct platform_device *dev)
353 if (interface == WMAX) {
354 lighting_control_state = WMAX_RUNNING;
355 } else if (interface == LEGACY) {
356 lighting_control_state = LEGACY_RUNNING;
358 global_led.max_brightness = 0x0F;
359 global_brightness = global_led.max_brightness;
362 * - zone_dev_attrs num_zones + 1 is for individual zones and then
364 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
365 * the lighting control + null terminated
366 * - zone_data num_zones is for the distinct zones
369 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
375 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
381 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
386 for (i = 0; i < quirks->num_zones; i++) {
387 sprintf(buffer, "zone%02X", i);
388 name = kstrdup(buffer, GFP_KERNEL);
391 sysfs_attr_init(&zone_dev_attrs[i].attr);
392 zone_dev_attrs[i].attr.name = name;
393 zone_dev_attrs[i].attr.mode = 0644;
394 zone_dev_attrs[i].show = zone_show;
395 zone_dev_attrs[i].store = zone_set;
396 zone_data[i].location = i;
397 zone_attrs[i] = &zone_dev_attrs[i].attr;
398 zone_data[i].attr = &zone_dev_attrs[i];
400 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
401 zone_attribute_group.attrs = zone_attrs;
403 led_classdev_register(&dev->dev, &global_led);
405 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
408 static void alienware_zone_exit(struct platform_device *dev)
410 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
411 led_classdev_unregister(&global_led);
412 if (zone_dev_attrs) {
414 for (i = 0; i < quirks->num_zones; i++)
415 kfree(zone_dev_attrs[i].attr.name);
417 kfree(zone_dev_attrs);
423 The HDMI mux sysfs node indicates the status of the HDMI input mux.
424 It can toggle between standard system GPU output and HDMI input.
426 static acpi_status alienware_hdmi_command(struct hdmi_args *in_args,
427 u32 command, int *out_data)
430 union acpi_object *obj;
431 struct acpi_buffer input;
432 struct acpi_buffer output;
434 input.length = (acpi_size) sizeof(*in_args);
435 input.pointer = in_args;
436 if (out_data != NULL) {
437 output.length = ACPI_ALLOCATE_BUFFER;
438 output.pointer = NULL;
439 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
440 command, &input, &output);
442 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
443 command, &input, NULL);
445 if (ACPI_SUCCESS(status) && out_data != NULL) {
446 obj = (union acpi_object *)output.pointer;
447 if (obj && obj->type == ACPI_TYPE_INTEGER)
448 *out_data = (u32) obj->integer.value;
454 static ssize_t show_hdmi_cable(struct device *dev,
455 struct device_attribute *attr, char *buf)
459 struct hdmi_args in_args = {
463 alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_CABLE,
465 if (ACPI_SUCCESS(status)) {
467 return scnprintf(buf, PAGE_SIZE,
468 "[unconnected] connected unknown\n");
469 else if (out_data == 1)
470 return scnprintf(buf, PAGE_SIZE,
471 "unconnected [connected] unknown\n");
473 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
474 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
477 static ssize_t show_hdmi_source(struct device *dev,
478 struct device_attribute *attr, char *buf)
482 struct hdmi_args in_args = {
486 alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_STATUS,
489 if (ACPI_SUCCESS(status)) {
491 return scnprintf(buf, PAGE_SIZE,
492 "[input] gpu unknown\n");
493 else if (out_data == 2)
494 return scnprintf(buf, PAGE_SIZE,
495 "input [gpu] unknown\n");
497 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
498 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
501 static ssize_t toggle_hdmi_source(struct device *dev,
502 struct device_attribute *attr,
503 const char *buf, size_t count)
506 struct hdmi_args args;
507 if (strcmp(buf, "gpu\n") == 0)
509 else if (strcmp(buf, "input\n") == 0)
513 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
515 status = alienware_hdmi_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
517 if (ACPI_FAILURE(status))
518 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
523 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
524 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
527 static struct attribute *hdmi_attrs[] = {
528 &dev_attr_cable.attr,
529 &dev_attr_source.attr,
533 static struct attribute_group hdmi_attribute_group = {
538 static void remove_hdmi(struct platform_device *dev)
540 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
543 static int create_hdmi(struct platform_device *dev)
547 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
549 goto error_create_hdmi;
557 static int __init alienware_wmi_init(void)
561 if (wmi_has_guid(LEGACY_CONTROL_GUID))
563 else if (wmi_has_guid(WMAX_CONTROL_GUID))
566 pr_warn("alienware-wmi: No known WMI GUID found\n");
570 dmi_check_system(alienware_quirks);
572 quirks = &quirk_unknown;
574 ret = platform_driver_register(&platform_driver);
576 goto fail_platform_driver;
577 platform_device = platform_device_alloc("alienware-wmi", -1);
578 if (!platform_device) {
580 goto fail_platform_device1;
582 ret = platform_device_add(platform_device);
584 goto fail_platform_device2;
586 if (interface == WMAX) {
587 ret = create_hdmi(platform_device);
592 ret = alienware_zone_init(platform_device);
594 goto fail_prep_zones;
599 alienware_zone_exit(platform_device);
601 platform_device_del(platform_device);
602 fail_platform_device2:
603 platform_device_put(platform_device);
604 fail_platform_device1:
605 platform_driver_unregister(&platform_driver);
606 fail_platform_driver:
610 module_init(alienware_wmi_init);
612 static void __exit alienware_wmi_exit(void)
614 if (platform_device) {
615 alienware_zone_exit(platform_device);
616 remove_hdmi(platform_device);
617 platform_device_unregister(platform_device);
618 platform_driver_unregister(&platform_driver);
622 module_exit(alienware_wmi_exit);