Linux 5.8-rc4
[linux/fpc-iii.git] / arch / sparc / kernel / led.c
blobbd48575172c323e2f21696b16de3b078c7ddb76a
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>
14 #include <asm/auxio.h>
16 #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
18 static inline void led_toggle(void)
20 unsigned char val = get_auxio();
21 unsigned char on, off;
23 if (val & AUXIO_LED) {
24 on = 0;
25 off = AUXIO_LED;
26 } else {
27 on = AUXIO_LED;
28 off = 0;
31 set_auxio(on, off);
34 static struct timer_list led_blink_timer;
35 static unsigned long led_blink_timer_timeout;
37 static void led_blink(struct timer_list *unused)
39 unsigned long timeout = led_blink_timer_timeout;
41 led_toggle();
43 /* reschedule */
44 if (!timeout) { /* blink according to load */
45 led_blink_timer.expires = jiffies +
46 ((1 + (avenrun[0] >> FSHIFT)) * HZ);
47 } else { /* blink at user specified interval */
48 led_blink_timer.expires = jiffies + (timeout * HZ);
50 add_timer(&led_blink_timer);
53 static int led_proc_show(struct seq_file *m, void *v)
55 if (get_auxio() & AUXIO_LED)
56 seq_puts(m, "on\n");
57 else
58 seq_puts(m, "off\n");
59 return 0;
62 static int led_proc_open(struct inode *inode, struct file *file)
64 return single_open(file, led_proc_show, NULL);
67 static ssize_t led_proc_write(struct file *file, const char __user *buffer,
68 size_t count, loff_t *ppos)
70 char *buf = NULL;
72 if (count > LED_MAX_LENGTH)
73 count = LED_MAX_LENGTH;
75 buf = memdup_user_nul(buffer, count);
76 if (IS_ERR(buf))
77 return PTR_ERR(buf);
79 /* work around \n when echo'ing into proc */
80 if (buf[count - 1] == '\n')
81 buf[count - 1] = '\0';
83 /* before we change anything we want to stop any running timers,
84 * otherwise calls such as on will have no persistent effect
86 del_timer_sync(&led_blink_timer);
88 if (!strcmp(buf, "on")) {
89 auxio_set_led(AUXIO_LED_ON);
90 } else if (!strcmp(buf, "toggle")) {
91 led_toggle();
92 } else if ((*buf > '0') && (*buf <= '9')) {
93 led_blink_timer_timeout = simple_strtoul(buf, NULL, 10);
94 led_blink(&led_blink_timer);
95 } else if (!strcmp(buf, "load")) {
96 led_blink_timer_timeout = 0;
97 led_blink(&led_blink_timer);
98 } else {
99 auxio_set_led(AUXIO_LED_OFF);
102 kfree(buf);
104 return count;
107 static const struct proc_ops led_proc_ops = {
108 .proc_open = led_proc_open,
109 .proc_read = seq_read,
110 .proc_lseek = seq_lseek,
111 .proc_release = single_release,
112 .proc_write = led_proc_write,
115 static struct proc_dir_entry *led;
117 #define LED_VERSION "0.1"
119 static int __init led_init(void)
121 timer_setup(&led_blink_timer, led_blink, 0);
123 led = proc_create("led", 0, NULL, &led_proc_ops);
124 if (!led)
125 return -ENOMEM;
127 printk(KERN_INFO
128 "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
129 LED_VERSION);
131 return 0;
134 static void __exit led_exit(void)
136 remove_proc_entry("led", NULL);
137 del_timer_sync(&led_blink_timer);
140 module_init(led_init);
141 module_exit(led_exit);
143 MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>");
144 MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems.");
145 MODULE_LICENSE("GPL");
146 MODULE_VERSION(LED_VERSION);