Linux Audio

Check our new training course

Loading...
v4.6
  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#define WMAX_METHOD_AMPLIFIER_CABLE	0x6
 37#define WMAX_METHOD_DEEP_SLEEP_CONTROL	0x0B
 38#define WMAX_METHOD_DEEP_SLEEP_STATUS	0x0C
 39
 40MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
 41MODULE_DESCRIPTION("Alienware special feature control");
 42MODULE_LICENSE("GPL");
 43MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
 44MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
 45
 46enum INTERFACE_FLAGS {
 47	LEGACY,
 48	WMAX,
 49};
 50
 51enum LEGACY_CONTROL_STATES {
 52	LEGACY_RUNNING = 1,
 53	LEGACY_BOOTING = 0,
 54	LEGACY_SUSPEND = 3,
 55};
 56
 57enum WMAX_CONTROL_STATES {
 58	WMAX_RUNNING = 0xFF,
 59	WMAX_BOOTING = 0,
 60	WMAX_SUSPEND = 3,
 61};
 62
 63struct quirk_entry {
 64	u8 num_zones;
 65	u8 hdmi_mux;
 66	u8 amplifier;
 67	u8 deepslp;
 68};
 69
 70static struct quirk_entry *quirks;
 71
 72static struct quirk_entry quirk_unknown = {
 73	.num_zones = 2,
 74	.hdmi_mux = 0,
 75	.amplifier = 0,
 76	.deepslp = 0,
 77};
 78
 79static struct quirk_entry quirk_x51_r1_r2 = {
 80	.num_zones = 3,
 81	.hdmi_mux = 0,
 82	.amplifier = 0,
 83	.deepslp = 0,
 84};
 85
 86static struct quirk_entry quirk_x51_r3 = {
 87	.num_zones = 4,
 88	.hdmi_mux = 0,
 89	.amplifier = 1,
 90	.deepslp = 0,
 91};
 92
 93static struct quirk_entry quirk_asm100 = {
 94	.num_zones = 2,
 95	.hdmi_mux = 1,
 96	.amplifier = 0,
 97	.deepslp = 0,
 98};
 99
