User Tools

Site Tools


programming:linux:kernel-ioctl

An ioctl called from within kernel code

This is an example of a network device MII ioctl, called from within kernel code.

const struct net_device_ops *ops = ndev->netdev_ops;
int err = -ENXIO;
 
if (ops->ndo_do_ioctl) {
	struct ifreq ifr;
	struct mii_ioctl_data mii;
 
	mm_segment_t oldfs = get_fs();
 
	mii.reg_num = MII_BMCR;
	ifr.ifr_ifru.ifru_data = (__force void __user *)&mii;
	set_fs(KERNEL_DS);
 
	err = ops->ndo_do_ioctl(ndev, &ifr, SIOCGMIIPHY);
	if (!err) {
		mii.val_in = (mii.val_out & ~BMCR_RESET) | BMCR_ISOLATE;
		err = ops->ndo_do_ioctl(ndev, &ifr, SIOCSMIIREG);
		netdev_dbg(ndev, "%s: MII ioctl setting BMCR = 0x%04x\n",
			__func__, mii.val_in);
	}
 
	set_fs(oldfs);
}
programming/linux/kernel-ioctl.txt · Last modified: 2019/03/06 11:12 by Ralf H.