在Ubuntu上为Android系统编写Linux内核驱动程序 + 编写加载动态模块ko

在智能手机时代,每个品牌的手机都有自己的个性特点。正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非iphone莫属了。 据统计,截止2011年5月,AppStore的应用软件数量达381062个,位居第一,而Android Market的应用软件数量达294738,紧随AppStore后面,并有望在8月份越过AppStore。随着Android系统逐步扩大市场占有率,终端设备的多样性亟需更多的移动开发人员的参与。 据业内统计,Android研发人才缺口至少30万。目前,对Android人才需求一类是偏向硬件驱动的Android人才需求,一类是偏向软件应用的Android人才需求。总的来说,对有志于从事Android硬件驱动的开发工程师来说,现在是一个大展拳脚的机会。那么,就让我们一起来看看如何为Android系统编写内核驱动程序吧。

        这里,我们不会为真实的硬件设备编写内核驱动程序。为了方便描述为Android系统编写内核驱动程序的过程,我们使用一个虚拟的硬件设备,这个设备只有一个4字节的寄存器,它可读可写。想起我们第一次学习程序语言时,都喜欢用“Hello, World”作为例子,这里,我们就把这个虚拟的设备命名为“hello”,而这个内核驱动程序也命名为hello驱动程序。其实,Android内核驱动程序和一般Linux内核驱动程序的编写方法是一样的,都是以Linux模块的形式实现的,具体可参考前面Android学习启动篇一文中提到的Linux Device Drivers一书。不过,这里我们还是从Android系统的角度来描述Android内核驱动程序的编写和编译过程。

       一. 参照前面两篇文章在Ubuntu上下载、编译和安装Android最新源代码在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)准备好Android内核驱动程序开发环境。

       二. 进入到kernel/common/drivers(kernel/drivers)目录,新建hello目录:

       USER-NAME@MACHINE-NAME:~/Android$ cd kernel/common/drivers

       USER-NAME@MACHINE-NAME:~/Android/kernel/common/drivers$ mkdir hello

       USER-NAME@MACHINE-NAME:~/Android$ cd kernel/drivers

       USER-NAME@MACHINE-NAME:~/Android/kernel/drivers$ mkdir hello

       三. 在hello目录中增加hello.h文件:
  1. #ifndef _HELLO_ANDROID_H_  
  2. #define _HELLO_ANDROID_H_  
  3.   
  4. #include <linux/cdev.h>  
  5. #include <linux/semaphore.h>  
  6.   
  7. #define HELLO_DEVICE_NODE_NAME  "hello"  
  8. #define HELLO_DEVICE_FILE_NAME  "hello"  
  9. #define HELLO_DEVICE_PROC_NAME  "hello"  
  10. #define HELLO_DEVICE_CLASS_NAME "hello"  
  11.   
  12. struct hello_android_dev {  
  13.     int val;  
  14.     struct semaphore sem;  
  15.     struct cdev dev;  
  16. };  
  17.   
  18. #endif  
   这个头文件定义了一些字符串常量宏,在后面我们要用到。此外,还定义了一个字符设备结构体hello_android_dev,这个就是我们虚拟的硬件设备了,val成员变量就代表设备里面的寄存器,它的类型为int,sem成员变量是一个信号量,是用同步访问寄存器val的,dev成员变量是一个内嵌的字符设备,这个Linux驱动程序自定义字符设备结构体的标准方法。

   四.在hello目录中增加hello.c文件,这是驱动程序的实现部分。驱动程序的功能主要是向上层提供访问设备的寄存器的值,包括读和写。这里,提供了三种访问设备寄存器的方法,一是通过proc文件系统来访问,二是通过传统的设备文件的方法来访问,三是通过devfs文件系统来访问。下面分段描述该驱动程序的实现。

   首先是包含必要的头文件和定义三种访问设备的方法:

  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/fs.h>  
  5. #include <linux/proc_fs.h>  
  6. #include <linux/device.h>  
  7. #include <asm/uaccess.h>  
  8.   
  9. #include "hello.h"  
  10.   
  11. /*主设备和从设备号变量*/  
  12. static int hello_major = 0;  
  13. static int hello_minor = 0;  
  14.   
  15. /*设备类别和设备变量*/  
  16. static struct class* hello_class = NULL;  
  17. static struct hello_android_dev* hello_dev = NULL;  
  18.   
  19. /*传统的设备文件操作方法*/  
  20. static int hello_open(struct inode* inode, struct file* filp);  
  21. static int hello_release(struct inode* inode, struct file* filp);  
  22. static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);  
  23. static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);  
  24.   
  25. /*设备文件操作方法表*/  
  26. static struct file_operations hello_fops = {  
  27.     .owner = THIS_MODULE,  
  28.     .open = hello_open,  
  29.     .release = hello_release,  
  30.     .read = hello_read,  
  31.     .write = hello_write,   
  32. };  
  33.   
  34. /*访问设置属性方法*/  
  35. static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr,  char* buf);  
  36. static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);  
  37.   
  38. /*定义设备属性*/  
  39. static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);  

        定义传统的设备文件访问方法,主要是定义hello_open、hello_release、hello_read和hello_write这四个打开、释放、读和写设备文件的方法:

  1. /*打开设备方法*/  
  2. static int hello_open(struct inode* inode, struct file* filp) {  
  3.     struct hello_android_dev* dev;          
  4.       
  5.     /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/  
  6.     dev = container_of(inode->i_cdev, struct hello_android_dev, dev);  
  7.     filp->private_data = dev;  
  8.       
  9.     return 0;  
  10. }  
  11.   
  12. /*设备文件释放时调用,空实现*/  
  13. static int hello_release(struct inode* inode, struct file* filp) {  
  14.     return 0;  
  15. }  
  16.   
  17. /*读取设备的寄存器val的值*/  
  18. static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {  
  19.     ssize_t err = 0;  
  20.     struct hello_android_dev* dev = filp->private_data;          
  21.   
  22.     /*同步访问*/  
  23.     if(down_interruptible(&(dev->sem))) {  
  24.         return -ERESTARTSYS;  
  25.     }  
  26.   
  27.     if(count < sizeof(dev->val)) {  
  28.         goto out;  
  29.     }          
  30.   
  31.     /*将寄存器val的值拷贝到用户提供的缓冲区*/  
  32.     if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) {  
  33.         err = -EFAULT;  
  34.         goto out;  
  35.     }  
  36.   
  37.     err = sizeof(dev->val);  
  38.   
  39. out:  
  40.     up(&(dev->sem));  
  41.     return err;  
  42. }  
  43.   
  44. /*写设备的寄存器值val*/  
  45. static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {  
  46.     struct hello_android_dev* dev = filp->private_data;  
  47.     ssize_t err = 0;          
  48.   
  49.     /*同步访问*/  
  50.     if(down_interruptible(&(dev->sem))) {  
  51.         return -ERESTARTSYS;          
  52.     }          
  53.   
  54.     if(count != sizeof(dev->val)) {  
  55.         goto out;          
  56.     }          
  57.   
  58.     /*将用户提供的缓冲区的值写到设备寄存器去*/  
  59.     if(copy_from_user(&(dev->val), buf, count)) {  
  60.         err = -EFAULT;  
  61.         goto out;  
  62.     }  
  63.   
  64.     err = sizeof(dev->val);  
  65.   
  66. out:  
  67.     up(&(dev->sem));  
  68.     return err;  
  69. }  

        定义通过devfs文件系统访问方法,这里把设备的寄存器val看成是设备的一个属性,通过读写这个属性来对设备进行访问,主要是实现hello_val_show和hello_val_store两个方法,同时定义了两个内部使用的访问val值的方法__hello_get_val和__hello_set_val:

  1. /*读取寄存器val的值到缓冲区buf中,内部使用*/  
  2. static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) {  
  3.     int val = 0;          
  4.   
  5.     /*同步访问*/  
  6.     if(down_interruptible(&(dev->sem))) {                  
  7.         return -ERESTARTSYS;          
  8.     }          
  9.   
  10.     val = dev->val;          
  11.     up(&(dev->sem));          
  12.   
  13.     return snprintf(buf, PAGE_SIZE, "%d\n", val);  
  14. }  
  15.   
  16. /*把缓冲区buf的值写到设备寄存器val中去,内部使用*/  
  17. static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count) {  
  18.     int val = 0;          
  19.   
  20.     /*将字符串转换成数字*/          
  21.     val = simple_strtol(buf, NULL, 10);          
  22.   
  23.     /*同步访问*/          
  24.     if(down_interruptible(&(dev->sem))) {                  
  25.         return -ERESTARTSYS;          
  26.     }          
  27.   
  28.     dev->val = val;          
  29.     up(&(dev->sem));  
  30.   
  31.     return count;  
  32. }  
  33.   
  34. /*读取设备属性val*/  
  35. static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {  
  36.     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);          
  37.   
  38.     return __hello_get_val(hdev, buf);  
  39. }  
  40.   
  41. /*写设备属性val*/  
  42. static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {   
  43.     struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);    
  44.       
  45.     return __hello_set_val(hdev, buf, count);  
  46. }  

        定义通过proc文件系统访问方法,主要实现了hello_proc_read和hello_proc_write两个方法,同时定义了在proc文件系统创建和删除文件的方法hello_create_proc和hello_remove_proc:

  1. /*读取设备寄存器val的值,保存在page缓冲区中*/  
  2. static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {  
  3.     if(off > 0) {  
  4.         *eof = 1;  
  5.         return 0;  
  6.     }  
  7.   
  8.     return __hello_get_val(hello_dev, page);  
  9. }  
  10.   
  11. /*把缓冲区的值buff保存到设备寄存器val中去*/  
  12. static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) {  
  13.     int err = 0;  
  14.     char* page = NULL;  
  15.   
  16.     if(len > PAGE_SIZE) {  
  17.         printk(KERN_ALERT"The buff is too large: %lu.\n", len);  
  18.         return -EFAULT;  
  19.     }  
  20.   
  21.     page = (char*)__get_free_page(GFP_KERNEL);  
  22.     if(!page) {                  
  23.         printk(KERN_ALERT"Failed to alloc page.\n");  
  24.         return -ENOMEM;  
  25.     }          
  26.   
  27.     /*先把用户提供的缓冲区值拷贝到内核缓冲区中去*/  
  28.     if(copy_from_user(page, buff, len)) {  
  29.         printk(KERN_ALERT"Failed to copy buff from user.\n");                  
  30.         err = -EFAULT;  
  31.         goto out;  
  32.     }  
  33.   
  34.     err = __hello_set_val(hello_dev, page, len);  
  35.   
  36. out:  
  37.     free_page((unsigned long)page);  
  38.     return err;  
  39. }  
  40.   
  41. /*创建/proc/hello文件*/  
  42. static void hello_create_proc(void) {  
  43.     struct proc_dir_entry* entry;  
  44.       
  45.     entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);  
  46.     if(entry) {  
  47.         entry->owner = THIS_MODULE;  
  48.         entry->read_proc = hello_proc_read;  
  49.         entry->write_proc = hello_proc_write;  
  50.     }  
  51. }  
  52.   
  53. /*删除/proc/hello文件*/  
  54. static void hello_remove_proc(void) {  
  55.     remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);  
  56. }  

   最后,定义模块加载和卸载方法,这里只要是执行设备注册和初始化操作:

  1. /*初始化设备*/  
  2. static int  __hello_setup_dev(struct hello_android_dev* dev) {  
  3.     int err;  
  4.     dev_t devno = MKDEV(hello_major, hello_minor);  
  5.   
  6.     memset(dev, 0, sizeof(struct hello_android_dev));  
  7.   
  8.     cdev_init(&(dev->dev), &hello_fops);  
  9.     dev->dev.owner = THIS_MODULE;  
  10.     dev->dev.ops = &hello_fops;          
  11.   
  12.     /*注册字符设备*/  
  13.     err = cdev_add(&(dev->dev),devno, 1);  
  14.     if(err) {  
  15.         return err;  
  16.     }          
  17.   
  18.     /*初始化信号量和寄存器val的值*/  
  19.     //init_MUTEX(&(dev->sem)); 
  20.    sema_init(&(dev->sem),1);
  21.     dev->val = 0;  
  22.   
  23.     return 0;  
  24. }  
  25.   
  26. /*模块加载方法*/  
  27. static int __init hello_init(void){   
  28.     int err = -1;  
  29.     dev_t dev = 0;  
  30.     struct device* temp = NULL;  
  31.   
  32.     printk(KERN_ALERT"Initializing hello device.\n");          
  33.   
  34.     /*动态分配主设备和从设备号*/  
  35.     err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);  
  36.     if(err < 0) {  
  37.         printk(KERN_ALERT"Failed to alloc char dev region.\n");  
  38.         goto fail;  
  39.     }  
  40.   
  41.     hello_major = MAJOR(dev);  
  42.     hello_minor = MINOR(dev);          
  43.   
  44.     /*分配helo设备结构体变量*/  
  45.     hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);  
  46.     if(!hello_dev) {  
  47.         err = -ENOMEM;  
  48.         printk(KERN_ALERT"Failed to alloc hello_dev.\n");  
  49.         goto unregister;  
  50.     }          
  51.   
  52.     /*初始化设备*/  
  53.     err = __hello_setup_dev(hello_dev);  
  54.     if(err) {  
  55.         printk(KERN_ALERT"Failed to setup dev: %d.\n", err);  
  56.         goto cleanup;  
  57.     }          
  58.   
  59.     /*在/sys/class/目录下创建设备类别目录hello*/  
  60.     hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);  
  61.     if(IS_ERR(hello_class)) {  
  62.         err = PTR_ERR(hello_class);  
  63.         printk(KERN_ALERT"Failed to create hello class.\n");  
  64.         goto destroy_cdev;  
  65.     }          
  66.   
  67.     /*在/dev/目录和/sys/class/hello目录下分别创建设备文件hello*/  
  68.     temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);  
  69.     if(IS_ERR(temp)) {  
  70.         err = PTR_ERR(temp);  
  71.         printk(KERN_ALERT"Failed to create hello device.");  
  72.         goto destroy_class;  
  73.     }          
  74.   
  75.     /*在/sys/class/hello/hello目录下创建属性文件val*/  
  76.     err = device_create_file(temp, &dev_attr_val);  
  77.     if(err < 0) {  
  78.         printk(KERN_ALERT"Failed to create attribute val.");                  
  79.         goto destroy_device;  
  80.     }  
  81.   
  82.     dev_set_drvdata(temp, hello_dev);          
  83.   
  84.     /*创建/proc/hello文件*/  
  85.     hello_create_proc();  
  86.   
  87.     printk(KERN_ALERT"Succedded to initialize hello device.\n");  
  88.     return 0;  
  89.   
  90. destroy_device:  
  91.     device_destroy(hello_class, dev);  
  92.   
  93. destroy_class:  
  94.     class_destroy(hello_class);  
  95.   
  96. destroy_cdev:  
  97.     cdev_del(&(hello_dev->dev));  
  98.   
  99. cleanup:  
  100.     kfree(hello_dev);  
  101.   
  102. unregister:  
  103.     unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);  
  104.   
  105. fail:  
  106.     return err;  
  107. }  
  108.   
  109. /*模块卸载方法*/  
  110. static void __exit hello_exit(void) {  
  111.     dev_t devno = MKDEV(hello_major, hello_minor);  
  112.   
  113.     printk(KERN_ALERT"Destroy hello device.\n");          
  114.   
  115.     /*删除/proc/hello文件*/  
  116.     hello_remove_proc();          
  117.   
  118.     /*销毁设备类别和设备*/  
  119.     if(hello_class) {  
  120.         device_destroy(hello_class, MKDEV(hello_major, hello_minor));  
  121.         class_destroy(hello_class);  
  122.     }          
  123.   
  124.     /*删除字符设备和释放设备内存*/  
  125.     if(hello_dev) {  
  126.         cdev_del(&(hello_dev->dev));  
  127.         kfree(hello_dev);  
  128.     }          
  129.   
  130.     /*释放设备号*/  
  131.     unregister_chrdev_region(devno, 1);  
  132. }  
  133.   
  134. MODULE_LICENSE("GPL");  
  135. MODULE_DESCRIPTION("First Android Driver");  
  136.   
  137. module_init(hello_init);  
  138. module_exit(hello_exit);  

    五.在hello目录中新增Kconfig和Makefile两个文件,其中Kconfig是在编译前执行配置命令make menuconfig时用到的,而Makefile是执行编译命令make是用到的:

       Kconfig文件的内容

       config HELLO
           tristate "First Android Driver"
           default n
           help
           This is the first android driver.
      Makefile文件的内容
      obj-$(CONFIG_HELLO) += hello.o //我的做法是直接写成 obj-y += hello.o 表示内建编译, 或者obj-m += hello.o 表示模块编译
      在Kconfig文件中,tristate表示编译选项HELLO支持在编译内核时,hello模块支持以模块、内建和不编译三种编译方法,默认是不编译,因此,在编译内核前,我们还需要执行make menuconfig命令来配置编译选项,使得hello可以以模块或者内建的方法进行编译。
      在Makefile文件中,根据选项HELLO的值,执行不同的编译方法。
      六. 修改arch/arm/Kconfig和drivers/kconfig两个文件,在menu "Device Drivers"和endmenu之间添加一行:
       source "drivers/hello/Kconfig"
        这样,执行make menuconfig时,就可以配置hello模块的编译选项了。. 
        七. 修改drivers/Makefile文件,添加一行:
         obj-$(CONFIG_HELLO) += hello/
        八. 配置编译选项:
         USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make menuconfig
        找到"Device Drivers" => "First Android Drivers"选项,设置为y。
        注意,如果内核不支持动态加载模块,这里不能选择m,虽然我们在Kconfig文件中配置了HELLO选项为tristate。要支持动态加载模块选项,必须要在配置菜单中选择Enable loadable module support选项;在支持动态卸载模块选项,必须要在Enable loadable module support菜单项中,选择Module unloading选项。
        九. 编译:
         USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make
        USER-NAME@MACHINE-NAME:~/Android/kernel$ make
        编译成功后,就可以在hello目录下看到hello.o文件了,这时候编译出来的zImage已经包含了hello驱动。
        十. 参照 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)一文所示,运行新编译的内核文件,验证hello驱动程序是否已经正常安装:
         USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel ./kernel/common/arch/arm/boot/zImage &
        USER-NAME@MACHINE-NAME:~/Android$ adb shell
         进入到dev目录,可以看到hello设备文件:
        root@android:/ # cd dev
        root@android:/dev # ls
         进入到proc目录,可以看到hello文件:
        root@android:/ # cd proc
        root@android:/proc # ls
         访问hello文件的值:
        root@android:/proc # cat hello
        0
        root@android:/proc # echo '5' > hello
        root@android:/proc # cat hello
        5
         进入到sys/class目录,可以看到hello目录:
        root@android:/ # cd sys/class
        root@android:/sys/class # ls
         进入到hello目录,可以看到hello目录:
        root@android:/sys/class # cd hello
        root@android:/sys/class/hello # ls
         进入到下一层hello目录,可以看到val文件:
        root@android:/sys/class/hello # cd hello
        root@android:/sys/class/hello/hello # ls
        访问属性文件val的值:
        root@android:/sys/class/hello/hello # cat val
        5
        root@android:/sys/class/hello/hello # echo '0'  > val
        root@android:/sys/class/hello/hello # cat val
        0
         至此,我们的hello内核驱动程序就完成了,并且验证一切正常。这里我们采用的是系统提供的方法和驱动程序进行交互,也就是通过proc文件系统和devfs文件系统的方法,下一篇文章中,我们将通过自己编译的C语言程序来访问/dev/hello文件来和hello驱动程序交互,敬请期待。

