zoukankan      html  css  js  c++  java
  • Linux /dev 自动创建设备节点

      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.   
      10. #define HELLO_MAJOR 250  
      11. #define HELLO_MINOR 0  
      12. #define NUMBER_OF_DEVICES 2  
      13.   
      14. struct class *hello_class;  
      15. static struct cdev cdev;  
      16. dev_t devno;  
      17.   
      18. static ssize_t hello_read(struct file *file, char __user *buf, size_t count,  
      19.                 loff_t *ppos)  
      20. {  
      21.     char *str = "hello world";  
      22.   
      23.     copy_to_user(buf,str,strlen(str));  
      24.     *(buf + strlen(str)) = ' ';  
      25.     return count;  
      26. }  
      27.   
      28. static ssize_t hello_open(struct inode *inode,struct file *file)  
      29. {  
      30.     return 0;   
      31. }  
      32.   
      33. static const struct file_operations hello_fops = {  
      34.         .open = hello_open,  
      35.         .read = hello_read,  
      36.         .owner = THIS_MODULE,  
      37. };  
      38.   
      39. static int __init hello_init(void)  
      40. {  
      41.     int ret;  
      42.     devno = MKDEV(HELLO_MAJOR,HELLO_MINOR);  
      43.   
      44.     if(HELLO_MAJOR){  
      45.         ret = register_chrdev_region(devno,NUMBER_OF_DEVICES,"chrdev");  
      46.     }else{  
      47.         ret = alloc_chrdev_region(&devno, 0, NUMBER_OF_DEVICES, "chrdev");  
      48.     }  
      49.     if(ret < 0){  
      50.         printk("%s register chrdev error ",__func__);  
      51.         return ret;  
      52.     }  
      53.   
      54.     hello_class = class_create(THIS_MODULE,"hello_char_calss");  
      55.     if(IS_ERR(hello_class)){  
      56.         printk("%s create class error ",__func__);  
      57.         return -1;  
      58.     }  
      59.   
      60.     device_create(hello_class, NULL, devno, NULL, "chrdev");  
      61.       
      62.       
      63.     cdev_init(&cdev, &hello_fops);  
      64.     cdev.owner = THIS_MODULE;  
      65.     cdev_add(&cdev, devno, NUMBER_OF_DEVICES);  
      66.   
      67.     return 0;  
      68. }  
      69.   
      70.   
      71. static void __exit hello_exit(void)  
      72. {  
      73.     printk("%s",__func__);  
      74.     cdev_del(&cdev);  
      75.     device_destroy(hello_class,devno);  
      76.     class_destroy(hello_class);  
      77.     unregister_chrdev_region(devno,NUMBER_OF_DEVICES);  
      78.   
      79. }  
      80.   
      81. module_init(hello_init);  
      82. module_exit(hello_exit);  
      83. MODULE_LICENSE("GPL");  
      84. MODULE_AUTHOR("oracleloyal@gmail.com"); 
      85. Makefile 内容
      86.   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.   
        4. int main(void)  
        5. {  
        6.     int fd;  
        7.     int i;    
        8.     char buf[50];  
        9.   
        10.     fd = open("/dev/chrdev",O_RDWR);  
        11.     if(fd < 0){  
        12.         printf("can't open dev ");  
        13.         return -1;  
        14.     }  
        15.   
        16.     read(fd,buf,11);  
        17.       
        18.     printf("%s",buf);  
        19.   
        20.     return 0;  
        21. 测试程序执行后会输出hello world.,
  • 相关阅读:
    全排列
    合并两个有序列表——递归&非递归
    学习笔记:oracle学习一:oracle11g体系结构之体系结构概述和逻辑存储结构
    学习笔记:oracle之win10安装卸载oracle 11gR2步骤及常见问题解决
    日常工作问题解决:配置NTP服务器以及一些常见错误解决
    日常工作问题解决:redhat6.9--解决yum功能不能正常使用和配置yum源
    日常工作问题解决:Redhat6.5--解决yum无法正常安装配置问题
    日常工作问题解决:使用vmvare克隆centos6虚拟机造成无eth0的解决办法
    日常工作问题解决:centos7下配置网卡以及查询网卡UUID
    日常工作问题解决:centos7下使用yum安装软件报yum.pid锁定
  • 原文地址:https://www.cnblogs.com/oracleloyal/p/5359376.html
Copyright © 2011-2022 走看看