100static struct quirk_entry quirk_asm200 = {
101	.num_zones = 2,
102	.hdmi_mux = 1,
103	.amplifier = 0,
104	.deepslp = 1,
105};
106
107static struct quirk_entry quirk_asm201 = {
108	.num_zones = 2,
109	.hdmi_mux = 1,
110	.amplifier = 1,
111	.deepslp = 1,
112};
113
114static int __init dmi_matched(const struct dmi_system_id *dmi)
115{
116	quirks = dmi->driver_data;
117	return 1;
118}
119
120static const struct dmi_system_id alienware_quirks[] __initconst = {
121	{
122	 .callback = dmi_matched,
123	 .ident = "Alienware X51 R3",
124	 .matches = {
125		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
126		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
127		     },
128	 .driver_data = &quirk_x51_r3,
129	 },
130	{
131	 .callback = dmi_matched,
132	 .ident = "Alienware X51 R2",
133	 .matches = {
134		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
135		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
136		     },
137	 .driver_data = &quirk_x51_r1_r2,
138	 },
139	{
140	 .callback = dmi_matched,
141	 .ident = "Alienware X51 R1",
142	 .matches = {
143		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
144		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
145		     },
146	 .driver_data = &quirk_x51_r1_r2,
147	 },
148	{
149	 .callback = dmi_matched,
150	 .ident = "Alienware ASM100",
151	 .matches = {
152		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
153		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
154		     },
155	 .driver_data = &quirk_asm100,
156	 },
157	{
158	 .callback = dmi_matched,
159	 .ident = "Alienware ASM200",
160	 .matches = {
161		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
162		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM200"),
163		     },
164	 .driver_data = &quirk_asm200,
165	 },
166	{
167	 .callback = dmi_matched,
168	 .ident = "Alienware ASM201",
169	 .matches = {
170		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
171		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM201"),
172		     },
173	 .driver_data = &quirk_asm201,
174	 },
175	{}
176};
177
178struct color_platform {
179	u8 blue;
180	u8 green;
181	u8 red;
182} __packed;
183
184struct platform_zone {
185	u8 location;
186	struct device_attribute *attr;
187	struct color_platform colors;
188};
189
190struct wmax_brightness_args {
191	u32 led_mask;
192	u32 percentage;
193};
194
195struct wmax_basic_args {
196	u8 arg;
197};
198
199struct legacy_led_args {
200	struct color_platform colors;
201	u8 brightness;
202	u8 state;
203} __packed;
204
205struct wmax_led_args {
206	u32 led_mask;
207	struct color_platform colors;
208	u8 state;
209} __packed;
210
211static struct platform_device *platform_device;
212static struct device_attribute *zone_dev_attrs;
213static struct attribute **zone_attrs;
214static struct platform_zone *zone_data;
215
216static struct platform_driver platform_driver = {
217	.driver = {
218		   .name = "alienware-wmi",
 
219		   }
220};
221
222static struct attribute_group zone_attribute_group = {
223	.name = "rgb_zones",
224};
225
226static u8 interface;
227static u8 lighting_control_state;
228static u8 global_brightness;
229
230/*
231 * Helpers used for zone control
232 */
233static int parse_rgb(const char *buf, struct platform_zone *zone)
234{
235	long unsigned int rgb;
236	int ret;
237	union color_union {
238		struct color_platform cp;
239		int package;
240	} repackager;
241
242	ret = kstrtoul(buf, 16, &rgb);
243	if (ret)
244		return ret;
245
246	/* RGB triplet notation is 24-bit hexadecimal */
247	if (rgb > 0xFFFFFF)
248		return -EINVAL;
249
250	repackager.package = rgb & 0x0f0f0f0f;
251	pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
252		 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
253	zone->colors = repackager.cp;
254	return 0;
255}
256
257static struct platform_zone *match_zone(struct device_attribute *attr)
258{
259	int i;
260	for (i = 0; i < quirks->num_zones; i++) {
261		if ((struct device_attribute *)zone_data[i].attr == attr) {
262			pr_debug("alienware-wmi: matched zone location: %d\n",
263				 zone_data[i].location);
264			return &zone_data[i];
265		}
266	}
267	return NULL;
268}
269
270/*
271 * Individual RGB zone control
272 */
273static int alienware_update_led(struct platform_zone *zone)
274{
275	int method_id;
276	acpi_status status;
277	char *guid;
278	struct acpi_buffer input;
279	struct legacy_led_args legacy_args;
280	struct wmax_led_args wmax_basic_args;
281	if (interface == WMAX) {
282		wmax_basic_args.led_mask = 1 << zone->location;
283		wmax_basic_args.colors = zone->colors;
284		wmax_basic_args.state = lighting_control_state;
285		guid = WMAX_CONTROL_GUID;
286		method_id = WMAX_METHOD_ZONE_CONTROL;
287
288		input.length = (acpi_size) sizeof(wmax_basic_args);
289		input.pointer = &wmax_basic_args;
290	} else {
291		legacy_args.colors = zone->colors;
292		legacy_args.brightness = global_brightness;
293		legacy_args.state = 0;
294		if (lighting_control_state == LEGACY_BOOTING ||
295		    lighting_control_state == LEGACY_SUSPEND) {
296			guid = LEGACY_POWER_CONTROL_GUID;
297			legacy_args.state = lighting_control_state;
298		} else
299			guid = LEGACY_CONTROL_GUID;
300		method_id = zone->location + 1;
301
302		input.length = (acpi_size) sizeof(legacy_args);
303		input.pointer = &legacy_args;
304	}
305	pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
306
307	status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
308	if (ACPI_FAILURE(status))
309		pr_err("alienware-wmi: zone set failure: %u\n", status);
310	return ACPI_FAILURE(status);
311}
312
313static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
314			 char *buf)
315{
316	struct platform_zone *target_zone;
317	target_zone = match_zone(attr);
318	if (target_zone == NULL)
319		return sprintf(buf, "red: -1, green: -1, blue: -1\n");
320	return sprintf(buf, "red: %d, green: %d, blue: %d\n",
321		       target_zone->colors.red,
322		       target_zone->colors.green, target_zone->colors.blue);
323
324}
325
326static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
327			const char *buf, size_t count)
328{
329	struct platform_zone *target_zone;
330	int ret;
331	target_zone = match_zone(attr);
332	if (target_zone == NULL) {
333		pr_err("alienware-wmi: invalid target zone\n");
334		return 1;
335	}
336	ret = parse_rgb(buf, target_zone);
337	if (ret)
338		return ret;
339	ret = alienware_update_led(target_zone);
340	return ret ? ret : count;
341}
342
343/*
344 * LED Brightness (Global)
345 */
346static int wmax_brightness(int brightness)
347{
348	acpi_status status;
349	struct acpi_buffer input;
350	struct wmax_brightness_args args = {
351		.led_mask = 0xFF,
352		.percentage = brightness,
353	};
354	input.length = (acpi_size) sizeof(args);
355	input.pointer = &args;
356	status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
357				     WMAX_METHOD_BRIGHTNESS, &input, NULL);
358	if (ACPI_FAILURE(status))
359		pr_err("alienware-wmi: brightness set failure: %u\n", status);
360	return ACPI_FAILURE(status);
361}
362
363static void global_led_set(struct led_classdev *led_cdev,
364			   enum led_brightness brightness)
365{
366	int ret;
367	global_brightness = brightness;
368	if (interface == WMAX)
369		ret = wmax_brightness(brightness);
370	else
371		ret = alienware_update_led(&zone_data[0]);
372	if (ret)
373		pr_err("LED brightness update failed\n");
374}
375
376static enum led_brightness global_led_get(struct led_classdev *led_cdev)
377{
378	return global_brightness;
379}
380
381static struct led_classdev global_led = {
382	.brightness_set = global_led_set,
383	.brightness_get = global_led_get,
384	.name = "alienware::global_brightness",
385};
386
387/*
388 * Lighting control state device attribute (Global)
389 */
390static ssize_t show_control_state(struct device *dev,
391				  struct device_attribute *attr, char *buf)
392{
393	if (lighting_control_state == LEGACY_BOOTING)
394		return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
395	else if (lighting_control_state == LEGACY_SUSPEND)
396		return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
397	return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
398}
399
400static ssize_t store_control_state(struct device *dev,
401				   struct device_attribute *attr,
402				   const char *buf, size_t count)
403{
404	long unsigned int val;
405	if (strcmp(buf, "booting\n") == 0)
406		val = LEGACY_BOOTING;
407	else if (strcmp(buf, "suspend\n") == 0)
408		val = LEGACY_SUSPEND;
409	else if (interface == LEGACY)
410		val = LEGACY_RUNNING;
411	else
412		val = WMAX_RUNNING;
413	lighting_control_state = val;
414	pr_debug("alienware-wmi: updated control state to %d\n",
415		 lighting_control_state);
416	return count;
417}
418
419static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
420		   store_control_state);
421
422static int alienware_zone_init(struct platform_device *dev)
423{
424	int i;
425	char buffer[10];
426	char *name;
427
428	if (interface == WMAX) {
 
429		lighting_control_state = WMAX_RUNNING;
430	} else if (interface == LEGACY) {
 
431		lighting_control_state = LEGACY_RUNNING;
432	}
433	global_led.max_brightness = 0x0F;
434	global_brightness = global_led.max_brightness;
435
436	/*
437	 *      - zone_dev_attrs num_zones + 1 is for individual zones and then
438	 *        null terminated
439	 *      - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
440	 *        the lighting control + null terminated
441	 *      - zone_data num_zones is for the distinct zones
442	 */
443	zone_dev_attrs =
444	    kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
445		    GFP_KERNEL);
446	if (!zone_dev_attrs)
447		return -ENOMEM;
448
449	zone_attrs =
450	    kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
451		    GFP_KERNEL);
452	if (!zone_attrs)
453		return -ENOMEM;
454
455	zone_data =
456	    kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
457		    GFP_KERNEL);
458	if (!zone_data)
459		return -ENOMEM;
460
461	for (i = 0; i < quirks->num_zones; i++) {
462		sprintf(buffer, "zone%02X", i);
463		name = kstrdup(buffer, GFP_KERNEL);
464		if (name == NULL)
465			return 1;
466		sysfs_attr_init(&zone_dev_attrs[i].attr);
467		zone_dev_attrs[i].attr.name = name;
468		zone_dev_attrs[i].attr.mode = 0644;
469		zone_dev_attrs[i].show = zone_show;
470		zone_dev_attrs[i].store = zone_set;
471		zone_data[i].location = i;
472		zone_attrs[i] = &zone_dev_attrs[i].attr;
473		zone_data[i].attr = &zone_dev_attrs[i];
474	}
475	zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
476	zone_attribute_group.attrs = zone_attrs;
477
478	led_classdev_register(&dev->dev, &global_led);
479
480	return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
481}
482
483static void alienware_zone_exit(struct platform_device *dev)
484{
485	sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
486	led_classdev_unregister(&global_led);
487	if (zone_dev_attrs) {
488		int i;
489		for (i = 0; i < quirks->num_zones; i++)
490			kfree(zone_dev_attrs[i].attr.name);
491	}
492	kfree(zone_dev_attrs);
493	kfree(zone_data);
494	kfree(zone_attrs);
495}
496
497static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
498					  u32 command, int *out_data)
499{
500	acpi_status status;
501	union acpi_object *obj;
502	struct acpi_buffer input;
503	struct acpi_buffer output;
504
505	input.length = (acpi_size) sizeof(*in_args);
506	input.pointer = in_args;
507	if (out_data != NULL) {
508		output.length = ACPI_ALLOCATE_BUFFER;
509		output.pointer = NULL;
510		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
511					     command, &input, &output);
512	} else
513		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
514					     command, &input, NULL);
515
516	if (ACPI_SUCCESS(status) && out_data != NULL) {
517		obj = (union acpi_object *)output.pointer;
518		if (obj && obj->type == ACPI_TYPE_INTEGER)
519			*out_data = (u32) obj->integer.value;
520	}
521	return status;
522
523}
524
525/*
526 *	The HDMI mux sysfs node indicates the status of the HDMI input mux.
527 *	It can toggle between standard system GPU output and HDMI input.
528 */
529static ssize_t show_hdmi_cable(struct device *dev,
530			       struct device_attribute *attr, char *buf)
531{
532	acpi_status status;
533	u32 out_data;
534	struct wmax_basic_args in_args = {
535		.arg = 0,
536	};
537	status =
538	    alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
539				   (u32 *) &out_data);
540	if (ACPI_SUCCESS(status)) {
541		if (out_data == 0)
542			return scnprintf(buf, PAGE_SIZE,
543					 "[unconnected] connected unknown\n");
544		else if (out_data == 1)
545			return scnprintf(buf, PAGE_SIZE,
546					 "unconnected [connected] unknown\n");
547	}
548	pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
549	return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
550}
551
552static ssize_t show_hdmi_source(struct device *dev,
553				struct device_attribute *attr, char *buf)
554{
555	acpi_status status;
556	u32 out_data;
557	struct wmax_basic_args in_args = {
 
 
 
558		.arg = 0,
559	};
560	status =
561	    alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
562				   (u32 *) &out_data);
 