老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注!


**********************************************************************************************************************************************************

如何动态编译和加载一个模块, 对于初期调试驱动, 相当的有用!

修改Makefile文件的内容
      obj-y += hello.o (表示内建编译)----》 obj-m += hello.o (表示模块编译)

1. 编译完内核后检测输出目录 out/target/product/<project name>/obj/KERNEL_OBJ/drivers/hello, 有文件 hello.ko

2. $adb push hello.ko /data/

3. 进入/sys/class/ 目录检查是否有hello目录,没有!

4. # cd data

    insmod hello.ko  //动态加载hello.ko 模块

5. 再次进入/sys/class/ 目录检查是否有hello目录,有了!

6. 进入data目录,rmmod hello

7. 进入/sys/class/ 目录检查是否有hello目录,又没有!

**********************************************************************************************************************************************************

添加 如何增加一个 input device, 更简单容易

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/input.h>

static int val = 0;
//static atomic_t    module_ready;
static struct input_dev *input_dev;

static ssize_t attr_input_show(struct class *class,
                struct class_attribute *attr, char *buf)
{
    return snprintf(buf, PAGE_SIZE, "%d\n", val);
}

static ssize_t attr_input_store(struct class *class,
                   struct class_attribute *attr,
                   const char *buf, size_t count)
{
    val = simple_strtol(buf, NULL, 10);
    return count;
}

