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