目录
一、读操作实现
二、写操作实现
三、读写代码示例
四、ioctl操作实现
五、ioctl代码示例
六、printk使用
七、多个设备的支持
代码示例
ssize_t xxx_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos);
完成功能:读取设备产生的数据
参数:
filp:指向open产生的struct file类型的对象,表示本次read对应的那次open
pbuf:指向用户空间一块内存,用来保存读到的数据
count:用户期望读取的字节数
ppos:对于需要位置指示器控制的设备操作有用,用来指示读取的起始位置,读完后也需要变更位置指示器的指示位置
返回值:
本次成功读取的字节数,失败返回-1
put_user(x,ptr)
x:char、int类型的简单变量名
unsigned long copy_to_user (void __user * to, const void * from, unsigned long n)
成功为返回0,失败非0。
ssize_t xxx_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos);
完成功能:向设备写入数据
参数:
filp:指向open产生的struct file类型的对象,表示本次write对应的那次open
pbuf:指向用户空间一块内存,用来保存被写的数据
count:用户期望写入的字节数
ppos:对于需要位置指示器控制的设备操作有用,用来指示写入的起始位置,写完后也需要变更位置指示器的指示位置
返回值:
本次成功写入的字节数,失败返回-1
get_user(x,ptr)
x:char、int类型的简单变量名
unsigned long copy_from_user (void * to, const void __user * from, unsigned long n)
成功为返回0,失败非0。
字符设备:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF_LEN 100
int major = 11;
int minor = 0;
int mychar_num = 1;
struct cdev mydev;
char mydev_buf[BUF_LEN];
int curlen;
int mychar_open(struct inode *pnode,struct file *pfile)
{
printk("open\n");
return 0;
}
int mychar_close(struct inode *pnode,struct file *pfile)
{
printk("close\n");
return 0;
}
ssize_t mychar_read(struct file *pfile,char __user *puser,size_t count,loff_t *p_pos)
{
int size = 0;
int ret = 0;
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser,pmydev->mydev_buf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(mydev_buf,mydev_buf + size,curlen - size);
curlen -= size;
return size;
}
ssize_t mychar_write(struct file *pfile,const char __user *puser,size_t count,loff_t *p_pos)
{
int size = 0;
int ret = 0;
if(count > BUF_LEN-pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen,puser,size);
if(ret)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
return size;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
};
int __init 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(&pgmydev->mydev,&myops);
/*将struct cdev对象添加到内核对应的数据结构里*/
mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev,devno,mychar_num);
return 0;
}
void __exit mychar_exit(void)
{
dev_t devno = MKDEV(major,minor);
cdev_del(&pgmydev->mydev);
unregister_chrdev_region(devno,mychar_num);
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
应用层:
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int fd = -1;
char buf[8] = "";
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;
}
write(fd,"hello",6);
read(fd,buf,8);
printf("buf=%s\n",buf);
close(fd);
fd = -1;
return 0;
}
一般情况下定义结构体存放写的内容
#include
#include
#include
#include
#include
#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 mydev_buf[BUF_LEN];
int curlen;
};
struct mychar_dev gmydev;
int mychar_open(struct inode *pnode,struct file *pfile)
{
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 *pfile,char __user *puser,size_t count,loff_t *p_pos)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
int size = 0;
int ret = 0;
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser,pmydev->mydev_buf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf,pmydev->mydev_buf + size,pmydev->curlen - size);
pmydev->curlen -= size;
return size;
}
ssize_t mychar_write(struct file *pfile,const char __user *puser,size_t count,loff_t *p_pos)
{
int size = 0;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
if(count > BUF_LEN-pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen,puser,size);
if(ret)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
return size;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
};
int __init mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
int i = 0;
/*申请设备号*/
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,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);
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
已知成员的地址获得所在结构体变量的地址:container_of(成员地址,结构体类型名,成员在结构体中的名称)
long xxx_ioctl (struct file *filp, unsigned int cmd, unsigned long arg);
功能:对相应设备做指定的控制操作(各种属性的设置获取等等)
参数:
filp:指向open产生的struct file类型的对象,表示本次ioctl对应的那次open
cmd:用来表示做的是哪一个操作
arg:和cmd配合用的参数
返回值:成功为0,失败-1
cmd组成:
#define _IOC(dir,type,nr,size) (((dir)<<_IOC_DIRSHIFT)| \
((type)<<_IOC_TYPESHIFT)| \
((nr)<<_IOC_NRSHIFT)| \
((size)<<_IOC_SIZESHIFT))
/* 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)))
/* 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)
muchar.h
#ifndef MY_CHAR_H
#define MY_CHAR_H
#include
#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
#include
#include
#include
#include
#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 mydev_buf[BUF_LEN];
int curlen;
};
struct mychar_dev gmydev;
int mychar_open(struct inode *pnode,struct file *pfile)
{
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 *pfile,char __user *puser,size_t count,loff_t *p_pos)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
int size = 0;
int ret = 0;
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser,pmydev->mydev_buf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf,pmydev->mydev_buf + size,pmydev->curlen - size);
pmydev->curlen -= size;
return size;
}
ssize_t mychar_write(struct file *pfile,const char __user *puser,size_t count,loff_t *p_pos)
{
int size = 0;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
if(count > BUF_LEN-pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen,puser,size);
if(ret)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
return size;
}
long mychar_ioctl(struct file *pfile,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 *)pfile->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 __init mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
int i = 0;
/*申请设备号*/
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,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);
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
testMycharApp.c
#include
#include
#include
#include
#include
#include
#include "mychar.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
#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来代表它
cdev_init
cdev.owner赋值
cdev_add
以上三个操作对每个具体设备都要进行
#include
#include
#include
#include
#include
#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 mydev_buf[BUF_LEN];
int curlen;
};
struct mychar_dev gmydev_arr[MYCHAR_DEV_CNT];
int mychar_open(struct inode *pnode,struct file *pfile)
{
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 *pfile,char __user *puser,size_t count,loff_t *p_pos)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
int size = 0;
int ret = 0;
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser,pmydev->mydev_buf,size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf,pmydev->mydev_buf + size,pmydev->curlen - size);
pmydev->curlen -= size;
return size;
}
ssize_t mychar_write(struct file *pfile,const char __user *puser,size_t count,loff_t *p_pos)
{
int size = 0;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
if(count > BUF_LEN-pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen,puser,size);
if(ret)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
return size;
}
long mychar_ioctl(struct file *pfile,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 *)pfile->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 __init mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
int i = 0;
/*申请设备号*/
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);
}
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);
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);