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