led.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/proc_fs.h>
  5. #include <linux/string.h>
  6. #include <asm/auxio.h>
  7. #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
  8. static inline void led_toggle(void)
  9. {
  10. unsigned char val = get_auxio();
  11. unsigned char on, off;
  12. if (val & AUXIO_LED) {
  13. on = 0;
  14. off = AUXIO_LED;
  15. } else {
  16. on = AUXIO_LED;
  17. off = 0;
  18. }
  19. set_auxio(on, off);
  20. }
  21. static struct timer_list led_blink_timer;
  22. static void led_blink(unsigned long timeout)
  23. {
  24. led_toggle();
  25. /* reschedule */
  26. if (!timeout) { /* blink according to load */
  27. led_blink_timer.expires = jiffies +
  28. ((1 + (avenrun[0] >> FSHIFT)) * HZ);
  29. led_blink_timer.data = 0;
  30. } else { /* blink at user specified interval */
  31. led_blink_timer.expires = jiffies + (timeout * HZ);
  32. led_blink_timer.data = timeout;
  33. }
  34. add_timer(&led_blink_timer);
  35. }
  36. static int led_read_proc(char *buf, char **start, off_t offset, int count,
  37. int *eof, void *data)
  38. {
  39. int len = 0;
  40. if (get_auxio() & AUXIO_LED)
  41. len = sprintf(buf, "on\n");
  42. else
  43. len = sprintf(buf, "off\n");
  44. return len;
  45. }
  46. static int led_write_proc(struct file *file, const char __user *buffer,
  47. unsigned long count, void *data)
  48. {
  49. char *buf = NULL;
  50. if (count > LED_MAX_LENGTH)
  51. count = LED_MAX_LENGTH;
  52. buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
  53. if (!buf)
  54. return -ENOMEM;
  55. if (copy_from_user(buf, buffer, count)) {
  56. kfree(buf);
  57. return -EFAULT;
  58. }
  59. buf[count] = '\0';
  60. /* work around \n when echo'ing into proc */
  61. if (buf[count - 1] == '\n')
  62. buf[count - 1] = '\0';
  63. /* before we change anything we want to stop any running timers,
  64. * otherwise calls such as on will have no persistent effect
  65. */
  66. del_timer_sync(&led_blink_timer);
  67. if (!strcmp(buf, "on")) {
  68. auxio_set_led(AUXIO_LED_ON);
  69. } else if (!strcmp(buf, "toggle")) {
  70. led_toggle();
  71. } else if ((*buf > '0') && (*buf <= '9')) {
  72. led_blink(simple_strtoul(buf, NULL, 10));
  73. } else if (!strcmp(buf, "load")) {
  74. led_blink(0);
  75. } else {
  76. auxio_set_led(AUXIO_LED_OFF);
  77. }
  78. kfree(buf);
  79. return count;
  80. }
  81. static struct proc_dir_entry *led;
  82. #define LED_VERSION "0.1"
  83. static int __init led_init(void)
  84. {
  85. init_timer(&led_blink_timer);
  86. led_blink_timer.function = led_blink;
  87. led = create_proc_entry("led", 0, NULL);
  88. if (!led)
  89. return -ENOMEM;
  90. led->read_proc = led_read_proc; /* reader function */
  91. led->write_proc = led_write_proc; /* writer function */
  92. led->owner = THIS_MODULE;
  93. printk(KERN_INFO
  94. "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
  95. LED_VERSION);
  96. return 0;
  97. }
  98. static void __exit led_exit(void)
  99. {
  100. remove_proc_entry("led", NULL);
  101. del_timer_sync(&led_blink_timer);
  102. }
  103. module_init(led_init);
  104. module_exit(led_exit);
  105. MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>");
  106. MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems.");
  107. MODULE_LICENSE("GPL");
  108. MODULE_VERSION(LED_VERSION);