FDD-book/examples/part-05/ch25-advanced/myfirst_ioctl.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

68 lines
1.6 KiB
C

/*
* myfirst_ioctl.c - ioctl dispatch for the myfirst driver.
*
* The switch is the driver's entire public ioctl surface. Adding a
* command means adding a case here and the matching declaration in
* myfirst_ioctl.h. Retiring a command means removing its case here
* and marking the number retired in the header.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/bus.h>
#include "myfirst.h"
#include "myfirst_debug.h"
#include "myfirst_ioctl.h"
int
myfirst_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
struct thread *td)
{
struct myfirst_softc *sc = dev->si_drv1;
int error = 0;
DPRINTF(sc, MYF_DBG_IOCTL, "ioctl: cmd=0x%lx\n", cmd);
switch (cmd) {
case MYFIRSTIOC_GETVER:
*(int *)data = MYFIRST_IOCTL_VERSION;
break;
case MYFIRSTIOC_RESET:
mtx_lock(&sc->sc_mtx);
sc->sc_total_reads = 0;
sc->sc_total_writes = 0;
mtx_unlock(&sc->sc_mtx);
break;
case MYFIRSTIOC_GETMSG:
mtx_lock(&sc->sc_mtx);
strlcpy((char *)data, sc->sc_msg, MYFIRST_MSG_MAX);
mtx_unlock(&sc->sc_mtx);
break;
case MYFIRSTIOC_SETMSG:
mtx_lock(&sc->sc_mtx);
strlcpy(sc->sc_msg, (const char *)data,
sizeof(sc->sc_msg));
sc->sc_msglen = strlen(sc->sc_msg);
mtx_unlock(&sc->sc_mtx);
devctl_notify("myfirst", device_get_nameunit(sc->sc_dev),
"MSG_CHANGED", NULL);
break;
case MYFIRSTIOC_GETCAPS:
*(uint32_t *)data = MYF_CAP_RESET | MYF_CAP_GETMSG |
MYF_CAP_SETMSG;
break;
default:
error = ENOIOCTL;
break;
}
return (error);
}