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