2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
16 #include <sys/param.h>
18 #include <sys/kernel.h>
19 #include <sys/systm.h>
20 #include <sys/limits.h>
21 #include <sys/malloc.h>
22 #include <sys/ctype.h>
24 #include <sys/queue.h>
25 #include <dev/led/led.h>
30 LIST_ENTRY(ledsc) list;
43 static struct unrhdr *led_unit;
44 static struct mtx led_mtx;
45 static struct sx led_sx;
46 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list);
47 static struct callout led_ch;
48 static int blinkers = 0;
50 static MALLOC_DEFINE(M_LED, "LED", "LED driver");
57 LIST_FOREACH(sc, &led_list, list) {
64 if (*sc->ptr == '.') {
68 } else if (*sc->ptr == 'U' || *sc->ptr == 'u') {
69 if (sc->last_second == time_second)
71 sc->last_second = time_second;
72 sc->func(sc->private, *sc->ptr == 'U');
73 } else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') {
74 sc->func(sc->private, 0);
75 sc->count = (*sc->ptr & 0xf) - 1;
76 } else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') {
77 sc->func(sc->private, 1);
78 sc->count = (*sc->ptr & 0xf) - 1;
85 callout_reset(&led_ch, hz / 10, led_timeout, p);
89 led_state(struct ledsc *sc, struct sbuf **sb, int state)
91 struct sbuf *sb2 = NULL;
96 sc->str = sbuf_data(*sb);
97 if (sc->ptr == NULL) {
99 callout_reset(&led_ch, hz / 10, led_timeout, NULL);
107 sc->func(sc->private, state);
115 led_parse(const char *s, struct sbuf **sb, int *state)
120 * Handle "on" and "off" immediately so people can flash really
121 * fast from userland if they want to
123 if (*s == '0' || *s == '1') {
129 *sb = sbuf_new_auto();
134 * Flash, default is 100msec/100msec.
135 * 'f2' sets 200msec/200msec etc.
138 if (s[1] >= '1' && s[1] <= '9')
142 sbuf_printf(*sb, "%c%c", 'A' + i, 'a' + i);
145 * Digits, flashes out numbers.
146 * 'd12' becomes -__________-_-______________________________
162 * String, roll your own.
163 * 'a-j' gives "off" for n/10 sec.
164 * 'A-J' gives "on" for n/10 sec.
165 * no delay before repeat
166 * 'sAaAbBa' becomes _-_--__-
170 if ((*s >= 'a' && *s <= 'j') ||
171 (*s >= 'A' && *s <= 'J') ||
172 *s == 'U' || *s <= 'u' ||
174 sbuf_bcat(*sb, s, 1);
183 * 1sec pause between repeats
184 * '... --- ...' -> _-_-_-___---_---_---___-_-_-__________
203 error = sbuf_finish(*sb);
204 if (error != 0 || sbuf_len(*sb) == 0) {
212 led_write(struct cdev *dev, struct uio *uio, int ioflag)
216 struct sbuf *sb = NULL;
217 int error, state = 0;
219 if (uio->uio_resid > 512)
221 s = malloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK);
222 s[uio->uio_resid] = '\0';
223 error = uiomove(s, uio->uio_resid, uio);
228 error = led_parse(s, &sb, &state);
235 error = led_state(sc, &sb, state);
236 mtx_unlock(&led_mtx);
243 led_set(char const *name, char const *cmd)
246 struct sbuf *sb = NULL;
247 int error, state = 0;
249 error = led_parse(cmd, &sb, &state);
253 LIST_FOREACH(sc, &led_list, list) {
254 if (strcmp(sc->name, name) == 0)
258 error = led_state(sc, &sb, state);
261 mtx_unlock(&led_mtx);
267 static struct cdevsw led_cdevsw = {
268 .d_version = D_VERSION,
269 .d_write = led_write,
274 led_create(led_t *func, void *priv, char const *name)
277 return (led_create_state(func, priv, name, 0));
280 led_create_state(led_t *func, void *priv, char const *name, int state)
284 sc = malloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO);
287 sc->name = strdup(name, M_LED);
288 sc->unit = alloc_unr(led_unit);
291 sc->dev = make_dev(&led_cdevsw, sc->unit,
292 UID_ROOT, GID_WHEEL, 0600, "led/%s", name);
296 sc->dev->si_drv1 = sc;
297 LIST_INSERT_HEAD(&led_list, sc, list);
299 sc->func(sc->private, state != 0);
300 mtx_unlock(&led_mtx);
306 led_destroy(struct cdev *dev)
315 LIST_REMOVE(sc, list);
316 if (LIST_EMPTY(&led_list))
317 callout_stop(&led_ch);
318 mtx_unlock(&led_mtx);
321 free_unr(led_unit, sc->unit);
323 if (sc->spec != NULL)
324 sbuf_delete(sc->spec);
325 free(sc->name, M_LED);
331 led_drvinit(void *unused)
334 led_unit = new_unrhdr(0, INT_MAX, NULL);
335 mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF);
336 sx_init(&led_sx, "LED sx");
337 callout_init_mtx(&led_ch, &led_mtx, 0);
340 SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL);