编写适用于 Oracle® Solaris 11.2 的设备驱动程序

退出打印视图

更新时间: 2014 年 9 月
 
 

close() 入口点(块驱动程序)

close(9E) 入口点使用与 open(9E) 相同的参数,但有一个例外。dev 是设备编号,而不是指向设备编号的指针。

close() 例程应采用与前面所述的 open(9E)入口点所采用的相同的方式来检验 otyp。在以下示例中,close() 必须确定何时可以真正关闭设备。关闭受块打开数和分层打开数的影响。

示例 16-3  块设备 close(9E) 例程
static int
xxclose(dev_t dev, int flag, int otyp, cred_t *credp)
{
    minor_t instance;
    struct xxstate *xsp;

    instance = getminor(dev);
    xsp = ddi_get_soft_state(statep, instance);
    if (xsp == NULL)
        return (ENXIO);
    mutex_enter(&xsp->mu);
    switch (otyp) {
      case OTYP_LYR:
          xsp->nlayered--;
          break;
      case OTYP_BLK:
          xsp->open = 0;
          break;
      default:
          mutex_exit(&xsp->mu);
         return (EINVAL);
    }

    if (xsp->open || xsp->nlayered) {
        /* not done yet */
        mutex_exit(&xsp->mu);
        return (0);
    }
    /* cleanup (rewind tape, free memory, etc.) */
    /* wait for I/O to drain */
    mutex_exit(&xsp->mu);

    return (0);
}