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/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>
35 #define MORSE_DELAY_BASE (HZ/2)
37 #define MORSE_STATE_BLINK_START 0
38 #define MORSE_STATE_BLINK_STOP 1
40 #define MORSE_DIT_LEN 1
41 #define MORSE_DAH_LEN 3
42 #define MORSE_SPACE_LEN 7
44 struct morse_trig_data
{
51 struct timer_list timer
;
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 /* [\]^_ */
65 static inline unsigned char tomorse(char c
) {
66 if (c
>= 'a' && c
<= 'z')
68 if (c
>= '"' && c
<= '_') {
69 return morsetable
[c
- '"'];
74 static inline unsigned long dit_len(struct morse_trig_data
*morse_data
)
76 return MORSE_DIT_LEN
*morse_data
->delay
;
79 static inline unsigned long dah_len(struct morse_trig_data
*morse_data
)
81 return MORSE_DAH_LEN
*morse_data
->delay
;
84 static inline unsigned long space_len(struct morse_trig_data
*morse_data
)
86 return MORSE_SPACE_LEN
*morse_data
->delay
;
89 static void morse_timer_function(unsigned long data
)
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;
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
):
104 brightness
= LED_FULL
;
105 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
106 morse_data
->morse
>>= 1;
108 case MORSE_STATE_BLINK_STOP
:
109 /* Coming off of a blink. */
110 morse_data
->state
= MORSE_STATE_BLINK_START
;
112 if (morse_data
->morse
> 1) {
113 /* Not done yet, just a one-dit pause. */
114 delay
= dit_len(morse_data
);
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
;
123 if (!*morse_data
->msgpos
) {
125 morse_data
->msgpos
= (char *)morse_data
->msg
;
126 delay
= space_len(morse_data
);
128 /* Inter-letter space */
129 delay
= dah_len(morse_data
);
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
;
137 morse_data
->msgpos
++;
141 mod_timer(&morse_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
144 led_set_brightness(led_cdev
, brightness
);
147 static ssize_t
morse_delay_show(struct class_device
*dev
, char *buf
)
149 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
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 class_device
*dev
, const char *buf
,
160 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
161 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
163 unsigned long state
= simple_strtoul(buf
, &after
, 10);
164 size_t count
= after
- buf
;
167 if (*after
&& isspace(*after
))
171 morse_data
->delay
= state
;
172 mod_timer(&morse_data
->timer
, jiffies
+ 1);
179 static ssize_t
morse_msg_show(struct class_device
*dev
, char *buf
)
181 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
182 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
184 if (!morse_data
->msg
)
185 sprintf(buf
, "<none>\n");
187 sprintf(buf
, "%s\n", morse_data
->msg
);
189 return strlen(buf
) + 1;
192 static ssize_t
morse_msg_store(struct class_device
*dev
, const char *buf
,
195 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
196 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
199 m
= kmalloc(size
, GFP_KERNEL
);
207 kfree(morse_data
->msg
);
210 morse_data
->msgpos
= NULL
;
211 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
213 mod_timer(&morse_data
->timer
, jiffies
+ 1);
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
);
221 static void morse_trig_activate(struct led_classdev
*led_cdev
)
223 struct morse_trig_data
*morse_data
;
226 morse_data
= kzalloc(sizeof(*morse_data
), GFP_KERNEL
);
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
;
235 rc
= class_device_create_file(led_cdev
->class_dev
,
236 &class_device_attr_delay
);
239 rc
= class_device_create_file(led_cdev
->class_dev
,
240 &class_device_attr_message
);
241 if (rc
) goto err_delay
;
243 led_cdev
->trigger_data
= morse_data
;
248 class_device_remove_file(led_cdev
->class_dev
,
249 &class_device_attr_delay
);
254 static void morse_trig_deactivate(struct led_classdev
*led_cdev
)
256 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
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
);
266 del_timer_sync(&morse_data
->timer
);
268 kfree(morse_data
->msg
);
273 static struct led_trigger morse_led_trigger
= {
275 .activate
= morse_trig_activate
,
276 .deactivate
= morse_trig_deactivate
,
279 static int __init
morse_trig_init(void)
281 return led_trigger_register(&morse_led_trigger
);
284 static void __exit
morse_trig_exit(void)
286 led_trigger_unregister(&morse_led_trigger
);
289 module_init(morse_trig_init
);
290 module_exit(morse_trig_exit
);
292 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
293 MODULE_DESCRIPTION("Morse LED trigger");
294 MODULE_LICENSE("GPL");