alienware-wmi.c 16 KB

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