简单的字符驱动代码,为何不需要注册

NUC-94210 2012-08-02 09:43:40

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <linux/fs.h>

struct cdev *gDev;
struct file_operations *gFile;
dev_t devNum;
unsigned int subDevNum = 1;

int reg_major = 232;

int reg_minor = 0;


int testOpen(struct inode *p, struct file *f)
{
printk(KERN_EMERG"testOpen\r\n");
return 0;
}


int testWrite(struct file *f, const char __user *u, size_t s, loff_t *l)
{
printk(KERN_EMERG"testWrite\r\n");
return 0;
}


int testRead(struct file *f, char __user *u, size_t s, loff_t *l)
{
printk(KERN_EMERG"testRead\r\n");
return 0;
}



void charDrvInit(void)
{

devNum = MKDEV(reg_major, reg_minor);
printk(KERN_EMERG"devNum is 0x%x\r\n", devNum);

gDev = kzalloc(sizeof(struct cdev), 0);

gFile = kzalloc(sizeof(struct file_operations), 0);

gFile->open = testOpen;
gFile->read = testRead;
gFile->write = testWrite;


gDev->owner = THIS_MODULE;


/****** 为何此处不需要注册 **********/
/**************************************
源代码中没有这段:
if(OK == register_chrdev_region(devNum, subDevNum, "testchar"))
{
printk(KERN_EMERG"register_chrdev_region ok\r\n");
}
else
{
printk(KERN_EMERG"register_chrdev_region error\r\n");
return ERROR;
}
为何这段不写上,不是都要注册么?
***********************************************************************/
cdev_init(gDev, gFile);
cdev_add(gDev, devNum, 3);

return;
}

void __exit charDrvExit(void)
{
return;
}


module_init(charDrvInit);

module_exit(charDrvExit);

MODULE_LICENSE("GPL");
...全文
84 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
falloutmx 2012-08-02
  • 打赏
  • 举报
回复
这是两种方法,具体有啥区别我没研究过

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