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 <linux/sched/loadavg.h>
13 #include <asm/auxio.h>
15 #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
17 static inline void led_toggle(void)
19 unsigned char val
= get_auxio();
20 unsigned char on
, off
;
22 if (val
& AUXIO_LED
) {
33 static struct timer_list led_blink_timer
;
34 static unsigned long led_blink_timer_timeout
;
36 static void led_blink(struct timer_list
*unused
)
38 unsigned long timeout
= led_blink_timer_timeout
;
43 if (!timeout
) { /* blink according to load */
44 led_blink_timer
.expires
= jiffies
+
45 ((1 + (avenrun
[0] >> FSHIFT
)) * HZ
);
46 } else { /* blink at user specified interval */
47 led_blink_timer
.expires
= jiffies
+ (timeout
* HZ
);
49 add_timer(&led_blink_timer
);
52 static int led_proc_show(struct seq_file
*m
, void *v
)
54 if (get_auxio() & AUXIO_LED
)
61 static int led_proc_open(struct inode
*inode
, struct file
*file
)
63 return single_open(file
, led_proc_show
, NULL
);
66 static ssize_t
led_proc_write(struct file
*file
, const char __user
*buffer
,
67 size_t count
, loff_t
*ppos
)
71 if (count
> LED_MAX_LENGTH
)
72 count
= LED_MAX_LENGTH
;
74 buf
= memdup_user_nul(buffer
, count
);
78 /* work around \n when echo'ing into proc */
79 if (buf
[count
- 1] == '\n')
80 buf
[count
- 1] = '\0';
82 /* before we change anything we want to stop any running timers,
83 * otherwise calls such as on will have no persistent effect
85 del_timer_sync(&led_blink_timer
);
87 if (!strcmp(buf
, "on")) {
88 auxio_set_led(AUXIO_LED_ON
);
89 } else if (!strcmp(buf
, "toggle")) {
91 } else if ((*buf
> '0') && (*buf
<= '9')) {
92 led_blink_timer_timeout
= simple_strtoul(buf
, NULL
, 10);
93 led_blink(&led_blink_timer
);
94 } else if (!strcmp(buf
, "load")) {
95 led_blink_timer_timeout
= 0;
96 led_blink(&led_blink_timer
);
98 auxio_set_led(AUXIO_LED_OFF
);
106 static const struct file_operations led_proc_fops
= {
107 .owner
= THIS_MODULE
,
108 .open
= led_proc_open
,
111 .release
= single_release
,
112 .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_fops
);
128 "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
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
);