shen_cdev.c
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/cdev.h>
//#include <linux/uaccess.h>
#define GLOBALMEM_SIZE 0x1000
#define MEM_CLEAR 0x1
#define GLOBALMEM_MAJOR 230
static int globalmem_major = GLOBALMEM_MAJOR;
module_param(globalmem_major, int, S_IRUGO);
struct globalmem_dev {
struct cdev cdev;
unsigned char mem[GLOBALMEM_SIZE];
};
struct globalmem_dev *globalmem_devp;
static int globalmem_open(struct inode *inode, struct file *filp)
{
return 0;
}
static int globalmem_release(struct inode *inode, struct file *filp)
{
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
static int globalmem_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg)
#else
static long globalmem_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
#endif
{
return 0;
}
static ssize_t globalmem_read(struct file *filp, char __user * buf, size_t size, loff_t * ppos)
{
ssize_t ret;
return ret;
}
static ssize_t globalmem_write(struct file *filp, const char __user * buf, size_t size, loff_t * ppos)
{
ssize_t ret = 0;
return ret;
}
static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig)
{
loff_t ret;
return ret;
}
static const struct file_operations globalmem_fops = {
.owner = THIS_MODULE,
.llseek = globalmem_llseek,
.read = globalmem_read,
.write = globalmem_write,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
.ioctl = globalmem_ioctl,
#else
//.unlocked_ioctl = globalmem_ioctl,
.compat_ioctl = globalmem_compat_ioctl,
#endif
.open = globalmem_open,
.release = globalmem_release,
};
static void globalmem_setup_cdev(struct globalmem_dev *dev, int index)
{
int err = 0;
int devno = MKDEV(globalmem_major, index);
cdev_init(&(dev->cdev), &globalmem_fops);
err = cdev_add(&(dev->cdev), devno, 1);
if (err) {
printk("ERR:%d, index:%d\n", err, index);
}
}
static int __init globalmem_init(void)
{
int ret = 0;
dev_t devno = MKDEV(globalmem_major, 0);
if (globalmem_major) {
ret = register_chrdev_region(devno, 1, "globalmem");
} else {
ret = alloc_chrdev_region(&devno, 0, 1, "globalmem");
}
if (ret < 0) {
printk("ret:%d, get devno fail\n", ret);
return ret;
}
globalmem_devp = kzalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
if (!globalmem_devp) {
ret = -ENOMEM;
goto fail_malloc;
}
return 0;
globalmem_setup_cdev(globalmem_devp, 0);
fail_malloc:
unregister_chrdev_region(devno, 1);
return ret;
}
static void __exit globalmem_exit(void)
{
cdev_del(&globalmem_devp->cdev);
kfree(globalmem_devp);
unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);
}
module_init(globalmem_init);
module_exit(globalmem_exit);
MODULE_AUTHOR("shencf");
MODULE_LICENSE("GPL v2");
Makefile:
KVERS = $(shell uname -r)
# Kernel modules
obj-m := shen_cdev.o
module-objs := shen_cdev.o
# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0
build: kernel_modules
kernel_modules:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules
clean:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean
简单字符设备驱动框架1
最新推荐文章于 2024-09-03 16:36:34 发布