led.c 3.1 KB

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