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