(5)x210:2.4之前的字符驱动-动态获得mymajor主设备号

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#define MYMAJOR 200
#define MYNAME "chardev"
int mymajor ;
static int test_chardev_open(struct inode *inode,struct file *file)
{
printk(KERN_INFO "test chardev_open\n");
return 0;
}
static int test_chardev_release(struct inode *inode,struct file *file){
printk(KERN_INFO "test chardev_release\n");
return 0;
}
static ssize_t test_chardev_read(struct file *file,char __user *buf,size_t size, loff_t *ppos)
{
printk(KERN_INFO " test read\n");
return 0;

}
static ssize_t test_chardev_write(struct file *file,
const char __user *ubuf,size_t count,loff_t * ppos){
printk(KERN_INFO " test write\n");
return 0;
}

static const struct file_operations test_fops={
.owner=THIS_MODULE,
.open=test_chardev_open,
.release=test_chardev_release,
.write=test_chardev_write,
.read=test_chardev_read,
};
static int __init chardev_init(void)
{ int ret=-1;
printk(KERN_INFO "jimmy module chardev init");
mymajor=register_chrdev(0,MYNAME,&test_fops); //由内核分配,然后mkdnod /dev/chardev mymajor 0生成设备文件

//注意/dev/chardev是MYNAME给出的,不能自己任意命名,这是测试发现的,与静态指定主设备的任意命名稍微不同

if(mymajor<0)

{
printk(KERN_ERR "register_chrdev fail\n");
return -EINVAL;
}

printk(KERN_INFO "register_chrdev success...myjor=%d.\n",mymajor);
return 0;
}
static void __exit chardev_exit(void)
{
    
printk(KERN_INFO "jimmy module chardev exit\n");
unregister_chrdev(MYMAJOR, MYNAME);
}

module_init(chardev_init);
module_exit(chardev_exit);
// MODULE_xxx这种宏作用是用来添加模块描述信息

MODULE_LICENSE("GPL");                // 描述模块的许可证

MODULE_AUTHOR("aston");                // 描述模块的作者

MODULE_DESCRIPTION("module test");    // 描述模块的介绍信息

MODULE_ALIAS("alias xxx");            // 描述模块的别名信息 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值