作为linux驱动小白,在这里谢谢song的指导,在这里记载一下内核模块的编译与加载(若发现错误,请给我留言)
以下是宋宝华老师的字符设备驱动程序
#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>
#define GLOBALMEM_SIZE 0x1000
#define MEM_CLEAR 0X1
#define GLOBALMEM_MAJOR 111
static int globalmem_major = GLOBALMEM_MAJOR;
struct globalmem_dev{
struct 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;
}
static int globalmem_ioctl(struct inode *inodep, 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 int p = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data;
if(p >= GLOBALMEM_SIZE)
return 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 %d\n", count, p);
}
return ret;
}
static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size,loff_t *ppos)
{
unsigned int p = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data;
if(p >= GLOBALMEM_SIZE)
return 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 "write %d bytes(s) from %d\n", count, p);
}
return ret;
}
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){
return -EINVAL;
break;
}
if((filp->f_pos + offset) <0){
return -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,
.ioctl = globalmem_ioctl,
.open = globalmem_open,
.release = globalmem_release,
};
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 globalmem %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);
kfree(globalmem_devp);
unregister_chrdev_region(MKDEV(globalmem_major,0),1);
}
MODULE_AUTHOR("lirui's first driver");
MODULE_LICENSE("Dual BSD/GPL");
module_param(globalmem_major,int,S_IRUGO);
module_init(globalmem_init);
module_exit(globalmem_exit);
到这里程序就结束了,下面就是内核模块的编译与加载了
第一步编写Makefile
以下是基于不同平台的Makefile
第一种
ifneq ($(KERNELRELEASE),)
obj-m := memdev.o
else
KDIR := /opt/linux-2.6.30.4
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif
第二种
ifneq ($(KERNELRELEASE),)
obj-m := memdev.o
else
KDIR := /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif
一,二种的区别主要在于KDIR不同,根据运行平台不同选择好路径
第一种编译出的模块运行于ARM平台(KDIR所指的路径为已配置好的基于arm平台的内核)。第二种运行于LINUX下。
第二步编译 make
在这注意,驱动程序放置在任何目录下都可以,但是必须与Makefile放置在一个目录下
第三步 加载模块 insmod xxx.ko
在这注意,若编写Makefile是指定的目录是已裁剪好的基于ARM平台的内核的目录,却在linux下insmod,则会出现错误insmod: error inserting 'memdev.ko': -1 Invalid module format
第四步 创建节点 mknod /dev/globalmem c 111 0
可以通过命令 cat /proc/devices 查看
第五步就是测试一下了
echo "hello word" > /etc/globalmem
cat /etc/globalmem
最后一步就是卸载了 rmmod memdev