4 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
6 * This file was based on: drivers/led/ledtrig-timer.c
7 * Copyright 2005-2006 Openedhand Ltd.
8 * Author: Richard Purdie <rpurdie@openedhand.com>
10 * also based on the patch '[PATCH] 2.5.59 morse code panics' posted
11 * in the LKML by Tomas Szepe at Thu, 30 Jan 2003
12 * Copyright (C) 2002 Andrew Rodland <arodland@noln.com>
13 * Copyright (C) 2003 Tomas Szepe <szepe@pinerecords.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License version 2 as published
17 * by the Free Software Foundation.
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
26 #include <linux/sched.h>
27 #include <linux/leds.h>
28 #include <linux/reboot.h>
29 #include <linux/ctype.h>
31 #define MORSE_DELAY_BASE (HZ/2)
33 #define MORSE_STATE_BLINK_START 0
34 #define MORSE_STATE_BLINK_STOP 1
36 #define MORSE_DIT_LEN 1
37 #define MORSE_DAH_LEN 3
38 #define MORSE_SPACE_LEN 7
40 struct morse_trig_data {
47 struct timer_list timer;
50 const unsigned char morsetable[] = {
51 0122, 0, 0310, 0, 0, 0163, /* "#$%&' */
52 055, 0155, 0, 0, 0163, 0141, 0152, 0051, /* ()*+,-./ */
53 077, 076, 074, 070, 060, 040, 041, 043, 047, 057, /* 0-9 */
54 0107, 0125, 0, 0061, 0, 0114, 0, /* :;<=>?@ */
55 006, 021, 025, 011, 002, 024, 013, 020, 004, /* A-I */
56 036, 015, 022, 007, 005, 017, 026, 033, 012, /* J-R */
57 010, 003, 014, 030, 016, 031, 035, 023, /* S-Z */
58 0, 0, 0, 0, 0154 /* [\]^_ */
61 static inline unsigned char tomorse(char c) {
62 if (c >= 'a' && c <= 'z')
64 if (c >= '"' && c <= '_') {
65 return morsetable[c - '"'];
70 static inline unsigned long dit_len(struct morse_trig_data *morse_data)
72 return MORSE_DIT_LEN*morse_data->delay;
75 static inline unsigned long dah_len(struct morse_trig_data *morse_data)
77 return MORSE_DAH_LEN*morse_data->delay;
80 static inline unsigned long space_len(struct morse_trig_data *morse_data)
82 return MORSE_SPACE_LEN*morse_data->delay;
85 static void morse_timer_function(unsigned long data)
87 struct led_classdev *led_cdev = (struct led_classdev *)data;
88 struct morse_trig_data *morse_data = led_cdev->trigger_data;
89 unsigned long brightness = LED_OFF;
90 unsigned long delay = 0;
95 switch (morse_data->state) {
96 case MORSE_STATE_BLINK_START:
97 /* Starting a new blink. We have a valid code in morse. */
98 delay = (morse_data->morse & 001) ? dah_len(morse_data):
100 brightness = LED_FULL;
101 morse_data->state = MORSE_STATE_BLINK_STOP;
102 morse_data->morse >>= 1;
104 case MORSE_STATE_BLINK_STOP:
105 /* Coming off of a blink. */
106 morse_data->state = MORSE_STATE_BLINK_START;
108 if (morse_data->morse > 1) {
109 /* Not done yet, just a one-dit pause. */
110 delay = dit_len(morse_data);
114 /* Get a new char, figure out how much space. */
115 /* First time through */
116 if (!morse_data->msgpos)
117 morse_data->msgpos = (char *)morse_data->msg;
119 if (!*morse_data->msgpos) {
121 morse_data->msgpos = (char *)morse_data->msg;
122 delay = space_len(morse_data);
124 /* Inter-letter space */
125 delay = dah_len(morse_data);
128 if (!(morse_data->morse = tomorse(*morse_data->msgpos))) {
129 delay = space_len(morse_data);
130 /* And get us back here */
131 morse_data->state = MORSE_STATE_BLINK_STOP;
133 morse_data->msgpos++;
137 mod_timer(&morse_data->timer, jiffies + msecs_to_jiffies(delay));
140 led_set_brightness(led_cdev, brightness);
143 static ssize_t _morse_delay_show(struct led_classdev *led_cdev, char *buf)
145 struct morse_trig_data *morse_data = led_cdev->trigger_data;
147 sprintf(buf, "%lu\n", morse_data->delay);
149 return strlen(buf) + 1;
152 static ssize_t _morse_delay_store(struct led_classdev *led_cdev,
153 const char *buf, size_t size)
155 struct morse_trig_data *morse_data = led_cdev->trigger_data;
157 unsigned long state = simple_strtoul(buf, &after, 10);
158 size_t count = after - buf;
161 if (*after && isspace(*after))
165 morse_data->delay = state;
166 mod_timer(&morse_data->timer, jiffies + 1);
173 static ssize_t _morse_msg_show(struct led_classdev *led_cdev, char *buf)
175 struct morse_trig_data *morse_data = led_cdev->trigger_data;
177 if (!morse_data->msg)
178 sprintf(buf, "<none>\n");
180 sprintf(buf, "%s\n", morse_data->msg);
182 return strlen(buf) + 1;
185 static ssize_t _morse_msg_store(struct led_classdev *led_cdev,
186 const char *buf, size_t size)
188 struct morse_trig_data *morse_data = led_cdev->trigger_data;
191 m = kmalloc(size, GFP_KERNEL);
199 kfree(morse_data->msg);
202 morse_data->msgpos = NULL;
203 morse_data->state = MORSE_STATE_BLINK_STOP;
205 mod_timer(&morse_data->timer, jiffies + 1);
210 static ssize_t morse_delay_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
213 struct led_classdev *led_cdev = dev_get_drvdata(dev);
215 return _morse_delay_show(led_cdev, buf);
218 static ssize_t morse_delay_store(struct device *dev,
219 struct device_attribute *attr, const char *buf, size_t size)
221 struct led_classdev *led_cdev = dev_get_drvdata(dev);
223 return _morse_delay_store(led_cdev, buf, size);
226 static ssize_t morse_msg_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
229 struct led_classdev *led_cdev = dev_get_drvdata(dev);
231 return _morse_msg_show(led_cdev, buf);
234 static ssize_t morse_msg_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t size)
237 struct led_classdev *led_cdev = dev_get_drvdata(dev);
239 return _morse_msg_store(led_cdev, buf, size);
242 static DEVICE_ATTR(delay, 0644, morse_delay_show, morse_delay_store);
243 static DEVICE_ATTR(message, 0644, morse_msg_show, morse_msg_store);
245 #define led_device_create_file(leddev, attr) \
246 device_create_file(leddev->dev, &dev_attr_ ## attr)
247 #define led_device_remove_file(leddev, attr) \
248 device_remove_file(leddev->dev, &dev_attr_ ## attr)
250 static void morse_trig_activate(struct led_classdev *led_cdev)
252 struct morse_trig_data *morse_data;
255 morse_data = kzalloc(sizeof(*morse_data), GFP_KERNEL);
259 morse_data->delay = MORSE_DELAY_BASE;
260 init_timer(&morse_data->timer);
261 morse_data->timer.function = morse_timer_function;
262 morse_data->timer.data = (unsigned long)led_cdev;
264 rc = led_device_create_file(led_cdev, delay);
267 rc = led_device_create_file(led_cdev, message);
268 if (rc) goto err_delay;
270 led_cdev->trigger_data = morse_data;
275 led_device_remove_file(led_cdev, delay);
280 static void morse_trig_deactivate(struct led_classdev *led_cdev)
282 struct morse_trig_data *morse_data = led_cdev->trigger_data;
287 led_device_remove_file(led_cdev, message);
288 led_device_remove_file(led_cdev, delay);
290 del_timer_sync(&morse_data->timer);
292 kfree(morse_data->msg);
297 static struct led_trigger morse_led_trigger = {
299 .activate = morse_trig_activate,
300 .deactivate = morse_trig_deactivate,
303 static int __init morse_trig_init(void)
305 return led_trigger_register(&morse_led_trigger);
308 static void __exit morse_trig_exit(void)
310 led_trigger_unregister(&morse_led_trigger);
313 module_init(morse_trig_init);
314 module_exit(morse_trig_exit);
316 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
317 MODULE_DESCRIPTION("Morse LED trigger");
318 MODULE_LICENSE("GPL");