linux驱动21:内核创建类class_create
利用mknod命令手动创建设备节点不方便,Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>static int major = 277;
static int minor = 0;
static dev_t helloNum;
struct class *helloClass = NULL;
struct device *helloDev = NULL;int hello_open(struct inode *pinode, struct file *pfile)
{printk("hello_open, minor:%d, major:%d\n", iminor(pinode), imajor(pinode));return 0;
}int hello_release(struct inode *pinode, struct file *pfile)
{printk("hello_release, minor:%d, major:%d\n", iminor(pinode), imajor(pinode)); return 0;
}static struct file_operations hello_ops = {.open = hello_open,.release = hello_release,
};static int hello_init(void)
{int ret = 0;printk("hello_init\n");ret = register_chrdev( major, "hello", &hello_ops);if(ret < 0){printk("register_chrdev failed.\n");return ret;}helloClass = class_create(THIS_MODULE, "hellocls");if (IS_ERR(helloClass)) {printk("class_create failed.\n");ret = PTR_ERR(helloClass);goto error_exit1;}helloNum = MKDEV(major,minor);printk("major:%d, minor:%d\n", MAJOR(helloNum), MINOR(helloNum));helloDev = device_create(helloClass, NULL, helloNum, NULL, "hello0");if (IS_ERR(helloDev)) {printk("device_create failed.\n");ret = PTR_ERR(helloDev);goto error_exit2;}return 0;error_exit2:class_destroy(helloClass);error_exit1:unregister_chrdev(major,"hello");return ret;
}static void hello_exit(void)
{printk("hello_exit\n");device_destroy(helloClass, helloNum);class_destroy(helloClass);unregister_chrdev(major,"hello");
}MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
linux驱动21:内核创建类class_create相关推荐
- Linux驱动开发—内核I2C驱动详解
Linux驱动开发--内核I2C驱动 I2C驱动文件结构 I2C数据传输过程 i2c_transfer i2c_msg I2C通讯常用的接口函数(老版本) 快速读写接口函数:(连续读写) 常用的读操作 ...
- Linux驱动框架之misc类设备驱动框架
1.何为misc设备 (1)misc中文名就是杂项设备\杂散设备,因为现在的硬件设备多种多样,有好些设备不好对他们进行一个单独的分类,所以就将这些设备全部归属于 杂散设备,也就是misc设备,例如像a ...
- linux内核驱动ldd3_手把手教Linux驱动7内核互斥锁
互斥体概述 信号量是在并行处理环境中对多个处理器访问某个公共资源进行保护的机制,mutex用于互斥操作.信号量的count初始化为1,down()/up()也可以实现类似mutex的作用. mutex ...
- 宋宝华_2010年11-12月Linux驱动和内核讲座PPT下载
12月29日,宋宝华老师在线讲座(按键和LCD驱动) cloudquan 2010-12-20 2/146 heyan0208 3 天前 00:37 宋宝华_2010年12月11日_& ...
- linux驱动:自动创建设备节点
在加载驱动模块后,就要自己使用mknod创建设备节点,这样虽然是可行的,但是比较麻烦.我们可以在__init()函数里面添加一些函数,自动创建设备节点.创建设备节点使用了两个函数 class_crea ...
- linux 设备节点 驱动,【Linux驱动】自动创建设备节点
开始学习驱动的时候,是将驱动程序编译成模块然后用mknod命令手动建立设备节点以提供给应用程序调用.这对于刚开始调试驱动程序的时候常用的一种方法.但是,当有种需要必须在系统启动的时候就将驱动程序就绪, ...
- Linux驱动之内核加载模块过程分析
Linux内核支持动态的加载模块运行:比如insmod first_drv.ko,这样就可以将模块加载到内核所在空间供应用程序调用.现在简单描述下insmod first_drv.ko的过程 1.in ...
- Linux驱动(8)--内核编译与配置
内核编译与配置 1. Menuconfig 2. Kconfig 3. Makefile 4. 内核配置与编译的流程 5. 注意 1. Menuconfig Linux编译器是通过.config文件确 ...
- Linux驱动:内核的中断机制之二--request_threaded_irq函数使用
内核开始支持中断线程(threaded interrupt handler),使用接口request_threaded_irq:原来的request_irq也继续支持.使用时可根据实际情况选择合适的接 ...
最新文章
- MySQL数据库使用连接更新表中某个字段数据
- Python os模块常用介绍
- 年方二八,人生的路口之上
- Flutter漫说:组件生命周期、State状态管理及局部重绘的实现(Inherit)
- .NET MVC 保存Session值,6位数验证码
- 论OD最原始的用途------找程序BUG
- Android客户端打包方案分享
- 时态知识图谱补全的方法及其进展
- python mysql 内存_Python将系统内存使用量写入mysql数据库
- 学习JavaScript需要什么基础?
- 不抛出异常的swap
- 使用maven给jar包生成对应的pom文件
- OpenGL 概念整理
- [并发]线程池关闭的问题
- MyBatis教程(看这一篇就够了)
- 两种典型的解空间树:子集树和排列树
- 自然语言处理nlp全领域综述
- 安装两个Mysql错误 ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)”
- 野指针的成因_C语言
- 股票策略:A股三季度配置偏向成长股