系统调用 getinfo(9E) 以获取仅为驱动程序所知的配置信息。次要设备号到设备实例的映射完全由驱动程序控制。有时系统需要询问驱动程序特定的 dev_t 代表哪个设备。
getinfo() 函数可以采用 DDI_INFO_DEVT2INSTANCE 或 DDI_INFO_DEVT2DEVINFO 作为其 infocmd 参数。DDI_INFO_DEVT2INSTANCE 命令请求设备的实例编号。DDI_INFO_DEVT2DEVINFO 命令请求指向设备的 dev_info 结构的指针。
如果是 DDI_INFO_DEVT2INSTANCE,则 arg 为 dev_t,并且 getinfo() 必须将 dev_t 中的次要设备号转换为实例编号。在以下示例中,次要设备号是实例编号,因此 getinfo() 仅传回次要设备号。在这种情况下,驱动程序不能假定状态结构可用,因为可能在调用 attach() 之前调用 getinfo()。由驱动程序定义的次要设备号和实例编号之间的映射关系可以与此示例中的不同。但是,在所有情况下,映射必须是静态的。
如果是 DDI_INFO_DEVT2DEVINFO,则 arg 仍为 dev_t,因此,getinfo() 首先将设备的实例编号解码。然后 getinfo() 传送回保存在相应设备的驱动程序软状态结构中的 dev_info 指针,如以下示例所示。
/* * getinfo(9e) * Return the instance number or device node given a dev_t */ static int xxgetinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) { int error; Pio *pio_p; int instance = getminor((dev_t)arg); switch (infocmd) { /* * return the device node if the driver has attached the * device instance identified by the dev_t value which was passed */ case DDI_INFO_DEVT2DEVINFO: pio_p = ddi_get_soft_state(pio_softstate, instance); if (pio_p == NULL) { *result = NULL; error = DDI_FAILURE; } else { mutex_enter(&pio_p->mutex); *result = pio_p->dip; mutex_exit(&pio_p->mutex); error = DDI_SUCCESS; } break; /* * the driver can always return the instance number given a dev_t * value, even if the instance is not attached. */ case DDI_INFO_DEVT2INSTANCE: *result = (void *)instance; error = DDI_SUCCESS; break; default: *result = NULL; error = DDI_FAILURE; } return (error); }
getinfo() 例程必须与驱动程序创建的次要节点保持同步。如果次要节点不同步,则任何热插拔操作都可能失败并导致系统混乱。