dell-led.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. MODULE_AUTHOR("Louis Davis/Jim Dailey");
  18. MODULE_DESCRIPTION("Dell LED Control Driver");
  19. MODULE_LICENSE("GPL");
  20. #define DELL_LED_BIOS_GUID "F6E4FE6E-909D-47cb-8BAB-C9F6F2F8D396"
  21. MODULE_ALIAS("wmi:" DELL_LED_BIOS_GUID);
  22. /* Error Result Codes: */
  23. #define INVALID_DEVICE_ID 250
  24. #define INVALID_PARAMETER 251
  25. #define INVALID_BUFFER 252
  26. #define INTERFACE_ERROR 253
  27. #define UNSUPPORTED_COMMAND 254
  28. #define UNSPECIFIED_ERROR 255
  29. /* Device ID */
  30. #define DEVICE_ID_PANEL_BACK 1
  31. /* LED Commands */
  32. #define CMD_LED_ON 16
  33. #define CMD_LED_OFF 17
  34. #define CMD_LED_BLINK 18
  35. struct bios_args {
  36. unsigned char length;
  37. unsigned char result_code;
  38. unsigned char device_id;
  39. unsigned char command;
  40. unsigned char on_time;
  41. unsigned char off_time;
  42. };
  43. static int dell_led_perform_fn(u8 length,
  44. u8 result_code,
  45. u8 device_id,
  46. u8 command,
  47. u8 on_time,
  48. u8 off_time)
  49. {
  50. struct bios_args *bios_return;
  51. u8 return_code;
  52. union acpi_object *obj;
  53. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  54. struct acpi_buffer input;
  55. acpi_status status;
  56. struct bios_args args;
  57. args.length = length;
  58. args.result_code = result_code;
  59. args.device_id = device_id;
  60. args.command = command;
  61. args.on_time = on_time;
  62. args.off_time = off_time;
  63. input.length = sizeof(struct bios_args);
  64. input.pointer = &args;
  65. status = wmi_evaluate_method(DELL_LED_BIOS_GUID,
  66. 1,
  67. 1,
  68. &input,
  69. &output);
  70. if (ACPI_FAILURE(status))
  71. return status;
  72. obj = output.pointer;
  73. if (!obj)
  74. return -EINVAL;
  75. else if (obj->type != ACPI_TYPE_BUFFER) {
  76. kfree(obj);
  77. return -EINVAL;
  78. }
  79. bios_return = ((struct bios_args *)obj->buffer.pointer);
  80. return_code = bios_return->result_code;
  81. kfree(obj);
  82. return return_code;
  83. }
  84. static int led_on(void)
  85. {
  86. return dell_led_perform_fn(3, /* Length of command */
  87. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  88. DEVICE_ID_PANEL_BACK, /* Device ID */
  89. CMD_LED_ON, /* Command */
  90. 0, /* not used */
  91. 0); /* not used */
  92. }
  93. static int led_off(void)
  94. {
  95. return dell_led_perform_fn(3, /* Length of command */
  96. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  97. DEVICE_ID_PANEL_BACK, /* Device ID */
  98. CMD_LED_OFF, /* Command */
  99. 0, /* not used */
  100. 0); /* not used */
  101. }
  102. static int led_blink(unsigned char on_eighths,
  103. unsigned char off_eighths)
  104. {
  105. return dell_led_perform_fn(5, /* Length of command */
  106. INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
  107. DEVICE_ID_PANEL_BACK, /* Device ID */
  108. CMD_LED_BLINK, /* Command */
  109. on_eighths, /* blink on in eigths of a second */
  110. off_eighths); /* blink off in eights of a second */
  111. }
  112. static void dell_led_set(struct led_classdev *led_cdev,
  113. enum led_brightness value)
  114. {
  115. if (value == LED_OFF)
  116. led_off();
  117. else
  118. led_on();
  119. }
  120. static int dell_led_blink(struct led_classdev *led_cdev,
  121. unsigned long *delay_on,
  122. unsigned long *delay_off)
  123. {
  124. unsigned long on_eighths;
  125. unsigned long off_eighths;
  126. /* The Dell LED delay is based on 125ms intervals.
  127. Need to round up to next interval. */
  128. on_eighths = (*delay_on + 124) / 125;
  129. if (0 == on_eighths)
  130. on_eighths = 1;
  131. if (on_eighths > 255)
  132. on_eighths = 255;
  133. *delay_on = on_eighths * 125;
  134. off_eighths = (*delay_off + 124) / 125;
  135. if (0 == off_eighths)
  136. off_eighths = 1;
  137. if (off_eighths > 255)
  138. off_eighths = 255;
  139. *delay_off = off_eighths * 125;
  140. led_blink(on_eighths, off_eighths);
  141. return 0;
  142. }
  143. static struct led_classdev dell_led = {
  144. .name = "dell::lid",
  145. .brightness = LED_OFF,
  146. .max_brightness = 1,
  147. .brightness_set = dell_led_set,
  148. .blink_set = dell_led_blink,
  149. .flags = LED_CORE_SUSPENDRESUME,
  150. };
  151. static int __init dell_led_init(void)
  152. {
  153. int error = 0;
  154. if (!wmi_has_guid(DELL_LED_BIOS_GUID))
  155. return -ENODEV;
  156. error = led_off();
  157. if (error != 0)
  158. return -ENODEV;
  159. return led_classdev_register(NULL, &dell_led);
  160. }
  161. static void __exit dell_led_exit(void)
  162. {
  163. led_classdev_unregister(&dell_led);
  164. led_off();
  165. }
  166. module_init(dell_led_init);
  167. module_exit(dell_led_exit);