Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/kernel.h>
0003 #include <linux/module.h>
0004 #include <linux/init.h>
0005 #include <linux/proc_fs.h>
0006 #include <linux/seq_file.h>
0007 #include <linux/slab.h>
0008 #include <linux/string.h>
0009 #include <linux/jiffies.h>
0010 #include <linux/timer.h>
0011 #include <linux/uaccess.h>
0012 #include <linux/sched/loadavg.h>
0013 
0014 #include <asm/auxio.h>
0015 
0016 #define LED_MAX_LENGTH 8 /* maximum chars written to proc file */
0017 
0018 static inline void led_toggle(void)
0019 {
0020     unsigned char val = get_auxio();
0021     unsigned char on, off;
0022 
0023     if (val & AUXIO_LED) {
0024         on = 0;
0025         off = AUXIO_LED;
0026     } else {
0027         on = AUXIO_LED;
0028         off = 0;
0029     }
0030 
0031     set_auxio(on, off);
0032 }
0033 
0034 static struct timer_list led_blink_timer;
0035 static unsigned long led_blink_timer_timeout;
0036 
0037 static void led_blink(struct timer_list *unused)
0038 {
0039     unsigned long timeout = led_blink_timer_timeout;
0040 
0041     led_toggle();
0042 
0043     /* reschedule */
0044     if (!timeout) { /* blink according to load */
0045         led_blink_timer.expires = jiffies +
0046             ((1 + (avenrun[0] >> FSHIFT)) * HZ);
0047     } else { /* blink at user specified interval */
0048         led_blink_timer.expires = jiffies + (timeout * HZ);
0049     }
0050     add_timer(&led_blink_timer);
0051 }
0052 
0053 #ifdef CONFIG_PROC_FS
0054 static int led_proc_show(struct seq_file *m, void *v)
0055 {
0056     if (get_auxio() & AUXIO_LED)
0057         seq_puts(m, "on\n");
0058     else
0059         seq_puts(m, "off\n");
0060     return 0;
0061 }
0062 
0063 static int led_proc_open(struct inode *inode, struct file *file)
0064 {
0065     return single_open(file, led_proc_show, NULL);
0066 }
0067 
0068 static ssize_t led_proc_write(struct file *file, const char __user *buffer,
0069                   size_t count, loff_t *ppos)
0070 {
0071     char *buf = NULL;
0072 
0073     if (count > LED_MAX_LENGTH)
0074         count = LED_MAX_LENGTH;
0075 
0076     buf = memdup_user_nul(buffer, count);
0077     if (IS_ERR(buf))
0078         return PTR_ERR(buf);
0079 
0080     /* work around \n when echo'ing into proc */
0081     if (buf[count - 1] == '\n')
0082         buf[count - 1] = '\0';
0083 
0084     /* before we change anything we want to stop any running timers,
0085      * otherwise calls such as on will have no persistent effect
0086      */
0087     del_timer_sync(&led_blink_timer);
0088 
0089     if (!strcmp(buf, "on")) {
0090         auxio_set_led(AUXIO_LED_ON);
0091     } else if (!strcmp(buf, "toggle")) {
0092         led_toggle();
0093     } else if ((*buf > '0') && (*buf <= '9')) {
0094         led_blink_timer_timeout = simple_strtoul(buf, NULL, 10);
0095         led_blink(&led_blink_timer);
0096     } else if (!strcmp(buf, "load")) {
0097         led_blink_timer_timeout = 0;
0098         led_blink(&led_blink_timer);
0099     } else {
0100         auxio_set_led(AUXIO_LED_OFF);
0101     }
0102 
0103     kfree(buf);
0104 
0105     return count;
0106 }
0107 
0108 static const struct proc_ops led_proc_ops = {
0109     .proc_open  = led_proc_open,
0110     .proc_read  = seq_read,
0111     .proc_lseek = seq_lseek,
0112     .proc_release   = single_release,
0113     .proc_write = led_proc_write,
0114 };
0115 #endif
0116 
0117 #define LED_VERSION "0.1"
0118 
0119 static int __init led_init(void)
0120 {
0121     timer_setup(&led_blink_timer, led_blink, 0);
0122 
0123 #ifdef CONFIG_PROC_FS
0124     if (!proc_create("led", 0, NULL, &led_proc_ops))
0125         return -ENOMEM;
0126 #endif
0127     printk(KERN_INFO
0128            "led: version %s, Lars Kotthoff <metalhead@metalhead.ws>\n",
0129            LED_VERSION);
0130 
0131     return 0;
0132 }
0133 
0134 static void __exit led_exit(void)
0135 {
0136     remove_proc_entry("led", NULL);
0137     del_timer_sync(&led_blink_timer);
0138 }
0139 
0140 module_init(led_init);
0141 module_exit(led_exit);
0142 
0143 MODULE_AUTHOR("Lars Kotthoff <metalhead@metalhead.ws>");
0144 MODULE_DESCRIPTION("Provides control of the front LED on SPARC systems.");
0145 MODULE_LICENSE("GPL");
0146 MODULE_VERSION(LED_VERSION);