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/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>
36 #define MORSE_DELAY_BASE (HZ/2)
38 #define MORSE_STATE_BLINK_START 0
39 #define MORSE_STATE_BLINK_STOP 1
41 #define MORSE_DIT_LEN 1
42 #define MORSE_DAH_LEN 3
43 #define MORSE_SPACE_LEN 7
45 struct morse_trig_data
{
52 struct timer_list timer
;
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 /* [\]^_ */
66 static inline unsigned char tomorse(char c
) {
67 if (c
>= 'a' && c
<= 'z')
69 if (c
>= '"' && c
<= '_') {
70 return morsetable
[c
- '"'];
75 static inline unsigned long dit_len(struct morse_trig_data
*morse_data
)
77 return MORSE_DIT_LEN
*morse_data
->delay
;
80 static inline unsigned long dah_len(struct morse_trig_data
*morse_data
)
82 return MORSE_DAH_LEN
*morse_data
->delay
;
85 static inline unsigned long space_len(struct morse_trig_data
*morse_data
)
87 return MORSE_SPACE_LEN
*morse_data
->delay
;
90 static void morse_timer_function(unsigned long data
)
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;
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
):
105 brightness
= LED_FULL
;
106 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
107 morse_data
->morse
>>= 1;
109 case MORSE_STATE_BLINK_STOP
:
110 /* Coming off of a blink. */
111 morse_data
->state
= MORSE_STATE_BLINK_START
;
113 if (morse_data
->morse
> 1) {
114 /* Not done yet, just a one-dit pause. */
115 delay
= dit_len(morse_data
);
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
;
124 if (!*morse_data
->msgpos
) {
126 morse_data
->msgpos
= (char *)morse_data
->msg
;
127 delay
= space_len(morse_data
);
129 /* Inter-letter space */
130 delay
= dah_len(morse_data
);
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
;
138 morse_data
->msgpos
++;
142 mod_timer(&morse_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
145 led_set_brightness(led_cdev
, brightness
);
148 static ssize_t
_morse_delay_show(struct led_classdev
*led_cdev
, char *buf
)
150 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
152 sprintf(buf
, "%lu\n", morse_data
->delay
);
154 return strlen(buf
) + 1;
157 static ssize_t
_morse_delay_store(struct led_classdev
*led_cdev
,
158 const char *buf
, size_t size
)
160 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
162 unsigned long state
= simple_strtoul(buf
, &after
, 10);
163 size_t count
= after
- buf
;
166 if (*after
&& isspace(*after
))
170 morse_data
->delay
= state
;
171 mod_timer(&morse_data
->timer
, jiffies
+ 1);
178 static ssize_t
_morse_msg_show(struct led_classdev
*led_cdev
, char *buf
)
180 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
182 if (!morse_data
->msg
)
183 sprintf(buf
, "<none>\n");
185 sprintf(buf
, "%s\n", morse_data
->msg
);
187 return strlen(buf
) + 1;
190 static ssize_t
_morse_msg_store(struct led_classdev
*led_cdev
,
191 const char *buf
, size_t size
)
193 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
196 m
= kmalloc(size
, GFP_KERNEL
);
204 kfree(morse_data
->msg
);
207 morse_data
->msgpos
= NULL
;
208 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
210 mod_timer(&morse_data
->timer
, jiffies
+ 1);
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
)
219 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
221 return _morse_delay_show(led_cdev
, buf
);
224 static ssize_t
morse_delay_store(struct device
*dev
,
225 struct device_attribute
*attr
, const char *buf
, size_t size
)
227 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
229 return _morse_delay_store(led_cdev
, buf
, size
);
232 static ssize_t
morse_msg_show(struct device
*dev
,
233 struct device_attribute
*attr
, char *buf
)
235 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
237 return _morse_msg_show(led_cdev
, buf
);
240 static ssize_t
morse_msg_store(struct device
*dev
,
241 struct device_attribute
*attr
, const char *buf
, size_t size
)
243 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
245 return _morse_msg_store(led_cdev
, buf
, size
);
248 static DEVICE_ATTR(delay
, 0644, morse_delay_show
, morse_delay_store
);
249 static DEVICE_ATTR(message
, 0644, morse_msg_show
, morse_msg_store
);
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)
257 static ssize_t
morse_delay_show(struct class_device
*dev
, char *buf
)
259 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
261 return _morse_delay_show(led_cdev
, buf
);
264 static ssize_t
morse_delay_store(struct class_device
*dev
, const char *buf
,
267 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
269 return _morse_delay_store(led_cdev
, buf
, size
);
272 static ssize_t
morse_msg_show(struct class_device
*dev
, char *buf
)
274 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
276 return _morse_msg_show(led_cdev
, buf
);
279 static ssize_t
morse_msg_store(struct class_device
*dev
, const char *buf
,
282 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
284 return _morse_msg_store(led_cdev
, buf
, size
);
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
);
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)
297 static void morse_trig_activate(struct led_classdev
*led_cdev
)
299 struct morse_trig_data
*morse_data
;
302 morse_data
= kzalloc(sizeof(*morse_data
), GFP_KERNEL
);
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
;
311 rc
= led_device_create_file(led_cdev
, delay
);
314 rc
= led_device_create_file(led_cdev
, message
);
315 if (rc
) goto err_delay
;
317 led_cdev
->trigger_data
= morse_data
;
322 led_device_remove_file(led_cdev
, delay
);
327 static void morse_trig_deactivate(struct led_classdev
*led_cdev
)
329 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
334 led_device_remove_file(led_cdev
, message
);
335 led_device_remove_file(led_cdev
, delay
);
337 del_timer_sync(&morse_data
->timer
);
339 kfree(morse_data
->msg
);
344 static struct led_trigger morse_led_trigger
= {
346 .activate
= morse_trig_activate
,
347 .deactivate
= morse_trig_deactivate
,
350 static int __init
morse_trig_init(void)
352 return led_trigger_register(&morse_led_trigger
);
355 static void __exit
morse_trig_exit(void)
357 led_trigger_unregister(&morse_led_trigger
);
360 module_init(morse_trig_init
);
361 module_exit(morse_trig_exit
);
363 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
364 MODULE_DESCRIPTION("Morse LED trigger");
365 MODULE_LICENSE("GPL");