Linux驱动---字符设备ioctl接口

1、为什么要引入ioctl

在这里插入图片描述

2、应用层系统调用

在这里插入图片描述

3、内核接口

在这里插入图片描述

4、调用关系

在这里插入图片描述

5、cmd及封装命令

在这里插入图片描述
在这里插入图片描述
封装命令这些宏定义,内核空间已经提供给我们,我们直接调用就可

在这里插入图片描述
从根目录找,有一个Documentation/ioctl/ioctl-number.txt,在这个目录里面有一些说明文档,里面放的一些内核已经使用过的一些设备类型(type),有一些是已经被占用;
这时我们写0,1,2这些就会和内核冲突(但是不太影响,也能用)

6、任何检测命令、地址的正确性?

在这里插入图片描述
在这里插入图片描述

头文件:

#ifndef _BEEP_H_
#define _BEEP_H_

#define DEV_FIFO_TYPE     'K'
#define DEV_FIFO_CLEAN    _IO(DEV_FIFO_TYPE,0)
#define DEV_FIFO_GETVALUE _IOR(DEV_FIFO_TYPE,1,int)
#define DEV_FIFO_SETVALUE _IOW(DEV_FIFO_TYPE,2,int)


#endif



实例代码:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/io.h>

#include "beep.h"


static int major = 237;
static int minor = 0;
static dev_t devno;
static struct cdev cdev;
struct device *class_dev = NULL;
struct class *cls;


static int hello_open (struct inode *inode, struct file *filep)
{
	printk("hello_open()\n");
	return 0;
}
static int hello_release (struct inode *inode, struct file *filep)
{
	printk("hello_release()\n");

	return 0;
}

#define KMAX_LEN 32
char kbuf[KMAX_LEN+1] = "kernel";


//read(fd,buff,40);

static ssize_t hello_read (struct file *filep, char __user *buf, size_t size, loff_t *pos)
{
	int error;

	
	if(size > strlen(kbuf))
	{
		size = strlen(kbuf);
	}

	if(copy_to_user(buf,kbuf, size))
	{
		error = -EFAULT;
		return error;
	}

	return size;
}
//write(fd,buff,40);
static ssize_t hello_write (struct file *filep, const char __user *buf, size_t size, loff_t *pos)
{
	int error;

	if(size > KMAX_LEN)
	{
		size = KMAX_LEN;
	}
	memset(kbuf,0,sizeof(kbuf));
	if(copy_from_user(kbuf, buf, size))
	{
		error = -EFAULT;
		return error;
	}
	printk("%s\n",kbuf);
	return size;
}

static int knum = 99;


/*

ioctl(fd,DEV_FIFO_GETVALUE, &num);
*/

long hello_ioctl (struct file *filep, unsigned int cmd, unsigned long arg)
{
	long err,ret;
	void __user *argp = (void __user *)arg;
	int __user *p = argp;


	if(_IOC_TYPE(cmd)!=DEV_FIFO_TYPE){
		pr_err("cmd   %u,bad magic 0x%x/0x%x.\n",cmd,_IOC_TYPE(cmd),DEV_FIFO_TYPE);
		return-ENOTTY;
	}
	if(_IOC_DIR(cmd)&_IOC_READ)
		ret=!access_ok(VERIFY_WRITE,(void __user*)arg,_IOC_SIZE(cmd));
	else if( _IOC_DIR(cmd)&_IOC_WRITE )
		ret=!access_ok(VERIFY_READ,(void   __user*)arg,_IOC_SIZE(cmd));

	if(ret){
		pr_err("bad   access %ld.\n",ret);
		return-EFAULT;
	}

	switch(cmd)
	{
		case DEV_FIFO_CLEAN:
			printk("DEV_FIFO_CLEAN\n");
			break;
		case DEV_FIFO_GETVALUE:
			err = put_user(knum, p);
			printk("DEV_FIFO_GETVALUE %d\n",knum);
			break;
		case DEV_FIFO_SETVALUE:
			err = get_user(knum, p);
			printk("DEV_FIFO_SETVALUE %d\n",knum);
			break;
		default:
			return -EINVAL;
			
	}
	return err;
}
static struct file_operations hello_ops = 
{
	.open = hello_open,
	.release = hello_release,
	.read = hello_read,
	.write = hello_write,
	.unlocked_ioctl = hello_ioctl,
};
static int hello_init(void)
{
	int result;
	int error;
	
	printk("hello_init \n");
	result = register_chrdev( major, "hello", &hello_ops);
	if(result < 0)
	{
		printk("register_chrdev fail \n");
		return result;
	}
	cls = class_create(THIS_MODULE, "hellocls");
	if (IS_ERR(cls)) {
		printk(KERN_ERR "class_create() failed for cls\n");
		result = PTR_ERR(cls);
		goto out_err_1;
	}
	devno = MKDEV(major, minor);
	
	class_dev = device_create(cls, NULL, devno, NULL, "hellodev");
	if (IS_ERR(class_dev)) {
		result = PTR_ERR(class_dev);
		goto out_err_2;
	}
	
	return 0;

out_err_2:
	class_destroy(cls);
out_err_1:
	unregister_chrdev(major,"hello");
	return 	result;
}
static void hello_exit(void)
{
	printk("hello_exit \n");
	device_destroy(cls, devno);
	class_destroy(cls);
	unregister_chrdev(major,"hello");
	return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
//proc/devices

测试程序:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "beep.h"
main()
{
	int fd;
	int len;
	int num;
	
	
	fd = open("/dev/hellodev",O_RDWR);
	if(fd<0)
	{
		perror("open fail \n");
		return;
	}

	ioctl(fd,DEV_FIFO_CLEAN);

	ioctl(fd,DEV_FIFO_GETVALUE,num);
	printf("num=%d\n", num);

	num = 77;
	ioctl(fd,DEV_FIFO_SETVALUE,num);
	 
	close(fd);
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值