Subject: Re: Device driver softc questions
To: Steven Grunza <>
From: Nathan J. Williams <nathanw@MIT.EDU>
List: tech-kern
Date: 03/15/2001 18:42:26
Steven Grunza <> writes:

> In the attach() routine, the second parameter is a pointer to a
> structure that appears to be called a softc.  This structure appears to
> hold information for the driver.  Where (in the source code) is the
> memory for this structure allocated?

In the config_attach() routine in kern/subr_autoconf.c, line 363 as of
version 1.55:

        /* get memory for all device vars */
        dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
            cold ? M_NOWAIT : M_WAITOK);

"ca->ca_devsize" is from the struct cfattach that the driver declares
and that ends up in the cfdata[] table defined in ioconf.c. 

It's worth noting that the autoconf system assumes that the first
piece of a softc is a "struct device", and Bad Things will happen if
you declare one otherwise and then step on what the autoconf system
thinks of as its data.

> But where is a pointer to this struct puc_softc saved so the driver can
> get to it later?  This driver doesn't have puc_open() and
> puc_close() calls but how would they figure out where the struct
> puc_softc is located?

By using the the device number to index into the cfdriver structure,
again defined in ioconf.c. An example from sys/dev/scsipi/sd.c (edited
down a bit):

extern struct cfdriver sd_cd;
sdclose(dev, flag, fmt, p)
        dev_t dev;
        int flag, fmt;
        struct proc *p;
        struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];

        - Nathan