2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
11 #include <sys/cdefs.h>
12 __FBSDID("$FreeBSD$");
14 #include <sys/param.h>
16 #include <sys/kernel.h>
17 #include <sys/systm.h>
18 #include <sys/malloc.h>
19 #include <sys/ctype.h>
21 #include <sys/queue.h>
22 #include <dev/led/led.h>
27 LIST_ENTRY(ledsc) list;
39 static struct unrhdr *led_unit;
40 static struct mtx led_mtx;
41 static struct sx led_sx;
42 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(&led_list);
43 static struct callout led_ch;
45 static MALLOC_DEFINE(M_LED, "LED", "LED driver");
53 LIST_FOREACH(sc, &led_list, list) {
60 if (*sc->ptr == '.') {
63 } else if (*sc->ptr == 'U' || *sc->ptr == 'u') {
64 if (sc->last_second == time_second)
66 sc->last_second = time_second;
67 sc->func(sc->private, *sc->ptr == 'U');
68 } else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') {
69 sc->func(sc->private, 0);
70 sc->count = (*sc->ptr & 0xf) - 1;
71 } else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') {
72 sc->func(sc->private, 1);
73 sc->count = (*sc->ptr & 0xf) - 1;
80 callout_reset(&led_ch, hz / 10, led_timeout, p);
85 led_state(struct cdev *dev, struct sbuf *sb, int state)
87 struct sbuf *sb2 = NULL;
96 sc->str = sbuf_data(sb);
101 sc->func(sc->private, state);
105 mtx_unlock(&led_mtx);
114 led_write(struct cdev *dev, struct uio *uio, int ioflag)
118 struct sbuf *sb = NULL;
121 if (dev->si_drv1 == NULL)
124 if (uio->uio_resid > 512)
126 s2 = s = malloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK);
127 s[uio->uio_resid] = '\0';
128 error = uiomove(s, uio->uio_resid, uio);
135 * Handle "on" and "off" immediately so people can flash really
136 * fast from userland if they want to
138 if (*s == '0' || *s == '1') {
139 error = led_state(dev, NULL, *s & 1);
144 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
152 * Flash, default is 100msec/100msec.
153 * 'f2' sets 200msec/200msec etc.
156 if (s[1] >= '1' && s[1] <= '9')
160 sbuf_printf(sb, "%c%c", 'A' + i, 'a' + i);
163 * Digits, flashes out numbers.
164 * 'd12' becomes -__________-_-______________________________
180 * String, roll your own.
181 * 'a-j' gives "off" for n/10 sec.
182 * 'A-J' gives "on" for n/10 sec.
183 * no delay before repeat
184 * 'sAaAbBa' becomes _-_--__-
188 if ((*s >= 'a' && *s <= 'j') ||
189 (*s >= 'A' && *s <= 'J') ||
190 *s == 'U' || *s <= 'u' ||
201 * 1sec pause between repeats
202 * '... --- ...' -> _-_-_-___---_---_---___-_-_-__________
224 if (sbuf_overflowed(sb)) {
228 if (sbuf_len(sb) == 0) {
233 return (led_state(dev, sb, 0));
236 static struct cdevsw led_cdevsw = {
237 .d_version = D_VERSION,
238 .d_write = led_write,
243 led_create(led_t *func, void *priv, char const *name)
247 sc = malloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO);
250 sc->unit = alloc_unr(led_unit);
253 sc->dev = make_dev(&led_cdevsw, unit2minor(sc->unit),
254 UID_ROOT, GID_WHEEL, 0600, "led/%s", name);
258 sc->dev->si_drv1 = sc;
259 if (LIST_EMPTY(&led_list))
260 callout_reset(&led_ch, hz / 10, led_timeout, NULL);
261 LIST_INSERT_HEAD(&led_list, sc, list);
262 sc->func(sc->private, 0);
263 mtx_unlock(&led_mtx);
269 led_destroy(struct cdev *dev)
277 LIST_REMOVE(sc, list);
278 if (LIST_EMPTY(&led_list))
279 callout_stop(&led_ch);
280 mtx_unlock(&led_mtx);
283 free_unr(led_unit, sc->unit);
285 if (sc->spec != NULL)
286 sbuf_delete(sc->spec);
292 led_drvinit(void *unused)
295 led_unit = new_unrhdr(0, minor2unit(MAXMINOR), NULL);
296 mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF);
297 sx_init(&led_sx, "LED sx");
298 callout_init(&led_ch, CALLOUT_MPSAFE);
301 SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL);