leds-clevo-mail.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 D410V",
  63. .matches = {
  64. DMI_MATCH(DMI_BOARD_VENDOR, "Clevo, Co."),
  65. DMI_MATCH(DMI_BOARD_NAME, "D400V/D470V"),
  66. DMI_MATCH(DMI_BOARD_VERSION, "SS78B"),
  67. DMI_MATCH(DMI_PRODUCT_VERSION, "Rev. A1")
  68. }
  69. },
  70. { }
  71. };
  72. static void clevo_mail_led_set(struct led_classdev *led_cdev,
  73. enum led_brightness value)
  74. {
  75. if (value == LED_OFF)
  76. i8042_command(NULL, CLEVO_MAIL_LED_OFF);
  77. else if (value <= LED_HALF)
  78. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  79. else
  80. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
  81. }
  82. static int clevo_mail_led_blink(struct led_classdev *led_cdev,
  83. unsigned long* delay_on,
  84. unsigned long* delay_off)
  85. {
  86. int status = -EINVAL;
  87. if (*delay_on == 0 /* ms */ && *delay_off == 0 /* ms */) {
  88. /* Special case: the leds subsystem requested us to
  89. * chose one user friendly blinking of the LED, and
  90. * start it. Let's blink the led slowly (0.5Hz).
  91. */
  92. *delay_on = 1000; /* ms */
  93. *delay_off = 1000; /* ms */
  94. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  95. status = 0;
  96. } else if (*delay_on == 500 /* ms */ && *delay_off == 500 /* ms */) {
  97. /* blink the led with 1Hz */
  98. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
  99. status = 0;
  100. } else if (*delay_on == 1000 /* ms */ && *delay_off == 1000 /* ms */) {
  101. /* blink the led with 0.5Hz */
  102. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  103. status = 0;
  104. } else {
  105. printk(KERN_DEBUG KBUILD_MODNAME
  106. ": clevo_mail_led_blink(..., %lu, %lu),"
  107. " returning -EINVAL (unsupported)\n",
  108. *delay_on, *delay_off);
  109. }
  110. return status;
  111. }
  112. static struct led_classdev clevo_mail_led = {
  113. .name = "clevo::mail",
  114. .brightness_set = clevo_mail_led_set,
  115. .blink_set = clevo_mail_led_blink,
  116. };
  117. static int __init clevo_mail_led_probe(struct platform_device *pdev)
  118. {
  119. return led_classdev_register(&pdev->dev, &clevo_mail_led);
  120. }
  121. static int clevo_mail_led_remove(struct platform_device *pdev)
  122. {
  123. led_classdev_unregister(&clevo_mail_led);
  124. return 0;
  125. }
  126. #ifdef CONFIG_PM
  127. static int clevo_mail_led_suspend(struct platform_device *dev,
  128. pm_message_t state)
  129. {
  130. led_classdev_suspend(&clevo_mail_led);
  131. return 0;
  132. }
  133. static int clevo_mail_led_resume(struct platform_device *dev)
  134. {
  135. led_classdev_resume(&clevo_mail_led);
  136. return 0;
  137. }
  138. #else
  139. #define clevo_mail_led_suspend NULL
  140. #define clevo_mail_led_resume NULL
  141. #endif
  142. static struct platform_driver clevo_mail_led_driver = {
  143. .probe = clevo_mail_led_probe,
  144. .remove = clevo_mail_led_remove,
  145. .suspend = clevo_mail_led_suspend,
  146. .resume = clevo_mail_led_resume,
  147. .driver = {
  148. .name = KBUILD_MODNAME,
  149. .owner = THIS_MODULE,
  150. },
  151. };
  152. static int __init clevo_mail_led_init(void)
  153. {
  154. int error = 0;
  155. int count = 0;
  156. /* Check with the help of DMI if we are running on supported hardware */
  157. if (!nodetect) {
  158. count = dmi_check_system(mail_led_whitelist);
  159. } else {
  160. count = 1;
  161. printk(KERN_ERR KBUILD_MODNAME ": Skipping DMI detection. "
  162. "If the driver works on your hardware please "
  163. "report model and the output of dmidecode in tracker "
  164. "at http://sourceforge.net/projects/clevo-mailled/\n");
  165. }
  166. if (!count)
  167. return -ENODEV;
  168. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  169. if (!IS_ERR(pdev)) {
  170. error = platform_driver_probe(&clevo_mail_led_driver,
  171. clevo_mail_led_probe);
  172. if (error) {
  173. printk(KERN_ERR KBUILD_MODNAME
  174. ": Can't probe platform driver\n");
  175. platform_device_unregister(pdev);
  176. }
  177. } else
  178. error = PTR_ERR(pdev);
  179. return error;
  180. }
  181. static void __exit clevo_mail_led_exit(void)
  182. {
  183. platform_device_unregister(pdev);
  184. platform_driver_unregister(&clevo_mail_led_driver);
  185. clevo_mail_led_set(NULL, LED_OFF);
  186. }
  187. module_init(clevo_mail_led_init);
  188. module_exit(clevo_mail_led_exit);