dell-led.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * dell_led.c - Dell LED Driver
  3. *
  4. * Copyright (C) 2010 Dell Inc.
  5. * Louis Davis <louis_davis@dell.com>
  6. * Jim Dailey <jim_dailey@dell.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/acpi.h>
  14. #include <linux/leds.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/dmi.h>
  18. #include <linux/dell-led.h>
  19. #include "../platform/x86/dell-smbios.h"
  20. MODULE_AUTHOR("Louis Davis/Jim Dailey");
  21. MODULE_DESCRIPTION("Dell LED Control Driver");
  22. MODULE_LICENSE("GPL");
  23. #define DELL_LED_BIOS_GUID "F6E4FE6E-909D-47cb-8BAB-C9F6F2F8D396"
  24. #define DELL_APP_GUID "A80593CE-A997-11DA-B012-B622A1EF5492"
  25. MODULE_ALIAS("wmi:" DELL_LED_BIOS_GUID);
  26. /* Error Result Codes: */
  27. #define INVALID_DEVICE_ID 250
  28. #define INVALID_PARAMETER 251
  29. #define INVALID_BUFFER 252
  30. #define INTERFACE_ERROR 253
  31. #define UNSUPPORTED_COMMAND 254
  32. #define UNSPECIFIED_ERROR 255
  33. /* Device ID */
  34. #define DEVICE_ID_PANEL_BACK 1
  35. /* LED Commands */
  36. #define CMD_LED_ON 16
  37. #define CMD_LED_OFF 17
  38. #define CMD_LED_BLINK 18
  39. #define GLOBAL_MIC_MUTE_ENABLE 0x364
  40. #define GLOBAL_MIC_MUTE_DISABLE 0x365
  41. static int dell_micmute_led_set(int state)
  42. {
  43. struct calling_interface_buffer *buffer;
  44. struct calling_interface_token *token;
  45. if (!wmi_has_guid(DELL_APP_GUID))
  46. return -ENODEV;
  47. if (state == 0)
  48. token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
  49. else if (state == 1)
  50. token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
  51. else
  52. return -EINVAL;
  53. if (!token)
  54. return -ENODEV;
  55. buffer = dell_smbios_get_buffer();
  56. buffer->input[0] = token->location;
  57. buffer->input[1] = token->value;
  58. dell_smbios_send_request(1, 0);
  59. dell_smbios_release_buffer();
  60. return state;
  61. }
  62. int dell_app_wmi_led_set(int whichled, int on)
  63. {
  64. int state = 0;
  65. switch (whichled) {
  66. case DELL_LED_MICMUTE:
  67. state = dell_micmute_led_set(on);
  68. break;
  69. default:
  70. pr_warn("led type %x is not supported\n", whichled);
  71. break;
  72. }
  73. return state;
  74. }
  75. EXPORT_SYMBOL_GPL(dell_app_wmi_led_set);
  76. struct bios_args {
  77. unsigned char length;
  78. unsigned char result_code;
  79. unsigned char device_id;
  80. unsigned char command;
  81. unsigned char on_time;
  82. unsigned char off_time;
  83. };
  84. static int dell_led_perform_fn(u8 length,
  85. u8 result_code,
  86. u8 device_id,
  87. u8 command,
  88. u8 on_time,
  89. u8 off_time)
  90. {
  91. struct bios_args *bios_return;
  92. u8 return_code;
  93. union acpi_object *obj;
  94. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  95. struct acpi_buffer input;
  96. acpi_status status;
  97. struct bios_args args;
  98. args.length = length;
  99. args.result_code = result_code;
  100. args.device_id = device_id;
  101. args.command = command;
  102. args.on_time = on_time;
  103. args.off_time = off_time;
  104. input.length = sizeof(struct bios_args);
  105. input.pointer = &args;
  106. status = wmi_evaluate_method(DELL_LED_BIOS_GUID,
  107. 1,
  108. 1,
  109. &input,
  110. &output);
  111. if (ACPI_FAILURE(status))
  112. return status;
  113. obj = output.pointer;
  114. if (!obj)
  115. return -EINVAL;
  116. else if (obj->type != ACPI_TYPE_BUFFER) {
  117. kfree(obj);
  118. return -EINVAL;
  119. }
  120. bios_return = ((struct bios_args *)obj->buffer.pointer);
  121. return_code = bios_return->result_code;
  122. kfree(obj);
  123. return return_code;
  124. }
  125. static int led_on(void)
  126. {
  127. return dell_led_perform_fn(3, /* Length of command */
  128. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  129. DEVICE_ID_PANEL_BACK, /* Device ID */
  130. CMD_LED_ON, /* Command */
  131. 0, /* not used */
  132. 0); /* not used */
  133. }
  134. static int led_off(void)
  135. {
  136. return dell_led_perform_fn(3, /* Length of command */
  137. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  138. DEVICE_ID_PANEL_BACK, /* Device ID */
  139. CMD_LED_OFF, /* Command */
  140. 0, /* not used */
  141. 0); /* not used */
  142. }
  143. static int led_blink(unsigned char on_eighths,
  144. unsigned char off_eighths)
  145. {
  146. return dell_led_perform_fn(5, /* Length of command */
  147. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  148. DEVICE_ID_PANEL_BACK, /* Device ID */
  149. CMD_LED_BLINK, /* Command */
  150. on_eighths, /* blink on in eigths of a second */
  151. off_eighths); /* blink off in eights of a second */
  152. }
  153. static void dell_led_set(struct led_classdev *led_cdev,
  154. enum led_brightness value)
  155. {
  156. if (value == LED_OFF)
  157. led_off();
  158. else
  159. led_on();
  160. }
  161. static int dell_led_blink(struct led_classdev *led_cdev,
  162. unsigned long *delay_on,
  163. unsigned long *delay_off)
  164. {
  165. unsigned long on_eighths;
  166. unsigned long off_eighths;
  167. /* The Dell LED delay is based on 125ms intervals.
  168. Need to round up to next interval. */
  169. on_eighths = (*delay_on + 124) / 125;
  170. if (0 == on_eighths)
  171. on_eighths = 1;
  172. if (on_eighths > 255)
  173. on_eighths = 255;
  174. *delay_on = on_eighths * 125;
  175. off_eighths = (*delay_off + 124) / 125;
  176. if (0 == off_eighths)
  177. off_eighths = 1;
  178. if (off_eighths > 255)
  179. off_eighths = 255;
  180. *delay_off = off_eighths * 125;
  181. led_blink(on_eighths, off_eighths);
  182. return 0;
  183. }
  184. static struct led_classdev dell_led = {
  185. .name = "dell::lid",
  186. .brightness = LED_OFF,
  187. .max_brightness = 1,
  188. .brightness_set = dell_led_set,
  189. .blink_set = dell_led_blink,
  190. .flags = LED_CORE_SUSPENDRESUME,
  191. };
  192. static int __init dell_led_init(void)
  193. {
  194. int error = 0;
  195. if (!wmi_has_guid(DELL_LED_BIOS_GUID) && !wmi_has_guid(DELL_APP_GUID))
  196. return -ENODEV;
  197. if (wmi_has_guid(DELL_LED_BIOS_GUID)) {
  198. error = led_off();
  199. if (error != 0)
  200. return -ENODEV;
  201. error = led_classdev_register(NULL, &dell_led);
  202. }
  203. return error;
  204. }
  205. static void __exit dell_led_exit(void)
  206. {
  207. int error = 0;
  208. if (wmi_has_guid(DELL_LED_BIOS_GUID)) {
  209. error = led_off();
  210. if (error == 0)
  211. led_classdev_unregister(&dell_led);
  212. }
  213. }
  214. module_init(dell_led_init);
  215. module_exit(dell_led_exit);