alienware-wmi.c 21 KB

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