• 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/leds.h>
25 
26 #define LEGACY_CONTROL_GUID		"A90597CE-A997-11DA-B012-B622A1EF5492"
27 #define LEGACY_POWER_CONTROL_GUID	"A80593CE-A997-11DA-B012-B622A1EF5492"
28 #define WMAX_CONTROL_GUID		"A70591CE-A997-11DA-B012-B622A1EF5492"
29 
30 #define WMAX_METHOD_HDMI_SOURCE		0x1
31 #define WMAX_METHOD_HDMI_STATUS		0x2
32 #define WMAX_METHOD_BRIGHTNESS		0x3
33 #define WMAX_METHOD_ZONE_CONTROL	0x4
34 #define WMAX_METHOD_HDMI_CABLE		0x5
35 #define WMAX_METHOD_AMPLIFIER_CABLE	0x6
36 #define WMAX_METHOD_DEEP_SLEEP_CONTROL	0x0B
37 #define WMAX_METHOD_DEEP_SLEEP_STATUS	0x0C
38 
39 MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
40 MODULE_DESCRIPTION("Alienware special feature control");
41 MODULE_LICENSE("GPL");
42 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
43 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
44 
45 enum INTERFACE_FLAGS {
46 	LEGACY,
47 	WMAX,
48 };
49 
50 enum LEGACY_CONTROL_STATES {
51 	LEGACY_RUNNING = 1,
52 	LEGACY_BOOTING = 0,
53 	LEGACY_SUSPEND = 3,
54 };
55 
56 enum WMAX_CONTROL_STATES {
57 	WMAX_RUNNING = 0xFF,
58 	WMAX_BOOTING = 0,
59 	WMAX_SUSPEND = 3,
60 };
61 
62 struct quirk_entry {
63 	u8 num_zones;
64 	u8 hdmi_mux;
65 	u8 amplifier;
66 	u8 deepslp;
67 };
68 
69 static struct quirk_entry *quirks;
70 
71 static struct quirk_entry quirk_unknown = {
72 	.num_zones = 2,
73 	.hdmi_mux = 0,
74 	.amplifier = 0,
75 	.deepslp = 0,
76 };
77 
78 static struct quirk_entry quirk_x51_r1_r2 = {
79 	.num_zones = 3,
80 	.hdmi_mux = 0,
81 	.amplifier = 0,
82 	.deepslp = 0,
83 };
84 
85 static struct quirk_entry quirk_x51_r3 = {
86 	.num_zones = 4,
87 	.hdmi_mux = 0,
88 	.amplifier = 1,
89 	.deepslp = 0,
90 };
91 
92 static struct quirk_entry quirk_asm100 = {
93 	.num_zones = 2,
94 	.hdmi_mux = 1,
95 	.amplifier = 0,
96 	.deepslp = 0,
97 };
98 
99 static struct quirk_entry quirk_asm200 = {
100 	.num_zones = 2,
101 	.hdmi_mux = 1,
102 	.amplifier = 0,
103 	.deepslp = 1,
104 };
105 
106 static struct quirk_entry quirk_asm201 = {
107 	.num_zones = 2,
108 	.hdmi_mux = 1,
109 	.amplifier = 1,
110 	.deepslp = 1,
111 };
112 
dmi_matched(const struct dmi_system_id * dmi)113 static int __init dmi_matched(const struct dmi_system_id *dmi)
114 {
115 	quirks = dmi->driver_data;
116 	return 1;
117 }
118 
119 static const struct dmi_system_id alienware_quirks[] __initconst = {
120 	{
121 	 .callback = dmi_matched,
122 	 .ident = "Alienware X51 R3",
123 	 .matches = {
124 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
125 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
126 		     },
127 	 .driver_data = &quirk_x51_r3,
128 	 },
129 	{
130 	 .callback = dmi_matched,
131 	 .ident = "Alienware X51 R2",
132 	 .matches = {
133 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
134 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
135 		     },
136 	 .driver_data = &quirk_x51_r1_r2,
137 	 },
138 	{
139 	 .callback = dmi_matched,
140 	 .ident = "Alienware X51 R1",
141 	 .matches = {
142 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
143 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
144 		     },
145 	 .driver_data = &quirk_x51_r1_r2,
146 	 },
147 	{
148 	 .callback = dmi_matched,
149 	 .ident = "Alienware ASM100",
150 	 .matches = {
151 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
152 		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
153 		     },
154 	 .driver_data = &quirk_asm100,
155 	 },
156 	{
157 	 .callback = dmi_matched,
158 	 .ident = "Alienware ASM200",
159 	 .matches = {
160 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
161 		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM200"),
162 		     },
163 	 .driver_data = &quirk_asm200,
164 	 },
165 	{
166 	 .callback = dmi_matched,
167 	 .ident = "Alienware ASM201",
168 	 .matches = {
169 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
170 		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM201"),
171 		     },
172 	 .driver_data = &quirk_asm201,
173 	 },
174 	{}
175 };
176 
177 struct color_platform {
178 	u8 blue;
179 	u8 green;
180 	u8 red;
181 } __packed;
182 
183 struct platform_zone {
184 	u8 location;
185 	struct device_attribute *attr;
186 	struct color_platform colors;
187 };
188 
189 struct wmax_brightness_args {
190 	u32 led_mask;
191 	u32 percentage;
192 };
193 
194 struct wmax_basic_args {
195 	u8 arg;
196 };
197 
198 struct legacy_led_args {
199 	struct color_platform colors;
200 	u8 brightness;
201 	u8 state;
202 } __packed;
203 
204 struct wmax_led_args {
205 	u32 led_mask;
206 	struct color_platform colors;
207 	u8 state;
208 } __packed;
209 
210 static struct platform_device *platform_device;
211 static struct device_attribute *zone_dev_attrs;
212 static struct attribute **zone_attrs;
213 static struct platform_zone *zone_data;
214 
215 static struct platform_driver platform_driver = {
216 	.driver = {
217 		   .name = "alienware-wmi",
218 		   }
219 };
220 
221 static struct attribute_group zone_attribute_group = {
222 	.name = "rgb_zones",
223 };
224 
225 static u8 interface;
226 static u8 lighting_control_state;
227 static u8 global_brightness;
228 
229 /*
230  * Helpers used for zone control
231  */
parse_rgb(const char * buf,struct platform_zone * zone)232 static int parse_rgb(const char *buf, struct platform_zone *zone)
233 {
234 	long unsigned int rgb;
235 	int ret;
236 	union color_union {
237 		struct color_platform cp;
238 		int package;
239 	} repackager;
240 
241 	ret = kstrtoul(buf, 16, &rgb);
242 	if (ret)
243 		return ret;
244 
245 	/* RGB triplet notation is 24-bit hexadecimal */
246 	if (rgb > 0xFFFFFF)
247 		return -EINVAL;
248 
249 	repackager.package = rgb & 0x0f0f0f0f;
250 	pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
251 		 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
252 	zone->colors = repackager.cp;
253 	return 0;
254 }
255 
match_zone(struct device_attribute * attr)256 static struct platform_zone *match_zone(struct device_attribute *attr)
257 {
258 	u8 zone;
259 
260 	for (zone = 0; zone < quirks->num_zones; zone++) {
261 		if ((struct device_attribute *)zone_data[zone].attr == attr) {
262 			pr_debug("alienware-wmi: matched zone location: %d\n",
263 				 zone_data[zone].location);
264 			return &zone_data[zone];
265 		}
266 	}
267 	return NULL;
268 }
269 
270 /*
271  * Individual RGB zone control
272  */
alienware_update_led(struct platform_zone * zone)273 static 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, 0, 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 
zone_show(struct device * dev,struct device_attribute * attr,char * buf)313 static 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 
zone_set(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)326 static 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  */
wmax_brightness(int brightness)346 static 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, 0,
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 
global_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)363 static 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 
global_led_get(struct led_classdev * led_cdev)376 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
377 {
378 	return global_brightness;
379 }
380 
381 static 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  */
show_control_state(struct device * dev,struct device_attribute * attr,char * buf)390 static 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 
store_control_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)400 static 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 
419 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
420 		   store_control_state);
421 
alienware_zone_init(struct platform_device * dev)422 static int alienware_zone_init(struct platform_device *dev)
423 {
424 	u8 zone;
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 (zone = 0; zone < quirks->num_zones; zone++) {
462 		sprintf(buffer, "zone%02hhX", zone);
463 		name = kstrdup(buffer, GFP_KERNEL);
464 		if (name == NULL)
465 			return 1;
466 		sysfs_attr_init(&zone_dev_attrs[zone].attr);
467 		zone_dev_attrs[zone].attr.name = name;
468 		zone_dev_attrs[zone].attr.mode = 0644;
469 		zone_dev_attrs[zone].show = zone_show;
470 		zone_dev_attrs[zone].store = zone_set;
471 		zone_data[zone].location = zone;
472 		zone_attrs[zone] = &zone_dev_attrs[zone].attr;
473 		zone_data[zone].attr = &zone_dev_attrs[zone];
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 
alienware_zone_exit(struct platform_device * dev)483 static void alienware_zone_exit(struct platform_device *dev)
484 {
485 	u8 zone;
486 
487 	sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
488 	led_classdev_unregister(&global_led);
489 	if (zone_dev_attrs) {
490 		for (zone = 0; zone < quirks->num_zones; zone++)
491 			kfree(zone_dev_attrs[zone].attr.name);
492 	}
493 	kfree(zone_dev_attrs);
494 	kfree(zone_data);
495 	kfree(zone_attrs);
496 }
497 
alienware_wmax_command(struct wmax_basic_args * in_args,u32 command,int * out_data)498 static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
499 					  u32 command, int *out_data)
500 {
501 	acpi_status status;
502 	union acpi_object *obj;
503 	struct acpi_buffer input;
504 	struct acpi_buffer output;
505 
506 	input.length = (acpi_size) sizeof(*in_args);
507 	input.pointer = in_args;
508 	if (out_data) {
509 		output.length = ACPI_ALLOCATE_BUFFER;
510 		output.pointer = NULL;
511 		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
512 					     command, &input, &output);
513 		if (ACPI_SUCCESS(status)) {
514 			obj = (union acpi_object *)output.pointer;
515 			if (obj && obj->type == ACPI_TYPE_INTEGER)
516 				*out_data = (u32)obj->integer.value;
517 		}
518 		kfree(output.pointer);
519 	} else {
520 		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
521 					     command, &input, NULL);
522 	}
523 	return status;
524 }
525 
526 /*
527  *	The HDMI mux sysfs node indicates the status of the HDMI input mux.
528  *	It can toggle between standard system GPU output and HDMI input.
529  */
show_hdmi_cable(struct device * dev,struct device_attribute * attr,char * buf)530 static ssize_t show_hdmi_cable(struct device *dev,
531 			       struct device_attribute *attr, char *buf)
532 {
533 	acpi_status status;
534 	u32 out_data;
535 	struct wmax_basic_args in_args = {
536 		.arg = 0,
537 	};
538 	status =
539 	    alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
540 				   (u32 *) &out_data);
541 	if (ACPI_SUCCESS(status)) {
542 		if (out_data == 0)
543 			return scnprintf(buf, PAGE_SIZE,
544 					 "[unconnected] connected unknown\n");
545 		else if (out_data == 1)
546 			return scnprintf(buf, PAGE_SIZE,
547 					 "unconnected [connected] unknown\n");
548 	}
549 	pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
550 	return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
551 }
552 
show_hdmi_source(struct device * dev,struct device_attribute * attr,char * buf)553 static ssize_t show_hdmi_source(struct device *dev,
554 				struct device_attribute *attr, char *buf)
555 {
556 	acpi_status status;
557 	u32 out_data;
558 	struct wmax_basic_args in_args = {
559 		.arg = 0,
560 	};
561 	status =
562 	    alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
563 				   (u32 *) &out_data);
564 
565 	if (ACPI_SUCCESS(status)) {
566 		if (out_data == 1)
567 			return scnprintf(buf, PAGE_SIZE,
568 					 "[input] gpu unknown\n");
569 		else if (out_data == 2)
570 			return scnprintf(buf, PAGE_SIZE,
571 					 "input [gpu] unknown\n");
572 	}
573 	pr_err("alienware-wmi: unknown HDMI source status: %u\n", status);
574 	return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
575 }
576 
toggle_hdmi_source(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)577 static ssize_t toggle_hdmi_source(struct device *dev,
578 				  struct device_attribute *attr,
579 				  const char *buf, size_t count)
580 {
581 	acpi_status status;
582 	struct wmax_basic_args args;
583 	if (strcmp(buf, "gpu\n") == 0)
584 		args.arg = 1;
585 	else if (strcmp(buf, "input\n") == 0)
586 		args.arg = 2;
587 	else
588 		args.arg = 3;
589 	pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
590 
591 	status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
592 
593 	if (ACPI_FAILURE(status))
594 		pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
595 		       status);
596 	return count;
597 }
598 
599 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
600 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
601 		   toggle_hdmi_source);
602 
603 static struct attribute *hdmi_attrs[] = {
604 	&dev_attr_cable.attr,
605 	&dev_attr_source.attr,
606 	NULL,
607 };
608 
609 static const struct attribute_group hdmi_attribute_group = {
610 	.name = "hdmi",
611 	.attrs = hdmi_attrs,
612 };
613 
remove_hdmi(struct platform_device * dev)614 static void remove_hdmi(struct platform_device *dev)
615 {
616 	if (quirks->hdmi_mux > 0)
617 		sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
618 }
619 
create_hdmi(struct platform_device * dev)620 static int create_hdmi(struct platform_device *dev)
621 {
622 	int ret;
623 
624 	ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
625 	if (ret)
626 		remove_hdmi(dev);
627 	return ret;
628 }
629 
630 /*
631  * Alienware GFX amplifier support
632  * - Currently supports reading cable status
633  * - Leaving expansion room to possibly support dock/undock events later
634  */
show_amplifier_status(struct device * dev,struct device_attribute * attr,char * buf)635 static ssize_t show_amplifier_status(struct device *dev,
636 				     struct device_attribute *attr, char *buf)
637 {
638 	acpi_status status;
639 	u32 out_data;
640 	struct wmax_basic_args in_args = {
641 		.arg = 0,
642 	};
643 	status =
644 	    alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
645 				   (u32 *) &out_data);
646 	if (ACPI_SUCCESS(status)) {
647 		if (out_data == 0)
648 			return scnprintf(buf, PAGE_SIZE,
649 					 "[unconnected] connected unknown\n");
650 		else if (out_data == 1)
651 			return scnprintf(buf, PAGE_SIZE,
652 					 "unconnected [connected] unknown\n");
653 	}
654 	pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
655 	return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
656 }
657 
658 static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
659 
660 static struct attribute *amplifier_attrs[] = {
661 	&dev_attr_status.attr,
662 	NULL,
663 };
664 
665 static const struct attribute_group amplifier_attribute_group = {
666 	.name = "amplifier",
667 	.attrs = amplifier_attrs,
668 };
669 
remove_amplifier(struct platform_device * dev)670 static void remove_amplifier(struct platform_device *dev)
671 {
672 	if (quirks->amplifier > 0)
673 		sysfs_remove_group(&dev->dev.kobj, &amplifier_attribute_group);
674 }
675 
create_amplifier(struct platform_device * dev)676 static int create_amplifier(struct platform_device *dev)
677 {
678 	int ret;
679 
680 	ret = sysfs_create_group(&dev->dev.kobj, &amplifier_attribute_group);
681 	if (ret)
682 		remove_amplifier(dev);
683 	return ret;
684 }
685 
686 /*
687  * Deep Sleep Control support
688  * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
689  */
show_deepsleep_status(struct device * dev,struct device_attribute * attr,char * buf)690 static ssize_t show_deepsleep_status(struct device *dev,
691 				     struct device_attribute *attr, char *buf)
692 {
693 	acpi_status status;
694 	u32 out_data;
695 	struct wmax_basic_args in_args = {
696 		.arg = 0,
697 	};
698 	status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
699 					(u32 *) &out_data);
700 	if (ACPI_SUCCESS(status)) {
701 		if (out_data == 0)
702 			return scnprintf(buf, PAGE_SIZE,
703 					 "[disabled] s5 s5_s4\n");
704 		else if (out_data == 1)
705 			return scnprintf(buf, PAGE_SIZE,
706 					 "disabled [s5] s5_s4\n");
707 		else if (out_data == 2)
708 			return scnprintf(buf, PAGE_SIZE,
709 					 "disabled s5 [s5_s4]\n");
710 	}
711 	pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
712 	return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
713 }
714 
toggle_deepsleep(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)715 static ssize_t toggle_deepsleep(struct device *dev,
716 				struct device_attribute *attr,
717 				const char *buf, size_t count)
718 {
719 	acpi_status status;
720 	struct wmax_basic_args args;
721 
722 	if (strcmp(buf, "disabled\n") == 0)
723 		args.arg = 0;
724 	else if (strcmp(buf, "s5\n") == 0)
725 		args.arg = 1;
726 	else
727 		args.arg = 2;
728 	pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
729 
730 	status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
731 					NULL);
732 
733 	if (ACPI_FAILURE(status))
734 		pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
735 			status);
736 	return count;
737 }
738 
739 static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
740 
741 static struct attribute *deepsleep_attrs[] = {
742 	&dev_attr_deepsleep.attr,
743 	NULL,
744 };
745 
746 static const struct attribute_group deepsleep_attribute_group = {
747 	.name = "deepsleep",
748 	.attrs = deepsleep_attrs,
749 };
750 
remove_deepsleep(struct platform_device * dev)751 static void remove_deepsleep(struct platform_device *dev)
752 {
753 	if (quirks->deepslp > 0)
754 		sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
755 }
756 
create_deepsleep(struct platform_device * dev)757 static int create_deepsleep(struct platform_device *dev)
758 {
759 	int ret;
760 
761 	ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
762 	if (ret)
763 		remove_deepsleep(dev);
764 	return ret;
765 }
766 
alienware_wmi_init(void)767 static int __init alienware_wmi_init(void)
768 {
769 	int ret;
770 
771 	if (wmi_has_guid(LEGACY_CONTROL_GUID))
772 		interface = LEGACY;
773 	else if (wmi_has_guid(WMAX_CONTROL_GUID))
774 		interface = WMAX;
775 	else {
776 		pr_warn("alienware-wmi: No known WMI GUID found\n");
777 		return -ENODEV;
778 	}
779 
780 	dmi_check_system(alienware_quirks);
781 	if (quirks == NULL)
782 		quirks = &quirk_unknown;
783 
784 	ret = platform_driver_register(&platform_driver);
785 	if (ret)
786 		goto fail_platform_driver;
787 	platform_device = platform_device_alloc("alienware-wmi", -1);
788 	if (!platform_device) {
789 		ret = -ENOMEM;
790 		goto fail_platform_device1;
791 	}
792 	ret = platform_device_add(platform_device);
793 	if (ret)
794 		goto fail_platform_device2;
795 
796 	if (quirks->hdmi_mux > 0) {
797 		ret = create_hdmi(platform_device);
798 		if (ret)
799 			goto fail_prep_hdmi;
800 	}
801 
802 	if (quirks->amplifier > 0) {
803 		ret = create_amplifier(platform_device);
804 		if (ret)
805 			goto fail_prep_amplifier;
806 	}
807 
808 	if (quirks->deepslp > 0) {
809 		ret = create_deepsleep(platform_device);
810 		if (ret)
811 			goto fail_prep_deepsleep;
812 	}
813 
814 	ret = alienware_zone_init(platform_device);
815 	if (ret)
816 		goto fail_prep_zones;
817 
818 	return 0;
819 
820 fail_prep_zones:
821 	alienware_zone_exit(platform_device);
822 fail_prep_deepsleep:
823 fail_prep_amplifier:
824 fail_prep_hdmi:
825 	platform_device_del(platform_device);
826 fail_platform_device2:
827 	platform_device_put(platform_device);
828 fail_platform_device1:
829 	platform_driver_unregister(&platform_driver);
830 fail_platform_driver:
831 	return ret;
832 }
833 
834 module_init(alienware_wmi_init);
835 
alienware_wmi_exit(void)836 static void __exit alienware_wmi_exit(void)
837 {
838 	if (platform_device) {
839 		alienware_zone_exit(platform_device);
840 		remove_hdmi(platform_device);
841 		platform_device_unregister(platform_device);
842 		platform_driver_unregister(&platform_driver);
843 	}
844 }
845 
846 module_exit(alienware_wmi_exit);
847