led.c 3.2 KB

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