bridge(4): document net.link.bridge.pfil_onlyip
[dragonfly.git] / sys / dev / misc / led / led.c
blobb381165bfc2881b69f8551f6061964ff59d487c9
1 /*-
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 * ----------------------------------------------------------------------------
9 * $FreeBSD: head/sys/dev/led/led.c 247008 2013-02-19 19:25:50Z mav $
12 #include <sys/param.h>
13 #include <sys/conf.h>
14 #include <sys/kernel.h>
15 #include <sys/systm.h>
16 #include <sys/limits.h>
17 #include <sys/malloc.h>
18 #include <sys/ctype.h>
19 #include <sys/sbuf.h>
20 #include <sys/queue.h>
21 #include <dev/misc/led/led.h>
22 #include <sys/uio.h>
23 #include <sys/device.h>
24 #include <sys/module.h>
26 struct ledsc {
27 LIST_ENTRY(ledsc) list;
28 char *name;
29 void *private;
30 int unit;
31 led_t *func;
32 struct cdev *dev;
33 struct sbuf *spec;
34 char *str;
35 char *ptr;
36 int count;
37 time_t last_second;
40 static struct unrhdr *led_unit;
41 static struct lock led_lock;
42 static struct lock led_lock2;
43 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list);
44 static struct callout led_ch;
45 static int blinkers = 0;
47 static MALLOC_DEFINE(M_LED, "LED", "LED driver");
49 static void
50 led_timeout(void *p)
52 struct ledsc *sc;
54 lockmgr(&led_lock, LK_EXCLUSIVE);
55 LIST_FOREACH(sc, &led_list, list) {
56 if (sc->ptr == NULL)
57 continue;
58 if (sc->count > 0) {
59 sc->count--;
60 continue;
62 if (*sc->ptr == '.') {
63 sc->ptr = NULL;
64 blinkers--;
65 continue;
66 } else if (*sc->ptr == 'U' || *sc->ptr == 'u') {
67 if (sc->last_second == time_second)
68 continue;
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;
78 sc->ptr++;
79 if (*sc->ptr == '\0')
80 sc->ptr = sc->str;
82 if (blinkers > 0)
83 callout_reset(&led_ch, hz / 10, led_timeout, p);
84 lockmgr(&led_lock, LK_RELEASE);
87 static int
88 led_state(struct ledsc *sc, struct sbuf **sb, int state)
90 struct sbuf *sb2 = NULL;
92 sb2 = sc->spec;
93 sc->spec = *sb;
94 if (*sb != NULL) {
95 sc->str = sbuf_data(*sb);
96 if (sc->ptr == NULL) {
97 blinkers++;
98 callout_reset(&led_ch, hz / 10, led_timeout, NULL);
100 sc->ptr = sc->str;
101 } else {
102 sc->str = NULL;
103 if (sc->ptr != NULL)
104 blinkers--;
105 sc->ptr = NULL;
106 sc->func(sc->private, state);
108 sc->count = 0;
109 *sb = sb2;
110 return(0);
113 static int
114 led_parse(const char *s, struct sbuf **sb, int *state)
116 int i, error;
119 * Handle "on" and "off" immediately so people can flash really
120 * fast from userland if they want to
122 if (*s == '0' || *s == '1') {
123 *state = *s & 1;
124 return (0);
127 *state = 0;
128 *sb = sbuf_new_auto();
129 if (*sb == NULL)
130 return (ENOMEM);
131 switch(s[0]) {
133 * Flash, default is 100msec/100msec.
134 * 'f2' sets 200msec/200msec etc.
136 case 'f':
137 if (s[1] >= '1' && s[1] <= '9')
138 i = s[1] - '1';
139 else
140 i = 0;
141 sbuf_printf(*sb, "%c%c", 'A' + i, 'a' + i);
142 break;
144 * Digits, flashes out numbers.
145 * 'd12' becomes -__________-_-______________________________
147 case 'd':
148 for(s++; *s; s++) {
149 if (!isdigit(*s))
150 continue;
151 i = *s - '0';
152 if (i == 0)
153 i = 10;
154 for (; i > 1; i--)
155 sbuf_cat(*sb, "Aa");
156 sbuf_cat(*sb, "Aj");
158 sbuf_cat(*sb, "jj");
159 break;
161 * String, roll your own.
162 * 'a-j' gives "off" for n/10 sec.
163 * 'A-J' gives "on" for n/10 sec.
164 * no delay before repeat
165 * 'sAaAbBa' becomes _-_--__-
167 case 's':
168 for(s++; *s; s++) {
169 if ((*s >= 'a' && *s <= 'j') ||
170 (*s >= 'A' && *s <= 'J') ||
171 *s == 'U' || *s <= 'u' ||
172 *s == '.')
173 sbuf_bcat(*sb, s, 1);
175 break;
177 * Morse.
178 * '.' becomes _-
179 * '-' becomes _---
180 * ' ' becomes __
181 * '\n' becomes ____
182 * 1sec pause between repeats
183 * '... --- ...' -> _-_-_-___---_---_---___-_-_-__________
185 case 'm':
186 for(s++; *s; s++) {
187 if (*s == '.')
188 sbuf_cat(*sb, "aA");
189 else if (*s == '-')
190 sbuf_cat(*sb, "aC");
191 else if (*s == ' ')
192 sbuf_cat(*sb, "b");
193 else if (*s == '\n')
194 sbuf_cat(*sb, "d");
196 sbuf_cat(*sb, "j");
197 break;
198 default:
199 sbuf_delete(*sb);
200 return (EINVAL);
202 error = sbuf_finish(*sb);
203 if (error != 0 || sbuf_len(*sb) == 0) {
204 *sb = NULL;
205 return (error);
207 return (0);
210 static int
211 led_open(struct dev_open_args *ap)
213 return (0);
216 static int
217 led_close(struct dev_close_args *ap)
219 return (0);
222 static int
223 led_write(struct dev_write_args *ap)
225 struct uio *uio = ap->a_uio;
226 cdev_t dev = ap->a_head.a_dev;
227 struct ledsc *sc;
228 char *s;
229 struct sbuf *sb = NULL;
230 int error, state = 0;
232 if (uio->uio_resid > 512)
233 return (EINVAL);
234 s = kmalloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK);
235 s[uio->uio_resid] = '\0';
236 error = uiomove(s, uio->uio_resid, uio);
237 if (error) {
238 kfree(s, M_DEVBUF);
239 return (error);
241 error = led_parse(s, &sb, &state);
242 kfree(s, M_DEVBUF);
243 if (error)
244 return (error);
245 lockmgr(&led_lock, LK_EXCLUSIVE);
246 sc = dev->si_drv1;
247 if (sc != NULL)
248 error = led_state(sc, &sb, state);
249 lockmgr(&led_lock, LK_RELEASE);
250 if (sb != NULL)
251 sbuf_delete(sb);
252 return (error);
256 led_set(char const *name, char const *cmd)
258 struct ledsc *sc;
259 struct sbuf *sb = NULL;
260 int error, state = 0;
262 error = led_parse(cmd, &sb, &state);
263 if (error)
264 return (error);
265 lockmgr(&led_lock, LK_EXCLUSIVE);
266 LIST_FOREACH(sc, &led_list, list) {
267 if (strcmp(sc->name, name) == 0)
268 break;
270 if (sc != NULL)
271 error = led_state(sc, &sb, state);
272 else
273 error = ENOENT;
274 lockmgr(&led_lock, LK_RELEASE);
275 if (sb != NULL)
276 sbuf_delete(sb);
277 return (0);
280 static struct dev_ops led_ops = {
281 { "LED", 0, D_MPSAFE },
282 .d_open = led_open,
283 .d_close = led_close,
284 .d_write = led_write,
287 struct cdev *
288 led_create(led_t *func, void *priv, char const *name)
291 return (led_create_state(func, priv, name, 0));
293 struct cdev *
294 led_create_state(led_t *func, void *priv, char const *name, int state)
296 struct ledsc *sc;
298 sc = kmalloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO);
300 lockmgr(&led_lock2, LK_EXCLUSIVE);
301 sc->name = kstrdup(name, M_LED);
302 sc->unit = alloc_unr(led_unit);
303 sc->private = priv;
304 sc->func = func;
305 sc->dev = make_dev(&led_ops, sc->unit,
306 UID_ROOT, GID_WHEEL, 0600, "led/%s", name);
307 lockmgr(&led_lock2, LK_RELEASE);
309 lockmgr(&led_lock, LK_EXCLUSIVE);
310 sc->dev->si_drv1 = sc;
311 LIST_INSERT_HEAD(&led_list, sc, list);
312 sc->func(sc->private, state != 0);
313 lockmgr(&led_lock, LK_RELEASE);
315 return (sc->dev);
318 void
319 led_destroy(struct cdev *dev)
321 struct ledsc *sc;
323 lockmgr(&led_lock, LK_EXCLUSIVE);
324 sc = dev->si_drv1;
325 dev->si_drv1 = NULL;
326 if (sc->ptr != NULL)
327 blinkers--;
328 LIST_REMOVE(sc, list);
329 if (LIST_EMPTY(&led_list))
330 callout_stop(&led_ch);
331 lockmgr(&led_lock, LK_RELEASE);
333 lockmgr(&led_lock2, LK_EXCLUSIVE);
334 free_unr(led_unit, sc->unit);
335 destroy_dev(dev);
336 if (sc->spec != NULL)
337 sbuf_delete(sc->spec);
338 kfree(sc->name, M_LED);
339 kfree(sc, M_LED);
340 lockmgr(&led_lock2, LK_RELEASE);
343 static int
344 led_drvinit(void)
347 led_unit = new_unrhdr(0, INT_MAX, NULL);
348 lockinit(&led_lock, "LED lock", 0, LK_CANRECURSE);
349 lockinit(&led_lock2, "LED lock2", 0, LK_CANRECURSE);
350 callout_init_mp(&led_ch);
351 return 0;
354 static int
355 led_drvexit(void)
357 int error = 0;
359 lockmgr(&led_lock, LK_EXCLUSIVE);
360 /* A minimal sanity check, before unloading. */
361 if (!LIST_EMPTY(&led_list))
362 error = EINVAL;
363 lockmgr(&led_lock, LK_RELEASE);
364 if (error == 0) {
365 callout_cancel(&led_ch);
366 delete_unrhdr(led_unit);
367 lockuninit(&led_lock);
368 lockuninit(&led_lock2);
370 return error;
373 static int
374 led_modevent(module_t mod, int type, void *unused)
376 int error;
378 switch (type) {
379 case MOD_LOAD:
380 error = led_drvinit();
381 break;
382 case MOD_UNLOAD:
383 error = led_drvexit();
384 break;
385 default:
386 error = EINVAL;
387 break;
389 return error;
392 static moduledata_t led_mod = {
393 "led",
394 led_modevent,
397 DECLARE_MODULE(led, led_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
398 MODULE_VERSION(led, 1);