//Cdev.h
#ifndef _LINUX_CDEV_H
#define _LINUX_CDEV_H
#ifdef __KERNEL__
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
void cdev_init(struct cdev *, const struct file_operations *);
struct cdev *cdev_alloc(void);
void cdev_put(struct cdev *p);
int cdev_add(struct cdev *, dev_t, unsigned);
void cdev_del(struct cdev *);
void cd_forget(struct inode *);
#endif
#endif
//for make dev_t dev
#define MAJOR(dev) ((dev)>>8)
#define MINOR(dev) ((dev) & 0xff)
#define MKDEV(ma,mi) ((ma)<<8 | (mi))
/*
* NOTE:
* read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
* can be called without the big kernel lock held in all filesystems.
*/
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 (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
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 *, struct dentry *, 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 (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
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 (*dir_notify)(struct file *filp, unsigned long arg);
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);
};
struct xxx_dev_t{
struct cdev cdev;
}
static int __init xxx_init();
static void __exit xxx_exit();
int xxx_open (struct inode *, struct file *);
int xxx_release (struct inode *, struct file *);
loff_t xxx_llseek (struct file *, loff_t, int);
ssize_t xxx_read (struct file *, char __user *, size_t, loff_t *);
ssize_t xxx_write (struct file *, const char __user *, size_t, loff_t *);
int xxx_ioctl (struct inode *, struct file *, unsigned int, unsigned long);
struct file_operations xxx_fops = {
.owner = THIS_MODULE,
.llseek=xxx_llseek,
.read = xxx_read,
.write = xxx_write,
.ioctl = xxx_ioct,
};
MODULE_AUTHOR("Bai Jie <bai.xjie@gmail.com>");
MODULE_LICENSE("Dual BSD/GPL");
module_param(xxx_major,int,S_IRUGO);
module_init(xxx_init);
module_exit(xxx_exit);
以下为源码:
#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 250
static int globalmem_major = GLOBALMEM_MAJOR;
struct globalmem_dev{
struct cdev cdev;
unsigned char mem[GLOBALMEM_SIZE];
};
struct globalmem_dev* globalmem_devp;
static const struct file_operations globalmem_fops;
static int __init globalmem_init(void)
{
printk(KERN_INFO "globalmem_init\n");
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);
memset(globalmem_devp,0,sizeof(struct globalmem_dev));
if(!globalmem_devp){
return -ENOMEM;
}
cdev_init(&globalmem_devp->cdev,&globalmem_fops);/*init cdev*/
globalmem_devp->cdev.owner = THIS_MODULE;
result =cdev_add(&globalmem_devp->cdev,devno,1);
if(result){
printk(KERN_NOTICE "Error %d adding globalmem",result);
}
return 0;
}
static void __exit globalmem_exit()
{
cdev_del(&globalmem_devp->cdev);
kfree(globalmem_devp);
unregister_chrdev_region(MKDEV(globalmem_major,0),1);
}
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 ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
unsigned long index_pos = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data;/*get device struct pointer*/
if(index_pos > GLOBALMEM_SIZE)
return 0;
if(count > GLOBALMEM_SIZE- index_pos)
count = GLOBALMEM_SIZE - index_pos;
if(copy_to_user(buf,(void*)(dev->mem + index_pos),count)){
ret = -EFAULT;
}else{
*ppos+=count;
ret = count;
printk(KERN_INFO "globalmem read %u bytes(s) from %lu\n",count,index_pos);
}
return ret;
}
ssize_t globalmem_write (struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
unsigned long index_pos = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data;/*get d
evice struct pointer*/
if(index_pos > GLOBALMEM_SIZE)
return 0;
if(count > GLOBALMEM_SIZE- index_pos)
count = GLOBALMEM_SIZE - index_pos;
if(copy_from_user(dev->mem + index_pos,buf,count))
ret = -EFAULT;
else{
*ppos+=count;
ret = count;
printk(KERN_INFO "globalmem written %u bytes(s) from %lu\n",count,index_pos);
}
return ret;
}
int globalmem_ioctl (struct inode *inode, 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");
break;
default:
printk(KERN_INFO "globalmem ictl unknow command(%d)",cmd);
return -EINVAL;
}
return 0;
}
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,
.ioctl = globalmem_ioctl,
.open = globalmem_open,
.release = globalmem_release,
};
MODULE_AUTHOR("baijie <bai.xjie@gmail.comm>");
MODULE_LICENSE("Dual BSD/GPL");
module_param(globalmem_major,int,S_IRUGO);
module_init(globalmem_init);
module_exit(globalmem_exit);
# insmod globalmem.ko
# dmesg | tail
globalmem_init
# lsmod
Module Size Used by
globalmem 7940 0
# cat /proc/devices
Character devices:
……
250 globalmem
# mknod /dev/globalmem c 250 0
# cat /dev/globalmem
# echo "baijie hello." > /dev/globalmem
# cat /dev/globalmem
baijie hello.
遇到问题:
insmod: error inserting 'globalmem.ko': -1 Invalid module format
由于当前系统中安装有多个GCC版本,当前GCC版本与内核版本不兼容问题,切换回对应的GCC版本就好。