lua: Fixed some cross-platform issues for PPC (and probably other architectures)
[openwrt.git] / target / linux / generic-2.6 / files-2.6.25 / 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/kernel.h>
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/jiffies.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/spinlock.h>
28 #include <linux/device.h>
29 #include <linux/sysdev.h>
30 #include <linux/timer.h>
31 #include <linux/ctype.h>
32 #include <linux/leds.h>
33
34 #include "leds.h"
35
36 #define MORSE_DELAY_BASE (HZ/2)
37
38 #define MORSE_STATE_BLINK_START 0
39 #define MORSE_STATE_BLINK_STOP 1
40
41 #define MORSE_DIT_LEN 1
42 #define MORSE_DAH_LEN 3
43 #define MORSE_SPACE_LEN 7
44
45 struct morse_trig_data {
46 unsigned long delay;
47 char *msg;
48
49 unsigned char morse;
50 unsigned char state;
51 char *msgpos;
52 struct timer_list timer;
53 };
54
55 const unsigned char morsetable[] = {
56 0122, 0, 0310, 0, 0, 0163, /* "#$%&' */
57 055, 0155, 0, 0, 0163, 0141, 0152, 0051, /* ()*+,-./ */
58 077, 076, 074, 070, 060, 040, 041, 043, 047, 057, /* 0-9 */
59 0107, 0125, 0, 0061, 0, 0114, 0, /* :;<=>?@ */
60 006, 021, 025, 011, 002, 024, 013, 020, 004, /* A-I */
61 036, 015, 022, 007, 005, 017, 026, 033, 012, /* J-R */
62 010, 003, 014, 030, 016, 031, 035, 023, /* S-Z */
63 0, 0, 0, 0, 0154 /* [\]^_ */
64 };
65
66 static inline unsigned char tomorse(char c) {
67 if (c >= 'a' && c <= 'z')
68 c = c - 'a' + 'A';
69 if (c >= '"' && c <= '_') {
70 return morsetable[c - '"'];
71 } else
72 return 0;
73 }
74
75 static inline unsigned long dit_len(struct morse_trig_data *morse_data)
76 {
77 return MORSE_DIT_LEN*morse_data->delay;
78 }
79
80 static inline unsigned long dah_len(struct morse_trig_data *morse_data)
81 {
82 return MORSE_DAH_LEN*morse_data->delay;
83 }
84
85 static inline unsigned long space_len(struct morse_trig_data *morse_data)
86 {
87 return MORSE_SPACE_LEN*morse_data->delay;
88 }
89
90 static void morse_timer_function(unsigned long data)
91 {
92 struct led_classdev *led_cdev = (struct led_classdev *)data;
93 struct morse_trig_data *morse_data = led_cdev->trigger_data;
94 unsigned long brightness = LED_OFF;
95 unsigned long delay = 0;
96
97 if (!morse_data->msg)
98 goto set_led;
99
100 switch (morse_data->state) {
101 case MORSE_STATE_BLINK_START:
102 /* Starting a new blink. We have a valid code in morse. */
103 delay = (morse_data->morse & 001) ? dah_len(morse_data):
104 dit_len(morse_data);
105 brightness = LED_FULL;
106 morse_data->state = MORSE_STATE_BLINK_STOP;
107 morse_data->morse >>= 1;
108 break;
109 case MORSE_STATE_BLINK_STOP:
110 /* Coming off of a blink. */
111 morse_data->state = MORSE_STATE_BLINK_START;
112
113 if (morse_data->morse > 1) {
114 /* Not done yet, just a one-dit pause. */
115 delay = dit_len(morse_data);
116 break;
117 }
118
119 /* Get a new char, figure out how much space. */
120 /* First time through */
121 if (!morse_data->msgpos)
122 morse_data->msgpos = (char *)morse_data->msg;
123
124 if (!*morse_data->msgpos) {
125 /* Repeating */
126 morse_data->msgpos = (char *)morse_data->msg;
127 delay = space_len(morse_data);
128 } else {
129 /* Inter-letter space */
130 delay = dah_len(morse_data);
131 }
132
133 if (!(morse_data->morse = tomorse(*morse_data->msgpos))) {
134 delay = space_len(morse_data);
135 /* And get us back here */
136 morse_data->state = MORSE_STATE_BLINK_STOP;
137 }
138 morse_data->msgpos++;
139 break;
140 }
141
142 mod_timer(&morse_data->timer, jiffies + msecs_to_jiffies(delay));
143
144 set_led:
145 led_set_brightness(led_cdev, brightness);
146 }
147
148 static ssize_t _morse_delay_show(struct led_classdev *led_cdev, char *buf)
149 {
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 led_classdev *led_cdev,
158 const char *buf, size_t size)
159 {
160 struct morse_trig_data *morse_data = led_cdev->trigger_data;
161 char *after;
162 unsigned long state = simple_strtoul(buf, &after, 10);
163 size_t count = after - buf;
164 int ret = -EINVAL;
165
166 if (*after && isspace(*after))
167 count++;
168
169 if (count == size) {
170 morse_data->delay = state;
171 mod_timer(&morse_data->timer, jiffies + 1);
172 ret = count;
173 }
174
175 return ret;
176 }
177
178 static ssize_t _morse_msg_show(struct led_classdev *led_cdev, char *buf)
179 {
180 struct morse_trig_data *morse_data = led_cdev->trigger_data;
181
182 if (!morse_data->msg)
183 sprintf(buf, "<none>\n");
184 else
185 sprintf(buf, "%s\n", morse_data->msg);
186
187 return strlen(buf) + 1;
188 }
189
190 static ssize_t _morse_msg_store(struct led_classdev *led_cdev,
191 const char *buf, size_t size)
192 {
193 struct morse_trig_data *morse_data = led_cdev->trigger_data;
194 char *m;
195
196 m = kmalloc(size, GFP_KERNEL);
197 if (!m)
198 return -ENOMEM;
199
200 memcpy(m,buf,size);
201 m[size]='\0';
202
203 if (morse_data->msg)
204 kfree(morse_data->msg);
205
206 morse_data->msg = m;
207 morse_data->msgpos = NULL;
208 morse_data->state = MORSE_STATE_BLINK_STOP;
209
210 mod_timer(&morse_data->timer, jiffies + 1);
211
212 return size;
213 }
214
215 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
216 static ssize_t morse_delay_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
218 {
219 struct led_classdev *led_cdev = dev_get_drvdata(dev);
220
221 return _morse_delay_show(led_cdev, buf);
222 }
223
224 static ssize_t morse_delay_store(struct device *dev,
225 struct device_attribute *attr, const char *buf, size_t size)
226 {
227 struct led_classdev *led_cdev = dev_get_drvdata(dev);
228
229 return _morse_delay_store(led_cdev, buf, size);
230 }
231
232 static ssize_t morse_msg_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234 {
235 struct led_classdev *led_cdev = dev_get_drvdata(dev);
236
237 return _morse_msg_show(led_cdev, buf);
238 }
239
240 static ssize_t morse_msg_store(struct device *dev,
241 struct device_attribute *attr, const char *buf, size_t size)
242 {
243 struct led_classdev *led_cdev = dev_get_drvdata(dev);
244
245 return _morse_msg_store(led_cdev, buf, size);
246 }
247
248 static DEVICE_ATTR(delay, 0644, morse_delay_show, morse_delay_store);
249 static DEVICE_ATTR(message, 0644, morse_msg_show, morse_msg_store);
250
251 #define led_device_create_file(leddev, attr) \
252 device_create_file(leddev->dev, &dev_attr_ ## attr)
253 #define led_device_remove_file(leddev, attr) \
254 device_remove_file(leddev->dev, &dev_attr_ ## attr)
255
256 #else
257 static ssize_t morse_delay_show(struct class_device *dev, char *buf)
258 {
259 struct led_classdev *led_cdev = class_get_devdata(dev);
260
261 return _morse_delay_show(led_cdev, buf);
262 }
263
264 static ssize_t morse_delay_store(struct class_device *dev, const char *buf,
265 size_t size)
266 {
267 struct led_classdev *led_cdev = class_get_devdata(dev);
268
269 return _morse_delay_store(led_cdev, buf, size);
270 }
271
272 static ssize_t morse_msg_show(struct class_device *dev, char *buf)
273 {
274 struct led_classdev *led_cdev = class_get_devdata(dev);
275
276 return _morse_msg_show(led_cdev, buf);
277 }
278
279 static ssize_t morse_msg_store(struct class_device *dev, const char *buf,
280 size_t size)
281 {
282 struct led_classdev *led_cdev = class_get_devdata(dev);
283
284 return _morse_msg_store(led_cdev, buf, size);
285 }
286
287 static CLASS_DEVICE_ATTR(delay, 0644, morse_delay_show, morse_delay_store);
288 static CLASS_DEVICE_ATTR(message, 0644, morse_msg_show, morse_msg_store);
289
290 #define led_device_create_file(leddev, attr) \
291 class_device_create_file(leddev->class_dev, &class_device_attr_ ## attr)
292 #define led_device_remove_file(leddev, attr) \
293 class_device_remove_file(leddev->class_dev, &class_device_attr_ ## attr)
294
295 #endif
296
297 static void morse_trig_activate(struct led_classdev *led_cdev)
298 {
299 struct morse_trig_data *morse_data;
300 int rc;
301
302 morse_data = kzalloc(sizeof(*morse_data), GFP_KERNEL);
303 if (!morse_data)
304 return;
305
306 morse_data->delay = MORSE_DELAY_BASE;
307 init_timer(&morse_data->timer);
308 morse_data->timer.function = morse_timer_function;
309 morse_data->timer.data = (unsigned long)led_cdev;
310
311 rc = led_device_create_file(led_cdev, delay);
312 if (rc) goto err;
313
314 rc = led_device_create_file(led_cdev, message);
315 if (rc) goto err_delay;
316
317 led_cdev->trigger_data = morse_data;
318
319 return;
320
321 err_delay:
322 led_device_remove_file(led_cdev, delay);
323 err:
324 kfree(morse_data);
325 }
326
327 static void morse_trig_deactivate(struct led_classdev *led_cdev)
328 {
329 struct morse_trig_data *morse_data = led_cdev->trigger_data;
330
331 if (!morse_data)
332 return;
333
334 led_device_remove_file(led_cdev, message);
335 led_device_remove_file(led_cdev, delay);
336
337 del_timer_sync(&morse_data->timer);
338 if (morse_data->msg)
339 kfree(morse_data->msg);
340
341 kfree(morse_data);
342 }
343
344 static struct led_trigger morse_led_trigger = {
345 .name = "morse",
346 .activate = morse_trig_activate,
347 .deactivate = morse_trig_deactivate,
348 };
349
350 static int __init morse_trig_init(void)
351 {
352 return led_trigger_register(&morse_led_trigger);
353 }
354
355 static void __exit morse_trig_exit(void)
356 {
357 led_trigger_unregister(&morse_led_trigger);
358 }
359
360 module_init(morse_trig_init);
361 module_exit(morse_trig_exit);
362
363 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
364 MODULE_DESCRIPTION("Morse LED trigger");
365 MODULE_LICENSE("GPL");
This page took 0.068149 seconds and 5 git commands to generate.