ioctl 命令的实现
ioctl 的 scull 实现只传递设备的配置参数, 并且象下面这样容易: switch(cmd)
{
case SCULL_IOCRESET:
scull_quantum = SCULL_QUANTUM; scull_qset = SCULL_QSET; break;
case SCULL_IOCSQUANTUM: /* Set: arg points to the value */ if (! capable (CAP_SYS_ADMIN))
return -EPERM;
retval = get_user(scull_quantum, (int user *)arg); break;
case SCULL_IOCTQUANTUM: /* Tell: arg is the value */ if (! capable (CAP_SYS_ADMIN))
return -EPERM; scull_quantum = arg; break;
case SCULL_IOCGQUANTUM: /* Get: arg is pointer to result */ retval = put_user(scull_quantum, (int user *)arg); break;
case SCULL_IOCQQUANTUM: /* Query: return it (it's positive) */ return scull_quantum;
case SCULL_IOCXQUANTUM: /* eXchange: use arg as pointer */ if (! capable (CAP_SYS_ADMIN))
return -EPERM; tmp = scull_quantum;
retval = get_user(scull_quantum, (int user *)arg);
if (retval == 0)
retval = put_user(tmp, (int user *)arg);
break;
case SCULL_IOCHQUANTUM: /* sHift: like Tell + Query */ if (! capable (CAP_SYS_ADMIN))
return -EPERM;
tmp = scull_quantum; scull_quantum = arg; return tmp;
default: /* redundant, as cmd was checked against MAXNR */ return -ENOTTY;
}
return retval;
scull 还包含 6 个入口项作用于 scull_qset. 这些入口项和给 scull_quantum 的是一 致的, 并且不值得展示出来.
从调用者的观点看(即从用户空间), 这 6 种传递和接收参数的方法看来如下: int quantum;
ioctl(fd,SCULL_IOCSQUANTUM, &quantum); /* Set by pointer */
ioctl(fd,SCULL_IOCTQUANTUM, quantum); /* Set by value */ ioctl(fd,SCULL_IOCGQUANTUM, &quantum); /* Get by pointer */ quantum = ioctl(fd,SCULL_IOCQQUANTUM); /* Get by return value */ ioctl(fd,SCULL_IOCXQUANTUM, &quantum); /* Exchange by pointer */
quantum = ioctl(fd,SCULL_IOCHQUANTUM, quantum); /* Exchange by value */
当然, 一个正常的驱动不可能实现这样一个调用模式的混合体. 我们这里这样做只是为了 演示做事情的不同方式. 但是, 正常地, 数据交换将一致地进行, 通过指针或者通过值, 并且要避免混合这 2 种技术.