xref: /linux/arch/sparc/kernel/led.c (revision f7511d5f66f01fc451747b24e79f3ada7a3af9af)
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/proc_fs.h>
5 #include <linux/string.h>
6 #include <linux/jiffies.h>
7 #include <linux/timer.h>
8 #include <linux/uaccess.h>
9 
10 #include <asm/auxio.h>
11 
12 #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
13 
14 static inline void led_toggle(void)
15 {
16 	unsigned char val = get_auxio();
17 	unsigned char on, off;
18 
19 	if (val & AUXIO_LED) {
20 		on = 0;
21 		off = AUXIO_LED;
22 	} else {
23 		on = AUXIO_LED;
24 		off = 0;
25 	}
26 
27 	set_auxio(on, off);
28 }
29 
30 static struct timer_list led_blink_timer;
31 
32 static void led_blink(unsigned long timeout)
33 {
34 	led_toggle();
35 
36 	/* reschedule */
37 	if (!timeout) { /* blink according to load */
38 		led_blink_timer.expires = jiffies +
39 			((1 + (avenrun[0] >> FSHIFT)) * HZ);
40 		led_blink_timer.data = 0;
41 	} else { /* blink at user specified interval */
42 		led_blink_timer.expires = jiffies + (timeout * HZ);
43 		led_blink_timer.data = timeout;
44 	}
45 	add_timer(&led_blink_timer);
46 }
47 
48 static int led_read_proc(char *buf, char **start, off_t offset, int count,
49 			 int *eof, void *data)
50 {
51 	int len = 0;
52 
53 	if (get_auxio() & AUXIO_LED)
54 		len = sprintf(buf, "on\n");
55 	else
56 		len = sprintf(buf, "off\n");
57 
58 	return len;
59 }
60 
61 static int led_write_proc(struct file *file, const char __user *buffer,
62 			  unsigned long count, void *data)
63 {
64 	char *buf = NULL;
65 
66 	if (count > LED_MAX_LENGTH)
67 		count = LED_MAX_LENGTH;
68 
69 	buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL);
70 	if (!buf)
71 		return -ENOMEM;
72 
73 	if (copy_from_user(buf, buffer, count)) {
74 		kfree(buf);
75 		return -EFAULT;
76 	}
77 
78 	buf[count] = '\0';
79 
80 	/* work around \n when echo'ing into proc */
81 	if (buf[count - 1] == '\n')
82 		buf[count - 1] = '\0';
83 
84 	/* before we change anything we want to stop any running timers,
85 	 * otherwise calls such as on will have no persistent effect
86 	 */
87 	del_timer_sync(&led_blink_timer);
88 
89 	if (!strcmp(buf, "on")) {
90 		auxio_set_led(AUXIO_LED_ON);
91 	} else if (!strcmp(buf, "toggle")) {
92 		led_toggle();
93 	} else if ((*buf > '0') && (*buf <= '9')) {
94 		led_blink(simple_strtoul(buf, NULL, 10));
95 	} else if (!strcmp(buf, "load")) {
96 		led_blink(0);
97 	} else {
98 		auxio_set_led(AUXIO_LED_OFF);
99 	}
100 
101 	kfree(buf);
102 
103 	return count;
104 }
105 
106 static struct proc_dir_entry *led;
107 
108 #define LED_VERSION	"0.1"
109 
110 static int __init led_init(void)
111 {
112 	init_timer(&led_blink_timer);
113 	led_blink_timer.function = led_blink;
114 
115 	led = create_proc_entry("led", 0, NULL);
116 	if (!led)
117 		return -ENOMEM;
118 
119 	led->read_proc = led_read_proc; /* reader function */
120 	led->write_proc = led_write_proc; /* writer function */
121 	led->owner = THIS_MODULE;
122 
123 	printk(KERN_INFO
124 	       "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
125 	       LED_VERSION);
126 
127 	return 0;
128 }
129 
130 static void __exit led_exit(void)
131 {
132 	remove_proc_entry("led", NULL);
133 	del_timer_sync(&led_blink_timer);
134 }
135 
136 module_init(led_init);
137 module_exit(led_exit);
138 
139 MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>");
140 MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems.");
141 MODULE_LICENSE("GPL");
142 MODULE_VERSION(LED_VERSION);
143