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>
33 #include <linux/slab.h>
37 #define MORSE_DELAY_BASE (HZ/2)
39 #define MORSE_STATE_BLINK_START 0
40 #define MORSE_STATE_BLINK_STOP 1
42 #define MORSE_DIT_LEN 1
43 #define MORSE_DAH_LEN 3
44 #define MORSE_SPACE_LEN 7
46 struct morse_trig_data
{
53 struct timer_list timer
;
56 const unsigned char morsetable
[] = {
57 0122, 0, 0310, 0, 0, 0163, /* "#$%&' */
58 055, 0155, 0, 0, 0163, 0141, 0152, 0051, /* ()*+,-./ */
59 077, 076, 074, 070, 060, 040, 041, 043, 047, 057, /* 0-9 */
60 0107, 0125, 0, 0061, 0, 0114, 0, /* :;<=>?@ */
61 006, 021, 025, 011, 002, 024, 013, 020, 004, /* A-I */
62 036, 015, 022, 007, 005, 017, 026, 033, 012, /* J-R */
63 010, 003, 014, 030, 016, 031, 035, 023, /* S-Z */
64 0, 0, 0, 0, 0154 /* [\]^_ */
67 static inline unsigned char tomorse(char c
) {
68 if (c
>= 'a' && c
<= 'z')
70 if (c
>= '"' && c
<= '_') {
71 return morsetable
[c
- '"'];
76 static inline unsigned long dit_len(struct morse_trig_data
*morse_data
)
78 return MORSE_DIT_LEN
*morse_data
->delay
;
81 static inline unsigned long dah_len(struct morse_trig_data
*morse_data
)
83 return MORSE_DAH_LEN
*morse_data
->delay
;
86 static inline unsigned long space_len(struct morse_trig_data
*morse_data
)
88 return MORSE_SPACE_LEN
*morse_data
->delay
;
91 static void morse_timer_function(unsigned long data
)
93 struct led_classdev
*led_cdev
= (struct led_classdev
*)data
;
94 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
95 unsigned long brightness
= LED_OFF
;
96 unsigned long delay
= 0;
101 switch (morse_data
->state
) {
102 case MORSE_STATE_BLINK_START
:
103 /* Starting a new blink. We have a valid code in morse. */
104 delay
= (morse_data
->morse
& 001) ? dah_len(morse_data
):
106 brightness
= LED_FULL
;
107 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
108 morse_data
->morse
>>= 1;
110 case MORSE_STATE_BLINK_STOP
:
111 /* Coming off of a blink. */
112 morse_data
->state
= MORSE_STATE_BLINK_START
;
114 if (morse_data
->morse
> 1) {
115 /* Not done yet, just a one-dit pause. */
116 delay
= dit_len(morse_data
);
120 /* Get a new char, figure out how much space. */
121 /* First time through */
122 if (!morse_data
->msgpos
)
123 morse_data
->msgpos
= (char *)morse_data
->msg
;
125 if (!*morse_data
->msgpos
) {
127 morse_data
->msgpos
= (char *)morse_data
->msg
;
128 delay
= space_len(morse_data
);
130 /* Inter-letter space */
131 delay
= dah_len(morse_data
);
134 if (!(morse_data
->morse
= tomorse(*morse_data
->msgpos
))) {
135 delay
= space_len(morse_data
);
136 /* And get us back here */
137 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
139 morse_data
->msgpos
++;
143 mod_timer(&morse_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
146 led_set_brightness(led_cdev
, brightness
);
149 static ssize_t
_morse_delay_show(struct led_classdev
*led_cdev
, char *buf
)
151 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
153 sprintf(buf
, "%lu\n", morse_data
->delay
);
155 return strlen(buf
) + 1;
158 static ssize_t
_morse_delay_store(struct led_classdev
*led_cdev
,
159 const char *buf
, size_t size
)
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 led_classdev
*led_cdev
, char *buf
)
181 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
183 if (!morse_data
->msg
)
184 sprintf(buf
, "<none>\n");
186 sprintf(buf
, "%s\n", morse_data
->msg
);
188 return strlen(buf
) + 1;
191 static ssize_t
_morse_msg_store(struct led_classdev
*led_cdev
,
192 const char *buf
, size_t size
)
194 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
197 m
= kmalloc(size
, GFP_KERNEL
);
205 kfree(morse_data
->msg
);
208 morse_data
->msgpos
= NULL
;
209 morse_data
->state
= MORSE_STATE_BLINK_STOP
;
211 mod_timer(&morse_data
->timer
, jiffies
+ 1);
216 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
217 static ssize_t
morse_delay_show(struct device
*dev
,
218 struct device_attribute
*attr
, char *buf
)
220 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
222 return _morse_delay_show(led_cdev
, buf
);
225 static ssize_t
morse_delay_store(struct device
*dev
,
226 struct device_attribute
*attr
, const char *buf
, size_t size
)
228 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
230 return _morse_delay_store(led_cdev
, buf
, size
);
233 static ssize_t
morse_msg_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
238 return _morse_msg_show(led_cdev
, buf
);
241 static ssize_t
morse_msg_store(struct device
*dev
,
242 struct device_attribute
*attr
, const char *buf
, size_t size
)
244 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
246 return _morse_msg_store(led_cdev
, buf
, size
);
249 static DEVICE_ATTR(delay
, 0644, morse_delay_show
, morse_delay_store
);
250 static DEVICE_ATTR(message
, 0644, morse_msg_show
, morse_msg_store
);
252 #define led_device_create_file(leddev, attr) \
253 device_create_file(leddev->dev, &dev_attr_ ## attr)
254 #define led_device_remove_file(leddev, attr) \
255 device_remove_file(leddev->dev, &dev_attr_ ## attr)
258 static ssize_t
morse_delay_show(struct class_device
*dev
, char *buf
)
260 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
262 return _morse_delay_show(led_cdev
, buf
);
265 static ssize_t
morse_delay_store(struct class_device
*dev
, const char *buf
,
268 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
270 return _morse_delay_store(led_cdev
, buf
, size
);
273 static ssize_t
morse_msg_show(struct class_device
*dev
, char *buf
)
275 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
277 return _morse_msg_show(led_cdev
, buf
);
280 static ssize_t
morse_msg_store(struct class_device
*dev
, const char *buf
,
283 struct led_classdev
*led_cdev
= class_get_devdata(dev
);
285 return _morse_msg_store(led_cdev
, buf
, size
);
288 static CLASS_DEVICE_ATTR(delay
, 0644, morse_delay_show
, morse_delay_store
);
289 static CLASS_DEVICE_ATTR(message
, 0644, morse_msg_show
, morse_msg_store
);
291 #define led_device_create_file(leddev, attr) \
292 class_device_create_file(leddev->class_dev, &class_device_attr_ ## attr)
293 #define led_device_remove_file(leddev, attr) \
294 class_device_remove_file(leddev->class_dev, &class_device_attr_ ## attr)
298 static void morse_trig_activate(struct led_classdev
*led_cdev
)
300 struct morse_trig_data
*morse_data
;
303 morse_data
= kzalloc(sizeof(*morse_data
), GFP_KERNEL
);
307 morse_data
->delay
= MORSE_DELAY_BASE
;
308 init_timer(&morse_data
->timer
);
309 morse_data
->timer
.function
= morse_timer_function
;
310 morse_data
->timer
.data
= (unsigned long)led_cdev
;
312 rc
= led_device_create_file(led_cdev
, delay
);
315 rc
= led_device_create_file(led_cdev
, message
);
316 if (rc
) goto err_delay
;
318 led_cdev
->trigger_data
= morse_data
;
323 led_device_remove_file(led_cdev
, delay
);
328 static void morse_trig_deactivate(struct led_classdev
*led_cdev
)
330 struct morse_trig_data
*morse_data
= led_cdev
->trigger_data
;
335 led_device_remove_file(led_cdev
, message
);
336 led_device_remove_file(led_cdev
, delay
);
338 del_timer_sync(&morse_data
->timer
);
340 kfree(morse_data
->msg
);
345 static struct led_trigger morse_led_trigger
= {
347 .activate
= morse_trig_activate
,
348 .deactivate
= morse_trig_deactivate
,
351 static int __init
morse_trig_init(void)
353 return led_trigger_register(&morse_led_trigger
);
356 static void __exit
morse_trig_exit(void)
358 led_trigger_unregister(&morse_led_trigger
);
361 module_init(morse_trig_init
);
362 module_exit(morse_trig_exit
);
364 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
365 MODULE_DESCRIPTION("Morse LED trigger");
366 MODULE_LICENSE("GPL");