static struct class *myinput_class;
static struct class_attribute input_attributes =
    __ATTR(val, 0666, attr_input_show, attr_input_store);

static int __init input_detect_init(void)
{
    int err;

    input_dev = input_allocate_device();
    if (!input_dev) {
        pr_err("%s: input_allocate_device failed\n",
            __func__);
        err = -ENOMEM;
        goto out;
    }

    input_dev->name = "simpleinput";
    input_set_capability(input_dev, EV_SW,SW_JACK_PHYSICAL_INSERT);

    err = input_register_device(input_dev);
    if (err) {
        pr_err("%s: input_register_device failed!\n",
            __func__);
        input_free_device(input_dev);
        goto out;
    }

    myinput_class = class_create(THIS_MODULE, "simple_input");
    if (IS_ERR(myinput_class)) {
        err = PTR_ERR(myinput_class);
        pr_err("%s: cannot create simpleinput_input class\n", __func__);
        goto out_input_device;
    }

    err = class_create_file(myinput_class, &input_attributes);
    if (err) {
        pr_err("%s: cannot create sysfs file\n", __func__);
        goto out_class;
    }

    return 0;

out_class:
    class_destroy(myinput_class);
out_input_device:
    input_unregister_device(input_dev);
out:
    pr_err("%s: cannot initialize, error %d\n", __func__, err);
    return err;
}
module_init(input_detect_init);

static void __exit input_detect_exit(void)
{
    class_remove_file(myinput_class, &input_attributes);
    class_destroy(myinput_class);
    input_unregister_device(input_dev);
}
module_exit(input_detect_exit);

可以进入 /sys/class/simple_input 目录验证, 还可以getevent -i 验证添加的simple_input 输入设备
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值