Linux /dev 自动创建设备节点
- #include <linux/module.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <asm/uaccess.h>
- #define HELLO_MAJOR 250
- #define HELLO_MINOR 0
- #define NUMBER_OF_DEVICES 2
- struct class *hello_class;
- static struct cdev cdev;
- dev_t devno;
- static ssize_t hello_read(struct file *file, char __user *buf, size_t count,
- loff_t *ppos)
- {
- char *str = "hello world";
- copy_to_user(buf,str,strlen(str));
- *(buf + strlen(str)) = '\n';
- return count;
- }
- static ssize_t hello_open(struct inode *inode,struct file *file)
- {
- return 0;
- }
- static const struct file_operations hello_fops = {
- .open = hello_open,
- .read = hello_read,
- .owner = THIS_MODULE,
- };
- static int __init hello_init(void)
- {
- int ret;
- devno = MKDEV(HELLO_MAJOR,HELLO_MINOR);
- if(HELLO_MAJOR){
- ret = register_chrdev_region(devno,NUMBER_OF_DEVICES,"chrdev");
- }else{
- ret = alloc_chrdev_region(&devno, 0, NUMBER_OF_DEVICES, "chrdev");
- }
- if(ret < 0){
- printk("%s register chrdev error\n",__func__);
- return ret;
- }
- hello_class = class_create(THIS_MODULE,"hello_char_calss");
- if(IS_ERR(hello_class)){
- printk("%s create class error\n",__func__);
- return -1;
- }
- device_create(hello_class, NULL, devno, NULL, "chrdev");
- cdev_init(&cdev, &hello_fops);
- cdev.owner = THIS_MODULE;
- cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
- return 0;
- }
- static void __exit hello_exit(void)
- {
- printk("%s",__func__);
- cdev_del(&cdev);
- device_destroy(hello_class,devno);
- class_destroy(hello_class);
- unregister_chrdev_region(devno,NUMBER_OF_DEVICES);
- }
- module_init(hello_init);
- module_exit(hello_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("oracleloyal@gmail.com");
- Makefile 内容
- 1 ifeq ($(KERNELRELEASE),)
2 #KERNEL_DIR:=/home/archermind/zhaoxi/bsw_ww02_2016/kernel/cht
3 KERNEL_DIR:=/usr/src/linux-headers-3.13.0-32-generic
4 PWD:=$(shell pwd)
5 modules:
6 $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
7 modules_install:
8 $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules_install
9 clean:
10 rm -rf .*.cmd *.ko *.o modules.order Module.symvers *mod.c
11 .PHONY: modules modules_install clean
12 else
13 modules-objs := my.o
14 obj-m := my.o
15 endif编译模块安装之后会在/sys/class/看到hello_char_class 以及目录内的chrdev,同时也会在/dev下看到udev为我们建立的节点chrdev.
测试程序
- #include <stdio.h>
- #include <fcntl.h>
- int main(void)
- {
- int fd;
- int i;
- char buf[50];
- fd = open("/dev/chrdev",O_RDWR);
- if(fd < 0){
- printf("can't open dev\n");
- return -1;
- }
- read(fd,buf,11);
- printf("%s",buf);
- return 0;
- }
- 测试程序执行后会输出hello world.,
Linux /dev 自动创建设备节点的更多相关文章
- linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)
代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...
- platform型设备在/dev目录下自动创建设备节点的分析【转】
转自:http://blog.csdn.net/rockrockwu/article/details/7357648 系统启动过程中platform设备.驱动注册完毕,为什么在/dev目录下就自动创建 ...
- I.MX6 linux eGalaxTouch 自动获取设备节点
I.MX6 linux eGalaxTouch 自动获取设备节点 \\\\\\\\\\\\\\-*- 目录 -*-///////////// | 一. 需求: | 二. /proc/bus/input ...
- linux下自动创建设备文件节点---class
在驱动模块初始化函数中实现设备节点的自动创建 我们在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的 ...
- Linux 内核驱动自动创建设备节点并挂载设备
*注:本文来自http://blog.csdn.net/lwj103862095/article/details/17470573 一.首先需要在最开始定义两个数据结构: static struct ...
- 使用class 自动创建设备节点
#include <linux/init.h>// __init __exit #include <linux/module.h> // module_init module_ ...
- Linux设备驱动实现自己主动创建设备节点
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #inclu ...
- linux driver ------ 字符设备驱动 之 “ 创建设备节点流程 ”
在字符设备驱动开发的入门教程中,最常见的就是用device_create()函数来创建设备节点了,但是在之后阅读内核源码的过程中却很少见device_create()的踪影了,取而代之的是device ...
- linux驱动之设备号与创建设备节点
设备号: 1.自己主动分配 major = register_chrdev(0,"first_drv",&first_sdv_fops);//注冊 注冊设备时给设备号写0, ...
随机推荐
- oracle PL/SQL(procedure language/SQL)程序设计(在PL/SQL中使用SQL)
在PL/SQL程序中,允许使用的SQL语句只有DML和事务控制语句,使用DDL语句是非法的.使用SELECT语句从数据库中选取数据时,只能返回一行数据.使用COMMIT, ROLLBACK, 和SA ...
- python 基础——运算符重载
方法 重载 调用 __init__ 构造函数 x = Class() __del__ 析构函数 del x __str__ 打印 print x __call__ 调用函数 x(*args) __ge ...
- WingIde的快捷键
tab:自动补全 Alt+1:打开所有折叠 Alt+2:折叠所有classes Alt+3:折叠所有函数和类 Alt+Backspace:删除光标所在单词的光标前的部分 ...
- 第03篇. 标准Web项目Jetty9内嵌API简单启动
一直以来,想改变一些自己早已经习惯的事情. 到了一定年龄,便要学会寡言,每一句话都要有用,有重量. 喜怒不形于色,大事淡然,有自己的底线. --胖先生 昨天,简单的说了一下关于Jetty9的配置,大家 ...
- hdu 1530 最大团模板
说明摘自:pushing my way 的博文 最大团 通过该博主的代码,总算理解了最大团问题,但是他实现时的代码效率却不算太高.因此在最后献上我的模板.加了IO优化目前的排名是: 6 yejinru ...
- AngularJS学习小结
在刚学习AngularJS的时候觉得好像挺简单的,看见老师每次用很少的代码就做出用源生代码或者JQuery要用多行代码才做出的效果的时候觉得好像思路很简单,也很好写就写出来了,但是等到我们自己做的时候 ...
- Swiper之滑块3
上章Swiper滑块2.Swiper滑块1都是手动的,这章我们来自动的! 其实只是加了Swiper的speed(滑动速度即slider自动滑动开始到结束的时间)属性而已(∩_∩),单位是ms < ...
- php中使用PHPExcel操作excel(xls)文件
读取中文的xls.csv文件会有问题,网上找了下资料,发现PHPExcel类库好用,官网地址:http://phpexcel.codeplex.com/ 1.读取xls文件内容 代码如下 复制代码 ...
- Javascript获取URL地址变量参数值的方法
今天碰到在做一个动态页面的时候,需要用到 URL 的参数值来作判断,从而决定某一块内容在当前页面是否显示.例如exampe.html?parm1=xxx&parm2=xxx&parm3 ...
- 调用WCF接口的方法
通过对接口调用可能出现的异常作出判断和处理,避免资源的浪费和占用~ public class SvcHelper { public static void Using(T client, Action ...