#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static unsigned int major = 0.
static unsigned int minor = 0.
static unsigned int devno.
static char *filename = "mydevice".
static struct cdev *mycdev = NULL.
static int mycdevflg = 0.
static int devnoflg = 0.
static int adddevflg = 0.
MODULE_LICENSE("Dual BSD/GPL").
static int myopen(struct inode *inodep, struct file *flipl)
{
printk("my open is run\n").
return 0.
}
static ssize_t myread(struct file *flip, char __user *buf, size_t size, loff_t offset)
{
printk("myread is ok!\n").
static int i = 0.
///copy_to_user(buf,from,size).
*buf = i .
return 0.
}
static int myrelease(struct inode *myindoe, struct file *flip)
{
printk("myrelease is run.\n").
return 0.
}
static struct file_operations myfops =
{
.owner = THIS_MODULE,
.open = myopen,
.read = myread,
.release= myrelease,
}.
/* 初始化设备的过程主要是三步:1,生成设备号.2初始化设备;3,添加到内核。
*/
static int __init myinit(void)
{
int result = -1.
if(major)
{
devno = MKDEV(major,minor). //生成设备号
result = register_chrdev_region(devno,1,filename).//注册设备号devno,1为设备的个数
devnoflg = 1.
}
else
{
result = alloc_chrdev_region(&.devno,minor,1,filename).//生成设备号兵注册,
mior为次设备号,这里注意的是指针devno?
major = MAJOR(devno).
devnoflg = 1.
}
if(result < 0)
{
printk("can t register the major num!\n").
devnoflg = 0.
return -1.
}
//mycdev = kmalloc(sizeof(struct cdev),GFP_KERNEL).
//如果采用cdev_init(struct cdev*,struct file *)方式的话,这个才需要。
mycdev = cdev_alloc().//申请cdev内存兵初始化设备cdev,不能在这之前申请内存,
否则要释放两次!
if(NULL == mycdev)
{
printk("can t request the memory!\n").
}
mycdevflg = 1.
//memset(mycdev,0,sizeof(mycdev)).
mycdev->owner = THIS_MODULE.//如果采用cdev_init(struct cdev*,struct file *)方式的话,
这两项可以去掉
mycdev->ops = &.myfops.//如果采用cdev_init(struct cdev*,struct file *)方式的话,
这两项可以去掉
result = cdev_add(mycdev,devno,1).//设备和设备号联系起来,
即通常说的添加设备到内核
if(result < 0)
{
printk("can t add cdev.2\n").
adddevflg = 0.
}
else
{
adddevflg = 1.
}
return 0.
}
/* 以和设备注册的次序"卸载",注意有的时候,当出错的时候,要注意是否用到这些操作,
所以要加上判断,否则比如对空指针free会导致系统崩溃!
*/
static void myexit(void)
{
printk("myexit begin.\n").
if(adddevflg)
{
cdev_del(mycdev).
adddevflg = 0.
}
if(mycdev)
{
kfree(mycdev).
mycdev = NULL.
}
if(devnoflg)
{
unregister_chrdev_region(devno,1).
devno = 0.
devnoflg = 0.
}
printk("exit over.\n").
}
module_init(myinit).
module_exit(myexit). |