itop4412学习记录_网络_瓜儿不甜的博客-CSDN博客
1. 本章知识点
学习结构体file_operations的结构与成员函数的赋值:
(1)所在目录
file_operations结构所在头文件目录:include/linux/fs.h,
如何引用:#include <linux/fs.h>
(2)常用函数有
• int (*open) (struct inode *, struct file *)
– 打开函数
• int (*release) (struct inode *, struct file *)
– 释放close函数
• long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long)
– io控制函数
• ssize_t (*read) (struct file *, char __user *, size_t, loff_t *)
– 读函数
• ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *)
– 写函数
• loff_t (*llseek) (struct file *, loff_t, int)
– 定位函数
(3)file_operations结构体
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
/* remove by cym 20130408 support for MT660.ko */
#if 0
//#ifdef CONFIG_SMM6260_MODEM
#if 1// liang, Pixtree also need to use ioctl interface...
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
#endif
/* end remove */
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
/* add by cym 20130408 support for MT6260 and Pixtree */
#if defined(CONFIG_SMM6260_MODEM) || defined(CONFIG_USE_GPIO_AS_I2C)
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
/* end add */
};
2. 驱动代码
(1)file_operation.c
/*包含初始化宏定义的头文件,代码中的module_init和module_exit在此文件中*/
#include <linux/init.h>
/*包含初始化加载模块的头文件,代码中的MODULE_LICENSE在此头文件中*/
#include <linux/module.h>
/*定义module_param module_param_array的头文件*/
#include <linux/moduleparam.h>
/*定义module_param module_param_array中perm的头文件*/
#include <linux/stat.h>
/*三个字符设备函数*/
#include <linux/fs.h>
/*MKDEV转换设备号数据类型的宏定义*/
#include <linux/kdev_t.h>
/*定义字符设备的结构体*/
#include <linux/cdev.h>
/*分配内存空间函数头文件*/
#include <linux/slab.h>
/*创建class类以及生成字符设备节点*/
#include <linux/device.h>
#define DEVICE_NAME "char_dev_node"
#define DEVICE_MINOR_NUM 2
#define DEV_MAJOR 0
#define DEV_MINOR 0
#define REGDEV_SIZE 10
MODULE_LICENSE("Dual BSD/GPL");
/*声明是开源的,没有内核版本限制*/
MODULE_AUTHOR("iTOPEET_dz");
/*声明作者*/
int basemajor = DEV_MAJOR;
int baseminor = DEV_MINOR;
static struct class *my_class;
/*输入主设备号*/
module_param(basemajor,int,S_IRUSR);
/*输入次设备号*/
module_param(baseminor,int,S_IRUSR);
struct reg_dev
{
char *data;
unsigned long size;
struct cdev char_dev;
};
struct reg_dev *my_devices;
static int char_dev_node_open(struct inode *inode, struct file *file){
printk(KERN_EMERG "char_dev_node_open() \n");
return 0;
}
static int char_dev_node_release(struct inode *inode, struct file *file){
printk(KERN_EMERG "char_dev_node_release() \n");
return 0;
}
static long char_dev_node_ioctl(struct file *file, unsigned int cmd, unsigned long arg){
printk(KERN_EMERG "char_dev_node_ioctl,cmd = %d, arg = %d\n",cmd,arg);
//printk(KERN_EMERG "cmd = %d \n",cmd);
//printk(KERN_EMERG "arg = %d \n",arg);
return 0;
}
ssize_t char_dev_node_read(struct file *file, char __user *buf, size_t len, loff_t *off){
printk(KERN_EMERG "char_dev_node_read() \n");
return 0;
}
ssize_t char_dev_node_write(struct file *file, const char __user *buf, size_t len, loff_t *off){
printk(KERN_EMERG "char_dev_node_write() \n");
return 0;
}
struct file_operations my_fops = {
.owner = THIS_MODULE,
.open=char_dev_node_open,
.release=char_dev_node_release,
.unlocked_ioctl=char_dev_node_ioctl,
.read=char_dev_node_read,
.write=char_dev_node_write,
};
/*设备注册到系统*/
static void reg_init_cdev(struct reg_dev *dev,int index){
int err;
int devno = MKDEV(basemajor,baseminor+index);
/*数据初始化*/
cdev_init(&dev->char_dev,&my_fops);
dev->char_dev.owner = THIS_MODULE;
dev->char_dev.ops = &my_fops;
/*注册到系统*/
err = cdev_add(&dev->char_dev,devno,1);
if(err){
printk(KERN_EMERG "cdev_add %d is fail! %d\n",index,err);
}
else{
printk(KERN_EMERG "cdev_add %d is success!\n",index);
}
}
static int scdev_init(void)
{
int ret = 0,i;
dev_t num_dev;
printk(KERN_EMERG "basemajor is %d!\n",basemajor);
printk(KERN_EMERG "baseminor is %d!\n",baseminor);
if(basemajor){
num_dev = MKDEV(basemajor,baseminor);
ret = register_chrdev_region(num_dev,DEVICE_MINOR_NUM,DEVICE_NAME);
}
else{
/*动态注册设备号*/
ret = alloc_chrdev_region(&num_dev,baseminor,DEVICE_MINOR_NUM,DEVICE_NAME);
/*获得主设备号*/
basemajor = MAJOR(num_dev);
printk(KERN_EMERG "adev_region req %d !\n",basemajor);
}
if(ret<0){
printk(KERN_EMERG "register_chrdev_region req %d is failed!\n",basemajor);
}
my_class = class_create(THIS_MODULE,DEVICE_NAME);
my_devices = kmalloc(DEVICE_MINOR_NUM * sizeof(struct reg_dev),GFP_KERNEL);
if(!my_devices){
ret = -ENOMEM;
goto fail;
}
memset(my_devices,0,DEVICE_MINOR_NUM * sizeof(struct reg_dev));
/*设备初始化*/
for(i=0;i<DEVICE_MINOR_NUM;i++){
my_devices[i].data = kmalloc(REGDEV_SIZE,GFP_KERNEL);
memset(my_devices[i].data,'0',REGDEV_SIZE);
/*设备注册到系统*/
reg_init_cdev(&my_devices[i],i);
device_create(my_class,NULL,MKDEV(basemajor,baseminor+i),NULL,DEVICE_NAME"%d",i);
}
my_devices[0].data[10]='\0';
my_devices[1].data[10]='\0';
printk(KERN_EMERG"my_devices[0].data: %s \n",my_devices[0].data);
printk(KERN_EMERG"my_devices[1].data: %s \n",my_devices[1].data);
printk(KERN_EMERG "scdev_init!\n");
/*打印信息,KERN_EMERG表示紧急信息*/
return 0;
fail:
/*注销设备号*/
unregister_chrdev_region(MKDEV(basemajor,baseminor),DEVICE_MINOR_NUM);
printk(KERN_EMERG "kmalloc is fail!\n");
kfree(my_devices[0].data);
kfree(my_devices[1].data);
kfree(my_devices);
printk(KERN_EMERG"my_devices[1].data: %s \n",my_devices[1].data);
return ret;
}
static void scdev_exit(void)
{
int i;
printk(KERN_EMERG "scdev_exit!\n");
/*除去字符设备*/
for(i=0;i<DEVICE_MINOR_NUM;i++){
cdev_del(&(my_devices[i].char_dev));
device_destroy(my_class,MKDEV(basemajor,baseminor+i));
}
class_destroy(my_class);
unregister_chrdev_region(MKDEV(basemajor,baseminor),DEVICE_MINOR_NUM);
kfree(my_devices[0].data);
kfree(my_devices[1].data);
kfree(my_devices);
printk(KERN_EMERG"my_devices[0].data: %s \n",my_devices[0].data);
printk(KERN_EMERG"my_devices[1].data: %s \n",my_devices[1].data);
}
module_init(scdev_init);
/*初始化函数*/
module_exit(scdev_exit);
/*卸载函数*/
(2)Makefile
#!/bin/bash
#通知编译器我们要编译模块的哪些源码
#这里是编译itop4412_hello.c这个文件编译成中间文件mini_linux_module.o
obj-m += file_operation.o
#源码目录变量,这里用户需要根据实际情况选择路径
#作者是将Linux的源码拷贝到目录/home/topeet/android4.0下并解压的
KDIR := /home/topeet/iTop4412_Kernel_3.0
#当前目录变量
PWD ?= $(shell pwd)
#make命名默认寻找第一个目标
#make -C就是指调用执行的路径
#$(KDIR)Linux源码目录,作者这里指的是/home/topeet/android4.0/iTop4412_Kernel_3.0
#$(PWD)当前目录变量
#modules要执行的操作
all:
make -C $(KDIR) M=$(PWD) modules
#make clean执行的操作是删除后缀为o的文件
clean:
rm -rf *.mod.c *.o *.order *.ko *.mod.o *.symvers
(3)file_test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
int main(int argc,char **argv){
int fd0=1;
int fd1=0;
int cmd=0;
char *node_path0="/dev/char_dev_node0";//char_dev_node0
char *node_path1="/dev/char_dev_node1";//char_dev_node1
if(fd0 = open(node_path0,O_RDWR|O_NDELAY)<0){
printf("open node char_dev_node0 failed!\n");
}
else{
printf("open node char_dev_node0 succ,fd0 = %d!\n",fd0);
ioctl(fd0,atoi(argv[1]),atoi(argv[2]));
}
//sleep(3);
close(fd0);
}
3. 调试信息
(1)加载驱动
[root@iTOP-4412]# insmod file_operation.ko
[ 2602.765773] basemajor is 0!
[ 2602.767089] baseminor is 0!
[ 2602.769868] adev_region req 248 !
[ 2602.787135] cdev_add 0 is success!
[ 2602.795639] cdev_add 1 is success!
[ 2602.805731] my_devices[0].data: 0000000000
[ 2602.808432] my_devices[1].data: 0000000000
[ 2602.813064] scdev_init!
(2)运行应用file_test
[root@iTOP-4412]# ./file_test 1 2
[21118.769419] char_dev_node_open()
[21118.771462] char_dev_node_ioctl,cmd = 1, arg = 2
[21118.775878] char_dev_node_release()
open node char_dev_node0 succ,fd0 = 3!
4. 遇到的问题
(1) ioctl调试信息不打印
应用程序file_test打印始终为:open node char_dev_node0 succ,fd0 = 0!, ioctl的打印一直出不来。
int main(int argc,char **argv){
int fd0=1;
int fd1=0;
int cmd=0;
char *node_path0="/dev/char_dev_node0";//char_dev_node0
char *node_path1="/dev/char_dev_node1";//char_dev_node1
if(fd0 = open(node_path0,O_RDWR|O_NDELAY)<0){
printf("open node char_dev_node0 failed!\n");
}
else{
printf("open node char_dev_node0 succ,fd0 = %d!\n",fd0);
ioctl(fd0,atoi(argv[1]),atoi(argv[2]));
}
//sleep(3);
close(fd0);
}
具体 调试信息如下:
[root@iTOP-4412]# ./file_test 1 2
[20974.531693] char_dev_node_open()
open node char_dev_node0 succ,fd0 = 0!
[20977.535285] char_dev_node_release()
解决办法:(if语句少了一对括号)
将if(fd0 = open(node_path0,O_RDWR|O_NDELAY)<0)改为
if((fd0 = open(node_path0,O_RDWR|O_NDELAY))<0)
调试信息如下,得到了想要的结果:
[root@iTOP-4412]# ./file_test 1 2
[21118.769419] char_dev_node_open()
[21118.771462] char_dev_node_ioctl,cmd = 1, arg = 2
[21118.775878] char_dev_node_release()
open node char_dev_node0 succ,fd0 = 3!
[root@iTOP-4412]