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 * $FreeBSD: head/sys/dev/led/led.c 247008 2013-02-19 19:25:50Z mav $ 10 */ 11 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 25 struct ledsc { 26 LIST_ENTRY(ledsc) list; 27 char *name; 28 void *private; 29 int unit; 30 led_t *func; 31 struct cdev *dev; 32 struct sbuf *spec; 33 char *str; 34 char *ptr; 35 int count; 36 time_t last_second; 37 }; 38 39 static struct unrhdr *led_unit; 40 static struct lock led_lock; 41 static struct lock led_lock2; 42 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list); 43 static struct callout led_ch; 44 static int blinkers = 0; 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 lockmgr(&led_lock, LK_EXCLUSIVE); 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 blinkers--; 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 if (blinkers > 0) 82 callout_reset(&led_ch, hz / 10, led_timeout, p); 83 lockmgr(&led_lock, LK_RELEASE); 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 if (sc->ptr == NULL) { 96 blinkers++; 97 callout_reset(&led_ch, hz / 10, led_timeout, NULL); 98 } 99 sc->ptr = sc->str; 100 } else { 101 sc->str = NULL; 102 if (sc->ptr != NULL) 103 blinkers--; 104 sc->ptr = NULL; 105 sc->func(sc->private, state); 106 } 107 sc->count = 0; 108 *sb = sb2; 109 return(0); 110 } 111 112 static int 113 led_parse(const char *s, struct sbuf **sb, int *state) 114 { 115 int i, error; 116 117 /* 118 * Handle "on" and "off" immediately so people can flash really 119 * fast from userland if they want to 120 */ 121 if (*s == '0' || *s == '1') { 122 *state = *s & 1; 123 return (0); 124 } 125 126 *state = 0; 127 *sb = sbuf_new_auto(); 128 if (*sb == NULL) 129 return (ENOMEM); 130 switch(s[0]) { 131 /* 132 * Flash, default is 100msec/100msec. 133 * 'f2' sets 200msec/200msec etc. 134 */ 135 case 'f': 136 if (s[1] >= '1' && s[1] <= '9') 137 i = s[1] - '1'; 138 else 139 i = 0; 140 sbuf_printf(*sb, "%c%c", 'A' + i, 'a' + i); 141 break; 142 /* 143 * Digits, flashes out numbers. 144 * 'd12' becomes -__________-_-______________________________ 145 */ 146 case 'd': 147 for(s++; *s; s++) { 148 if (!isdigit(*s)) 149 continue; 150 i = *s - '0'; 151 if (i == 0) 152 i = 10; 153 for (; i > 1; i--) 154 sbuf_cat(*sb, "Aa"); 155 sbuf_cat(*sb, "Aj"); 156 } 157 sbuf_cat(*sb, "jj"); 158 break; 159 /* 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 _-_--__- 165 */ 166 case 's': 167 for(s++; *s; s++) { 168 if ((*s >= 'a' && *s <= 'j') || 169 (*s >= 'A' && *s <= 'J') || 170 *s == 'U' || *s <= 'u' || 171 *s == '.') 172 sbuf_bcat(*sb, s, 1); 173 } 174 break; 175 /* 176 * Morse. 177 * '.' becomes _- 178 * '-' becomes _--- 179 * ' ' becomes __ 180 * '\n' becomes ____ 181 * 1sec pause between repeats 182 * '... --- ...' -> _-_-_-___---_---_---___-_-_-__________ 183 */ 184 case 'm': 185 for(s++; *s; s++) { 186 if (*s == '.') 187 sbuf_cat(*sb, "aA"); 188 else if (*s == '-') 189 sbuf_cat(*sb, "aC"); 190 else if (*s == ' ') 191 sbuf_cat(*sb, "b"); 192 else if (*s == '\n') 193 sbuf_cat(*sb, "d"); 194 } 195 sbuf_cat(*sb, "j"); 196 break; 197 default: 198 sbuf_delete(*sb); 199 return (EINVAL); 200 } 201 error = sbuf_finish(*sb); 202 if (error != 0 || sbuf_len(*sb) == 0) { 203 *sb = NULL; 204 return (error); 205 } 206 return (0); 207 } 208 209 static int 210 led_open(struct dev_open_args *ap) 211 { 212 return (0); 213 } 214 215 static int 216 led_close(struct dev_close_args *ap) 217 { 218 return (0); 219 } 220 221 static int 222 led_write(struct dev_write_args *ap) 223 { 224 struct uio *uio = ap->a_uio; 225 cdev_t dev = ap->a_head.a_dev; 226 struct ledsc *sc; 227 char *s; 228 struct sbuf *sb = NULL; 229 int error, state = 0; 230 231 if (uio->uio_resid > 512) 232 return (EINVAL); 233 s = kmalloc(uio->uio_resid + 1, M_DEVBUF, M_WAITOK); 234 s[uio->uio_resid] = '\0'; 235 error = uiomove(s, uio->uio_resid, uio); 236 if (error) { 237 kfree(s, M_DEVBUF); 238 return (error); 239 } 240 error = led_parse(s, &sb, &state); 241 kfree(s, M_DEVBUF); 242 if (error) 243 return (error); 244 lockmgr(&led_lock, LK_EXCLUSIVE); 245 sc = dev->si_drv1; 246 if (sc != NULL) 247 error = led_state(sc, &sb, state); 248 lockmgr(&led_lock, LK_RELEASE); 249 if (sb != NULL) 250 sbuf_delete(sb); 251 return (error); 252 } 253 254 int 255 led_set(char const *name, char const *cmd) 256 { 257 struct ledsc *sc; 258 struct sbuf *sb = NULL; 259 int error, state = 0; 260 261 error = led_parse(cmd, &sb, &state); 262 if (error) 263 return (error); 264 lockmgr(&led_lock, LK_EXCLUSIVE); 265 LIST_FOREACH(sc, &led_list, list) { 266 if (strcmp(sc->name, name) == 0) 267 break; 268 } 269 if (sc != NULL) 270 error = led_state(sc, &sb, state); 271 else 272 error = ENOENT; 273 lockmgr(&led_lock, LK_RELEASE); 274 if (sb != NULL) 275 sbuf_delete(sb); 276 return (0); 277 } 278 279 static struct dev_ops led_ops = { 280 { "LED", 0, 0 }, 281 .d_open = led_open, 282 .d_close = led_close, 283 .d_write = led_write, 284 }; 285 286 struct cdev * 287 led_create(led_t *func, void *priv, char const *name) 288 { 289 290 return (led_create_state(func, priv, name, 0)); 291 } 292 struct cdev * 293 led_create_state(led_t *func, void *priv, char const *name, int state) 294 { 295 struct ledsc *sc; 296 297 sc = kmalloc(sizeof *sc, M_LED, M_WAITOK | M_ZERO); 298 299 lockmgr(&led_lock2, LK_EXCLUSIVE); 300 sc->name = kstrdup(name, M_LED); 301 sc->unit = alloc_unr(led_unit); 302 sc->private = priv; 303 sc->func = func; 304 sc->dev = make_dev(&led_ops, sc->unit, 305 UID_ROOT, GID_WHEEL, 0600, "led/%s", name); 306 lockmgr(&led_lock2, LK_RELEASE); 307 308 lockmgr(&led_lock, LK_EXCLUSIVE); 309 sc->dev->si_drv1 = sc; 310 LIST_INSERT_HEAD(&led_list, sc, list); 311 sc->func(sc->private, state != 0); 312 lockmgr(&led_lock, LK_RELEASE); 313 314 return (sc->dev); 315 } 316 317 void 318 led_destroy(struct cdev *dev) 319 { 320 struct ledsc *sc; 321 322 lockmgr(&led_lock, LK_EXCLUSIVE); 323 sc = dev->si_drv1; 324 dev->si_drv1 = NULL; 325 if (sc->ptr != NULL) 326 blinkers--; 327 LIST_REMOVE(sc, list); 328 if (LIST_EMPTY(&led_list)) 329 callout_stop(&led_ch); 330 lockmgr(&led_lock, LK_RELEASE); 331 332 lockmgr(&led_lock2, LK_EXCLUSIVE); 333 free_unr(led_unit, sc->unit); 334 destroy_dev(dev); 335 if (sc->spec != NULL) 336 sbuf_delete(sc->spec); 337 kfree(sc->name, M_LED); 338 kfree(sc, M_LED); 339 lockmgr(&led_lock2, LK_RELEASE); 340 } 341 342 static void 343 led_drvinit(void *unused) 344 { 345 346 led_unit = new_unrhdr(0, INT_MAX, NULL); 347 lockinit(&led_lock, "LED lock", 0, LK_CANRECURSE); 348 lockinit(&led_lock2, "LED lock2", 0, LK_CANRECURSE); 349 callout_init_mp(&led_ch); 350 } 351 352 SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL); 353