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