alienware-wmi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. .owner = THIS_MODULE,
  138. }
  139. };
  140. static struct attribute_group zone_attribute_group = {
  141. .name = "rgb_zones",
  142. };
  143. static u8 interface;
  144. static u8 lighting_control_state;
  145. static u8 global_brightness;
  146. /*
  147. * Helpers used for zone control
  148. */
  149. static int parse_rgb(const char *buf, struct platform_zone *zone)
  150. {
  151. long unsigned int rgb;
  152. int ret;
  153. union color_union {
  154. struct color_platform cp;
  155. int package;
  156. } repackager;
  157. ret = kstrtoul(buf, 16, &rgb);
  158. if (ret)
  159. return ret;
  160. /* RGB triplet notation is 24-bit hexadecimal */
  161. if (rgb > 0xFFFFFF)
  162. return -EINVAL;
  163. repackager.package = rgb & 0x0f0f0f0f;
  164. pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
  165. repackager.cp.red, repackager.cp.green, repackager.cp.blue);
  166. zone->colors = repackager.cp;
  167. return 0;
  168. }
  169. static struct platform_zone *match_zone(struct device_attribute *attr)
  170. {
  171. int i;
  172. for (i = 0; i < quirks->num_zones; i++) {
  173. if ((struct device_attribute *)zone_data[i].attr == attr) {
  174. pr_debug("alienware-wmi: matched zone location: %d\n",
  175. zone_data[i].location);
  176. return &zone_data[i];
  177. }
  178. }
  179. return NULL;
  180. }
  181. /*
  182. * Individual RGB zone control
  183. */
  184. static int alienware_update_led(struct platform_zone *zone)
  185. {
  186. int method_id;
  187. acpi_status status;
  188. char *guid;
  189. struct acpi_buffer input;
  190. struct legacy_led_args legacy_args;
  191. struct wmax_led_args wmax_args;
  192. if (interface == WMAX) {
  193. wmax_args.led_mask = 1 << zone->location;
  194. wmax_args.colors = zone->colors;
  195. wmax_args.state = lighting_control_state;
  196. guid = WMAX_CONTROL_GUID;
  197. method_id = WMAX_METHOD_ZONE_CONTROL;
  198. input.length = (acpi_size) sizeof(wmax_args);
  199. input.pointer = &wmax_args;
  200. } else {
  201. legacy_args.colors = zone->colors;
  202. legacy_args.brightness = global_brightness;
  203. legacy_args.state = 0;
  204. if (lighting_control_state == LEGACY_BOOTING ||
  205. lighting_control_state == LEGACY_SUSPEND) {
  206. guid = LEGACY_POWER_CONTROL_GUID;
  207. legacy_args.state = lighting_control_state;
  208. } else
  209. guid = LEGACY_CONTROL_GUID;
  210. method_id = zone->location + 1;
  211. input.length = (acpi_size) sizeof(legacy_args);
  212. input.pointer = &legacy_args;
  213. }
  214. pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
  215. status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
  216. if (ACPI_FAILURE(status))
  217. pr_err("alienware-wmi: zone set failure: %u\n", status);
  218. return ACPI_FAILURE(status);
  219. }
  220. static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct platform_zone *target_zone;
  224. target_zone = match_zone(attr);
  225. if (target_zone == NULL)
  226. return sprintf(buf, "red: -1, green: -1, blue: -1\n");
  227. return sprintf(buf, "red: %d, green: %d, blue: %d\n",
  228. target_zone->colors.red,
  229. target_zone->colors.green, target_zone->colors.blue);
  230. }
  231. static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
  232. const char *buf, size_t count)
  233. {
  234. struct platform_zone *target_zone;
  235. int ret;
  236. target_zone = match_zone(attr);
  237. if (target_zone == NULL) {
  238. pr_err("alienware-wmi: invalid target zone\n");
  239. return 1;
  240. }
  241. ret = parse_rgb(buf, target_zone);
  242. if (ret)
  243. return ret;
  244. ret = alienware_update_led(target_zone);
  245. return ret ? ret : count;
  246. }
  247. /*
  248. * LED Brightness (Global)
  249. */
  250. static int wmax_brightness(int brightness)
  251. {
  252. acpi_status status;
  253. struct acpi_buffer input;
  254. struct wmax_brightness_args args = {
  255. .led_mask = 0xFF,
  256. .percentage = brightness,
  257. };
  258. input.length = (acpi_size) sizeof(args);
  259. input.pointer = &args;
  260. status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
  261. WMAX_METHOD_BRIGHTNESS, &input, NULL);
  262. if (ACPI_FAILURE(status))
  263. pr_err("alienware-wmi: brightness set failure: %u\n", status);
  264. return ACPI_FAILURE(status);
  265. }
  266. static void global_led_set(struct led_classdev *led_cdev,
  267. enum led_brightness brightness)
  268. {
  269. int ret;
  270. global_brightness = brightness;
  271. if (interface == WMAX)
  272. ret = wmax_brightness(brightness);
  273. else
  274. ret = alienware_update_led(&zone_data[0]);
  275. if (ret)
  276. pr_err("LED brightness update failed\n");
  277. }
  278. static enum led_brightness global_led_get(struct led_classdev *led_cdev)
  279. {
  280. return global_brightness;
  281. }
  282. static struct led_classdev global_led = {
  283. .brightness_set = global_led_set,
  284. .brightness_get = global_led_get,
  285. .name = "alienware::global_brightness",
  286. };
  287. /*
  288. * Lighting control state device attribute (Global)
  289. */
  290. static ssize_t show_control_state(struct device *dev,
  291. struct device_attribute *attr, char *buf)
  292. {
  293. if (lighting_control_state == LEGACY_BOOTING)
  294. return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
  295. else if (lighting_control_state == LEGACY_SUSPEND)
  296. return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
  297. return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
  298. }
  299. static ssize_t store_control_state(struct device *dev,
  300. struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. long unsigned int val;
  304. if (strcmp(buf, "booting\n") == 0)
  305. val = LEGACY_BOOTING;
  306. else if (strcmp(buf, "suspend\n") == 0)
  307. val = LEGACY_SUSPEND;
  308. else if (interface == LEGACY)
  309. val = LEGACY_RUNNING;
  310. else
  311. val = WMAX_RUNNING;
  312. lighting_control_state = val;
  313. pr_debug("alienware-wmi: updated control state to %d\n",
  314. lighting_control_state);
  315. return count;
  316. }
  317. static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
  318. store_control_state);
  319. static int alienware_zone_init(struct platform_device *dev)
  320. {
  321. int i;
  322. char buffer[10];
  323. char *name;
  324. if (interface == WMAX) {
  325. lighting_control_state = WMAX_RUNNING;
  326. } else if (interface == LEGACY) {
  327. lighting_control_state = LEGACY_RUNNING;
  328. }
  329. global_led.max_brightness = 0x0F;
  330. global_brightness = global_led.max_brightness;
  331. /*
  332. * - zone_dev_attrs num_zones + 1 is for individual zones and then
  333. * null terminated
  334. * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
  335. * the lighting control + null terminated
  336. * - zone_data num_zones is for the distinct zones
  337. */
  338. zone_dev_attrs =
  339. kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
  340. GFP_KERNEL);
  341. if (!zone_dev_attrs)
  342. return -ENOMEM;
  343. zone_attrs =
  344. kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
  345. GFP_KERNEL);
  346. if (!zone_attrs)
  347. return -ENOMEM;
  348. zone_data =
  349. kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
  350. GFP_KERNEL);
  351. if (!zone_data)
  352. return -ENOMEM;
  353. for (i = 0; i < quirks->num_zones; i++) {
  354. sprintf(buffer, "zone%02X", i);
  355. name = kstrdup(buffer, GFP_KERNEL);
  356. if (name == NULL)
  357. return 1;
  358. sysfs_attr_init(&zone_dev_attrs[i].attr);
  359. zone_dev_attrs[i].attr.name = name;
  360. zone_dev_attrs[i].attr.mode = 0644;
  361. zone_dev_attrs[i].show = zone_show;
  362. zone_dev_attrs[i].store = zone_set;
  363. zone_data[i].location = i;
  364. zone_attrs[i] = &zone_dev_attrs[i].attr;
  365. zone_data[i].attr = &zone_dev_attrs[i];
  366. }
  367. zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
  368. zone_attribute_group.attrs = zone_attrs;
  369. led_classdev_register(&dev->dev, &global_led);
  370. return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
  371. }
  372. static void alienware_zone_exit(struct platform_device *dev)
  373. {
  374. sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
  375. led_classdev_unregister(&global_led);
  376. if (zone_dev_attrs) {
  377. int i;
  378. for (i = 0; i < quirks->num_zones; i++)
  379. kfree(zone_dev_attrs[i].attr.name);
  380. }
  381. kfree(zone_dev_attrs);
  382. kfree(zone_data);
  383. kfree(zone_attrs);
  384. }
  385. /*
  386. The HDMI mux sysfs node indicates the status of the HDMI input mux.
  387. It can toggle between standard system GPU output and HDMI input.
  388. */
  389. static acpi_status alienware_hdmi_command(struct hdmi_args *in_args,
  390. u32 command, int *out_data)
  391. {
  392. acpi_status status;
  393. union acpi_object *obj;
  394. struct acpi_buffer input;
  395. struct acpi_buffer output;
  396. input.length = (acpi_size) sizeof(*in_args);
  397. input.pointer = in_args;
  398. if (out_data != NULL) {
  399. output.length = ACPI_ALLOCATE_BUFFER;
  400. output.pointer = NULL;
  401. status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
  402. command, &input, &output);
  403. } else
  404. status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
  405. command, &input, NULL);
  406. if (ACPI_SUCCESS(status) && out_data != NULL) {
  407. obj = (union acpi_object *)output.pointer;
  408. if (obj && obj->type == ACPI_TYPE_INTEGER)
  409. *out_data = (u32) obj->integer.value;
  410. }
  411. return status;
  412. }
  413. static ssize_t show_hdmi_cable(struct device *dev,
  414. struct device_attribute *attr, char *buf)
  415. {
  416. acpi_status status;
  417. u32 out_data;
  418. struct hdmi_args in_args = {
  419. .arg = 0,
  420. };
  421. status =
  422. alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_CABLE,
  423. (u32 *) &out_data);
  424. if (ACPI_SUCCESS(status)) {
  425. if (out_data == 0)
  426. return scnprintf(buf, PAGE_SIZE,
  427. "[unconnected] connected unknown\n");
  428. else if (out_data == 1)
  429. return scnprintf(buf, PAGE_SIZE,
  430. "unconnected [connected] unknown\n");
  431. }
  432. pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
  433. return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
  434. }
  435. static ssize_t show_hdmi_source(struct device *dev,
  436. struct device_attribute *attr, char *buf)
  437. {
  438. acpi_status status;
  439. u32 out_data;
  440. struct hdmi_args in_args = {
  441. .arg = 0,
  442. };
  443. status =
  444. alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_STATUS,
  445. (u32 *) &out_data);
  446. if (ACPI_SUCCESS(status)) {
  447. if (out_data == 1)
  448. return scnprintf(buf, PAGE_SIZE,
  449. "[input] gpu unknown\n");
  450. else if (out_data == 2)
  451. return scnprintf(buf, PAGE_SIZE,
  452. "input [gpu] unknown\n");
  453. }
  454. pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
  455. return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
  456. }
  457. static ssize_t toggle_hdmi_source(struct device *dev,
  458. struct device_attribute *attr,
  459. const char *buf, size_t count)
  460. {
  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. status = alienware_hdmi_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
  471. if (ACPI_FAILURE(status))
  472. pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
  473. status);
  474. return count;
  475. }
  476. static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
  477. static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
  478. toggle_hdmi_source);
  479. static struct attribute *hdmi_attrs[] = {
  480. &dev_attr_cable.attr,
  481. &dev_attr_source.attr,
  482. NULL,
  483. };
  484. static struct attribute_group hdmi_attribute_group = {
  485. .name = "hdmi",
  486. .attrs = hdmi_attrs,
  487. };
  488. static void remove_hdmi(struct platform_device *dev)
  489. {
  490. if (quirks->hdmi_mux > 0)
  491. sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
  492. }
  493. static int create_hdmi(struct platform_device *dev)
  494. {
  495. int ret;
  496. ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
  497. if (ret)
  498. goto error_create_hdmi;
  499. return 0;
  500. error_create_hdmi:
  501. remove_hdmi(dev);
  502. return ret;
  503. }
  504. static int __init alienware_wmi_init(void)
  505. {
  506. int ret;
  507. if (wmi_has_guid(LEGACY_CONTROL_GUID))
  508. interface = LEGACY;
  509. else if (wmi_has_guid(WMAX_CONTROL_GUID))
  510. interface = WMAX;
  511. else {
  512. pr_warn("alienware-wmi: No known WMI GUID found\n");
  513. return -ENODEV;
  514. }
  515. dmi_check_system(alienware_quirks);
  516. if (quirks == NULL)
  517. quirks = &quirk_unknown;
  518. ret = platform_driver_register(&platform_driver);
  519. if (ret)
  520. goto fail_platform_driver;
  521. platform_device = platform_device_alloc("alienware-wmi", -1);
  522. if (!platform_device) {
  523. ret = -ENOMEM;
  524. goto fail_platform_device1;
  525. }
  526. ret = platform_device_add(platform_device);
  527. if (ret)
  528. goto fail_platform_device2;
  529. if (quirks->hdmi_mux > 0) {
  530. ret = create_hdmi(platform_device);
  531. if (ret)
  532. goto fail_prep_hdmi;
  533. }
  534. ret = alienware_zone_init(platform_device);
  535. if (ret)
  536. goto fail_prep_zones;
  537. return 0;
  538. fail_prep_zones:
  539. alienware_zone_exit(platform_device);
  540. fail_prep_hdmi:
  541. platform_device_del(platform_device);
  542. fail_platform_device2:
  543. platform_device_put(platform_device);
  544. fail_platform_device1:
  545. platform_driver_unregister(&platform_driver);
  546. fail_platform_driver:
  547. return ret;
  548. }
  549. module_init(alienware_wmi_init);
  550. static void __exit alienware_wmi_exit(void)
  551. {
  552. if (platform_device) {
  553. alienware_zone_exit(platform_device);
  554. remove_hdmi(platform_device);
  555. platform_device_unregister(platform_device);
  556. platform_driver_unregister(&platform_driver);
  557. }
  558. }
  559. module_exit(alienware_wmi_exit);