Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/led/led.c
Show All 11 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/conf.h> | #include <sys/conf.h> | ||||
#include <sys/ctype.h> | #include <sys/ctype.h> | ||||
#include <sys/kernel.h> | #include <sys/kernel.h> | ||||
#include <sys/kthread.h> | |||||
#include <sys/limits.h> | #include <sys/limits.h> | ||||
#include <sys/lock.h> | #include <sys/lock.h> | ||||
#include <sys/malloc.h> | #include <sys/malloc.h> | ||||
#include <sys/mutex.h> | #include <sys/mutex.h> | ||||
#include <sys/queue.h> | #include <sys/queue.h> | ||||
#include <sys/sbuf.h> | #include <sys/sbuf.h> | ||||
#include <sys/sx.h> | #include <sys/sx.h> | ||||
#include <sys/systm.h> | #include <sys/systm.h> | ||||
#include <sys/uio.h> | #include <sys/uio.h> | ||||
#include <dev/led/led.h> | #include <dev/led/led.h> | ||||
struct ledsc { | struct ledsc { | ||||
LIST_ENTRY(ledsc) list; | LIST_ENTRY(ledsc) list; | ||||
char *name; | char *name; | ||||
void *private; | void *private; | ||||
int unit; | int unit; | ||||
led_t *func; | led_t *func; | ||||
struct cdev *dev; | struct cdev *dev; | ||||
struct sbuf *spec; | struct sbuf *spec; | ||||
char *str; | char *str; | ||||
char *ptr; | char *ptr; | ||||
int count; | int count; | ||||
time_t last_second; | time_t last_second; | ||||
bool in_use; | |||||
bool destroying; | |||||
}; | }; | ||||
static struct unrhdr *led_unit; | static struct unrhdr *led_unit; | ||||
static struct mtx led_mtx; | static struct mtx led_mtx; | ||||
static struct sx led_sx; | static struct sx led_sx; | ||||
static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list); | static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(led_list); | ||||
static struct callout led_ch; | |||||
static int blinkers = 0; | static int blinkers = 0; | ||||
static MALLOC_DEFINE(M_LED, "LED", "LED driver"); | static MALLOC_DEFINE(M_LED, "LED", "LED driver"); | ||||
static void | static void | ||||
led_timeout(void *p) | led_timeout(void) | ||||
{ | { | ||||
struct ledsc *sc; | struct ledsc *sc; | ||||
int state; | |||||
LIST_FOREACH(sc, &led_list, list) { | LIST_FOREACH(sc, &led_list, list) { | ||||
if (sc->ptr == NULL) | if (sc->ptr == NULL) | ||||
continue; | continue; | ||||
if (sc->count > 0) { | if (sc->count > 0) { | ||||
sc->count--; | sc->count--; | ||||
continue; | continue; | ||||
} | } | ||||
if (*sc->ptr == '.') { | if (*sc->ptr == '.') { | ||||
sc->ptr = NULL; | sc->ptr = NULL; | ||||
blinkers--; | blinkers--; | ||||
continue; | continue; | ||||
} else if (*sc->ptr == 'U' || *sc->ptr == 'u') { | } else if (*sc->ptr == 'U' || *sc->ptr == 'u') { | ||||
if (sc->last_second == time_second) | if (sc->last_second == time_second) | ||||
continue; | continue; | ||||
sc->last_second = time_second; | sc->last_second = time_second; | ||||
sc->func(sc->private, *sc->ptr == 'U'); | state = *sc->ptr == 'U'; | ||||
} else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') { | } else if (*sc->ptr >= 'a' && *sc->ptr <= 'j') { | ||||
sc->func(sc->private, 0); | state = 0; | ||||
sc->count = (*sc->ptr & 0xf) - 1; | sc->count = (*sc->ptr & 0xf) - 1; | ||||
} else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') { | } else if (*sc->ptr >= 'A' && *sc->ptr <= 'J') { | ||||
sc->func(sc->private, 1); | state = 1; | ||||
sc->count = (*sc->ptr & 0xf) - 1; | sc->count = (*sc->ptr & 0xf) - 1; | ||||
} | } | ||||
sc->ptr++; | sc->ptr++; | ||||
if (*sc->ptr == '\0') | if (*sc->ptr == '\0') | ||||
sc->ptr = sc->str; | sc->ptr = sc->str; | ||||
sc->in_use = true; | |||||
mtx_unlock(&led_mtx); | |||||
adrian: can we not keep adding unlock; thing; lock as a pattern? That means that things like the led… | |||||
sc->func(sc->private, state); | |||||
mtx_lock(&led_mtx); | |||||
sc->in_use = false; | |||||
if (sc->destroying) | |||||
wakeup(sc); | |||||
} | } | ||||
} | |||||
static void | |||||
led_blinker_thread(void *arg __unused) | |||||
{ | |||||
int timeout; | |||||
mtx_lock(&led_mtx); | |||||
for (;;) { | |||||
if (blinkers > 0) | if (blinkers > 0) | ||||
callout_reset(&led_ch, hz / 10, led_timeout, p); | timeout = hz / 10; | ||||
else | |||||
timeout = 0; | |||||
(void)mtx_sleep(&led_list, &led_mtx, 0, "blnkwt", timeout); | |||||
led_timeout(); | |||||
} | } | ||||
mtx_unlock(&led_mtx); | |||||
} | |||||
static int | static int | ||||
led_state(struct ledsc *sc, struct sbuf **sb, int state) | led_state(struct ledsc *sc, struct sbuf **sb, int state) | ||||
{ | { | ||||
struct sbuf *sb2 = NULL; | struct sbuf *sb2 = NULL; | ||||
sb2 = sc->spec; | sb2 = sc->spec; | ||||
sc->spec = *sb; | sc->spec = *sb; | ||||
sc->count = 0; | |||||
if (*sb != NULL) { | if (*sb != NULL) { | ||||
sc->str = sbuf_data(*sb); | sc->str = sbuf_data(*sb); | ||||
if (sc->ptr == NULL) { | if (sc->ptr == NULL) { | ||||
blinkers++; | blinkers++; | ||||
callout_reset(&led_ch, hz / 10, led_timeout, NULL); | if (blinkers == 1) | ||||
wakeup(&led_list); | |||||
} | } | ||||
sc->ptr = sc->str; | sc->ptr = sc->str; | ||||
} else { | } else { | ||||
sc->str = NULL; | sc->str = NULL; | ||||
if (sc->ptr != NULL) | if (sc->ptr != NULL) | ||||
blinkers--; | blinkers--; | ||||
sc->ptr = NULL; | sc->ptr = NULL; | ||||
sc->in_use = true; | |||||
mtx_unlock(&led_mtx); | |||||
sc->func(sc->private, state); | sc->func(sc->private, state); | ||||
mtx_lock(&led_mtx); | |||||
sc->in_use = false; | |||||
} | } | ||||
sc->count = 0; | |||||
*sb = sb2; | *sb = sb2; | ||||
return(0); | return(0); | ||||
} | } | ||||
static int | static int | ||||
led_parse(const char *s, struct sbuf **sb, int *state) | led_parse(const char *s, struct sbuf **sb, int *state) | ||||
{ | { | ||||
int i, error; | int i, error; | ||||
▲ Show 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | led_create_state(led_t *func, void *priv, char const *name, int state) | ||||
sc->func = func; | sc->func = func; | ||||
sc->dev = make_dev(&led_cdevsw, sc->unit, | sc->dev = make_dev(&led_cdevsw, sc->unit, | ||||
UID_ROOT, GID_WHEEL, 0600, "led/%s", name); | UID_ROOT, GID_WHEEL, 0600, "led/%s", name); | ||||
sx_xunlock(&led_sx); | sx_xunlock(&led_sx); | ||||
mtx_lock(&led_mtx); | mtx_lock(&led_mtx); | ||||
sc->dev->si_drv1 = sc; | sc->dev->si_drv1 = sc; | ||||
LIST_INSERT_HEAD(&led_list, sc, list); | LIST_INSERT_HEAD(&led_list, sc, list); | ||||
mtx_unlock(&led_mtx); | |||||
if (state != -1) | if (state != -1) | ||||
sc->func(sc->private, state != 0); | sc->func(sc->private, state != 0); | ||||
mtx_unlock(&led_mtx); | |||||
return (sc->dev); | return (sc->dev); | ||||
} | } | ||||
void | void | ||||
led_destroy(struct cdev *dev) | led_destroy(struct cdev *dev) | ||||
{ | { | ||||
struct ledsc *sc; | struct ledsc *sc; | ||||
mtx_lock(&led_mtx); | mtx_lock(&led_mtx); | ||||
sc = dev->si_drv1; | sc = dev->si_drv1; | ||||
sc->destroying = true; | |||||
while (sc->in_use) | |||||
(void)mtx_sleep(sc, &led_mtx, 0, "ledbsy", 0); | |||||
dev->si_drv1 = NULL; | dev->si_drv1 = NULL; | ||||
if (sc->ptr != NULL) | if (sc->ptr != NULL) | ||||
blinkers--; | blinkers--; | ||||
LIST_REMOVE(sc, list); | LIST_REMOVE(sc, list); | ||||
if (LIST_EMPTY(&led_list)) | |||||
callout_stop(&led_ch); | |||||
mtx_unlock(&led_mtx); | mtx_unlock(&led_mtx); | ||||
sx_xlock(&led_sx); | sx_xlock(&led_sx); | ||||
free_unr(led_unit, sc->unit); | free_unr(led_unit, sc->unit); | ||||
destroy_dev(dev); | destroy_dev(dev); | ||||
if (sc->spec != NULL) | if (sc->spec != NULL) | ||||
sbuf_delete(sc->spec); | sbuf_delete(sc->spec); | ||||
free(sc->name, M_LED); | free(sc->name, M_LED); | ||||
free(sc, M_LED); | free(sc, M_LED); | ||||
sx_xunlock(&led_sx); | sx_xunlock(&led_sx); | ||||
} | } | ||||
static void | static void | ||||
led_drvinit(void *unused) | led_drvinit(void *unused) | ||||
{ | { | ||||
led_unit = new_unrhdr(0, INT_MAX, NULL); | led_unit = new_unrhdr(0, INT_MAX, NULL); | ||||
mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF); | mtx_init(&led_mtx, "LED mtx", NULL, MTX_DEF); | ||||
sx_init(&led_sx, "LED sx"); | sx_init(&led_sx, "LED sx"); | ||||
callout_init_mtx(&led_ch, &led_mtx, 0); | |||||
if (kproc_create(led_blinker_thread, NULL, NULL, 0, 0, | |||||
"led blinker") != 0) { | |||||
panic("led_drvinit: can't create thread"); | |||||
} | |||||
} | } | ||||
SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL); | SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL); |
can we not keep adding unlock; thing; lock as a pattern? That means that things like the led list can be modified whilst the func() is being called.