led.c 3.1 KB

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