xref: /freebsd/sys/dev/led/led.c (revision 8fc257994d0ce2396196d7a06d50d20c8015f4b7)
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  * ----------------------------------------------------------------------------
8  *
9  */
10 
11 #include <sys/cdefs.h>
12 __FBSDID("$FreeBSD$");
13 
14 #include <sys/param.h>
15 #include <sys/conf.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>
21 #include <sys/sbuf.h>
22 #include <sys/queue.h>
23 #include <dev/led/led.h>
24 #include <sys/uio.h>
25 #include <sys/sx.h>
26 
27 struct ledsc {
28 	LIST_ENTRY(ledsc)	list;
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;
38 };
39 
40 static struct unrhdr *led_unit;
41 static struct mtx led_mtx;
42 static struct sx led_sx;
43 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list);
44 static struct callout led_ch;
45 
46 static MALLOC_DEFINE(M_LED, "LED", "LED driver");
47 
48 static void
49 led_timeout(void *p)
50 {
51 	struct ledsc	*sc;
52 
53 	mtx_lock(&led_mtx);
54 	LIST_FOREACH(sc, &led_list, list) {
55 		if (sc->ptr == NULL)
56 			continue;
57 		if (sc->count > 0) {
58 			sc->count--;
59 			continue;
60 		}
61 		if (*sc->ptr == '.') {
62 			sc->ptr = NULL;
63 			continue;
64 		} else if (*sc->ptr == 'U' || *sc->ptr == 'u') {
65 			if (sc->last_second == time_second)
66 				continue;
67 			sc->last_second = time_second;
68 			sc->func(sc->private, *sc->ptr == 'U');
69 		} else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') {
70 			sc->func(sc->private, 0);
71 			sc->count = (*sc->ptr & 0xf) - 1;
72 		} else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') {
73 			sc->func(sc->private, 1);
74 			sc->count = (*sc->ptr & 0xf) - 1;
75 		}
76 		sc->ptr++;
77 		if (*sc->ptr == '\0')
78 			sc->ptr = sc->str;
79 	}
80 	mtx_unlock(&led_mtx);
81 	callout_reset(&led_ch, hz / 10, led_timeout, p);
82 	return;
83 }
84 
85 static int
86 led_state(struct cdev *dev, struct sbuf *sb, int state)
87 {
88 	struct sbuf *sb2 = NULL;
89 	struct ledsc *sc;
90 
91 	mtx_lock(&led_mtx);
92 	sc = dev->si_drv1;
93 	if (sc != NULL) {
94 		sb2 = sc->spec;
95 		sc->spec = sb;
96 		if (sb != NULL) {
97 			sc->str = sbuf_data(sb);
98 			sc->ptr = sc->str;
99 		} else {
100 			sc->str = NULL;
101 			sc->ptr = NULL;
102 			sc->func(sc->private, state);
103 		}
104 		sc->count = 0;
105 	}
106 	mtx_unlock(&led_mtx);
107 	if (sb2 != NULL)
108 		sbuf_delete(sb2);
109 	if (sc == NULL)
110 		return (ENXIO);
111 	return(0);
112 }
113 
114 static int
115 led_write(struct cdev *dev, struct uio *uio, int ioflag)
116 {
117 	int error;
118 	char *s, *s2;
119 	struct sbuf *sb = NULL;
120 	int i;
121 
122 	if (dev->si_drv1 == NULL)
123 		return (ENXIO);
124 
125 	if (uio->uio_resid > 512)
126 		return (EINVAL);
127 	s2 = s = malloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK);
128 	s[uio->uio_resid] = '\0';
129 	error = uiomove(s, uio->uio_resid, uio);
130 	if (error) {
131 		free(s2, M_DEVBUF);
132 		return (error);
133 	}
134 
135 	/*
136 	 * Handle "on" and "off" immediately so people can flash really
137 	 * fast from userland if they want to
138 	 */
139 	if (*s == '0' || *s == '1') {
140 		error = led_state(dev, NULL, *s & 1);
141 		free(s2, M_DEVBUF);
142 		return(error);
143 	}
144 
145 	sb = sbuf_new_auto();
146 	if (sb == NULL) {
147 		free(s2, M_DEVBUF);
148 		return (ENOMEM);
149 	}
150 
151 	switch(s[0]) {
152 		/*
153 		 * Flash, default is 100msec/100msec.
154 		 * 'f2' sets 200msec/200msec etc.
155 		 */
156 		case 'f':
157 			if (s[1] >= '1' && s[1] <= '9')
158 				i = s[1] - '1';
159 			else
160 				i = 0;
161 			sbuf_printf(sb, "%c%c", 'A' + i, 'a' + i);
162 			break;
163 		/*
164 		 * Digits, flashes out numbers.
165 		 * 'd12' becomes -__________-_-______________________________
166 		 */
167 		case 'd':
168 			for(s++; *s; s++) {
169 				if (!isdigit(*s))
170 					continue;
171 				i = *s - '0';
172 				if (i == 0)
173 					i = 10;
174 				for (; i > 1; i--)
175 					sbuf_cat(sb, "Aa");
176 				sbuf_cat(sb, "Aj");
177 			}
178 			sbuf_cat(sb, "jj");
179 			break;
180 		/*
181 		 * String, roll your own.
182 		 * 'a-j' gives "off" for n/10 sec.
183 		 * 'A-J' gives "on" for n/10 sec.
184 		 * no delay before repeat
185 		 * 'sAaAbBa' becomes _-_--__-
186 		 */
187 		case 's':
188 			for(s++; *s; s++) {
189 				if ((*s >= 'a' && *s <= 'j') ||
190 				    (*s >= 'A' && *s <= 'J') ||
191 				    *s == 'U' || *s <= 'u' ||
192 					*s == '.')
193 					sbuf_bcat(sb, s, 1);
194 			}
195 			break;
196 		/*
197 		 * Morse.
198 		 * '.' becomes _-
199 		 * '-' becomes _---
200 		 * ' ' becomes __
201 		 * '\n' becomes ____
202 		 * 1sec pause between repeats
203 		 * '... --- ...' -> _-_-_-___---_---_---___-_-_-__________
204 		 */
205 		case 'm':
206 			for(s++; *s; s++) {
207 				if (*s == '.')
208 					sbuf_cat(sb, "aA");
209 				else if (*s == '-')
210 					sbuf_cat(sb, "aC");
211 				else if (*s == ' ')
212 					sbuf_cat(sb, "b");
213 				else if (*s == '\n')
214 					sbuf_cat(sb, "d");
215 			}
216 			sbuf_cat(sb, "j");
217 			break;
218 		default:
219 			sbuf_delete(sb);
220 			free(s2, M_DEVBUF);
221 			return (EINVAL);
222 	}
223 	sbuf_finish(sb);
224 	free(s2, M_DEVBUF);
225 	if (sbuf_overflowed(sb)) {
226 		sbuf_delete(sb);
227 		return (ENOMEM);
228 	}
229 	if (sbuf_len(sb) == 0) {
230 		sbuf_delete(sb);
231 		return (0);
232 	}
233 
234 	return (led_state(dev, sb, 0));
235 }
236 
237 static struct cdevsw led_cdevsw = {
238 	.d_version =	D_VERSION,
239 	.d_write =	led_write,
240 	.d_name =	"LED",
241 };
242 
243 struct cdev *
244 led_create(led_t *func, void *priv, char const *name)
245 {
246 
247 	return (led_create_state(func, priv, name, 0));
248 }
249 struct cdev *
250 led_create_state(led_t *func, void *priv, char const *name, int state)
251 {
252 	struct ledsc	*sc;
253 
254 	sc = malloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO);
255 
256 	sx_xlock(&led_sx);
257 	sc->unit = alloc_unr(led_unit);
258 	sc->private = priv;
259 	sc->func = func;
260 	sc->dev = make_dev(&led_cdevsw, sc->unit,
261 	    UID_ROOT, GID_WHEEL, 0600, "led/%s", name);
262 	sx_xunlock(&led_sx);
263 
264 	mtx_lock(&led_mtx);
265 	sc->dev->si_drv1 = sc;
266 	if (LIST_EMPTY(&led_list))
267 		callout_reset(&led_ch, hz / 10, led_timeout, NULL);
268 	LIST_INSERT_HEAD(&led_list, sc, list);
269 	sc->func(sc->private, state != 0);
270 	mtx_unlock(&led_mtx);
271 
272 	return (sc->dev);
273 }
274 
275 void
276 led_destroy(struct cdev *dev)
277 {
278 	struct ledsc *sc;
279 
280 	mtx_lock(&led_mtx);
281 	sc = dev->si_drv1;
282 	dev->si_drv1 = NULL;
283 
284 	LIST_REMOVE(sc, list);
285 	if (LIST_EMPTY(&led_list))
286 		callout_stop(&led_ch);
287 	mtx_unlock(&led_mtx);
288 
289 	sx_xlock(&led_sx);
290 	free_unr(led_unit, sc->unit);
291 	destroy_dev(dev);
292 	if (sc->spec != NULL)
293 		sbuf_delete(sc->spec);
294 	free(sc, M_LED);
295 	sx_xunlock(&led_sx);
296 }
297 
298 static void
299 led_drvinit(void *unused)
300 {
301 
302 	led_unit = new_unrhdr(0, INT_MAX, NULL);
303 	mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF);
304 	sx_init(&led_sx, "LED sx");
305 	callout_init(&led_ch, CALLOUT_MPSAFE);
306 }
307 
308 SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL);
309