Linux驱动入门


1. 如何编写驱动程序

  1. 确定主设备号,也可以让内核分配。
  2. 定义自己的 file_operations 结构体。
static struct file_operations hello_drv = {
	.owner	 = THIS_MODULE,
	.open    = hello_drv_open,
	.read    = hello_drv_read,
	.write   = hello_drv_write,
	.release = hello_drv_close,
};
  1. 实现对应的 drv_open/drv_write 等函数,填入 file_operations 结构体。
static int hello_drv_open (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}
  1. 把 file_operation 结构体告诉内核:register_chrdev。
major = register_chrdev(0, "hello", &hello_drv);
  1. 写一个入口函数来注册驱动程序,安装驱动程序时会调用它。
static int __init hello_init(void)
{
	int err;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	major = register_chrdev(0, "hello", &hello_drv);  
	return 0;
}
  1. 写一个出口函数来卸载驱动程序,出口函数调用 unregister_chrdev。
static void __exit hello_exit(void)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	unregister_chrdev(major, "hello");
}
  1. 其他完善:提供设备信息,自动创建节点:class_create,device_create。
hello_class = class_create(THIS_MODULE, "hello_class");
err = PTR_ERR(hello_class);
if (IS_ERR(hello_class)) {
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	unregister_chrdev(major, "hello");
	return -1;
}
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello");

device_destroy(hello_class, MKDEV(major, 0));
class_destroy(hello_class);
  1. 绑定入口函数和出口函数以及声明开源协议,用了 Linux 内核就要遵守这个协议开源,不然不让用。
module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值