通过IOCTL 进行内核态与用户态交互

 

#include <linux/init.h>
#include <linux/printk.h>
#include <linux/module.h>

#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
/*============================= 模块信息=========================================*/
#define HI_NAME "HelloWorld"
#define HI_MOD_VER "V1.0.0"

/*=============================MISC设备驱动 IOCTL=======================================*/
#define IOC_MAGIC 'x'
#define IOC_CMD_1 _IOWR(IOC_MAGIC, 1, __u32)

struct ioc_data_s{
	unsigned int size;
	char buf[10];
};

struct ioc_data_s mydata = {
	.size = 10,
	.buf  = "World",
};
/*=============================MISC设备驱动=======================================*/
#define HI_MISC_DEV_NAME "hi1"

static int hi_misc_open(struct inode *inode, struct file *file)
{
	return 0;
}
static long hi_misc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct ioc_data_s indata;
	switch(cmd){
		case IOC_CMD_1:
			copy_from_user(&indata, (struct ioc_data_s*)arg, sizeof(struct ioc_data_s));
			printk("from user: %s\n",indata.buf);
			copy_to_user((void*)arg, &mydata, sizeof(struct ioc_data_s));
			break;
	}

	return 0;
}
static const struct file_operations hi_misc_fops = {
	.owner = THIS_MODULE,
	//.open = hi_misc_open,
	.unlocked_ioctl = hi_misc_ioctl,
};

static struct miscdevice hi_misc_dev =
{
	.minor = MISC_DYNAMIC_MINOR,
	.name = HI_MISC_DEV_NAME,
	.fops = &hi_misc_fops,
};
/*=============================模块框架接口========================================*/
static int __init hi_mod_init(void)
{
#if 1
	int ret;
	ret = misc_register(&hi_misc_dev);
	if(ret < 0){
		printk("hi misc_register failed(ret=%d)!\n", ret);
	}
#endif
	return 0;
}

static void __exit hi_mod_exit(void)
{
#if 1
	misc_deregister(&hi_misc_dev);
#endif
}
/*=============================模块框架===========================================*/

module_init(hi_mod_init);
module_exit(hi_mod_exit);

MODULE_AUTHOR("momo");
MODULE_DESCRIPTION("HelloWorld driver using misc dev.");
MODULE_VERSION( HI_MOD_VER " (" __DATE__ " " __TIME__ ")");
MODULE_LICENSE("GPL");

 

用户态代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <linux/ioctl.h>
#define IOC_MAGIC 'x'
#define IOC_CMD_1 _IOWR(IOC_MAGIC, 1, unsigned int)

struct ioc_data_s{
	unsigned int size;
	char buf[10];
};

struct ioc_data_s mydata = {
    .size = 10,
    .buf  = "hello",
};
 
int main()
{
	int fd = 0;
	char buf[10] = "Hello";

	fd = open("/dev/hi1",O_RDWR);
	if (fd < 0)
	{
		printf("打开设备hi1失败!\n");
		return -1;
	}

	if (ioctl(fd, IOC_CMD_1, &mydata) < 0)
	{
		return -1;
	}

	printf("from kernel:%s\n",mydata.buf);
	close(fd);
	return 0;
}

 

posted @ 2023-09-25 11:46  秦舒云  阅读(106)  评论(0编辑  收藏  举报