summaryrefslogtreecommitdiffstats
path: root/target/linux/generic-2.6/files-2.6.28/drivers/leds/ledtrig-morse.c
blob: cb47480a3631dce833a266eac93562692e7bff8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 *  LED Morse Trigger
 *
 *  Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
 *
 *  This file was based on: drivers/led/ledtrig-timer.c
 *	Copyright 2005-2006 Openedhand Ltd.
 *	Author: Richard Purdie <rpurdie@openedhand.com>
 *
 *  also based on the patch '[PATCH] 2.5.59 morse code panics' posted
 *  in the LKML by Tomas Szepe at Thu, 30 Jan 2003
 *	Copyright (C) 2002 Andrew Rodland <arodland@noln.com>
 *	Copyright (C) 2003 Tomas Szepe <szepe@pinerecords.com>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/leds.h>

#include "leds.h"

#define MORSE_DELAY_BASE	(HZ/2)

#define MORSE_STATE_BLINK_START	0
#define MORSE_STATE_BLINK_STOP	1

#define MORSE_DIT_LEN	1
#define MORSE_DAH_LEN	3
#define MORSE_SPACE_LEN	7

struct morse_trig_data {
	unsigned long delay;
	char *msg;

	unsigned char morse;
	unsigned char state;
	char *msgpos;
	struct timer_list timer;
};

const unsigned char morsetable[] = {
	0122, 0, 0310, 0, 0, 0163,				/* "#$%&' */
	055, 0155, 0, 0, 0163, 0141, 0152, 0051, 		/* ()*+,-./ */
	077, 076, 074, 070, 060, 040, 041, 043, 047, 057,	/* 0-9 */
	0107, 0125, 0, 0061, 0, 0114, 0, 			/* :;<=>?@ */
	006, 021, 025, 011, 002, 024, 013, 020, 004,		/* A-I */
	036, 015, 022, 007, 005, 017, 026, 033, 012,		/* J-R */
	010, 003, 014, 030, 016, 031, 035, 023,			/* S-Z */
	0, 0, 0, 0, 0154					/* [\]^_ */
};

static inline unsigned char tomorse(char c) {
	if (c >= 'a' && c <= 'z')
		c = c - 'a' + 'A';
	if (c >= '"' && c <= '_') {
		return morsetable[c - '"'];
	} else
		return 0;
}

static inline unsigned long dit_len(struct morse_trig_data *morse_data)
{
	return MORSE_DIT_LEN*morse_data->delay;
}

static inline unsigned long dah_len(struct morse_trig_data *morse_data)
{
	return MORSE_DAH_LEN*morse_data->delay;
}

static inline unsigned long space_len(struct morse_trig_data *morse_data)
{
	return MORSE_SPACE_LEN*morse_data->delay;
}

static void morse_timer_function(unsigned long data)
{
	struct led_classdev *led_cdev = (struct led_classdev *)data;
	struct morse_trig_data *morse_data = led_cdev->trigger_data;
	unsigned long brightness = LED_OFF;
	unsigned long delay = 0;

	if (!morse_data->msg)
		goto set_led;

	switch (morse_data->state) {
	case MORSE_STATE_BLINK_START:
		/* Starting a new blink.  We have a valid code in morse. */
		delay = (morse_data->morse & 001) ? dah_len(morse_data):
			dit_len(morse_data);
		brightness = LED_FULL;
		morse_data->state = MORSE_STATE_BLINK_STOP;
		morse_data->morse >>= 1;
		break;
	case MORSE_STATE_BLINK_STOP:
		/* Coming off of a blink. */
		morse_data->state = MORSE_STATE_BLINK_START;

		if (morse_data->morse > 1) {
			/* Not done yet, just a one-dit pause. */
			delay = dit_len(morse_data);
			break;
		}

		/* Get a new char, figure out how much space. */
		/* First time through */
		if (!morse_data->msgpos)
			morse_data->msgpos = (char *)morse_data->msg;

		if (!*morse_data->msgpos) {
			/* Repeating */
			morse_data->msgpos = (char *)morse_data->msg;
			delay = space_len(morse_data);
		} else {
			/* Inter-letter space */
			delay = dah_len(morse_data);
		}

		if (!(morse_data->morse = tomorse(*morse_data->msgpos))) {
			delay = space_len(morse_data);
			/* And get us back here */
			morse_data->state = MORSE_STATE_BLINK_STOP;
		}
		morse_data->msgpos++;
		break;
	}

	mod_timer(&morse_data->timer, jiffies + msecs_to_jiffies(delay));

set_led:
	led_set_brightness(led_cdev, brightness);
}

static ssize_t _morse_delay_show(struct led_classdev *led_cdev, char *buf)
{
	struct morse_trig_data *morse_data = led_cdev->trigger_data;

	sprintf(buf, "%lu\n", morse_data->delay);

	return strlen(buf) + 1;
}

static ssize_t _morse_delay_store(struct led_classdev *led_cdev,
		const char *buf, size_t size)
{
	struct morse_trig_data *morse_data = led_cdev->trigger_data;
	char *after;
	unsigned long state = simple_strtoul(buf, &after, 10);
	size_t count = after - buf;
	int ret = -EINVAL;

	if (*after && isspace(*after))
		count++;

	if (count == size) {
		morse_data->delay = state;
		mod_timer(&morse_data->timer, jiffies + 1);
		ret = count;
	}

	return ret;
}

