对相应设备做指定的控制操作(各种属性的设置获取等等)
long xxx_ioctl (struct file *filp, unsigned int cmd, unsigned long arg);
功能:对相应设备做指定的控制操作(各种属性的设置获取等等)
参数:
filp:指向open产生的struct file类型的对象,表示本次ioctl对应的那次open
cmd:用来表示做的是哪一个操作
? arg:和cmd配合用的参数
返回值:成功为0,失败-1
cmd组成
dir(direction),ioctl 命令访问模式(属性数据传输方向),占据 2 bit,可以为 IOC_NONE、IOC_READ、IOC_WRITE、IOC_READ | _IOC_WRITE,分别指示了四种访问模式:无数据、读数据、写数据、读写数据;
type(device type),设备类型,占据 8 bit,在一些文献中翻译为 “幻数” 或者 “魔数”,可以为任意 char 型字符,例如 ‘a’、’b’、’c’ 等等,其主要作用是使 ioctl 命令有唯一的设备标识;
nr(number),命令编号/序数,占据 8 bit,可以为任意 unsigned char 型数据,取值范围 0~255,如果定义了多个 ioctl 命令,通常
从 0 开始编号递增;
size,涉及到 ioctl 函数 第三个参数 arg ,占据 13bit 或者 14bit(体系相关,arm 架构一般为 14 位),指定了 arg 的数据类型及长度,如果在驱动的 ioctl 实现中不检查,通常可以忽略该参数;
#define _IOC(dir,type,nr,size) (((dir)<<_IOC_DIRSHIFT)| \
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((type)<<_IOC_TYPESHIFT)| \
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((nr)<<_IOC_NRSHIFT)| \
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ((size)<<_IOC_SIZESHIFT))
//用于解码ioctl数字
/* used to create numbers */
?
// 定义不带参数的 ioctl 命令
#define _IO(type,nr) ? _IOC(_IOC_NONE,(type),(nr),0)
?
//定义带读参数的ioctl命令(copy_to_user) size为类型名
#define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
?
//定义带写参数的 ioctl 命令(copy_from_user) size为类型名
#define _IOW(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
?
//定义带读写参数的 ioctl 命令 size为类型名
#define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
?
//用于解码ioctl数字
/* used to decode ioctl numbers */
#define _IOC_DIR(nr) ? ? ? (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
#define _IOC_TYPE(nr) ? ? ? (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
#define _IOC_NR(nr) ? ? (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
#define _IOC_SIZE(nr) ? ? (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
头文件位置
mychar.h
#ifndef MY_CHAR_H
#define MY_CHAR_H
#include <asm/ioctl.h>
#define MY_CHAR_MAGIC 'k'
#define MYCHAR_IOCTL_GET_MAXLEN _IOR(MY_CHAR_MAGIC,1,int*)
#define MYCHAR_IOCTL_GET_CURLEN _IOR(MY_CHAR_MAGIC,2,int*)
#endif
mychar.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include "mychar.h"
#define BUF_LEN 100
int major = 11;
int minor = 0;
int mychar_num = 1;
//新建结构体类型
struct mychar_dev
{
struct cdev mydev;
char mydef_buf[BUF_LEN]; //相当于结构体的私有变量
int curlen; //相当于结构体的私有变量
};
struct mychar_dev gmydev;
int mychar_open(struct inode *pnode, struct file *pfile)
{
//利用private_data私有变量来指向全局变量结构体地址
pfile->private_data = (void*)(container_of(pnode->i_cdev,struct mychar_dev,mydev));
printk("mychar_open is called\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
printk("mychar_close is called\n");
return 0;
}
ssize_t mychar_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos)
{
int ret = 0;
int size = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
//将内核空间中的数据复制到用户空间
ret = copy_to_user(pbuf,pmydev->mydef_buf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
//读完之后把后面的内容再拷贝过来,同时更新curlen
memcpy(pmydev->mydef_buf,pmydev->mydef_buf+size,pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
return size;
}
ssize_t mychar_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos)
{
int size = 0;
int ret = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
if(count > BUF_LEN - pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
//将用户空间中的数据复制到内核空间中
ret = copy_from_user(pmydev->mydef_buf + pmydev->curlen, pbuf, size);
if(ret)
{
printk("copy_from_user failed\n");
return -1;
}
//更新curlen
pmydev->curlen = pmydev->curlen + size;
return size;
}
long mychar_ioctl(struct file *filp, unsigned int cmd,unsigned long arg)
{
int __user *pret = (int *)arg;
int maxlen = BUF_LEN;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
switch(cmd)
{
case MYCHAR_IOCTL_GET_MAXLEN:
ret = copy_to_user(pret,&maxlen,sizeof(int));
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
case MYCHAR_IOCTL_GET_CURLEN:
ret = copy_to_user(pret,&pmydev->curlen,sizeof(int));
if(ret)
{
printk("copy_to_user CURLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
//结构体初始化:部分变量赋值初始化
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl
};
int mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major, minor);
/* 申请设备号 */
ret = register_chrdev_region(devno, mychar_num, "mychar");
if (ret) {
ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
if (ret) {
printk("get devno failed\n");
return -1;
}
major = MAJOR(devno); // 容易遗漏,注意
}
/* 给struct cdev对象指定操作函数集 */
cdev_init(&gmydev.mydev, &myops);
/* 将 struct cdev对象添加到内核对应的数据结构里 */
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, mychar_num);
return 0;
}
void __exit mychar_exit(void)
{
dev_t devno = MKDEV(major, minor);
cdev_del(&gmydev.mydev);
unregister_chrdev_region(devno, mychar_num);
}
//表示支持GPL的开源协议
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
Makefile
ifeq ($(KERNELRELEASE),)
ifeq ($(ARCH),arm)
KERNELDIR ?= /home/linux/Linux_4412/kernel/linux-3.14
ROOTFS ?= /opt/4412/rootfs
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install
clean:
rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versions
else
CONFIG_MODULE_SIG=n
obj-m += mychar.o
endif
testmychar_app.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "mychar.h"
#include <stdio.h>
int main(int argc,char *argv[])
{
int fd = -1;
char buf[8] = "";
int max = 0;
int cur = 0;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
fd = open(argv[1],O_RDWR);
if(fd < 0)
{
printf("open %s failed\n",argv[1]);
return 2;
}
ioctl(fd,MYCHAR_IOCTL_GET_MAXLEN,&max);
printf("max len is %d\n",max);
write(fd,"hello",6);
ioctl(fd,MYCHAR_IOCTL_GET_CURLEN,&cur);
printf("cur len is %d\n",cur);
read(fd,buf,8);
printf("buf=%s\n",buf);
close(fd);
fd = -1;
return 0;
}
?编译执行,测试获取设备参数
?
//日志级别
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
?
#define KERN_WARNING "<4>" /* warning conditions */
?
#define KERN_NOTICE "<5>" /* normal but significant condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */
?
用法:printk(KERN_INFO"....",....)
? ?
? ?printk(KERN_INFO"Hello World"); =====> printk("<6>""Hello World") ====> printk("<6>Hello World")
?
dmesg --level=emerg,alert,crit,err,warn,notice,info,debug?
(dmesg中7个级别对应printk中7个级别)
#define HELLO_DEBUG
#undef PDEBUG
#ifdef HELLO_DEBUG
#define PDEBUG(fmt, args...) printk(KERN_DEBUG fmt, ##args)
#else
#define PDEBUG(fmt, args...)
#endif
本节讲得是第二个情况,必须有一个struct cdev来代表它
以上三个操作对每个具体设备都要进行
multimychar.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include "mychar.h"
#define BUF_LEN 100
#define MYCHAR_DEV_CNT 3
int major = 11;
int minor = 0;
int mychar_num = MYCHAR_DEV_CNT; //支持多个设备
//新建结构体类型
struct mychar_dev
{
struct cdev mydev;
char mydef_buf[BUF_LEN]; //相当于结构体的私有变量
int curlen; //相当于结构体的私有变量
};
struct mychar_dev gmydev_arr[MYCHAR_DEV_CNT]; //多个设备实现:创建结构体数组
int mychar_open(struct inode *pnode, struct file *pfile)
{
//利用private_data私有变量来指向全局变量结构体地址
pfile->private_data = (void*)(container_of(pnode->i_cdev,struct mychar_dev,mydev));
printk("mychar_open is called\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
printk("mychar_close is called\n");
return 0;
}
ssize_t mychar_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos)
{
int ret = 0;
int size = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
//将内核空间中的数据复制到用户空间
ret = copy_to_user(pbuf,pmydev->mydef_buf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
//读完之后把后面的内容再拷贝过来,同时更新curlen
memcpy(pmydev->mydef_buf,pmydev->mydef_buf+size,pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
return size;
}
ssize_t mychar_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos)
{
int size = 0;
int ret = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
if(count > BUF_LEN - pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
//将用户空间中的数据复制到内核空间中
ret = copy_from_user(pmydev->mydef_buf + pmydev->curlen, pbuf, size);
if(ret)
{
printk("copy_from_user failed\n");
return -1;
}
//更新curlen
pmydev->curlen = pmydev->curlen + size;
return size;
}
long mychar_ioctl(struct file *filp, unsigned int cmd,unsigned long arg)
{
int __user *pret = (int *)arg;
int maxlen = BUF_LEN;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
switch(cmd)
{
case MYCHAR_IOCTL_GET_MAXLEN:
ret = copy_to_user(pret,&maxlen,sizeof(int));
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
case MYCHAR_IOCTL_GET_CURLEN:
ret = copy_to_user(pret,&pmydev->curlen,sizeof(int));
if(ret)
{
printk("copy_to_user CURLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
//结构体初始化:部分变量赋值初始化
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl
};
int mychar_init(void)
{
int ret = 0;
int i = 0;
dev_t devno = MKDEV(major, minor);
/* 申请设备号 */
ret = register_chrdev_region(devno, mychar_num, "mychar");
if (ret) {
ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
if (ret) {
printk("get devno failed\n");
return -1;
}
major = MAJOR(devno); // 容易遗漏,注意
}
for(i = 0;i < MYCHAR_DEV_CNT;i++)
{
devno = MKDEV(major,minor+i); //设备号需要重新组合
/* 给struct cdev对象指定操作函数集 */
cdev_init(&gmydev_arr[i].mydev, &myops);
/* 将 struct cdev对象添加到内核对应的数据结构里 */
gmydev_arr[i].mydev.owner = THIS_MODULE;
cdev_add(&gmydev_arr[i].mydev, devno, 1); //这里需要填1
}
return 0;
}
void __exit mychar_exit(void)
{
dev_t devno = MKDEV(major, minor);
int i = 0;
for(i = 0; i< MYCHAR_DEV_CNT; i ++)
{
cdev_del(&gmydev_arr[i].mydev);
}
unregister_chrdev_region(devno, mychar_num);
}
//表示支持GPL的开源协议
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
mychar.h(未修改)
#ifndef MY_CHAR_H
#define MY_CHAR_H
#include <asm/ioctl.h>
#define MY_CHAR_MAGIC 'k'
#define MYCHAR_IOCTL_GET_MAXLEN _IOR(MY_CHAR_MAGIC,1,int*)
#define MYCHAR_IOCTL_GET_CURLEN _IOR(MY_CHAR_MAGIC,2,int*)
#endif
testmychar_app.c(维修工)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "mychar.h"
#include <stdio.h>
int main(int argc,char *argv[])
{
int fd = -1;
char buf[8] = "";
int max = 0;
int cur = 0;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
fd = open(argv[1],O_RDWR);
if(fd < 0)
{
printf("open %s failed\n",argv[1]);
return 2;
}
ioctl(fd,MYCHAR_IOCTL_GET_MAXLEN,&max);
printf("max len is %d\n",max);
write(fd,"hello",6);
ioctl(fd,MYCHAR_IOCTL_GET_CURLEN,&cur);
printf("cur len is %d\n",cur);
read(fd,buf,8);
printf("buf=%s\n",buf);
close(fd);
fd = -1;
return 0;
}
Makefile
ifeq ($(KERNELRELEASE),)
ifeq ($(ARCH),arm)
KERNELDIR ?= /home/linux/Linux_4412/kernel/linux-3.14
ROOTFS ?= /opt/4412/rootfs
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install
clean:
rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versions
else
CONFIG_MODULE_SIG=n
obj-m += mychar.o
endif
编译运行
?