2003-11-03 09:47:30 +00:00
|
|
|
/*-
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
2008-05-29 12:50:46 +00:00
|
|
|
#include <sys/limits.h>
|
2003-11-03 09:47:30 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/ctype.h>
|
|
|
|
#include <sys/sbuf.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <dev/led/led.h>
|
|
|
|
#include <sys/uio.h>
|
2005-01-29 16:33:51 +00:00
|
|
|
#include <sys/sx.h>
|
2003-11-03 09:47:30 +00:00
|
|
|
|
|
|
|
struct ledsc {
|
|
|
|
LIST_ENTRY(ledsc) list;
|
2011-03-24 08:56:12 +00:00
|
|
|
char *name;
|
2003-11-03 09:47:30 +00:00
|
|
|
void *private;
|
2005-01-29 16:33:51 +00:00
|
|
|
int unit;
|
2003-11-03 09:47:30 +00:00
|
|
|
led_t *func;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2003-11-03 09:47:30 +00:00
|
|
|
struct sbuf *spec;
|
|
|
|
char *str;
|
|
|
|
char *ptr;
|
|
|
|
int count;
|
2005-01-29 16:33:51 +00:00
|
|
|
time_t last_second;
|
2003-11-03 09:47:30 +00:00
|
|
|
};
|
|
|
|
|
2005-01-29 16:33:51 +00:00
|
|
|
static struct unrhdr *led_unit;
|
2003-11-03 09:47:30 +00:00
|
|
|
static struct mtx led_mtx;
|
2005-01-29 16:33:51 +00:00
|
|
|
static struct sx led_sx;
|
2009-12-28 22:56:30 +00:00
|
|
|
static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list);
|
2005-01-29 16:33:51 +00:00
|
|
|
static struct callout led_ch;
|
2013-02-19 19:25:50 +00:00
|
|
|
static int blinkers = 0;
|
2003-11-03 09:47:30 +00:00
|
|
|
|
2005-02-10 12:02:37 +00:00
|
|
|
static MALLOC_DEFINE(M_LED, "LED", "LED driver");
|
2003-11-03 09:47:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
led_timeout(void *p)
|
|
|
|
{
|
|
|
|
struct ledsc *sc;
|
|
|
|
|
|
|
|
LIST_FOREACH(sc, &led_list, list) {
|
|
|
|
if (sc->ptr == NULL)
|
|
|
|
continue;
|
|
|
|
if (sc->count > 0) {
|
|
|
|
sc->count--;
|
|
|
|
continue;
|
|
|
|
}
|
2004-04-27 13:09:21 +00:00
|
|
|
if (*sc->ptr == '.') {
|
|
|
|
sc->ptr = NULL;
|
2013-02-19 19:25:50 +00:00
|
|
|
blinkers--;
|
2004-04-27 13:09:21 +00:00
|
|
|
continue;
|
2005-01-29 16:33:51 +00:00
|
|
|
} else if (*sc->ptr == 'U' || *sc->ptr == 'u') {
|
|
|
|
if (sc->last_second == time_second)
|
|
|
|
continue;
|
|
|
|
sc->last_second = time_second;
|
|
|
|
sc->func(sc->private, *sc->ptr == 'U');
|
2004-04-27 13:09:21 +00:00
|
|
|
} else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') {
|
2003-12-12 12:17:28 +00:00
|
|
|
sc->func(sc->private, 0);
|
2005-01-29 16:33:51 +00:00
|
|
|
sc->count = (*sc->ptr & 0xf) - 1;
|
2004-04-27 13:09:21 +00:00
|
|
|
} else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') {
|
2003-12-12 12:17:28 +00:00
|
|
|
sc->func(sc->private, 1);
|
2005-01-29 16:33:51 +00:00
|
|
|
sc->count = (*sc->ptr & 0xf) - 1;
|
2004-04-27 13:09:21 +00:00
|
|
|
}
|
2003-12-12 12:17:28 +00:00
|
|
|
sc->ptr++;
|
|
|
|
if (*sc->ptr == '\0')
|
2003-11-03 09:47:30 +00:00
|
|
|
sc->ptr = sc->str;
|
|
|
|
}
|
2013-02-19 19:25:50 +00:00
|
|
|
if (blinkers > 0)
|
|
|
|
callout_reset(&led_ch, hz / 10, led_timeout, p);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 16:33:51 +00:00
|
|
|
static int
|
2011-03-24 08:56:12 +00:00
|
|
|
led_state(struct ledsc *sc, struct sbuf **sb, int state)
|
2005-01-29 16:33:51 +00:00
|
|
|
{
|
|
|
|
struct sbuf *sb2 = NULL;
|
|
|
|
|
2011-03-24 08:56:12 +00:00
|
|
|
sb2 = sc->spec;
|
|
|
|
sc->spec = *sb;
|
|
|
|
if (*sb != NULL) {
|
|
|
|
sc->str = sbuf_data(*sb);
|
2013-02-19 19:25:50 +00:00
|
|
|
if (sc->ptr == NULL) {
|
|
|
|
blinkers++;
|
|
|
|
callout_reset(&led_ch, hz / 10, led_timeout, NULL);
|
|
|
|
}
|
2011-03-24 08:56:12 +00:00
|
|
|
sc->ptr = sc->str;
|
|
|
|
} else {
|
|
|
|
sc->str = NULL;
|
2013-02-19 19:25:50 +00:00
|
|
|
if (sc->ptr != NULL)
|
|
|
|
blinkers--;
|
2011-03-24 08:56:12 +00:00
|
|
|
sc->ptr = NULL;
|
|
|
|
sc->func(sc->private, state);
|
2005-01-29 16:33:51 +00:00
|
|
|
}
|
2011-03-24 08:56:12 +00:00
|
|
|
sc->count = 0;
|
|
|
|
*sb = sb2;
|
2005-01-29 16:33:51 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2003-11-03 09:47:30 +00:00
|
|
|
static int
|
2011-03-24 08:56:12 +00:00
|
|
|
led_parse(const char *s, struct sbuf **sb, int *state)
|
2003-11-03 09:47:30 +00:00
|
|
|
{
|
2011-03-24 08:56:12 +00:00
|
|
|
int i, error;
|
2003-11-03 09:47:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle "on" and "off" immediately so people can flash really
|
|
|
|
* fast from userland if they want to
|
|
|
|
*/
|
|
|
|
if (*s == '0' || *s == '1') {
|
2011-03-24 08:56:12 +00:00
|
|
|
*state = *s & 1;
|
|
|
|
return (0);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 08:56:12 +00:00
|
|
|
*state = 0;
|
|
|
|
*sb = sbuf_new_auto();
|
|
|
|
if (*sb == NULL)
|
2003-11-03 09:47:30 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
switch(s[0]) {
|
|
|
|
/*
|
|
|
|
* Flash, default is 100msec/100msec.
|
|
|
|
* 'f2' sets 200msec/200msec etc.
|
|
|
|
*/
|
|
|
|
case 'f':
|
|
|
|
if (s[1] >= '1' && s[1] <= '9')
|
|
|
|
i = s[1] - '1';
|
|
|
|
else
|
|
|
|
i = 0;
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_printf(*sb, "%c%c", 'A' + i, 'a' + i);
|
2003-11-03 09:47:30 +00:00
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Digits, flashes out numbers.
|
|
|
|
* 'd12' becomes -__________-_-______________________________
|
|
|
|
*/
|
|
|
|
case 'd':
|
|
|
|
for(s++; *s; s++) {
|
|
|
|
if (!isdigit(*s))
|
|
|
|
continue;
|
|
|
|
i = *s - '0';
|
|
|
|
if (i == 0)
|
|
|
|
i = 10;
|
|
|
|
for (; i > 1; i--)
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "Aa");
|
|
|
|
sbuf_cat(*sb, "Aj");
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "jj");
|
2003-11-03 09:47:30 +00:00
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* String, roll your own.
|
2003-11-27 20:23:32 +00:00
|
|
|
* 'a-j' gives "off" for n/10 sec.
|
|
|
|
* 'A-J' gives "on" for n/10 sec.
|
2003-11-03 09:47:30 +00:00
|
|
|
* no delay before repeat
|
|
|
|
* 'sAaAbBa' becomes _-_--__-
|
|
|
|
*/
|
|
|
|
case 's':
|
|
|
|
for(s++; *s; s++) {
|
2004-04-27 13:09:21 +00:00
|
|
|
if ((*s >= 'a' && *s <= 'j') ||
|
|
|
|
(*s >= 'A' && *s <= 'J') ||
|
2005-01-29 16:33:51 +00:00
|
|
|
*s == 'U' || *s <= 'u' ||
|
2004-04-27 13:09:21 +00:00
|
|
|
*s == '.')
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_bcat(*sb, s, 1);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Morse.
|
|
|
|
* '.' becomes _-
|
|
|
|
* '-' becomes _---
|
|
|
|
* ' ' becomes __
|
|
|
|
* '\n' becomes ____
|
|
|
|
* 1sec pause between repeats
|
|
|
|
* '... --- ...' -> _-_-_-___---_---_---___-_-_-__________
|
|
|
|
*/
|
|
|
|
case 'm':
|
|
|
|
for(s++; *s; s++) {
|
|
|
|
if (*s == '.')
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "aA");
|
2003-11-03 09:47:30 +00:00
|
|
|
else if (*s == '-')
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "aC");
|
2003-11-03 09:47:30 +00:00
|
|
|
else if (*s == ' ')
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "b");
|
2003-11-03 15:45:42 +00:00
|
|
|
else if (*s == '\n')
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "d");
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_cat(*sb, "j");
|
2003-11-03 09:47:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-03-24 08:56:12 +00:00
|
|
|
sbuf_delete(*sb);
|
2003-12-12 12:17:28 +00:00
|
|
|
return (EINVAL);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
2011-03-24 08:56:12 +00:00
|
|
|
error = sbuf_finish(*sb);
|
|
|
|
if (error != 0 || sbuf_len(*sb) == 0) {
|
|
|
|
*sb = NULL;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
led_write(struct cdev *dev, struct uio *uio, int ioflag)
|
|
|
|
{
|
|
|
|
struct ledsc *sc;
|
|
|
|
char *s;
|
|
|
|
struct sbuf *sb = NULL;
|
|
|
|
int error, state = 0;
|
|
|
|
|
|
|
|
if (uio->uio_resid > 512)
|
|
|
|
return (EINVAL);
|
|
|
|
s = malloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK);
|
|
|
|
s[uio->uio_resid] = '\0';
|
|
|
|
error = uiomove(s, uio->uio_resid, uio);
|
|
|
|
if (error) {
|
|
|
|
free(s, M_DEVBUF);
|
2010-09-09 18:35:08 +00:00
|
|
|
return (error);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
2011-03-24 08:56:12 +00:00
|
|
|
error = led_parse(s, &sb, &state);
|
|
|
|
free(s, M_DEVBUF);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
mtx_lock(&led_mtx);
|
|
|
|
sc = dev->si_drv1;
|
|
|
|
if (sc != NULL)
|
|
|
|
error = led_state(sc, &sb, state);
|
|
|
|
mtx_unlock(&led_mtx);
|
|
|
|
if (sb != NULL)
|
|
|
|
sbuf_delete(sb);
|
|
|
|
return (error);
|
|
|
|
}
|
2003-11-03 09:47:30 +00:00
|
|
|
|
2011-03-24 08:56:12 +00:00
|
|
|
int
|
|
|
|
led_set(char const *name, char const *cmd)
|
|
|
|
{
|
|
|
|
struct ledsc *sc;
|
|
|
|
struct sbuf *sb = NULL;
|
|
|
|
int error, state = 0;
|
|
|
|
|
|
|
|
error = led_parse(cmd, &sb, &state);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
mtx_lock(&led_mtx);
|
|
|
|
LIST_FOREACH(sc, &led_list, list) {
|
|
|
|
if (strcmp(sc->name, name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sc != NULL)
|
|
|
|
error = led_state(sc, &sb, state);
|
|
|
|
else
|
|
|
|
error = ENOENT;
|
|
|
|
mtx_unlock(&led_mtx);
|
|
|
|
if (sb != NULL)
|
|
|
|
sbuf_delete(sb);
|
|
|
|
return (0);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct cdevsw led_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
2004-02-14 20:03:36 +00:00
|
|
|
.d_write = led_write,
|
|
|
|
.d_name = "LED",
|
2003-11-03 09:47:30 +00:00
|
|
|
};
|
|
|
|
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *
|
2003-11-03 09:47:30 +00:00
|
|
|
led_create(led_t *func, void *priv, char const *name)
|
2007-04-23 12:42:15 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
return (led_create_state(func, priv, name, 0));
|
|
|
|
}
|
|
|
|
struct cdev *
|
|
|
|
led_create_state(led_t *func, void *priv, char const *name, int state)
|
2003-11-03 09:47:30 +00:00
|
|
|
{
|
|
|
|
struct ledsc *sc;
|
|
|
|
|
|
|
|
sc = malloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO);
|
2005-01-29 16:33:51 +00:00
|
|
|
|
|
|
|
sx_xlock(&led_sx);
|
2011-03-24 08:56:12 +00:00
|
|
|
sc->name = strdup(name, M_LED);
|
2005-01-29 16:33:51 +00:00
|
|
|
sc->unit = alloc_unr(led_unit);
|
2003-11-03 09:47:30 +00:00
|
|
|
sc->private = priv;
|
|
|
|
sc->func = func;
|
2008-09-26 14:19:52 +00:00
|
|
|
sc->dev = make_dev(&led_cdevsw, sc->unit,
|
2005-01-29 16:33:51 +00:00
|
|
|
UID_ROOT, GID_WHEEL, 0600, "led/%s", name);
|
|
|
|
sx_xunlock(&led_sx);
|
|
|
|
|
2003-11-03 09:47:30 +00:00
|
|
|
mtx_lock(&led_mtx);
|
2005-01-29 16:33:51 +00:00
|
|
|
sc->dev->si_drv1 = sc;
|
2003-11-03 09:47:30 +00:00
|
|
|
LIST_INSERT_HEAD(&led_list, sc, list);
|
2015-05-24 07:45:42 +00:00
|
|
|
if (state != -1)
|
|
|
|
sc->func(sc->private, state != 0);
|
2003-11-03 09:47:30 +00:00
|
|
|
mtx_unlock(&led_mtx);
|
2005-01-29 16:33:51 +00:00
|
|
|
|
2003-11-03 09:47:30 +00:00
|
|
|
return (sc->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-16 09:47:26 +00:00
|
|
|
led_destroy(struct cdev *dev)
|
2003-11-03 09:47:30 +00:00
|
|
|
{
|
|
|
|
struct ledsc *sc;
|
|
|
|
|
|
|
|
mtx_lock(&led_mtx);
|
2005-01-29 16:33:51 +00:00
|
|
|
sc = dev->si_drv1;
|
|
|
|
dev->si_drv1 = NULL;
|
2013-02-19 19:25:50 +00:00
|
|
|
if (sc->ptr != NULL)
|
|
|
|
blinkers--;
|
2003-11-03 09:47:30 +00:00
|
|
|
LIST_REMOVE(sc, list);
|
2005-01-29 16:33:51 +00:00
|
|
|
if (LIST_EMPTY(&led_list))
|
|
|
|
callout_stop(&led_ch);
|
2003-11-03 09:47:30 +00:00
|
|
|
mtx_unlock(&led_mtx);
|
2005-01-29 16:33:51 +00:00
|
|
|
|
|
|
|
sx_xlock(&led_sx);
|
|
|
|
free_unr(led_unit, sc->unit);
|
|
|
|
destroy_dev(dev);
|
2003-11-23 10:22:51 +00:00
|
|
|
if (sc->spec != NULL)
|
|
|
|
sbuf_delete(sc->spec);
|
2011-03-24 08:56:12 +00:00
|
|
|
free(sc->name, M_LED);
|
2003-11-03 09:47:30 +00:00
|
|
|
free(sc, M_LED);
|
2005-01-29 16:33:51 +00:00
|
|
|
sx_xunlock(&led_sx);
|
2003-11-03 09:47:30 +00:00
|
|
|
}
|
2005-01-29 16:33:51 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
led_drvinit(void *unused)
|
|
|
|
{
|
|
|
|
|
2008-05-29 12:50:46 +00:00
|
|
|
led_unit = new_unrhdr(0, INT_MAX, NULL);
|
2005-01-29 16:33:51 +00:00
|
|
|
mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF);
|
|
|
|
sx_init(&led_sx, "LED sx");
|
2013-02-19 19:25:50 +00:00
|
|
|
callout_init_mtx(&led_ch, &led_mtx, 0);
|
2005-01-29 16:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL);
|