1. #include <linux/module.h>
    2. #include <linux/module.h>
    3. #include <linux/kernel.h>
    4. #include <linux/init.h>
    5. #include <linux/fs.h>
    6. #include <linux/cdev.h>
    7. #include <linux/device.h>
    8. #include <asm/uaccess.h>
    9. #define HELLO_MAJOR 250
    10. #define HELLO_MINOR 0
    11. #define NUMBER_OF_DEVICES 2
    12. struct class *hello_class;
    13. static struct cdev cdev;
    14. dev_t devno;
    15. static ssize_t hello_read(struct file *file, char __user *buf, size_t count,
    16. loff_t *ppos)
    17. {
    18. char *str = "hello world";
    19. copy_to_user(buf,str,strlen(str));
    20. *(buf + strlen(str)) = '\n';
    21. return count;
    22. }
    23. static ssize_t hello_open(struct inode *inode,struct file *file)
    24. {
    25. return 0;
    26. }
    27. static const struct file_operations hello_fops = {
    28. .open = hello_open,
    29. .read = hello_read,
    30. .owner = THIS_MODULE,
    31. };
    32. static int __init hello_init(void)
    33. {
    34. int ret;
    35. devno = MKDEV(HELLO_MAJOR,HELLO_MINOR);
    36. if(HELLO_MAJOR){
    37. ret = register_chrdev_region(devno,NUMBER_OF_DEVICES,"chrdev");
    38. }else{
    39. ret = alloc_chrdev_region(&devno, 0, NUMBER_OF_DEVICES, "chrdev");
    40. }
    41. if(ret < 0){
    42. printk("%s register chrdev error\n",__func__);
    43. return ret;
    44. }
    45. hello_class = class_create(THIS_MODULE,"hello_char_calss");
    46. if(IS_ERR(hello_class)){
    47. printk("%s create class error\n",__func__);
    48. return -1;
    49. }
    50. device_create(hello_class, NULL, devno, NULL, "chrdev");
    51. cdev_init(&cdev, &hello_fops);
    52. cdev.owner = THIS_MODULE;
    53. cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
    54. return 0;
    55. }
    56. static void __exit hello_exit(void)
    57. {
    58. printk("%s",__func__);
    59. cdev_del(&cdev);
    60. device_destroy(hello_class,devno);
    61. class_destroy(hello_class);
    62. unregister_chrdev_region(devno,NUMBER_OF_DEVICES);
    63. }
    64. module_init(hello_init);
    65. module_exit(hello_exit);
    66. MODULE_LICENSE("GPL");
    67. MODULE_AUTHOR("oracleloyal@gmail.com");
    68. Makefile 内容
    69. 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.

      测试程序

      1. #include <stdio.h>
      2. #include <fcntl.h>
      3. int main(void)
      4. {
      5. int fd;
      6. int i;
      7. char buf[50];
      8. fd = open("/dev/chrdev",O_RDWR);
      9. if(fd < 0){
      10. printf("can't open dev\n");
      11. return -1;
      12. }
      13. read(fd,buf,11);
      14. printf("%s",buf);
      15. return 0;
      16. }
      17. 测试程序执行后会输出hello world.,

Linux /dev 自动创建设备节点的更多相关文章

  1. linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)

    代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...

  2. platform型设备在/dev目录下自动创建设备节点的分析【转】

    转自:http://blog.csdn.net/rockrockwu/article/details/7357648 系统启动过程中platform设备.驱动注册完毕,为什么在/dev目录下就自动创建 ...

  3. I.MX6 linux eGalaxTouch 自动获取设备节点

    I.MX6 linux eGalaxTouch 自动获取设备节点 \\\\\\\\\\\\\\-*- 目录 -*-///////////// | 一. 需求: | 二. /proc/bus/input ...

  4. linux下自动创建设备文件节点---class

    在驱动模块初始化函数中实现设备节点的自动创建 我们在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的 ...

  5. Linux 内核驱动自动创建设备节点并挂载设备

    *注:本文来自http://blog.csdn.net/lwj103862095/article/details/17470573 一.首先需要在最开始定义两个数据结构: static struct ...

  6. 使用class 自动创建设备节点

    #include <linux/init.h>// __init __exit #include <linux/module.h> // module_init module_ ...

  7. Linux设备驱动实现自己主动创建设备节点

    #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #inclu ...

  8. linux driver ------ 字符设备驱动 之 “ 创建设备节点流程 ”

    在字符设备驱动开发的入门教程中,最常见的就是用device_create()函数来创建设备节点了,但是在之后阅读内核源码的过程中却很少见device_create()的踪影了,取而代之的是device ...

  9. linux驱动之设备号与创建设备节点

    设备号: 1.自己主动分配 major = register_chrdev(0,"first_drv",&first_sdv_fops);//注冊 注冊设备时给设备号写0, ...

随机推荐

  1. WGS84坐标系下,经纬度如何换算成米

    参考博客:显示瓦片地图  http://www.cnblogs.com/rhinoxy/p/4995731.html 注意:这里的计算方法精度相差比较大,不满足精确计算的需要. 需要理解的GIS概念: ...

  2. Linux之用户管理

    1.添加普通用户 [root@server ~]# useradd chenjiafa   //添加一个名为chenjiafa的用户[root@server ~]# passwd chenjiafa  ...

  3. css中常用的几种居中方法

    在前端面试中,大都会问你div居中的方法: 文笔不好,就随便寥寥几句话概括了, 不过以后文笔肯定会变得更好一些的. 今天我们就细数一下几种方法: 1,使用position:absolute,设置lef ...

  4. Backbone.js学习之View

    千呼万唤始出来,终于到最后一个要点View了.照旧,先来一睹官方文档: Backbone views are almost more convention than they are code - t ...

  5. C#--深入分析委托与事件

    本篇文章将为你介绍一下 Delegate 的使用方式,逐渐揭开 C# 当中事件(Event)的由来,它能使处理委托类型的过程变得更加简单. 还将为您解释委托的协变与逆变,以及如何使用 Delegate ...

  6. C语言经典参考书籍

    <C程序设计语言> Brian W.Kernighan,Dennis M.Ritchie 编著:C语言的开山之作.C程序员应该人手一本. <C语言参考手册> Samuel P. ...

  7. SQL Server 2008复制发布订阅(数据同步)

    数据库同步问题 1.有一台主数据库服务器A和另外一台数据库服务器B,客户端首先访问数据库B,当B数据库服务器挂掉时就访问A,当对数据库B进行DML操作时,同时对A进行更新,如果A与B之间通讯失败,则将 ...

  8. 根据数据库内容动态生成html页面

    之前使用了很多方法,但是都很复杂. 项目里包括了数据库的管理页面,对数据库进行修改(新增,插入,删除)等之后,在另一个页面使用. 使用时采用按下相应label弹出所有信息的方法,以html的形式将数据 ...

  9. C# DateTime 日期加1天 减一天 加一月 减一月 等方法(转)

    //今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-).ToShortDateString ...

  10. iOS - 移动设备防丢失App

    一.原理 二.数据获取 三.报警