dell-led.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. MODULE_AUTHOR("Louis Davis/Jim Dailey");
  20. MODULE_DESCRIPTION("Dell LED Control Driver");
  21. MODULE_LICENSE("GPL");
  22. #define DELL_LED_BIOS_GUID "F6E4FE6E-909D-47cb-8BAB-C9F6F2F8D396"
  23. #define DELL_APP_GUID "A80593CE-A997-11DA-B012-B622A1EF5492"
  24. MODULE_ALIAS("wmi:" DELL_LED_BIOS_GUID);
  25. /* Error Result Codes: */
  26. #define INVALID_DEVICE_ID 250
  27. #define INVALID_PARAMETER 251
  28. #define INVALID_BUFFER 252
  29. #define INTERFACE_ERROR 253
  30. #define UNSUPPORTED_COMMAND 254
  31. #define UNSPECIFIED_ERROR 255
  32. /* Device ID */
  33. #define DEVICE_ID_PANEL_BACK 1
  34. /* LED Commands */
  35. #define CMD_LED_ON 16
  36. #define CMD_LED_OFF 17
  37. #define CMD_LED_BLINK 18
  38. struct app_wmi_args {
  39. u16 class;
  40. u16 selector;
  41. u32 arg1;
  42. u32 arg2;
  43. u32 arg3;
  44. u32 arg4;
  45. u32 res1;
  46. u32 res2;
  47. u32 res3;
  48. u32 res4;
  49. char dummy[92];
  50. };
  51. #define GLOBAL_MIC_MUTE_ENABLE 0x364
  52. #define GLOBAL_MIC_MUTE_DISABLE 0x365
  53. struct dell_bios_data_token {
  54. u16 tokenid;
  55. u16 location;
  56. u16 value;
  57. };
  58. struct __attribute__ ((__packed__)) dell_bios_calling_interface {
  59. struct dmi_header header;
  60. u16 cmd_io_addr;
  61. u8 cmd_io_code;
  62. u32 supported_cmds;
  63. struct dell_bios_data_token damap[];
  64. };
  65. static struct dell_bios_data_token dell_mic_tokens[2];
  66. static int dell_wmi_perform_query(struct app_wmi_args *args)
  67. {
  68. struct app_wmi_args *bios_return;
  69. union acpi_object *obj;
  70. struct acpi_buffer input;
  71. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  72. acpi_status status;
  73. u32 rc = -EINVAL;
  74. input.length = 128;
  75. input.pointer = args;
  76. status = wmi_evaluate_method(DELL_APP_GUID, 0, 1, &input, &output);
  77. if (!ACPI_SUCCESS(status))
  78. goto err_out0;
  79. obj = output.pointer;
  80. if (!obj)
  81. goto err_out0;
  82. if (obj->type != ACPI_TYPE_BUFFER)
  83. goto err_out1;
  84. bios_return = (struct app_wmi_args *)obj->buffer.pointer;
  85. rc = bios_return->res1;
  86. if (rc)
  87. goto err_out1;
  88. memcpy(args, bios_return, sizeof(struct app_wmi_args));
  89. rc = 0;
  90. err_out1:
  91. kfree(obj);
  92. err_out0:
  93. return rc;
  94. }
  95. static void __init find_micmute_tokens(const struct dmi_header *dm, void *dummy)
  96. {
  97. struct dell_bios_calling_interface *calling_interface;
  98. struct dell_bios_data_token *token;
  99. int token_size = sizeof(struct dell_bios_data_token);
  100. int i = 0;
  101. if (dm->type == 0xda && dm->length > 17) {
  102. calling_interface = container_of(dm,
  103. struct dell_bios_calling_interface, header);
  104. token = &calling_interface->damap[i];
  105. while (token->tokenid != 0xffff) {
  106. if (token->tokenid == GLOBAL_MIC_MUTE_DISABLE)
  107. memcpy(&dell_mic_tokens[0], token, token_size);
  108. else if (token->tokenid == GLOBAL_MIC_MUTE_ENABLE)
  109. memcpy(&dell_mic_tokens[1], token, token_size);
  110. i++;
  111. token = &calling_interface->damap[i];
  112. }
  113. }
  114. }
  115. static int dell_micmute_led_set(int state)
  116. {
  117. struct app_wmi_args args;
  118. struct dell_bios_data_token *token;
  119. if (!wmi_has_guid(DELL_APP_GUID))
  120. return -ENODEV;
  121. if (state == 0 || state == 1)
  122. token = &dell_mic_tokens[state];
  123. else
  124. return -EINVAL;
  125. memset(&args, 0, sizeof(struct app_wmi_args));
  126. args.class = 1;
  127. args.arg1 = token->location;
  128. args.arg2 = token->value;
  129. dell_wmi_perform_query(&args);
  130. return state;
  131. }
  132. int dell_app_wmi_led_set(int whichled, int on)
  133. {
  134. int state = 0;
  135. switch (whichled) {
  136. case DELL_LED_MICMUTE:
  137. state = dell_micmute_led_set(on);
  138. break;
  139. default:
  140. pr_warn("led type %x is not supported\n", whichled);
  141. break;
  142. }
  143. return state;
  144. }
  145. EXPORT_SYMBOL_GPL(dell_app_wmi_led_set);
  146. static int __init dell_micmute_led_init(void)
  147. {
  148. memset(dell_mic_tokens, 0, sizeof(struct dell_bios_data_token) * 2);
  149. dmi_walk(find_micmute_tokens, NULL);
  150. return 0;
  151. }
  152. struct bios_args {
  153. unsigned char length;
  154. unsigned char result_code;
  155. unsigned char device_id;
  156. unsigned char command;
  157. unsigned char on_time;
  158. unsigned char off_time;
  159. };
  160. static int dell_led_perform_fn(u8 length,
  161. u8 result_code,
  162. u8 device_id,
  163. u8 command,
  164. u8 on_time,
  165. u8 off_time)
  166. {
  167. struct bios_args *bios_return;
  168. u8 return_code;
  169. union acpi_object *obj;
  170. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  171. struct acpi_buffer input;
  172. acpi_status status;
  173. struct bios_args args;
  174. args.length = length;
  175. args.result_code = result_code;
  176. args.device_id = device_id;
  177. args.command = command;
  178. args.on_time = on_time;
  179. args.off_time = off_time;
  180. input.length = sizeof(struct bios_args);
  181. input.pointer = &args;
  182. status = wmi_evaluate_method(DELL_LED_BIOS_GUID,
  183. 1,
  184. 1,
  185. &input,
  186. &output);
  187. if (ACPI_FAILURE(status))
  188. return status;
  189. obj = output.pointer;
  190. if (!obj)
  191. return -EINVAL;
  192. else if (obj->type != ACPI_TYPE_BUFFER) {
  193. kfree(obj);
  194. return -EINVAL;
  195. }
  196. bios_return = ((struct bios_args *)obj->buffer.pointer);
  197. return_code = bios_return->result_code;
  198. kfree(obj);
  199. return return_code;
  200. }
  201. static int led_on(void)
  202. {
  203. return dell_led_perform_fn(3, /* Length of command */
  204. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  205. DEVICE_ID_PANEL_BACK, /* Device ID */
  206. CMD_LED_ON, /* Command */
  207. 0, /* not used */
  208. 0); /* not used */
  209. }
  210. static int led_off(void)
  211. {
  212. return dell_led_perform_fn(3, /* Length of command */
  213. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  214. DEVICE_ID_PANEL_BACK, /* Device ID */
  215. CMD_LED_OFF, /* Command */
  216. 0, /* not used */
  217. 0); /* not used */
  218. }
  219. static int led_blink(unsigned char on_eighths,
  220. unsigned char off_eighths)
  221. {
  222. return dell_led_perform_fn(5, /* Length of command */
  223. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  224. DEVICE_ID_PANEL_BACK, /* Device ID */
  225. CMD_LED_BLINK, /* Command */
  226. on_eighths, /* blink on in eigths of a second */
  227. off_eighths); /* blink off in eights of a second */
  228. }
  229. static void dell_led_set(struct led_classdev *led_cdev,
  230. enum led_brightness value)
  231. {
  232. if (value == LED_OFF)
  233. led_off();
  234. else
  235. led_on();
  236. }
  237. static int dell_led_blink(struct led_classdev *led_cdev,
  238. unsigned long *delay_on,
  239. unsigned long *delay_off)
  240. {
  241. unsigned long on_eighths;
  242. unsigned long off_eighths;
  243. /* The Dell LED delay is based on 125ms intervals.
  244. Need to round up to next interval. */
  245. on_eighths = (*delay_on + 124) / 125;
  246. if (0 == on_eighths)
  247. on_eighths = 1;
  248. if (on_eighths > 255)
  249. on_eighths = 255;
  250. *delay_on = on_eighths * 125;
  251. off_eighths = (*delay_off + 124) / 125;
  252. if (0 == off_eighths)
  253. off_eighths = 1;
  254. if (off_eighths > 255)
  255. off_eighths = 255;
  256. *delay_off = off_eighths * 125;
  257. led_blink(on_eighths, off_eighths);
  258. return 0;
  259. }
  260. static struct led_classdev dell_led = {
  261. .name = "dell::lid",
  262. .brightness = LED_OFF,
  263. .max_brightness = 1,
  264. .brightness_set = dell_led_set,
  265. .blink_set = dell_led_blink,
  266. .flags = LED_CORE_SUSPENDRESUME,
  267. };
  268. static int __init dell_led_init(void)
  269. {
  270. int error = 0;
  271. if (!wmi_has_guid(DELL_LED_BIOS_GUID) && !wmi_has_guid(DELL_APP_GUID))
  272. return -ENODEV;
  273. if (wmi_has_guid(DELL_APP_GUID))
  274. error = dell_micmute_led_init();
  275. if (wmi_has_guid(DELL_LED_BIOS_GUID)) {
  276. error = led_off();
  277. if (error != 0)
  278. return -ENODEV;
  279. error = led_classdev_register(NULL, &dell_led);
  280. }
  281. return error;
  282. }
  283. static void __exit dell_led_exit(void)
  284. {
  285. int error = 0;
  286. if (wmi_has_guid(DELL_LED_BIOS_GUID)) {
  287. error = led_off();
  288. if (error == 0)
  289. led_classdev_unregister(&dell_led);
  290. }
  291. }
  292. module_init(dell_led_init);
  293. module_exit(dell_led_exit);