led.c 3.0 KB

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