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/limits.h>
19 #include <sys/malloc.h>
20 #include <sys/ctype.h>
22 #include <sys/queue.h>
23 #include <dev/led/led.h>
28 LIST_ENTRY(ledsc) list;
41 static struct unrhdr *led_unit;
42 static struct mtx led_mtx;
43 static struct sx led_sx;
44 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list);
45 static struct callout led_ch;
46 static int blinkers = 0;
48 static MALLOC_DEFINE(M_LED, "LED", "LED driver");
55 LIST_FOREACH(sc, &led_list, list) {
62 if (*sc->ptr == '.') {
66 } else if (*sc->ptr == 'U' || *sc->ptr == 'u') {
67 if (sc->last_second == time_second)
69 sc->last_second = time_second;
70 sc->func(sc->private, *sc->ptr == 'U');
71 } else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') {
72 sc->func(sc->private, 0);
73 sc->count = (*sc->ptr & 0xf) - 1;
74 } else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') {
75 sc->func(sc->private, 1);
76 sc->count = (*sc->ptr & 0xf) - 1;
83 callout_reset(&led_ch, hz / 10, led_timeout, p);
87 led_state(struct ledsc *sc, struct sbuf **sb, int state)
89 struct sbuf *sb2 = NULL;
94 sc->str = sbuf_data(*sb);
95 if (sc->ptr == NULL) {
97 callout_reset(&led_ch, hz / 10, led_timeout, NULL);
105 sc->func(sc->private, state);
113 led_parse(const char *s, struct sbuf **sb, int *state)
118 * Handle "on" and "off" immediately so people can flash really
119 * fast from userland if they want to
121 if (*s == '0' || *s == '1') {
127 *sb = sbuf_new_auto();
132 * Flash, default is 100msec/100msec.
133 * 'f2' sets 200msec/200msec etc.
136 if (s[1] >= '1' && s[1] <= '9')
140 sbuf_printf(*sb, "%c%c", 'A' + i, 'a' + i);
143 * Digits, flashes out numbers.
144 * 'd12' becomes -__________-_-______________________________
160 * String, roll your own.
161 * 'a-j' gives "off" for n/10 sec.
162 * 'A-J' gives "on" for n/10 sec.
163 * no delay before repeat
164 * 'sAaAbBa' becomes _-_--__-
168 if ((*s >= 'a' && *s <= 'j') ||
169 (*s >= 'A' && *s <= 'J') ||
170 *s == 'U' || *s <= 'u' ||
172 sbuf_bcat(*sb, s, 1);
181 * 1sec pause between repeats
182 * '... --- ...' -> _-_-_-___---_---_---___-_-_-__________
201 error = sbuf_finish(*sb);
202 if (error != 0 || sbuf_len(*sb) == 0) {
210 led_write(struct cdev *dev, struct uio *uio, int ioflag)
214 struct sbuf *sb = NULL;
215 int error, state = 0;
217 if (uio->uio_resid > 512)
219 s = malloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK);
220 s[uio->uio_resid] = '\0';
221 error = uiomove(s, uio->uio_resid, uio);
226 error = led_parse(s, &sb, &state);
233 error = led_state(sc, &sb, state);
234 mtx_unlock(&led_mtx);
241 led_set(char const *name, char const *cmd)
244 struct sbuf *sb = NULL;
245 int error, state = 0;
247 error = led_parse(cmd, &sb, &state);
251 LIST_FOREACH(sc, &led_list, list) {
252 if (strcmp(sc->name, name) == 0)
256 error = led_state(sc, &sb, state);
259 mtx_unlock(&led_mtx);
265 static struct cdevsw led_cdevsw = {
266 .d_version = D_VERSION,
267 .d_write = led_write,
272 led_create(led_t *func, void *priv, char const *name)
275 return (led_create_state(func, priv, name, 0));
278 led_create_state(led_t *func, void *priv, char const *name, int state)
282 sc = malloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO);
285 sc->name = strdup(name, M_LED);
286 sc->unit = alloc_unr(led_unit);
289 sc->dev = make_dev(&led_cdevsw, sc->unit,
290 UID_ROOT, GID_WHEEL, 0600, "led/%s", name);
294 sc->dev->si_drv1 = sc;
295 LIST_INSERT_HEAD(&led_list, sc, list);
296 sc->func(sc->private, state != 0);
297 mtx_unlock(&led_mtx);
303 led_destroy(struct cdev *dev)
312 LIST_REMOVE(sc, list);
313 if (LIST_EMPTY(&led_list))
314 callout_stop(&led_ch);
315 mtx_unlock(&led_mtx);
318 free_unr(led_unit, sc->unit);
320 if (sc->spec != NULL)
321 sbuf_delete(sc->spec);
322 free(sc->name, M_LED);
328 led_drvinit(void *unused)
331 led_unit = new_unrhdr(0, INT_MAX, NULL);
332 mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF);
333 sx_init(&led_sx, "LED sx");
334 callout_init_mtx(&led_ch, &led_mtx, 0);
337 SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL);