1
0
mirror of synced 2026-01-11 23:42:59 +00:00

ioctl on/off switch on stats + ctrl pgm

This commit is contained in:
Romain Dolbeau 2021-08-23 04:48:05 -04:00
parent eede097217
commit 170c540cf7
3 changed files with 93 additions and 3 deletions

View File

@ -58,7 +58,7 @@ CFATTACH_DECL_NEW(sbusfpga_stat, sizeof(struct sbusfpga_sbus_bus_stat_softc),
dev_type_open(sbusfpga_stat_open);
dev_type_close(sbusfpga_stat_close);
dev_type_ioctl(sbusfpga_stat_ioctl);
const struct cdevsw sbusfpga_stat_cdevsw = {
@ -66,7 +66,7 @@ const struct cdevsw sbusfpga_stat_cdevsw = {
.d_close = sbusfpga_stat_close,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_ioctl = sbusfpga_stat_ioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
@ -187,9 +187,42 @@ sbusfpga_stat_attach(device_t parent, device_t self, void *aux)
callout_init(&sc->sc_display, CALLOUT_MPSAFE);
callout_setfunc(&sc->sc_display, sbusfpga_stat_display, sc);
/* disable by default */
sc->sc_enable = 0;
/* do it once during boot*/
callout_schedule(&sc->sc_display, sc->sc_delay);
}
#define SBUSFPGA_STAT_ON _IO(0, 1)
#define SBUSFPGA_STAT_OFF _IO(0, 0)
int
sbusfpga_stat_ioctl (dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct sbusfpga_sbus_bus_stat_softc *sc = device_lookup_private(&sbusfpga_stat_cd, minor(dev));
int err = 0;
switch (cmd) {
case SBUSFPGA_STAT_ON:
if (!sc->sc_enable) {
sc->sc_enable = 1;
callout_schedule(&sc->sc_display, sc->sc_delay);
}
break;
case SBUSFPGA_STAT_OFF:
if (sc->sc_enable) {
callout_stop(&sc->sc_display);
sc->sc_enable = 0;
}
break;
default:
err = ENOTTY;
break;
}
return err;
}
static void sbusfpga_stat_display(void *args) {
struct sbusfpga_sbus_bus_stat_softc *sc = args;
unsigned int c = sbus_bus_stat_stat_cycle_counter_read(sc), c2;
@ -219,5 +252,6 @@ static void sbusfpga_stat_display(void *args) {
sbus_bus_stat_sbus_master_error_virtual_read(sc));
}
sbus_bus_stat_stat_ctrl_write(sc, 0);
callout_schedule(&sc->sc_display, sc->sc_delay);
if (sc->sc_enable)
callout_schedule(&sc->sc_display, sc->sc_delay);
}

View File

@ -39,6 +39,7 @@ struct sbusfpga_sbus_bus_stat_softc {
int sc_bufsiz; /* Size of buffer */
callout_t sc_display;
int sc_delay;
int sc_enable;
};
#endif /* _SBUSFPGA_STAT_H_ */

View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/ioccom.h>
#include <errno.h>
#define SBUSFPGA_STAT_ON _IO(0, 1)
#define SBUSFPGA_STAT_OFF _IO(0, 0)
int main(int argc, char **argv) {
const char const * device = "/dev/sbusfpga_stat0";
int devfd;
int onoff;
if (argc != 2) {
fprintf(stderr, "Usage: %s on|off\n", argv[0]);
return -1;
}
if (strncmp("on", argv[1], 2) == 0) {
onoff = 1;
} else if (strncmp("off", argv[1], 3) == 0) {
onoff = 0;
} else {
fprintf(stderr, "Usage: %s on|off\n", argv[0]);
return -1;
}
if ( (devfd = open(device, O_RDWR)) == -1) {
perror("can't open device file");
return -1;
}
switch (onoff) {
case 0:
if (ioctl(devfd, SBUSFPGA_STAT_OFF, NULL)) {
perror("Turning statistics off failed.");
close(devfd);
return -1;
}
break;
case 1:
if (ioctl(devfd, SBUSFPGA_STAT_ON, NULL)) {
perror("Turning statistics on failed.");
close(devfd);
return -1;
}
break;
}
return 0;
}