leds-clevo-mail.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <linux/module.h>
  2. #include <linux/platform_device.h>
  3. #include <linux/err.h>
  4. #include <linux/leds.h>
  5. #include <linux/io.h>
  6. #include <linux/dmi.h>
  7. #include <linux/i8042.h>
  8. #define CLEVO_MAIL_LED_OFF 0x0084
  9. #define CLEVO_MAIL_LED_BLINK_1HZ 0x008A
  10. #define CLEVO_MAIL_LED_BLINK_0_5HZ 0x0083
  11. MODULE_AUTHOR("Márton Németh <nm127@freemail.hu>");
  12. MODULE_DESCRIPTION("Clevo mail LED driver");
  13. MODULE_LICENSE("GPL");
  14. static unsigned int __initdata nodetect;
  15. module_param_named(nodetect, nodetect, bool, 0);
  16. MODULE_PARM_DESC(nodetect, "Skip DMI hardware detection");
  17. static struct platform_device *pdev;
  18. static int __init clevo_mail_led_dmi_callback(const struct dmi_system_id *id)
  19. {
  20. printk(KERN_INFO KBUILD_MODNAME ": '%s' found\n", id->ident);
  21. return 1;
  22. }
  23. /*
  24. * struct mail_led_whitelist - List of known good models
  25. *
  26. * Contains the known good models this driver is compatible with.
  27. * When adding a new model try to be as strict as possible. This
  28. * makes it possible to keep the false positives (the model is
  29. * detected as working, but in reality it is not) as low as
  30. * possible.
  31. */
  32. static struct dmi_system_id __initdata mail_led_whitelist[] = {
  33. {
  34. .callback = clevo_mail_led_dmi_callback,
  35. .ident = "Clevo D410J",
  36. .matches = {
  37. DMI_MATCH(DMI_SYS_VENDOR, "VIA"),
  38. DMI_MATCH(DMI_PRODUCT_NAME, "K8N800"),
  39. DMI_MATCH(DMI_PRODUCT_VERSION, "VT8204B")
  40. }
  41. },
  42. {
  43. .callback = clevo_mail_led_dmi_callback,
  44. .ident = "Clevo M5x0N",
  45. .matches = {
  46. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  47. DMI_MATCH(DMI_PRODUCT_NAME, "M5x0N")
  48. }
  49. },
  50. {
  51. .callback = clevo_mail_led_dmi_callback,
  52. .ident = "Positivo Mobile",
  53. .matches = {
  54. DMI_MATCH(DMI_BOARD_VENDOR, "CLEVO Co. "),
  55. DMI_MATCH(DMI_BOARD_NAME, "M5X0V "),
  56. DMI_MATCH(DMI_PRODUCT_NAME, "Positivo Mobile"),
  57. DMI_MATCH(DMI_PRODUCT_VERSION, "VT6198")
  58. }
  59. },
  60. {
  61. .callback = clevo_mail_led_dmi_callback,
  62. .ident = "Clevo D400P",
  63. .matches = {
  64. DMI_MATCH(DMI_BOARD_VENDOR, "Clevo"),
  65. DMI_MATCH(DMI_BOARD_NAME, "D400P"),
  66. DMI_MATCH(DMI_BOARD_VERSION, "Rev.A"),
  67. DMI_MATCH(DMI_PRODUCT_VERSION, "0106")
  68. }
  69. },
  70. {
  71. .callback = clevo_mail_led_dmi_callback,
  72. .ident = "Clevo D410V",
  73. .matches = {
  74. DMI_MATCH(DMI_BOARD_VENDOR, "Clevo, Co."),
  75. DMI_MATCH(DMI_BOARD_NAME, "D400V/D470V"),
  76. DMI_MATCH(DMI_BOARD_VERSION, "SS78B"),
  77. DMI_MATCH(DMI_PRODUCT_VERSION, "Rev. A1")
  78. }
  79. },
  80. { }
  81. };
  82. static void clevo_mail_led_set(struct led_classdev *led_cdev,
  83. enum led_brightness value)
  84. {
  85. if (value == LED_OFF)
  86. i8042_command(NULL, CLEVO_MAIL_LED_OFF);
  87. else if (value <= LED_HALF)
  88. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  89. else
  90. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
  91. }
  92. static int clevo_mail_led_blink(struct led_classdev *led_cdev,
  93. unsigned long *delay_on,
  94. unsigned long *delay_off)
  95. {
  96. int status = -EINVAL;
  97. if (*delay_on == 0 /* ms */ && *delay_off == 0 /* ms */) {
  98. /* Special case: the leds subsystem requested us to
  99. * chose one user friendly blinking of the LED, and
  100. * start it. Let's blink the led slowly (0.5Hz).
  101. */
  102. *delay_on = 1000; /* ms */
  103. *delay_off = 1000; /* ms */
  104. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  105. status = 0;
  106. } else if (*delay_on == 500 /* ms */ && *delay_off == 500 /* ms */) {
  107. /* blink the led with 1Hz */
  108. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
  109. status = 0;
  110. } else if (*delay_on == 1000 /* ms */ && *delay_off == 1000 /* ms */) {
  111. /* blink the led with 0.5Hz */
  112. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  113. status = 0;
  114. } else {
  115. printk(KERN_DEBUG KBUILD_MODNAME
  116. ": clevo_mail_led_blink(..., %lu, %lu),"
  117. " returning -EINVAL (unsupported)\n",
  118. *delay_on, *delay_off);
  119. }
  120. return status;
  121. }
  122. static struct led_classdev clevo_mail_led = {
  123. .name = "clevo::mail",
  124. .brightness_set = clevo_mail_led_set,
  125. .blink_set = clevo_mail_led_blink,
  126. };
  127. static int __init clevo_mail_led_probe(struct platform_device *pdev)
  128. {
  129. return led_classdev_register(&pdev->dev, &clevo_mail_led);
  130. }
  131. static int clevo_mail_led_remove(struct platform_device *pdev)
  132. {
  133. led_classdev_unregister(&clevo_mail_led);
  134. return 0;
  135. }
  136. #ifdef CONFIG_PM
  137. static int clevo_mail_led_suspend(struct platform_device *dev,
  138. pm_message_t state)
  139. {
  140. led_classdev_suspend(&clevo_mail_led);
  141. return 0;
  142. }
  143. static int clevo_mail_led_resume(struct platform_device *dev)
  144. {
  145. led_classdev_resume(&clevo_mail_led);
  146. return 0;
  147. }
  148. #else
  149. #define clevo_mail_led_suspend NULL
  150. #define clevo_mail_led_resume NULL
  151. #endif
  152. static struct platform_driver clevo_mail_led_driver = {
  153. .probe = clevo_mail_led_probe,
  154. .remove = clevo_mail_led_remove,
  155. .suspend = clevo_mail_led_suspend,
  156. .resume = clevo_mail_led_resume,
  157. .driver = {
  158. .name = KBUILD_MODNAME,
  159. .owner = THIS_MODULE,
  160. },
  161. };
  162. static int __init clevo_mail_led_init(void)
  163. {
  164. int error = 0;
  165. int count = 0;
  166. /* Check with the help of DMI if we are running on supported hardware */
  167. if (!nodetect) {
  168. count = dmi_check_system(mail_led_whitelist);
  169. } else {
  170. count = 1;
  171. printk(KERN_ERR KBUILD_MODNAME ": Skipping DMI detection. "
  172. "If the driver works on your hardware please "
  173. "report model and the output of dmidecode in tracker "
  174. "at http://sourceforge.net/projects/clevo-mailled/\n");
  175. }
  176. if (!count)
  177. return -ENODEV;
  178. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  179. if (!IS_ERR(pdev)) {
  180. error = platform_driver_probe(&clevo_mail_led_driver,
  181. clevo_mail_led_probe);
  182. if (error) {
  183. printk(KERN_ERR KBUILD_MODNAME
  184. ": Can't probe platform driver\n");
  185. platform_device_unregister(pdev);
  186. }
  187. } else
  188. error = PTR_ERR(pdev);
  189. return error;
  190. }
  191. static void __exit clevo_mail_led_exit(void)
  192. {
  193. platform_device_unregister(pdev);
  194. platform_driver_unregister(&clevo_mail_led_driver);
  195. clevo_mail_led_set(NULL, LED_OFF);
  196. }
  197. module_init(clevo_mail_led_init);
  198. module_exit(clevo_mail_led_exit);