563
564	if (ACPI_SUCCESS(status)) {
565		if (out_data == 1)
 
 
 
566			return scnprintf(buf, PAGE_SIZE,
567					 "[input] gpu unknown\n");
568		else if (out_data == 2)
569			return scnprintf(buf, PAGE_SIZE,
570					 "input [gpu] unknown\n");
571	}
572	pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
573	return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
574}
575
576static ssize_t toggle_hdmi_source(struct device *dev,
577				  struct device_attribute *attr,
578				  const char *buf, size_t count)
579{
 
580	acpi_status status;
581	struct wmax_basic_args args;
582	if (strcmp(buf, "gpu\n") == 0)
583		args.arg = 1;
584	else if (strcmp(buf, "input\n") == 0)
585		args.arg = 2;
586	else
587		args.arg = 3;
588	pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
589
590	status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
591
 
592	if (ACPI_FAILURE(status))
593		pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
594		       status);
595	return count;
596}
597
598static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
599static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
600		   toggle_hdmi_source);
601
602static struct attribute *hdmi_attrs[] = {
603	&dev_attr_cable.attr,
604	&dev_attr_source.attr,
605	NULL,
606};
607
608static struct attribute_group hdmi_attribute_group = {
609	.name = "hdmi",
610	.attrs = hdmi_attrs,
611};
612
613static void remove_hdmi(struct platform_device *dev)
614{
615	if (quirks->hdmi_mux > 0)
616		sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
617}
618
619static int create_hdmi(struct platform_device *dev)
620{
621	int ret;
622
623	ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
624	if (ret)
625		remove_hdmi(dev);
626	return ret;
627}
628
629/*
630 * Alienware GFX amplifier support
631 * - Currently supports reading cable status
632 * - Leaving expansion room to possibly support dock/undock events later
633 */
634static ssize_t show_amplifier_status(struct device *dev,
635				     struct device_attribute *attr, char *buf)
636{
637	acpi_status status;
638	u32 out_data;
639	struct wmax_basic_args in_args = {
640		.arg = 0,
641	};
642	status =
643	    alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
644				   (u32 *) &out_data);
645	if (ACPI_SUCCESS(status)) {
646		if (out_data == 0)
647			return scnprintf(buf, PAGE_SIZE,
648					 "[unconnected] connected unknown\n");
649		else if (out_data == 1)
650			return scnprintf(buf, PAGE_SIZE,
651					 "unconnected [connected] unknown\n");
652	}
653	pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
654	return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
655}
656
657static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
658
659static struct attribute *amplifier_attrs[] = {
660	&dev_attr_status.attr,
661	NULL,
662};
663
664static struct attribute_group amplifier_attribute_group = {
665	.name = "amplifier",
666	.attrs = amplifier_attrs,
667};
668
669static void remove_amplifier(struct platform_device *dev)
670{
671	if (quirks->amplifier > 0)
672		sysfs_remove_group(&dev->dev.kobj, &amplifier_attribute_group);
673}
674
675static int create_amplifier(struct platform_device *dev)
676{
677	int ret;
678
679	ret = sysfs_create_group(&dev->dev.kobj, &amplifier_attribute_group);
680	if (ret)
681		remove_amplifier(dev);
682	return ret;
683}
684
685/*
686 * Deep Sleep Control support
687 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
688 */
689static ssize_t show_deepsleep_status(struct device *dev,
690				     struct device_attribute *attr, char *buf)
691{
692	acpi_status status;
693	u32 out_data;
694	struct wmax_basic_args in_args = {
695		.arg = 0,
696	};
697	status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
698					(u32 *) &out_data);
699	if (ACPI_SUCCESS(status)) {
700		if (out_data == 0)
701			return scnprintf(buf, PAGE_SIZE,
702					 "[disabled] s5 s5_s4\n");
703		else if (out_data == 1)
704			return scnprintf(buf, PAGE_SIZE,
705					 "disabled [s5] s5_s4\n");
706		else if (out_data == 2)
707			return scnprintf(buf, PAGE_SIZE,
708					 "disabled s5 [s5_s4]\n");
709	}
710	pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
711	return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
712}
713
714static ssize_t toggle_deepsleep(struct device *dev,
715				struct device_attribute *attr,
716				const char *buf, size_t count)
717{
718	acpi_status status;
719	struct wmax_basic_args args;
720
721	if (strcmp(buf, "disabled\n") == 0)
722		args.arg = 0;
723	else if (strcmp(buf, "s5\n") == 0)
724		args.arg = 1;
725	else
726		args.arg = 2;
727	pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
728
729	status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
730					NULL);
731
732	if (ACPI_FAILURE(status))
733		pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
734			status);
735	return count;
736}
737
738static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
739
740static struct attribute *deepsleep_attrs[] = {
741	&dev_attr_deepsleep.attr,
742	NULL,
743};
744
745static struct attribute_group deepsleep_attribute_group = {
746	.name = "deepsleep",
747	.attrs = deepsleep_attrs,
748};
749
750static void remove_deepsleep(struct platform_device *dev)
751{
752	if (quirks->deepslp > 0)
753		sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
754}
755
756static int create_deepsleep(struct platform_device *dev)
757{
758	int ret;
759
760	ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
761	if (ret)
762		remove_deepsleep(dev);
763	return ret;
764}
765
766static int __init alienware_wmi_init(void)
767{
768	int ret;
769
770	if (wmi_has_guid(LEGACY_CONTROL_GUID))
771		interface = LEGACY;
772	else if (wmi_has_guid(WMAX_CONTROL_GUID))
773		interface = WMAX;
774	else {
775		pr_warn("alienware-wmi: No known WMI GUID found\n");
776		return -ENODEV;
777	}
778
779	dmi_check_system(alienware_quirks);
780	if (quirks == NULL)
781		quirks = &quirk_unknown;
782
783	ret = platform_driver_register(&platform_driver);
784	if (ret)
785		goto fail_platform_driver;
786	platform_device = platform_device_alloc("alienware-wmi", -1);
787	if (!platform_device) {
788		ret = -ENOMEM;
789		goto fail_platform_device1;
790	}
791	ret = platform_device_add(platform_device);
792	if (ret)
793		goto fail_platform_device2;
794
795	if (quirks->hdmi_mux > 0) {
796		ret = create_hdmi(platform_device);
797		if (ret)
798			goto fail_prep_hdmi;
799	}
800
801	if (quirks->amplifier > 0) {
802		ret = create_amplifier(platform_device);
803		if (ret)
804			goto fail_prep_amplifier;
805	}
806
807	if (quirks->deepslp > 0) {
808		ret = create_deepsleep(platform_device);
809		if (ret)
810			goto fail_prep_deepsleep;
811	}
812
813	ret = alienware_zone_init(platform_device);
814	if (ret)
815		goto fail_prep_zones;
816
817	return 0;
818
819fail_prep_zones:
820	alienware_zone_exit(platform_device);
821fail_prep_deepsleep:
822fail_prep_amplifier:
823fail_prep_hdmi:
824	platform_device_del(platform_device);
825fail_platform_device2:
826	platform_device_put(platform_device);
827fail_platform_device1:
828	platform_driver_unregister(&platform_driver);
829fail_platform_driver:
830	return ret;
831}
832
833module_init(alienware_wmi_init);
834
835static void __exit alienware_wmi_exit(void)
836{
837	if (platform_device) {
838		alienware_zone_exit(platform_device);
839		remove_hdmi(platform_device);
840		platform_device_unregister(platform_device);
841		platform_driver_unregister(&platform_driver);
842	}
843}
844
845module_exit(alienware_wmi_exit);
v3.15
  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
 36MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
 37MODULE_DESCRIPTION("Alienware special feature control");
 38MODULE_LICENSE("GPL");
 39MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
 40MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
 41
 42enum INTERFACE_FLAGS {
 43	LEGACY,
 44	WMAX,
 45};
 46
 47enum LEGACY_CONTROL_STATES {
 48	LEGACY_RUNNING = 1,
 49	LEGACY_BOOTING = 0,
 50	LEGACY_SUSPEND = 3,
 51};
 52
 53enum WMAX_CONTROL_STATES {
 54	WMAX_RUNNING = 0xFF,
 55	WMAX_BOOTING = 0,
 56	WMAX_SUSPEND = 3,
 57};
 58
 59struct quirk_entry {
 60	u8 num_zones;
 
 
 
 61};
 62
 63static struct quirk_entry *quirks;
 64
 65static struct quirk_entry quirk_unknown = {
 66	.num_zones = 2,
 
 
 
 67};
 68
 69static struct quirk_entry quirk_x51_family = {
 70	.num_zones = 3,
 
 
 
 71};
 72
 73static int dmi_matched(const struct dmi_system_id *dmi)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 74{
 75	quirks = dmi->driver_data;
 76	return 1;
 77}
 78
 79static struct dmi_system_id alienware_quirks[] = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 80	{
 81	 .callback = dmi_matched,
 82	 .ident = "Alienware X51 R1",
 83	 .matches = {
 84		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
 85		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
 86		     },
 87	 .driver_data = &quirk_x51_family,
 88	 },
 89	{
 90	 .callback = dmi_matched,
 91	 .ident = "Alienware X51 R2",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 92	 .matches = {
 93		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
 94		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
 95		     },
 96	 .driver_data = &quirk_x51_family,
 97	 },
 98	{}
 99};
