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>
12 #include <asm/auxio.h>
14 #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
16 static inline void led_toggle(void)
18 unsigned char val
= get_auxio();
19 unsigned char on
, off
;
21 if (val
& AUXIO_LED
) {
32 static struct timer_list led_blink_timer
;
34 static void led_blink(unsigned long timeout
)
39 if (!timeout
) { /* blink according to load */
40 led_blink_timer
.expires
= jiffies
+
41 ((1 + (avenrun
[0] >> FSHIFT
)) * HZ
);
42 led_blink_timer
.data
= 0;
43 } else { /* blink at user specified interval */
44 led_blink_timer
.expires
= jiffies
+ (timeout
* HZ
);
45 led_blink_timer
.data
= timeout
;
47 add_timer(&led_blink_timer
);
50 static int led_proc_show(struct seq_file
*m
, void *v
)
52 if (get_auxio() & AUXIO_LED
)
59 static int led_proc_open(struct inode
*inode
, struct file
*file
)
61 return single_open(file
, led_proc_show
, NULL
);
64 static ssize_t
led_proc_write(struct file
*file
, const char __user
*buffer
,
65 size_t count
, loff_t
*ppos
)
69 if (count
> LED_MAX_LENGTH
)
70 count
= LED_MAX_LENGTH
;
72 buf
= kmalloc(sizeof(char) * (count
+ 1), GFP_KERNEL
);
76 if (copy_from_user(buf
, buffer
, count
)) {
83 /* work around \n when echo'ing into proc */
84 if (buf
[count
- 1] == '\n')
85 buf
[count
- 1] = '\0';
87 /* before we change anything we want to stop any running timers,
88 * otherwise calls such as on will have no persistent effect
90 del_timer_sync(&led_blink_timer
);
92 if (!strcmp(buf
, "on")) {
93 auxio_set_led(AUXIO_LED_ON
);
94 } else if (!strcmp(buf
, "toggle")) {
96 } else if ((*buf
> '0') && (*buf
<= '9')) {
97 led_blink(simple_strtoul(buf
, NULL
, 10));
98 } else if (!strcmp(buf
, "load")) {
101 auxio_set_led(AUXIO_LED_OFF
);
109 static const struct file_operations led_proc_fops
= {
110 .owner
= THIS_MODULE
,
111 .open
= led_proc_open
,
114 .release
= single_release
,
115 .write
= led_proc_write
,
118 static struct proc_dir_entry
*led
;
120 #define LED_VERSION "0.1"
122 static int __init
led_init(void)
124 init_timer(&led_blink_timer
);
125 led_blink_timer
.function
= led_blink
;
127 led
= proc_create("led", 0, NULL
, &led_proc_fops
);
132 "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
138 static void __exit
led_exit(void)
140 remove_proc_entry("led", NULL
);
141 del_timer_sync(&led_blink_timer
);
144 module_init(led_init
);
145 module_exit(led_exit
);
147 MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>");
148 MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems.");
149 MODULE_LICENSE("GPL");
150 MODULE_VERSION(LED_VERSION
);