Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 5 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 7 | #include <linux/string.h> |
David S. Miller | b80a718 | 2008-02-24 18:45:09 -0800 | [diff] [blame] | 8 | #include <linux/jiffies.h> |
| 9 | #include <linux/timer.h> |
| 10 | #include <linux/uaccess.h> |
Ingo Molnar | badaff8 | 2017-02-08 08:45:17 +0100 | [diff] [blame] | 11 | #include <linux/sched/loadavg.h> |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 12 | |
| 13 | #include <asm/auxio.h> |
| 14 | |
| 15 | #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */ |
| 16 | |
| 17 | static inline void led_toggle(void) |
| 18 | { |
| 19 | unsigned char val = get_auxio(); |
| 20 | unsigned char on, off; |
| 21 | |
| 22 | if (val & AUXIO_LED) { |
| 23 | on = 0; |
| 24 | off = AUXIO_LED; |
| 25 | } else { |
| 26 | on = AUXIO_LED; |
| 27 | off = 0; |
| 28 | } |
| 29 | |
| 30 | set_auxio(on, off); |
| 31 | } |
| 32 | |
| 33 | static struct timer_list led_blink_timer; |
| 34 | |
| 35 | static void led_blink(unsigned long timeout) |
| 36 | { |
| 37 | led_toggle(); |
| 38 | |
| 39 | /* reschedule */ |
| 40 | if (!timeout) { /* blink according to load */ |
| 41 | led_blink_timer.expires = jiffies + |
| 42 | ((1 + (avenrun[0] >> FSHIFT)) * HZ); |
| 43 | led_blink_timer.data = 0; |
| 44 | } else { /* blink at user specified interval */ |
| 45 | led_blink_timer.expires = jiffies + (timeout * HZ); |
| 46 | led_blink_timer.data = timeout; |
| 47 | } |
| 48 | add_timer(&led_blink_timer); |
| 49 | } |
| 50 | |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 51 | static int led_proc_show(struct seq_file *m, void *v) |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 52 | { |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 53 | if (get_auxio() & AUXIO_LED) |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 54 | seq_puts(m, "on\n"); |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 55 | else |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 56 | seq_puts(m, "off\n"); |
| 57 | return 0; |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 60 | static int led_proc_open(struct inode *inode, struct file *file) |
| 61 | { |
| 62 | return single_open(file, led_proc_show, NULL); |
| 63 | } |
| 64 | |
| 65 | static ssize_t led_proc_write(struct file *file, const char __user *buffer, |
| 66 | size_t count, loff_t *ppos) |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 67 | { |
| 68 | char *buf = NULL; |
| 69 | |
| 70 | if (count > LED_MAX_LENGTH) |
| 71 | count = LED_MAX_LENGTH; |
| 72 | |
| 73 | buf = kmalloc(sizeof(char) * (count + 1), GFP_KERNEL); |
| 74 | if (!buf) |
| 75 | return -ENOMEM; |
| 76 | |
| 77 | if (copy_from_user(buf, buffer, count)) { |
| 78 | kfree(buf); |
| 79 | return -EFAULT; |
| 80 | } |
| 81 | |
| 82 | buf[count] = '\0'; |
| 83 | |
| 84 | /* work around \n when echo'ing into proc */ |
| 85 | if (buf[count - 1] == '\n') |
| 86 | buf[count - 1] = '\0'; |
| 87 | |
| 88 | /* before we change anything we want to stop any running timers, |
| 89 | * otherwise calls such as on will have no persistent effect |
| 90 | */ |
| 91 | del_timer_sync(&led_blink_timer); |
| 92 | |
| 93 | if (!strcmp(buf, "on")) { |
| 94 | auxio_set_led(AUXIO_LED_ON); |
| 95 | } else if (!strcmp(buf, "toggle")) { |
| 96 | led_toggle(); |
| 97 | } else if ((*buf > '0') && (*buf <= '9')) { |
| 98 | led_blink(simple_strtoul(buf, NULL, 10)); |
| 99 | } else if (!strcmp(buf, "load")) { |
| 100 | led_blink(0); |
| 101 | } else { |
| 102 | auxio_set_led(AUXIO_LED_OFF); |
| 103 | } |
| 104 | |
| 105 | kfree(buf); |
| 106 | |
| 107 | return count; |
| 108 | } |
| 109 | |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 110 | static const struct file_operations led_proc_fops = { |
| 111 | .owner = THIS_MODULE, |
| 112 | .open = led_proc_open, |
| 113 | .read = seq_read, |
| 114 | .llseek = seq_lseek, |
| 115 | .release = single_release, |
| 116 | .write = led_proc_write, |
| 117 | }; |
| 118 | |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 119 | static struct proc_dir_entry *led; |
| 120 | |
| 121 | #define LED_VERSION "0.1" |
| 122 | |
| 123 | static int __init led_init(void) |
| 124 | { |
| 125 | init_timer(&led_blink_timer); |
| 126 | led_blink_timer.function = led_blink; |
| 127 | |
Alexey Dobriyan | 399dc43 | 2008-06-03 15:21:21 -0700 | [diff] [blame] | 128 | led = proc_create("led", 0, NULL, &led_proc_fops); |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 129 | if (!led) |
| 130 | return -ENOMEM; |
Lars Kotthoff | ee1858d | 2005-11-07 14:08:04 -0800 | [diff] [blame] | 131 | |
| 132 | printk(KERN_INFO |
| 133 | "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n", |
| 134 | LED_VERSION); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static void __exit led_exit(void) |
| 140 | { |
| 141 | remove_proc_entry("led", NULL); |
| 142 | del_timer_sync(&led_blink_timer); |
| 143 | } |
| 144 | |
| 145 | module_init(led_init); |
| 146 | module_exit(led_exit); |
| 147 | |
| 148 | MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>"); |
| 149 | MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems."); |
| 150 | MODULE_LICENSE("GPL"); |
| 151 | MODULE_VERSION(LED_VERSION); |