100
101struct color_platform {
102	u8 blue;
103	u8 green;
104	u8 red;
105} __packed;
106
107struct platform_zone {
108	u8 location;
109	struct device_attribute *attr;
110	struct color_platform colors;
111};
112
113struct wmax_brightness_args {
114	u32 led_mask;
115	u32 percentage;
116};
117
118struct hdmi_args {
119	u8 arg;
120};
121
122struct legacy_led_args {
123	struct color_platform colors;
124	u8 brightness;
125	u8 state;
126} __packed;
127
128struct wmax_led_args {
129	u32 led_mask;
130	struct color_platform colors;
131	u8 state;
132} __packed;
133
134static struct platform_device *platform_device;
135static struct device_attribute *zone_dev_attrs;
136static struct attribute **zone_attrs;
137static struct platform_zone *zone_data;
138
139static struct platform_driver platform_driver = {
140	.driver = {
141		   .name = "alienware-wmi",
142		   .owner = THIS_MODULE,
143		   }
144};
145
146static struct attribute_group zone_attribute_group = {
147	.name = "rgb_zones",
148};
149
150static u8 interface;
151static u8 lighting_control_state;
152static u8 global_brightness;
153
154/*
155 * Helpers used for zone control
156*/
157static int parse_rgb(const char *buf, struct platform_zone *zone)
158{
159	long unsigned int rgb;
160	int ret;
161	union color_union {
162		struct color_platform cp;
163		int package;
164	} repackager;
165
166	ret = kstrtoul(buf, 16, &rgb);
167	if (ret)
168		return ret;
169
170	/* RGB triplet notation is 24-bit hexadecimal */
171	if (rgb > 0xFFFFFF)
172		return -EINVAL;
173
174	repackager.package = rgb & 0x0f0f0f0f;
175	pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
176		 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
177	zone->colors = repackager.cp;
178	return 0;
179}
180
181static struct platform_zone *match_zone(struct device_attribute *attr)
182{
183	int i;
184	for (i = 0; i < quirks->num_zones; i++) {
185		if ((struct device_attribute *)zone_data[i].attr == attr) {
186			pr_debug("alienware-wmi: matched zone location: %d\n",
187				 zone_data[i].location);
188			return &zone_data[i];
189		}
190	}
191	return NULL;
192}
193
194/*
195 * Individual RGB zone control
196*/
197static int alienware_update_led(struct platform_zone *zone)
198{
199	int method_id;
200	acpi_status status;
201	char *guid;
202	struct acpi_buffer input;
203	struct legacy_led_args legacy_args;
204	struct wmax_led_args wmax_args;
205	if (interface == WMAX) {
206		wmax_args.led_mask = 1 << zone->location;
207		wmax_args.colors = zone->colors;
208		wmax_args.state = lighting_control_state;
209		guid = WMAX_CONTROL_GUID;
210		method_id = WMAX_METHOD_ZONE_CONTROL;
211
212		input.length = (acpi_size) sizeof(wmax_args);
213		input.pointer = &wmax_args;
214	} else {
215		legacy_args.colors = zone->colors;
216		legacy_args.brightness = global_brightness;
217		legacy_args.state = 0;
218		if (lighting_control_state == LEGACY_BOOTING ||
219		    lighting_control_state == LEGACY_SUSPEND) {
220			guid = LEGACY_POWER_CONTROL_GUID;
221			legacy_args.state = lighting_control_state;
222		} else
223			guid = LEGACY_CONTROL_GUID;
224		method_id = zone->location + 1;
225
226		input.length = (acpi_size) sizeof(legacy_args);
227		input.pointer = &legacy_args;
228	}
229	pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
230
231	status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
232	if (ACPI_FAILURE(status))
233		pr_err("alienware-wmi: zone set failure: %u\n", status);
234	return ACPI_FAILURE(status);
235}
236
237static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
238			 char *buf)
239{
240	struct platform_zone *target_zone;
241	target_zone = match_zone(attr);
242	if (target_zone == NULL)
243		return sprintf(buf, "red: -1, green: -1, blue: -1\n");
244	return sprintf(buf, "red: %d, green: %d, blue: %d\n",
245		       target_zone->colors.red,
246		       target_zone->colors.green, target_zone->colors.blue);
247
248}
249
250static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
251			const char *buf, size_t count)
252{
253	struct platform_zone *target_zone;
254	int ret;
255	target_zone = match_zone(attr);
256	if (target_zone == NULL) {
257		pr_err("alienware-wmi: invalid target zone\n");
258		return 1;
259	}
260	ret = parse_rgb(buf, target_zone);
261	if (ret)
262		return ret;
263	ret = alienware_update_led(target_zone);
264	return ret ? ret : count;
265}
266
267/*
268 * LED Brightness (Global)
269*/
270static int wmax_brightness(int brightness)
271{
272	acpi_status status;
273	struct acpi_buffer input;
274	struct wmax_brightness_args args = {
275		.led_mask = 0xFF,
276		.percentage = brightness,
277	};
278	input.length = (acpi_size) sizeof(args);
279	input.pointer = &args;
280	status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
281				     WMAX_METHOD_BRIGHTNESS, &input, NULL);
282	if (ACPI_FAILURE(status))
283		pr_err("alienware-wmi: brightness set failure: %u\n", status);
284	return ACPI_FAILURE(status);
285}
286
287static void global_led_set(struct led_classdev *led_cdev,
288			   enum led_brightness brightness)
289{
290	int ret;
291	global_brightness = brightness;
292	if (interface == WMAX)
293		ret = wmax_brightness(brightness);
294	else
295		ret = alienware_update_led(&zone_data[0]);
296	if (ret)
297		pr_err("LED brightness update failed\n");
298}
299
300static enum led_brightness global_led_get(struct led_classdev *led_cdev)
301{
302	return global_brightness;
303}
304
305static struct led_classdev global_led = {
306	.brightness_set = global_led_set,
307	.brightness_get = global_led_get,
308	.name = "alienware::global_brightness",
309};
310
311/*
312 * Lighting control state device attribute (Global)
313*/
314static ssize_t show_control_state(struct device *dev,
315				  struct device_attribute *attr, char *buf)
316{
317	if (lighting_control_state == LEGACY_BOOTING)
318		return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
319	else if (lighting_control_state == LEGACY_SUSPEND)
320		return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
321	return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
322}
323
324static ssize_t store_control_state(struct device *dev,
325				   struct device_attribute *attr,
326				   const char *buf, size_t count)
327{
328	long unsigned int val;
329	if (strcmp(buf, "booting\n") == 0)
330		val = LEGACY_BOOTING;
331	else if (strcmp(buf, "suspend\n") == 0)
332		val = LEGACY_SUSPEND;
333	else if (interface == LEGACY)
334		val = LEGACY_RUNNING;
335	else
336		val = WMAX_RUNNING;
337	lighting_control_state = val;
338	pr_debug("alienware-wmi: updated control state to %d\n",
339		 lighting_control_state);
340	return count;
341}
342
343static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
344		   store_control_state);
345
346static int alienware_zone_init(struct platform_device *dev)
347{
348	int i;
349	char buffer[10];
350	char *name;
351
352	if (interface == WMAX) {
353		global_led.max_brightness = 100;
354		lighting_control_state = WMAX_RUNNING;
355	} else if (interface == LEGACY) {
356		global_led.max_brightness = 0x0F;
357		lighting_control_state = LEGACY_RUNNING;
358	}
 
359	global_brightness = global_led.max_brightness;
360
361	/*
362	 *      - zone_dev_attrs num_zones + 1 is for individual zones and then
363	 *        null terminated
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
367	 */
368	zone_dev_attrs =
369	    kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
370		    GFP_KERNEL);
371	if (!zone_dev_attrs)
372		return -ENOMEM;
373
374	zone_attrs =
375	    kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
376		    GFP_KERNEL);
377	if (!zone_attrs)
378		return -ENOMEM;
379
380	zone_data =
381	    kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
382		    GFP_KERNEL);
383	if (!zone_data)
384		return -ENOMEM;
385
386	for (i = 0; i < quirks->num_zones; i++) {
387		sprintf(buffer, "zone%02X", i);
388		name = kstrdup(buffer, GFP_KERNEL);
389		if (name == NULL)
390			return 1;
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];
399	}
400	zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
401	zone_attribute_group.attrs = zone_attrs;
402
403	led_classdev_register(&dev->dev, &global_led);
404
405	return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
406}
407
408static void alienware_zone_exit(struct platform_device *dev)
409{
410	sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
411	led_classdev_unregister(&global_led);
412	if (zone_dev_attrs) {
413		int i;
414		for (i = 0; i < quirks->num_zones; i++)
415			kfree(zone_dev_attrs[i].attr.name);
416	}
417	kfree(zone_dev_attrs);
418	kfree(zone_data);
419	kfree(zone_attrs);
420}
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422/*
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.
425*/
426static ssize_t show_hdmi(struct device *dev, struct device_attribute *attr,
427			 char *buf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428{
429	acpi_status status;
430	struct acpi_buffer input;
431	union acpi_object *obj;
432	u32 tmp = 0;
433	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
434	struct hdmi_args in_args = {
435		.arg = 0,
436	};
437	input.length = (acpi_size) sizeof(in_args);
438	input.pointer = &in_args;
439	status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
440				     WMAX_METHOD_HDMI_STATUS, &input, &output);
441
442	if (ACPI_SUCCESS(status)) {
443		obj = (union acpi_object *)output.pointer;
444		if (obj && obj->type == ACPI_TYPE_INTEGER)
445			tmp = (u32) obj->integer.value;
446		if (tmp == 1)
447			return scnprintf(buf, PAGE_SIZE,
448					 "[input] gpu unknown\n");
449		else if (tmp == 2)
450			return scnprintf(buf, PAGE_SIZE,
451					 "input [gpu] unknown\n");
452	}
453	pr_err("alienware-wmi: unknown HDMI status: %d\n", status);
454	return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
455}
456
457static ssize_t toggle_hdmi(struct device *dev, struct device_attribute *attr,
458			   const char *buf, size_t count)
 