static ssize_t _morse_msg_show(struct led_classdev *led_cdev, char *buf)
{
	struct morse_trig_data *morse_data = led_cdev->trigger_data;

	if (!morse_data->msg)
		sprintf(buf, "<none>\n");
	else
		sprintf(buf, "%s\n", morse_data->msg);

	return strlen(buf) + 1;
}

static ssize_t _morse_msg_store(struct led_classdev *led_cdev,
		const char *buf, size_t size)
{
	struct morse_trig_data *morse_data = led_cdev->trigger_data;
	char *m;

	m = kmalloc(size, GFP_KERNEL);
	if (!m)
		return -ENOMEM;

	memcpy(m,buf,size);
	m[size]='\0';

	if (morse_data->msg)
		kfree(morse_data->msg);

	morse_data->msg = m;
	morse_data->msgpos = NULL;
	morse_data->state = MORSE_STATE_BLINK_STOP;

	mod_timer(&morse_data->timer, jiffies + 1);

	return size;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
static ssize_t morse_delay_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	return _morse_delay_show(led_cdev, buf);
}

static ssize_t morse_delay_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	return _morse_delay_store(led_cdev, buf, size);
}

static ssize_t morse_msg_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	return _morse_msg_show(led_cdev, buf);
}

static ssize_t morse_msg_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	return _morse_msg_store(led_cdev, buf, size);
}

static DEVICE_ATTR(delay, 0644, morse_delay_show, morse_delay_store);
static DEVICE_ATTR(message, 0644, morse_msg_show, morse_msg_store);

#define led_device_create_file(leddev, attr) \
	device_create_file(leddev->dev, &dev_attr_ ## attr)
#define led_device_remove_file(leddev, attr) \
	device_remove_file(leddev->dev, &dev_attr_ ## attr)

#else
static ssize_t morse_delay_show(struct class_device *dev, char *buf)
{
	struct led_classdev *led_cdev = class_get_devdata(dev);

	return _morse_delay_show(led_cdev, buf);
}

static ssize_t morse_delay_store(struct class_device *dev, const char *buf,
		size_t size)
{
	struct led_classdev *led_cdev = class_get_devdata(dev);

	return _morse_delay_store(led_cdev, buf, size);
}

static ssize_t morse_msg_show(struct class_device *dev, char *buf)
{
	struct led_classdev *led_cdev = class_get_devdata(dev);

	return _morse_msg_show(led_cdev, buf);
}

static ssize_t morse_msg_store(struct class_device *dev, const char *buf,
				size_t size)
{
	struct led_classdev *led_cdev = class_get_devdata(dev);

	return _morse_msg_store(led_cdev, buf, size);
}

static CLASS_DEVICE_ATTR(delay, 0644, morse_delay_show, morse_delay_store);
static CLASS_DEVICE_ATTR(message, 0644, morse_msg_show, morse_msg_store);

#define led_device_create_file(leddev, attr) \
	class_device_create_file(leddev->class_dev, &class_device_attr_ ## attr)
#define led_device_remove_file(leddev, attr) \
	class_device_remove_file(leddev->class_dev, &class_device_attr_ ## attr)

#endif

static void morse_trig_activate(struct led_classdev *led_cdev)
{
	struct morse_trig_data *morse_data;
	int rc;

	morse_data = kzalloc(sizeof(*morse_data), GFP_KERNEL);
	if (!morse_data)
		return;

	morse_data->delay = MORSE_DELAY_BASE;
	init_timer(&morse_data->timer);
	morse_data->timer.function = morse_timer_function;
	morse_data->timer.data = (unsigned long)led_cdev;

	rc = led_device_create_file(led_cdev, delay);
	if (rc) goto err;

	rc = led_device_create_file(led_cdev, message);
	if (rc) goto err_delay;

	led_cdev->trigger_data = morse_data;

	return;

err_delay:
	led_device_remove_file(led_cdev, delay);
err:
	kfree(morse_data);
}

static void morse_trig_deactivate(struct led_classdev *led_cdev)
{
	struct morse_trig_data *morse_data = led_cdev->trigger_data;

	if (!morse_data)
		return;

	led_device_remove_file(led_cdev, message);
	led_device_remove_file(led_cdev, delay);

	del_timer_sync(&morse_data->timer);
	if (morse_data->msg)
		kfree(morse_data->msg);

	kfree(morse_data);
}

static struct led_trigger morse_led_trigger = {
	.name		= "morse",
	.activate	= morse_trig_activate,
	.deactivate	= morse_trig_deactivate,
};

static int __init morse_trig_init(void)
{
	return led_trigger_register(&morse_led_trigger);
}

static void __exit morse_trig_exit(void)
{
	led_trigger_unregister(&morse_led_trigger);
}

module_init(morse_trig_init);
module_exit(morse_trig_exit);

MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
MODULE_DESCRIPTION("Morse LED trigger");
MODULE_LICENSE("GPL");