[adm5120] morse LED trigger cleanups
[openwrt/svn-archive/archive.git] / target / linux / adm5120 / files / drivers / leds / ledtrig-morse.c
1 /*
2 * LED Morse Trigger
3 *
4 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
5 *
6 * This file was based on: drivers/led/ledtrig-timer.c
7 * Copyright 2005-2006 Openedhand Ltd.
8 * Author: Richard Purdie <rpurdie@openedhand.com>
9 *
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>
14 *
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.
18 *
19 */
20
21 #include <linux/module.h>
22 #include <linux/jiffies.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/spinlock.h>
27 #include <linux/device.h>
28 #include <linux/sysdev.h>
29 #include <linux/timer.h>
30 #include <linux/ctype.h>
31 #include <linux/leds.h>
32
33 #include "leds.h"
34
35 #define MORSE_DELAY_BASE (HZ/2)
36
37 #define MORSE_STATE_BLINK_START 0
38 #define MORSE_STATE_BLINK_STOP 1
39
40 #define MORSE_DIT_LEN 1
41 #define MORSE_DAH_LEN 3
42 #define MORSE_SPACE_LEN 7
43
44 struct morse_trig_data {
45 unsigned long delay;
46 char *msg;
47
48 unsigned char morse;
49 unsigned char state;
50 char *msgpos;
51 struct timer_list timer;
52 };
53
54 const unsigned char morsetable[] = {
55 0122, 0, 0310, 0, 0, 0163, /* "#$%&' */
56 055, 0155, 0, 0, 0163, 0141, 0152, 0051, /* ()*+,-./ */
57 077, 076, 074, 070, 060, 040, 041, 043, 047, 057, /* 0-9 */
58 0107, 0125, 0, 0061, 0, 0114, 0, /* :;<=>?@ */
59 006, 021, 025, 011, 002, 024, 013, 020, 004, /* A-I */
60 036, 015, 022, 007, 005, 017, 026, 033, 012, /* J-R */
61 010, 003, 014, 030, 016, 031, 035, 023, /* S-Z */
62 0, 0, 0, 0, 0154 /* [\]^_ */
63 };
64
65 static inline unsigned char tomorse(char c) {
66 if (c >= 'a' && c <= 'z')
67 c = c - 'a' + 'A';
68 if (c >= '"' && c <= '_') {
69 return morsetable[c - '"'];
70 } else
71 return 0;
72 }
73
74 static inline unsigned long dit_len(struct morse_trig_data *morse_data)
75 {
76 return MORSE_DIT_LEN*morse_data->delay;
77 }
78
79 static inline unsigned long dah_len(struct morse_trig_data *morse_data)
80 {
81 return MORSE_DAH_LEN*morse_data->delay;
82 }
83
84 static inline unsigned long space_len(struct morse_trig_data *morse_data)
85 {
86 return MORSE_SPACE_LEN*morse_data->delay;
87 }
88
89 static void morse_timer_function(unsigned long data)
90 {
91 struct led_classdev *led_cdev = (struct led_classdev *) data;
92 struct morse_trig_data *morse_data = led_cdev->trigger_data;
93 unsigned long brightness = LED_OFF;
94 unsigned long delay = 0;
95
96 if (!morse_data->msg)
97 goto set_led;
98
99 switch (morse_data->state) {
100 case MORSE_STATE_BLINK_START:
101 /* Starting a new blink. We have a valid code in morse. */
102 delay = (morse_data->morse & 001) ? dah_len(morse_data):
103 dit_len(morse_data);
104 brightness = LED_FULL;
105 morse_data->state = MORSE_STATE_BLINK_STOP;
106 morse_data->morse >>= 1;
107 break;
108 case MORSE_STATE_BLINK_STOP:
109 /* Coming off of a blink. */
110 morse_data->state = MORSE_STATE_BLINK_START;
111
112 if (morse_data->morse > 1) {
113 /* Not done yet, just a one-dit pause. */
114 delay = dit_len(morse_data);
115 break;
116 }
117
118 /* Get a new char, figure out how much space. */
119 /* First time through */
120 if (!morse_data->msgpos)
121 morse_data->msgpos = (char *)morse_data->msg;
122
123 if (!*morse_data->msgpos) {
124 /* Repeating */
125 morse_data->msgpos = (char *)morse_data->msg;
126 delay = space_len(morse_data);
127 } else {
128 /* Inter-letter space */
129 delay = dah_len(morse_data);
130 }
131
132 if (!(morse_data->morse = tomorse(*morse_data->msgpos))) {
133 delay = space_len(morse_data);
134 /* And get us back here */
135 morse_data->state = MORSE_STATE_BLINK_STOP;
136 }
137 morse_data->msgpos++;
138 break;
139 }
140
141 mod_timer(&morse_data->timer, jiffies + msecs_to_jiffies(delay));
142
143 set_led:
144 led_set_brightness(led_cdev, brightness);
145 }
146
147 static ssize_t morse_delay_show(struct class_device *dev, char *buf)
148 {
149 struct led_classdev *led_cdev = class_get_devdata(dev);
150 struct morse_trig_data *morse_data = led_cdev->trigger_data;
151
152 sprintf(buf, "%lu\n", morse_data->delay);
153
154 return strlen(buf) + 1;
155 }
156
157 static ssize_t morse_delay_store(struct class_device *dev, const char *buf,
158 size_t size)
159 {
160 struct led_classdev *led_cdev = class_get_devdata(dev);
161 struct morse_trig_data *morse_data = led_cdev->trigger_data;
162 char *after;
163 unsigned long state = simple_strtoul(buf, &after, 10);
164 size_t count = after - buf;
165 int ret = -EINVAL;
166
167 if (*after && isspace(*after))
168 count++;
169
170 if (count == size) {
171 morse_data->delay = state;
172 mod_timer(&morse_data->timer, jiffies + 1);
173 ret = count;
174 }
175
176 return ret;
177 }
178
179 static ssize_t morse_msg_show(struct class_device *dev, char *buf)
180 {
181 struct led_classdev *led_cdev = class_get_devdata(dev);
182 struct morse_trig_data *morse_data = led_cdev->trigger_data;
183
184 if (!morse_data->msg)
185 sprintf(buf, "<none>\n");
186 else
187 sprintf(buf, "%s\n", morse_data->msg);
188
189 return strlen(buf) + 1;
190 }
191
192 static ssize_t morse_msg_store(struct class_device *dev, const char *buf,
193 size_t size)
194 {
195 struct led_classdev *led_cdev = class_get_devdata(dev);
196 struct morse_trig_data *morse_data = led_cdev->trigger_data;
197 char *m;
198
199 m = kmalloc(size, GFP_KERNEL);
200 if (!m)
201 return -ENOMEM;
202
203 memcpy(m,buf,size);
204 m[size]='\0';
205
206 if (morse_data->msg)
207 kfree(morse_data->msg);
208
209 morse_data->msg = m;
210 morse_data->msgpos = NULL;
211 morse_data->state = MORSE_STATE_BLINK_STOP;
212
213 mod_timer(&morse_data->timer, jiffies + 1);
214
215 return size;
216 }
217
218 static CLASS_DEVICE_ATTR(delay, 0644, morse_delay_show, morse_delay_store);
219 static CLASS_DEVICE_ATTR(message, 0644, morse_msg_show, morse_msg_store);
220
221 static void morse_trig_activate(struct led_classdev *led_cdev)
222 {
223 struct morse_trig_data *morse_data;
224 int rc;
225
226 morse_data = kzalloc(sizeof(*morse_data), GFP_KERNEL);
227 if (!morse_data)
228 return;
229
230 morse_data->delay = MORSE_DELAY_BASE;
231 init_timer(&morse_data->timer);
232 morse_data->timer.function = morse_timer_function;
233 morse_data->timer.data = (unsigned long)led_cdev;
234
235 rc = class_device_create_file(led_cdev->class_dev,
236 &class_device_attr_delay);
237 if (rc) goto err;
238
239 rc = class_device_create_file(led_cdev->class_dev,
240 &class_device_attr_message);
241 if (rc) goto err_delay;
242
243 led_cdev->trigger_data = morse_data;
244
245 return;
246
247 err_delay:
248 class_device_remove_file(led_cdev->class_dev,
249 &class_device_attr_delay);
250 err:
251 kfree(morse_data);
252 }
253
254 static void morse_trig_deactivate(struct led_classdev *led_cdev)
255 {
256 struct morse_trig_data *morse_data = led_cdev->trigger_data;
257
258 if (!morse_data)
259 return;
260
261 class_device_remove_file(led_cdev->class_dev,
262 &class_device_attr_message);
263 class_device_remove_file(led_cdev->class_dev,
264 &class_device_attr_delay);
265
266 del_timer_sync(&morse_data->timer);
267 if (morse_data->msg)
268 kfree(morse_data->msg);
269
270 kfree(morse_data);
271 }
272
273 static struct led_trigger morse_led_trigger = {
274 .name = "morse",
275 .activate = morse_trig_activate,
276 .deactivate = morse_trig_deactivate,
277 };
278
279 static int __init morse_trig_init(void)
280 {
281 return led_trigger_register(&morse_led_trigger);
282 }
283
284 static void __exit morse_trig_exit(void)
285 {
286 led_trigger_unregister(&morse_led_trigger);
287 }
288
289 module_init(morse_trig_init);
290 module_exit(morse_trig_exit);
291
292 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
293 MODULE_DESCRIPTION("Morse LED trigger");
294 MODULE_LICENSE("GPL");