459{
460	struct acpi_buffer input;
461	acpi_status status;
462	struct hdmi_args args;
463	if (strcmp(buf, "gpu\n") == 0)
464		args.arg = 1;
465	else if (strcmp(buf, "input\n") == 0)
466		args.arg = 2;
467	else
468		args.arg = 3;
469	pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
470	input.length = (acpi_size) sizeof(args);
471	input.pointer = &args;
472	status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
473				     WMAX_METHOD_HDMI_SOURCE, &input, NULL);
474	if (ACPI_FAILURE(status))
475		pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
476		       status);
477	return count;
478}
479
480static DEVICE_ATTR(hdmi, S_IRUGO | S_IWUSR, show_hdmi, toggle_hdmi);
 
 
 
 
 
 
 
 
481
482static void remove_hdmi(struct platform_device *device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483{
484	device_remove_file(&device->dev, &dev_attr_hdmi);
 
485}
486
487static int create_hdmi(void)
488{
489	int ret = -ENOMEM;
490	ret = device_create_file(&platform_device->dev, &dev_attr_hdmi);
 
491	if (ret)
492		goto error_create_hdmi;
493	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
495error_create_hdmi:
496	remove_hdmi(platform_device);
 
497	return ret;
498}
499
500static int __init alienware_wmi_init(void)
501{
502	int ret;
503
504	if (wmi_has_guid(LEGACY_CONTROL_GUID))
505		interface = LEGACY;
506	else if (wmi_has_guid(WMAX_CONTROL_GUID))
507		interface = WMAX;
508	else {
509		pr_warn("alienware-wmi: No known WMI GUID found\n");
510		return -ENODEV;
511	}
512
513	dmi_check_system(alienware_quirks);
514	if (quirks == NULL)
515		quirks = &quirk_unknown;
516
517	ret = platform_driver_register(&platform_driver);
518	if (ret)
519		goto fail_platform_driver;
520	platform_device = platform_device_alloc("alienware-wmi", -1);
521	if (!platform_device) {
522		ret = -ENOMEM;
523		goto fail_platform_device1;
524	}
525	ret = platform_device_add(platform_device);
526	if (ret)
527		goto fail_platform_device2;
528
529	if (interface == WMAX) {
530		ret = create_hdmi();
531		if (ret)
532			goto fail_prep_hdmi;
533	}
534
 
 
 
 
 
 
 
 
 
 
 
 
535	ret = alienware_zone_init(platform_device);
536	if (ret)
537		goto fail_prep_zones;
538
539	return 0;
540
541fail_prep_zones:
542	alienware_zone_exit(platform_device);
 
 
543fail_prep_hdmi:
544	platform_device_del(platform_device);
545fail_platform_device2:
546	platform_device_put(platform_device);
547fail_platform_device1:
548	platform_driver_unregister(&platform_driver);
549fail_platform_driver:
550	return ret;
551}
552
553module_init(alienware_wmi_init);
554
555static void __exit alienware_wmi_exit(void)
556{
557	if (platform_device) {
558		alienware_zone_exit(platform_device);
559		remove_hdmi(platform_device);
560		platform_device_unregister(platform_device);
561		platform_driver_unregister(&platform_driver);
562	}
563}
564
565module_exit(alienware_wmi_exit);