zoukankan      html  css  js  c++  java
  • 自动创建字符设备,不需mknod

    自动创建设备文件
    1.自动创建设备文件的流程
    字符设备驱动模块 --》创建一个设备驱动class--->创建属于class的device--->调用mdev工具(自动完成)--> 生成设备文件

    mdev工具会根据/sys下的class找到相对应的device,然后根据device创建设备文件

    class /sys/class
    device /sys/device

    1.1创建class /注销class

    struct class * class_create(struct module *owner,const char *name);
    参数:
    struct module *owner---> class属于哪些一个module --->THIS_MODULE
    const char *name --->自定义的类名.

    返回值:
    NULL -->失败


    void class_destroy(struct class * cls)


    例:
    struct class *led_class=class_create(THIS_MODULE,"led_class");
    class_destroy(led_class)


    1.2创建device /注销
    struct device *device_create(struct class *class, struct device *parent,
    dev_t devt, void *drvdata, const char *fmt, ...)
    参数:
    struct class *class --->当前device属于那个class
    struct device *parent --->device的parent,一般为NULL
    dev_t devt --->设备号
    void *drvdata --->设备的私有数据,一般为NULL
    const char *fmt ---》 device的名字,也就是设备文件名

    返回值:
    struct device *
    NULL -->失败

    void device_destroy(struct class * class,dev_t devt);


    struct device *dp=device_create(led_class,NULL,dev_no,NULL,"led_drv") ---> /dev/led_drv (字符设备)
    if(dp==NULL)
    {

    }

      1 #include<linux/module.h>
      2 #include<linux/kernel.h>
      3 #include<linux/cdev.h>
      4 #include<linux/fs.h>
      5 #include<linux/kdev_t.h>
      6 #include<linux/types.h>
      7 #include<linux/uaccess.h>
      8 #include<linux/string.h>
      9 #include<linux/ioport.h>
     10 #include<asm/io.h>
     11 #include<linux/init.h>
     12 #include<linux/device.h>
     13 #define  GPJ2CON_PA 0xe0200280
     14 #define  GPJ2DAT_PA 0xe0200280
     15 unsigned int *GPJ2CON_VA;
     16 unsigned int *GPJ2DAT_VA;
     17 struct cdev chrdev;
     18 struct resource *res;
     19 struct class *cla;
     20 struct device *dev;
     21 unsigned int TestMajor=0;
     22 unsigned int TestMinor=0;
     23 dev_t dev_no;
     24 int ret;
     25 
     26 int testopen(struct inode *inode,struct file *file)
     27 {
     28     unsigned int a = ioread32(GPJ2CON_VA);
     29     a |= 0x1111;
     30     iowrite32(a,GPJ2CON_VA);
     31     printk("cdev init
    ");
     32     return 0;
     33     
     34 }
     35 ssize_t testwrite(struct file *file, const char __user *usr, size_t len, loff_t *off)
     36 {
     37     unsigned int a;
     38     
     39     copy_from_user(&a,usr,len);
     40     iowrite32(a,GPJ2DAT_VA);
     41     
     42 
     43 }
     44 ssize_t testread(struct file *file, char __user *usr, size_t len, loff_t *off)
     45 {
     46     unsigned int a = ioread32(GPJ2DAT_VA);
     47     copy_to_user(usr,&a,len);
     48 
     49 
     50 }
     51 int testrelease(struct inode *inode, struct file *file)
     52 {
     53     printk("close
    ");
     54     return 0;
     55 
     56 }
     57 
     58 struct file_operations fops=
     59 {
     60     .owner=THIS_MODULE,
     61     .open = testopen,
     62     .write = testwrite,
     63     .read = testread,
     64     .release = testrelease,
     65 };
     66 static int __init test_init(void)
     67 {
     68     dev_no = MKDEV(TestMajor,TestMinor);
     69     if(dev_no>0)
     70     {
     71         ret = register_chrdev_region(dev_no,1,"chrdev_test");    
     72     }
     73     else
     74     {
     75         alloc_chrdev_region(&dev_no,0,1,"chrdev_test");
     76     }
     77     if(ret<0)
     78     {
     79         return ret;
     80     }
     81     cdev_init(&chrdev,&fops);
     82     chrdev.owner=THIS_MODULE;
     83     cdev_add(&chrdev,dev_no,1);
     84     res = request_mem_region(GPJ2CON_PA,8,"gpj2_led");
     85     GPJ2CON_VA = ioremap(GPJ2CON_PA,8);
     86     GPJ2DAT_VA = GPJ2CON_VA + 1;
     87     cla = class_create(THIS_MODULE,"led_class");
     88     if(cla == NULL)
     89     {
     90         printk("class_creat() error!
    ");
     91     }
     92     dev = device_create(cla,NULL,dev_no,NULL,"chrdev_test");
     93     if(dev == NULL)
     94     {
     95         printk("device_creat() error!
    ");
     96     }
     97 
     98     return 0;
     99 }
    100 
    101 static int __exit test_exit(void)
    102 {
    103     unregister_chrdev_region(dev_no,1);
    104     cdev_del(&chrdev);
    105     iounmap(GPJ2CON_VA);
    106     release_mem_region(GPJ2CON_PA,8);
    107     device_destroy(cla,dev_no);
    108     class_destroy(cla);
    109     return 0;
    110 }
    111 
    112 module_init(test_init);
    113 module_exit(test_exit);
    114 
    115 
    116 MODULE_AUTHOR("FENG");
    117 MODULE_DESCRIPTION("the first module of char drivers");
    118 MODULE_LICENSE("GPL");
    119 MODULE_VERSION("V1.0");
  • 相关阅读:
    (转)C#开发微信门户及应用(5)--用户分组信息管理
    (转)C#开发微信门户及应用(4)--关注用户列表及详细信息管理
    (转)C#开发微信门户及应用(3)--文本消息和图文消息的应答
    (转)C#开发微信门户及应用(2)--微信消息的处理和应答
    (转)C#开发微信门户及应用(1)--开始使用微信接口
    范仁义js课程---41、函数介绍
    函数和方法的区别
    范仁义js课程---40、对象的小实例
    范仁义js课程---39、对象的json写法
    范仁义js课程---38、对象存储
  • 原文地址:https://www.cnblogs.com/defen/p/5466392.html
Copyright © 2011-2022 走看看