linux-2.6.38.8 内核下实现《linux设备驱动开发详解》globalmem 驱动实例
--摘要
《linux设备驱动开发详解》第6章介绍了globalmem驱动实例,但是其实现环境是在比较低的内核版本下的,对于使用Fedora15的用户参照书上介绍的步骤会出现很多问题,这是由于Fedora15使用了2.6.38版的内核,这个内核比较新,进行了许多的更新,导致书本上介绍的部分代码已不支持了。本文介绍了2.6.38.8内核下,实现globalmem驱动实例的整个过程.最后贴出经修改后的完整的globalmem.c代码,供大家参考。
--正文
1.将书上的代码清单 6.17 复制到globalmem.c,创建Makefile:
obj-m:=globalmem.o
2.开始编译模块:
make -C /usr/src/linux-2.6.38.8/ M=/home/lufee/mydirve/ modules
“/usr/src/linux-2.6.38.8/”是内核源码树所在目录,“/home/lufee/mydirve/”是 globalmem.c所在的目录,
上述编译改写为 make -C /usr/src/linux-2.6.38.8/ M=$pwd modules 运行后会发现mismatch的错误,不知是什么原因(当前目录已经是/home/lufee/mydirve/,$pwd照理说也对呀)
发现编译出现错误:
/home/lufee/mydiraver//globalmem.c:17:9: 警告:在‘globalmem_major’的声明中,类型默认为‘int’ [-Wimplicit-int]
/home/lufee/mydiraver//globalmem.c:55:58: 错误:expected ‘;’, ‘,’ or ‘)’ before ‘_user’
/home/lufee/mydiraver//globalmem.c:83:65: 错误:expected ‘;’, ‘,’ or ‘)’ before ‘_user’
/home/lufee/mydiraver//globalmem.c:158:10: 错误:‘globalmem_read’未声明(不在函数内)
/home/lufee/mydiraver//globalmem.c:159:11: 错误:‘globalmem_write’未声明(不在函数内)
/home/lufee/mydiraver//globalmem.c:160:2: 错误:初始值设定项里有未知的字段‘ioctl’
/home/lufee/mydiraver//globalmem.c:160:2: 警告:从不兼容的指针类型初始化 [默认启用]
/home/lufee/mydiraver//globalmem.c:160:2: 警告:(在‘globalmem_fops.aio_read’的初始化附近) [默认启用]
/home/lufee/mydiraver//globalmem.c: 在函数‘globalmem_init’中:
/home/lufee/mydiraver//globalmem.c:193:2: 错误:隐式声明函数‘kmalloc’ [-Werror=implicit-function-declaration]
/home/lufee/mydiraver//globalmem.c:193:17: 警告:赋值时将整数赋给指针,未作类型转换 [默认启用]
/home/lufee/mydiraver//globalmem.c: 在函数‘globalmem_exit’中:
/home/lufee/mydiraver//globalmem.c:210:2: 错误:隐式声明函数‘kfree’ [-Werror=implicit-function-declaration]
cc1:有些警告被当作是错误
3.下面逐步排除错误
/home/lufee/mydiraver//globalmem.c:55:58: 错误:expected ‘;’, ‘,’ or ‘)’ before ‘_user’
/home/lufee/mydiraver//globalmem.c:83:65: 错误:expected ‘;’, ‘,’ or ‘)’ before ‘_user’
/home/lufee/mydiraver//globalmem.c:158:10: 错误:‘globalmem_read’未声明(不在函数内)
/home/lufee/mydiraver//globalmem.c:159:11: 错误:‘globalmem_write’未声明(不在函数内)
将‘globalmem_read’、‘globalmem_write’声明中将_ _user改成 __user就可以了,'_'之间是没有空格的!
/home/lufee/mydiraver//globalmem.c:160:2: 错误:初始值设定项里有未知的字段‘ioctl’
这个错误网上搜索发现2.6.38版本内核 file_operation结构体已经删除了ioctl函数,取代的是:
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
file_operation结构体在 /usr/src/linux-2.6.28.8/include/linux/fs.h定义
找到一篇LDD3 scull的解决方案(http://www.linuxidc.com/Linux/2011-06/37818.htm)
globalmem解决方案:
在globalmem.c中将globalmem_ioctl函数定义做如下修改即可:
static long globalmem_ioctl( struct file *filp,unsigned int cmd, unsigned long arg)
在file_operation 赋值处修改:
.unlocked_ioctl = globalmem_ioctl,
/home/lufee/mydiraver//globalmem.c:193:2: 错误:隐式声明函数‘kmalloc’ [-Werror=implicit-function-declaration]
/home/lufee/mydiraver//globalmem.c:210:2: 错误:隐式声明函数‘kfree’ [-Werror=implicit-function-declaration]
隐式声明函数错误的意思是函数在未声明之前就被调用了,需要先声明这两个函数,版本变化导致这个错误的出现。
解决方法:
找到kmalloc与kfree的声明头文件并include进来就可以了
#include <linux/slab.h>
4. 加载模块出现错误:insmod: error inserting 'globalmem.ko': -1 Device or resource busy
出现这个错误是因为设备号冲突了,将预设设备号254改为0 使模块动态申请设备号:
#define GLOBALMEM_MAJOR 0 /*预设的 globalmem 的主设备号*/
5.通过“mknod /dev/globalmem c 250 0”命令创建“/dev/globalmem”设备节点
250是globalmem动态申请到的设备号,不同主机会有不同。可用命令:cat /proc/devices 查看globalmem的设备号
6. 演示
按照书上介绍的步骤实验即可。不过实验中发现执行“cat /dev/globalmem”命令后会有一个提示信息:
[root@lufee-linux mydiraver]# cat /dev/globalmem
hello word
cat: /dev/globalmem: 没有那个设备或地址
7.修改后的globalmem.c完整代码
贴出经我修改后的globalmem.c完整代码供大家参考
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/slab.h>/*kmalloc,kfree 头文件*/
#define GLOBALMEM_SIZE 0x1000 /*全局内存最大 4KB*/
#define MEM_CLEAR 0x1 /*清零全局内存*/
#define GLOBALMEM_MAJOR 0 /*预设的 globalmem 的主设备号*/
static int globalmem_major = GLOBALMEM_MAJOR;
struct globalmem_dev
{
struct cdev cdev; /*cdev 结构体*/
unsigned char mem[GLOBALMEM_SIZE]; /*全局内存*/
};
struct globalmem_dev *globalmem_devp; /*设备结构体指针*/
/*文件打开函数*/
int globalmem_open(struct inode *inode, struct file *filp)
{
/*将设备结构体指针赋值给文件私有数据指针*/
filp->private_data = globalmem_devp;
return 0;
}
/*文件释放函数*/
int globalmem_release(struct inode *inode, struct file *filp)
{
return 0;
}
/* ioctl 设备控制函数 */
static long globalmem_ioctl( struct file *filp,
unsigned int cmd, unsigned long arg)
{
struct globalmem_dev *dev = filp->private_data;/*获得设备结构体指针*/
switch (cmd)
{
case MEM_CLEAR:
memset(dev->mem, 0, GLOBALMEM_SIZE);
printk(KERN_INFO "globalmem is set to zero\n");
break;
default:
return - EINVAL;
}
return 0;
}
/*读函数*/
static ssize_t globalmem_read(struct file *filp, char __user *buf,
size_t size,loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data; /*获得设备结构体指针*/
/*分析和获取有效的写长度*/
if (p >= GLOBALMEM_SIZE)
return count ? - ENXIO: 0;
if (count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
/*内核空间→用户空间*/
if (copy_to_user(buf, (void*)(dev->mem + p), count))
{
ret = - EFAULT;
}
else
{
*ppos += count;
ret = count;
printk(KERN_INFO "read %d bytes(s) from %ld\n", count, p);
}
return ret;
}
/*写函数*/
static ssize_t globalmem_write(struct file *filp, const char __user *buf,
size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data; /*获得设备结构体指针*/
/*分析和获取有效的写长度*/
if (p >= GLOBALMEM_SIZE)
return count ? - ENXIO: 0;
if (count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
/*用户空间→内核空间*/
if (copy_from_user(dev->mem + p, buf, count))
ret = - EFAULT;
else
{
*ppos += count;
ret = count;
printk(KERN_INFO "written %d bytes(s) from %ld\n", count, p);
}
return ret;
}
/* seek 文件定位函数 */
static loff_t globalmem_llseek(struct file *filp, loff_t offset,int orig)
{
loff_t ret = 0;
switch (orig)
{
case 0:
/*相对文件开始位置偏移*/
if (offset < 0)
{
ret = - EINVAL;
break;
}
if ((unsigned int)offset > GLOBALMEM_SIZE)
{
ret = - EINVAL;
break;
}
filp->f_pos = (unsigned int)offset;
ret = filp->f_pos;
break;
case 1:
/*相对文件当前位置偏移*/
if ((filp->f_pos + offset) > GLOBALMEM_SIZE)
{
ret = - EINVAL;
break;
}
if ((filp->f_pos + offset) < 0)
{
ret = - EINVAL;
break;
}
filp->f_pos += offset;
ret = filp->f_pos;
break;
default:
ret = - EINVAL;
break;
}
return ret;
}
/*文件操作结构体*/
static const struct file_operations globalmem_fops =
{
.owner = THIS_MODULE,
.llseek = globalmem_llseek,
.read = globalmem_read,
.write = globalmem_write,
.unlocked_ioctl = globalmem_ioctl,
.open = globalmem_open,
.release = globalmem_release,
};
/*初始化并注册 cdev*/
static void globalmem_setup_cdev(struct globalmem_dev *dev, int index)
{
int err, devno = MKDEV(globalmem_major, index);
cdev_init(&dev->cdev, &globalmem_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &globalmem_fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding LED%d", err, index);
}
/*设备驱动模块加载函数*/
int globalmem_init(void)
{
int result;
dev_t devno = MKDEV(globalmem_major, 0);
/* 申请设备号*/
if (globalmem_major)
result = register_chrdev_region(devno, 1, "globalmem");
else /* 动态申请设备号 */
{
result = alloc_chrdev_region(&devno, 0, 1, "globalmem");
globalmem_major = MAJOR(devno);
}
if (result < 0)
return result;
/* 动态申请设备结构体的内存*/
globalmem_devp = kmalloc(sizeof(struct globalmem_dev),GFP_KERNEL);
if (!globalmem_devp)
/*申请失败*/
{
result = - ENOMEM;
goto fail_malloc;
}
memset(globalmem_devp, 0, sizeof(struct globalmem_dev));
globalmem_setup_cdev(globalmem_devp, 0);
return 0;
fail_malloc: unregister_chrdev_region(devno, 1);
return result;
}
/*模块卸载函数*/
void globalmem_exit(void)
{
cdev_del(&globalmem_devp->cdev); /*注销 cdev*/
kfree(globalmem_devp);/*释放设备结构体内存*/
unregister_chrdev_region(MKDEV(globalmem_major, 0), 1); /*释放设备号*/
}
MODULE_AUTHOR("Song Baohua");
MODULE_LICENSE("Dual BSD/GPL");
module_param(globalmem_major, int, S_IRUGO);
module_init(globalmem_init);
module_exit(globalmem_exit);