Writing Device Drivers


The system calls getinfo(9E) to obtain configuration information that only the driver knows. The mapping of minor numbers to device instances is entirely under the control of the driver. The system sometimes needs to ask the driver which device a particular dev_t represents.

getinfo(9E) is called during module loading and at other times during the life of the driver. It can take one of two commands as its infocmd argument: DDI_INFO_DEVT2INSTANCE, which asks for a device's instance number, and DDI_INFO_DEVT2DEVINFO, which asks for pointer to the device's dev_info structure.

In the DDI_INFO_DEVT2INSTANCE case, arg is a dev_t, and getinfo(9E) must translate the minor number to an instance number. In the following example, the minor number is the instance number, so it simply passes back the minor number. In this case, the driver must not assume that a state structure is available, since getinfo(9E) may be called before attach(9E). The mapping the driver defines between minor device number and instance number does not necessarily follow the mapping shown in the example. In all cases, however, the mapping must be static.

In the DDI_INFO_DEVT2DEVINFO case, arg is again a dev_t, so getinfo(9E) first decodes the instance number for the device. It then passes back the dev_info pointer saved in the driver's soft state structure for the appropriate device. This is shown in Example 5-6.

Example 5-6 getinfo(9E) Routine

static int
xxgetinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
  void **result)
	struct xxstate *xsp;
	dev_t		dev;
	int		instance, error;
	switch (infocmd) {
	    	dev = (dev_t)arg;
	    	*result = (void *)getminor(dev);
	    	error = DDI_SUCCESS;

	    	dev = (dev_t)arg;
	    	instance = getminor(dev);
	    	xsp = ddi_get_soft_state(statep, instance);
	    	if (xsp == NULL)
			    return (DDI_FAILURE);
	    	*result = (void *)xsp->dip;
	    	error = DDI_SUCCESS;

	    	error = DDI_FAILURE;
	return (error);