Subject: sysctl: How to get softc address in a helper function?
To: None <tech-kern@NetBSD.org>
From: TAMURA Kent <kent@NetBSD.org>
List: tech-kern
Date: 10/24/2004 15:35:02
How to get softc address in a sysctl helper function?  I'm
trying to implement per-device-instance sysctl items (not
per-device-driver like ath and bge.)

The foo_attach() function creates a node and leaf items, and
saves their MIB numbers into a softc instance.  To check values
in a helper function, we need to refer the softc to determine
which sysctl item is processed.  I have no idea how to pass the
softc address to the helper function.


The following is a dirty hacking code using sysctlnode::__rsvd.
Of course, I don't commit it.

--------------------------------
	/* sysctl setup */
	err = sysctl_createv(NULL, 0, NULL, NULL, 0,
			     CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0,
			     CTL_HW, CTL_EOL);
	if (err != 0)
		goto sysctl_err;
	err = sysctl_createv(NULL, 0, NULL, &node, 0,
			     CTLTYPE_NODE, sc->sc_dev.dv_xname, NULL, NULL, 0,
			     NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
	if (err != 0)
		goto sysctl_err;
	node_mib = node->sysctl_num;
	err = sysctl_createv(NULL, 0, NULL, &node, CTLFLAG_READWRITE,
			     CTLTYPE_INT, "ac97rate",
			     SYSCTL_DESCR("AC'97 link rate; normal value is 48000"),
			     auich_sysctl_verify, 0, &sc->sc_ac97_clock, 0,
			     CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
	if (err != 0)
		goto sysctl_err;
	sc->sc_ac97_clock_mib = node->sysctl_num;
	node->__rsvd = (uint32_t)sc;	/* XXX */
--------------------------------

static int
auich_sysctl_verify(SYSCTLFN_ARGS)
{
	int error, t;
	struct sysctlnode node;
	struct auich_softc *sc;

	node = *rnode;
	t = *(int*)rnode->sysctl_data;
	node.sysctl_data = &t;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	sc = (struct auich_softc*)node.__rsvd; /* XXX */
	if (node.sysctl_num == sc->sc_ac97_clock_mib) {
		if (t < 48000 || t > 96000)
			return EINVAL;
	}
	*(int*)rnode->sysctl_data = t;

	return 0;
}
--------------------------------

-- 
TAMURA Kent <kent2004? at hauN.org> <kent at NetBSD.org>