zoukankan      html  css  js  c++  java
  • MTK Android添加驱动模块






    [编写linux驱动程序]



    一、编写驱动核心程序



    这里说的驱动核心程序是指运行在内核空间的,完全按linux驱动格式编写的,基本上与android没什么关系,一般包括xxx.h和xxx.c文件。



    进入到kernel/drivers目录,新建snsled目录,然后建立对应的snsled.h和snsled.c文件:



    //snsled.h


    <span style="color: rgb(0, 136, 0);">#ifndef</span><span style="color: rgb(0, 0, 0);"> _SNSLED_H_
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> _SNSLED_H_
    
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_NUM </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_CLASS_NAME  </span><span style="color: rgb(187, 68, 68);">"snsled"</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_DEVICE_NAME	</span><span style="color: rgb(187, 68, 68);">"snsled"</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_NODE_NAME   </span><span style="color: rgb(187, 68, 68);">"snsled"</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_PROC_NAME	</span><span style="color: rgb(187, 68, 68);">"snsled"</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_IOC_MAGIC   </span><span style="color: rgb(187, 68, 68);">'k'</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_IO_ON        </span><span style="color: rgb(0, 102, 102);">2323</span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">//_IO(SNSLED_IOC_MAGIC, 0)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_IO_OFF       </span><span style="color: rgb(0, 102, 102);">2324</span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">//_IO(SNSLED_IOC_MAGIC, 1)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_IOW_PWM     	</span><span style="color: rgb(0, 102, 102);">2325</span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">//_IOW(SNSLED_IOC_MAGIC, 2, int)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNSLED_IOR_PWM     	</span><span style="color: rgb(0, 102, 102);">2326</span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">//_IOR(SNSLED_IOC_MAGIC, 3, int)</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> snsled_cntx </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> r1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> semaphore sem</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);"> 
    	</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> cdev cdev</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#endif</span>


    //snsled.c


    <span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/module.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/moduleparam.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/init.h></span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/kernel.h></span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">/* printk() */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/slab.h></span><span style="color: rgb(0, 0, 0);">		</span><span style="color: rgb(0, 136, 0);">/* kmalloc() */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/fs.h></span><span style="color: rgb(0, 0, 0);">		</span><span style="color: rgb(0, 136, 0);">/* everything... */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/errno.h></span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">/* error codes */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/types.h></span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">/* size_t */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/proc_fs.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/fcntl.h></span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">/* O_ACCMODE */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/seq_file.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/cdev.h></span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><asm/system.h></span><span style="color: rgb(0, 0, 0);">		</span><span style="color: rgb(0, 136, 0);">/* cli(), *_flags */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><asm/uaccess.h></span><span style="color: rgb(0, 0, 0);">	</span><span style="color: rgb(0, 136, 0);">/* copy_*_user */</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">//#include <asm/semaphore.h> /* semaphore */</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/semaphore.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><linux/device.h></span><span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 136, 0);">/*class_create*/</span><span style="color: rgb(0, 0, 0);">  
    
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"snsled.h"</span><span style="color: rgb(0, 0, 0);">		</span><span style="color: rgb(0, 136, 0);">/* local definitions */</span><span style="color: rgb(0, 0, 0);">
    
    
    </span><span style="color: rgb(0, 136, 0);">/*
    #include <linux/module.h>
    #include <linux/platform_device.h>
    #include <linux/delay.h>
    #include <linux/string.h>
    #include <linux/ctype.h>
    #include <linux/leds.h>
    #include <linux/leds-mt65xx.h>
    #include <linux/workqueue.h>
    #include <linux/wakelock.h>
    #include <linux/slab.h>
    
    #include <cust_leds.h>*/</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#if defined (CONFIG_ARCH_MT6573)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><mach/mt6573_pwm.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><mach/mt6573_gpio.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><mach/pmu6573_sw.h></span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#elif</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>defined</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">CONFIG_ARCH_MT6516</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><mach/mt6516_pwm.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><mach/mt6516_gpio.h></span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#endif</span><span style="color: rgb(0, 0, 0);">
    
    
    </span><span style="color: rgb(0, 136, 0);">/*====macros====*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> BUF_SIZE </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 102, 102);">64</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNS_LED_CONTROL_LINE				GPIO99	</span><span style="color: rgb(0, 136, 0);">//GPIO39</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNS_LED_CONTROL_LINE_GPIO_MODE		GPIO_MODE_00	
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> SNS_LED_CONTROL_LINE_PWM_MODE		GPIO_MODE_01
    
    
    
    </span><span style="color: rgb(0, 136, 0);">/*====declares====*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 0, 102);">ssize_t</span><span style="color: rgb(0, 0, 0);"> snsled_read</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> __user </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">size_t</span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">loff_t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">f_pos</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 0, 102);">ssize_t</span><span style="color: rgb(0, 0, 0);"> snsled_write</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> __user </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">size_t</span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">loff_t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">f_pos</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>long</strong></span><span style="color: rgb(0, 0, 0);"> snsled_unlocked_ioctl</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>unsigned</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> cmd</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>unsigned</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>long</strong></span><span style="color: rgb(0, 0, 0);"> arg</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> inode </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">inode</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_release</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> inode </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">inode</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_turn_on</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_turn_off</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_set_pwm</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> arg</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    
    </span><span style="color: rgb(0, 136, 0);">/*====global====*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> g_snsled_major </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> g_snsled_minor </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> snsled_cntx </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>class</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">g_snsled_class </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file_operations g_snsled_fops </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">owner </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> THIS_MODULE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">read </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_read</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">write </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_write</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> 
        </span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">unlocked_ioctl </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_unlocked_ioctl</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">open </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_open</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">release </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_release</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">
    
    
    </span><span style="color: rgb(0, 136, 0);">/*====implements====*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 0, 102);">ssize_t</span><span style="color: rgb(0, 0, 0);"> snsled_read</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> __user </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">size_t</span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">loff_t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">f_pos</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#if 1</span><span style="color: rgb(0, 0, 0);">
        printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled read.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#else</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> tmp_buf</span><span style="color: rgb(102, 102, 0);">[</span><span style="color: rgb(0, 102, 102);">512</span><span style="color: rgb(102, 102, 0);">]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> len </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);">sprintf</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">tmp_buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"snsled read.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">copy_to_user</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> tmp_buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">))</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(0, 136, 0);">//do nothing</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#endif</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(102, 0, 102);">ssize_t</span><span style="color: rgb(0, 0, 0);"> snsled_write</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> __user </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">size_t</span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">loff_t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">f_pos</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled write.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>long</strong></span><span style="color: rgb(0, 0, 0);"> snsled_unlocked_ioctl</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>unsigned</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> cmd</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>unsigned</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>long</strong></span><span style="color: rgb(0, 0, 0);"> arg</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>long</strong></span><span style="color: rgb(0, 0, 0);"> retval </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:cmd=%d, arg=%d.
    "</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> cmd</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> arg</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    	
    	</span><span style="color: rgb(0, 136, 0);">/*
    	//extract the type and number bitfields, and don't decode
    	//wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()
    	if (_IOC_TYPE(cmd) != SNSLED_IOC_MAGIC) return -ENOTTY;
    	//if (_IOC_NR(cmd) > SNSLED_IOC_MAXNR) return -ENOTTY;
    
    	//to verify *arg is in user space
    	if (_IOC_DIR(cmd) & _IOC_READ)
    		err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
    	else if (_IOC_DIR(cmd) & _IOC_WRITE)
    		err =  !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
    	if (err) return -EFAULT;
           */</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>switch</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">cmd</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(170, 34, 255); "><strong>case</strong></span><span style="color: rgb(0, 0, 0);"> SNSLED_IO_ON</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
                printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:on.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    			</span><span style="color: rgb(0, 136, 0);">//if(mt_set_gpio_out(SNS_LED_CONTROL_LINE,GPIO_OUT_ONE)){printk("Snsled set gpio failed!! 
    ");}</span><span style="color: rgb(0, 0, 0);">
    			snsled_turn_on</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>break</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    			
            </span><span style="color: rgb(170, 34, 255); "><strong>case</strong></span><span style="color: rgb(0, 0, 0);"> SNSLED_IO_OFF</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
                printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:off.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    			</span><span style="color: rgb(0, 136, 0);">//if(mt_set_gpio_out(SNS_LED_CONTROL_LINE,GPIO_OUT_ZERO)){printk("Snsled set gpio failed!! 
    ");}</span><span style="color: rgb(0, 0, 0);">
    			snsled_turn_off</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>break</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    			
            </span><span style="color: rgb(170, 34, 255); "><strong>case</strong></span><span style="color: rgb(0, 0, 0);"> SNSLED_IOW_PWM</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">            
    			printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:set pwm, arg=%d.
    "</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> arg</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(0, 136, 0);">//retval = __get_user(g_snsled_ptr->r1, (int __user *)arg);</span><span style="color: rgb(0, 0, 0);">
                snsled_set_pwm</span><span style="color: rgb(102, 102, 0);">((</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> __user </span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">arg</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>break</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
            </span><span style="color: rgb(170, 34, 255); "><strong>case</strong></span><span style="color: rgb(0, 0, 0);"> SNSLED_IOR_PWM</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">   
    			</span><span style="color: rgb(0, 136, 0);">#if 0</span><span style="color: rgb(0, 0, 0);">
                retval </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> __put_user</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">r1</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> __user </span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">arg</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:read r1:%i.
    "</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">r1</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    			</span><span style="color: rgb(0, 136, 0);">#endif</span><span style="color: rgb(0, 0, 0);">
    			printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:read pwm -- not configured yet.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>break</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
            </span><span style="color: rgb(170, 34, 255); "><strong>default</strong></span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
                printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled ioctl:you got the wrong command.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>break</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> retval</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> inode </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">inode</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled: snsled_open.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#if 0	</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_mode</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE_GPIO_MODE</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio mode failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_dir</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_DIR_OUT</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio dir failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_out</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_OUT_ONE</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#endif</span><span style="color: rgb(0, 0, 0);">	
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_release</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> inode </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">inode</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">filp</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled: snsled_release.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#if 0	</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_mode</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE_GPIO_MODE</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio mode failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_dir</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_DIR_OUT</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio dir failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_out</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_OUT_ZERO</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#endif</span><span style="color: rgb(0, 0, 0);">	
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_turn_on</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled: snsled_turn_on.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_mode</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE_PWM_MODE</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio mode failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_dir</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_DIR_OUT</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio dir failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_out</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_OUT_ZERO</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_turn_off</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled: snsled_turn_off.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_mode</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE_GPIO_MODE</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio mode failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_dir</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_DIR_OUT</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio dir failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">mt_set_gpio_out</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNS_LED_CONTROL_LINE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">GPIO_OUT_ZERO</span><span style="color: rgb(102, 102, 0);">)){</span><span style="color: rgb(0, 0, 0);">printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Snsled set gpio failed!! 
    "</span><span style="color: rgb(102, 102, 0);">);}</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">//for old mode</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">/**
    struct _PWM_OLDMODE_REGS {
    	U16 IDLE_VALUE; //0
    	U16 GUARD_VALUE;	//0
    	U16 GDURATION;	//~
    	U16 WAVE_NUM;	//0
    	U16 DATA_WIDTH;	//high level, 13bits, 0~8191
    	U16 THRESH;	//t
    }PWM_MODE_OLD_REGS;
    **/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_set_pwm</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> arg</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> pwm_spec_config pwm_setting</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">pwm_no </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> PWM1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled: snsled_open begin.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    	
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">mode </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> PWM_MODE_OLD</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">clk_div </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> CLK_DIV16</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 136, 0);">//CLK_DIV128;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">clk_src </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> PWM_CLK_OLD_MODE_32K</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">	
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">PWM_MODE_OLD_REGS</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">IDLE_VALUE </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">PWM_MODE_OLD_REGS</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">GUARD_VALUE </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">PWM_MODE_OLD_REGS</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">GDURATION </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">8100</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">PWM_MODE_OLD_REGS</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">WAVE_NUM </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">PWM_MODE_OLD_REGS</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">DATA_WIDTH </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">8100</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	pwm_setting</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">PWM_MODE_OLD_REGS</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">THRESH </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">8100</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
    	pwm_set_spec_config</span><span style="color: rgb(102, 102, 0);">(&</span><span style="color: rgb(0, 0, 0);">pwm_setting</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">	
    	printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Snsled: snsled_open done.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    
    
    
    </span><span style="color: rgb(0, 136, 0);">//alloc device major</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> vircdex_alloc_major</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 0, 102);">dev_t</span><span style="color: rgb(0, 0, 0);"> devt </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> result </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        
        result </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> alloc_chrdev_region</span><span style="color: rgb(102, 102, 0);">(&</span><span style="color: rgb(0, 0, 0);">devt</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> g_snsled_minor</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> SNSLED_NUM</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> SNSLED_NODE_NAME</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        g_snsled_major </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> MAJOR</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">devt</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> result</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_release_major</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 0, 102);">dev_t</span><span style="color: rgb(0, 0, 0);"> devt </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> MKDEV</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_major</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> g_snsled_minor</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        unregister_chrdev_region</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">devt</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_setup_dev</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> snsled_cntx </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> devno </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> MKDEV</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_major</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> g_snsled_minor</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        
        cdev_init</span><span style="color: rgb(102, 102, 0);">(&(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">cdev</span><span style="color: rgb(102, 102, 0);">),</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">g_snsled_fops</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">cdev</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">owner </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> THIS_MODULE</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> cdev_add</span><span style="color: rgb(102, 102, 0);">(&</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">cdev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> devno</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">err</span><span style="color: rgb(102, 102, 0);">){</span><span style="color: rgb(0, 0, 0);">   
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(0, 136, 0);">//init_MUTEX(&(dev->sem));</span><span style="color: rgb(0, 0, 0);">
        sema_init</span><span style="color: rgb(102, 102, 0);">(&(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">sem</span><span style="color: rgb(102, 102, 0);">),</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_unsetup_dev</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> snsled_cntx </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        cdev_del</span><span style="color: rgb(102, 102, 0);">(&(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">cdev</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_create_devfiles</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">dev_t</span><span style="color: rgb(0, 0, 0);"> devt</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 136, 0);">//, const struct device_attribute *attr) {</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">    
        </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> device </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">dev </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
                              
        g_snsled_class </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> class_create</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">THIS_MODULE</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> SNSLED_CLASS_NAME</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">IS_ERR</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">))</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> PTR_ERR</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Failed to create class.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> CLASS_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        
        dev </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> device_create</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> devt</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> SNSLED_DEVICE_NAME</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(0, 136, 0);">//dev = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);  </span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(0, 136, 0);">//device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 );</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(0, 136, 0);">//dev = device_create(g_snsled_class, NULL, MKDEV(MYDRIVER_Major, 0), NULL, DEVICE_NAME);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">IS_ERR</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">))</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> PTR_ERR</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"Failed to create device.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> DEVICE_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(0, 136, 0);">/*err = device_create_file(dev, attr);  
        if(err < 0) {  
            printk(KERN_ALERT"Failed to create attribute file.");                  
            goto DEVICE_CREATE_FILE_ERR;  
        }*/</span><span style="color: rgb(0, 0, 0);">          
        printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT </span><span style="color: rgb(187, 68, 68);">"seems ok.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 136, 0);">//zmk@@debug    </span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        
    DEVICE_CREATE_FILE_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        device_destroy</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> devt</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    DEVICE_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        class_destroy</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">             
    CLASS_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);"> 
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_delete_devfiles</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">dev_t</span><span style="color: rgb(0, 0, 0);"> devt</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        device_destroy</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> devt</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        class_destroy</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_class</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(0, 136, 0);">//device_remove_file(dev, attr);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_read_proc</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">**</span><span style="color: rgb(0, 0, 0);">start</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">off_t</span><span style="color: rgb(0, 0, 0);"> offset</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
                                </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> count</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">eof</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">data</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> len </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);">sprintf</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"snsled read proc.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> len</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_create_proc_file</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> proc_dir_entry </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">entry </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
     
        entry </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> create_proc_read_entry</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNSLED_PROC_NAME</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
                                NULL</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> snsled_read_proc</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
                                NULL</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">entry</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>else</strong></span><span style="color: rgb(0, 0, 0);">                            
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">                        
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">                       
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">                       
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_delete_proc_file</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        remove_proc_entry</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">SNSLED_PROC_NAME</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    MODULE_LICENSE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"GPL"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> snsled_init</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 0, 102);">dev_t</span><span style="color: rgb(0, 0, 0);"> devt </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(0, 136, 0);">//[1] alloc node number</span><span style="color: rgb(0, 0, 0);">
        err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> vircdex_alloc_major</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT</span><span style="color: rgb(187, 68, 68);">"alloc major failed.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> ALLOC_MAJOR_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        devt </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> MKDEV</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_major</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> g_snsled_minor</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(0, 136, 0);">//[2] device object init    </span><span style="color: rgb(0, 0, 0);">
        g_snsled_ptr </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> kmalloc</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>sizeof</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> snsled_cntx</span><span style="color: rgb(102, 102, 0);">),</span><span style="color: rgb(0, 0, 0);"> GFP_KERNEL</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 0, 0);">ENOMEM</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT</span><span style="color: rgb(187, 68, 68);">"kmalloc failed.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> KMALLOC_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        memset</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>sizeof</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> snsled_cntx</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(0, 136, 0);">//[3] setup device</span><span style="color: rgb(0, 0, 0);">
        err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_setup_dev</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT</span><span style="color: rgb(187, 68, 68);">"device setup failed.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> DEVICE_SETUP_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
        
        </span><span style="color: rgb(0, 136, 0);">//[4] create files in directory "/dev/" and "/sys/" </span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(0, 136, 0);">///err = snsled_create_devfiles(devt, attr);</span><span style="color: rgb(0, 0, 0);">
        err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_create_devfiles</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">devt</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT</span><span style="color: rgb(187, 68, 68);">"devfiles create failed.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> DEVFILES_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
        </span><span style="color: rgb(0, 136, 0);">//[5] create proc file</span><span style="color: rgb(0, 0, 0);">
        err </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> snsled_create_proc_file</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
            printk</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">KERN_ALERT</span><span style="color: rgb(187, 68, 68);">"proc file create failed.
    "</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
            </span><span style="color: rgb(170, 34, 255); "><strong>goto</strong></span><span style="color: rgb(0, 0, 0);"> PROC_FILE_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
            
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
        
    PROC_FILE_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        snsled_delete_devfiles</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">devt</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    DEVFILES_CREATE_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        snsled_unsetup_dev</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    DEVICE_SETUP_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        kfree</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);"> 
    ALLOC_MAJOR_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        snsled_release_major</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
    KMALLOC_ERR</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> err</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> snsled_exit</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 0, 102);">dev_t</span><span style="color: rgb(0, 0, 0);"> devt </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> MKDEV</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_major</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> g_snsled_minor</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        
        snsled_delete_proc_file</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
        snsled_delete_devfiles</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">devt</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        snsled_unsetup_dev</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
        kfree</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">g_snsled_ptr</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);"> 
        snsled_release_major</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    module_init</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">snsled_init</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    module_exit</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">snsled_exit</span><span style="color: rgb(102, 102, 0);">);</span>


    二、配置Kconfig



    在snsled目录中,新建Kconfig文件:


           config SNSLED
               tristate "snsled Driver"
               default n #y ?
               help
               This is the sns led driver.
    


    其中,tristate表示编译选项HELLO支持在编译内核时,hello模块支持以模块、内建和不编译三种编译方法。



    三、配置Makefile



    1、在snsled目录中,新建snsled文件夹对应的Makefile:


    #obj-$(CONFIG_SNSLED) += snsled.o
    obj-y += snsled.o
    


    上面根据 CONFIG_SNSLED的值确定是否编译,y为编译。



    2、修改snsled的父目录 drivers/下的Makefile,加入:


    obj-$(CONFIG_HELLO) += snsled/
    


    这样便能在编译时编译到snsled这个文件夹。



    四、配置系统的autoconfig



    打开 mediatek/config/bbk73_gb/autoconfig/kconfig/project,加入:


    CONFIG_SNSLED=y
    


    在这里定义变量 CONFIG_SNSLED.



    • ps:目前这里配置好像还不能snsled自动编译进去,目前的操作是直接在用到CONFIG_SNSLED的地方用y替代。


    五、编译


    ./makeMtk bbk73_gb remake kernel bootimage
    






    [编写hal模块]



    一、新建xxx.h文件



    进入"hardware/libhardware/include/hardware"目录,新建vircdev.h文件:


    <span style="color: rgb(0, 136, 0);">#ifndef</span><span style="color: rgb(0, 0, 0);"> ANDROID_HELLO_INTERFACE_H
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> ANDROID_HELLO_INTERFACE_H
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hardware.h></span><span style="color: rgb(0, 0, 0);">
    
    __BEGIN_DECLS
    
    </span><span style="color: rgb(0, 136, 0);">/*定义模块ID*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> HELLO_HARDWARE_MODULE_ID </span><span style="color: rgb(187, 68, 68);">"hello"</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">/*硬件模块结构体*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_module_t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(0, 0, 0);"> common</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">/*硬件接口结构体*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(0, 0, 0);"> common</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> fd</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(*</span><span style="color: rgb(0, 0, 0);">set_val</span><span style="color: rgb(102, 102, 0);">)(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(*</span><span style="color: rgb(0, 0, 0);">get_val</span><span style="color: rgb(102, 102, 0);">)(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">
    
    __END_DECLS
    
    </span><span style="color: rgb(0, 136, 0);">#endif</span>


    这里按照Android硬件抽象层规范的要求,分别定义模块ID、模块结构体hello_module_t以及硬件接口结构体hello_device_t。在硬件接口结构体中,fd表示设备文件描述符,对应linux下我们经常接触到的设备文件"/dev/xxx",set_val和get_val为该HAL对上提供的函数接口。







    二、新建xxx.c文件



    进入到hardware/libhardware/modules目录,新建hello目录,并添加hello.c文件。 hello.c的内容较多,我们分段来看。



    1、包含相关头文件和定义相关结构


    <span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> LOG_TAG </span><span style="color: rgb(187, 68, 68);">"HelloStub"</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hardware.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hello.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><fcntl.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><errno.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><cutils/log.h></span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><cutils/atomic.h></span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> DEVICE_NAME </span><span style="color: rgb(187, 68, 68);">"/dev/hello"</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> MODULE_NAME </span><span style="color: rgb(187, 68, 68);">"Hello"</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> MODULE_AUTHOR </span><span style="color: rgb(187, 68, 68);">"shyluo@gmail.com"</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">/*设备打开和关闭接口*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_device_open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> name</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(102, 102, 0);">**</span><span style="color: rgb(0, 0, 0);"> device</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_device_close</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> device</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">/*设备访问接口*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_set_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_get_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">/*模块方法表*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_methods_t</span><span style="color: rgb(0, 0, 0);"> hello_module_methods </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	open</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> hello_device_open
    </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(0, 136, 0);">/*模块实例变量*/</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_module_t</span><span style="color: rgb(0, 0, 0);"> HAL_MODULE_INFO_SYM </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	common</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		tag</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> HARDWARE_MODULE_TAG</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    		version_major</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    		version_minor</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    		id</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> HELLO_HARDWARE_MODULE_ID</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    		name</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> MODULE_NAME</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    		author</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> MODULE_AUTHOR</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    		methods</span><span style="color: rgb(102, 102, 0);">:</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">hello_module_methods</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span>


    这里,实例变量名必须为HAL_MODULE_INFO_SYM,tag也必须为HARDWARE_MODULE_TAG,这是Android硬件抽象层规范规定的。



    2、定义hello_device_open函数


    <span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_device_open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>char</strong></span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> name</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(102, 102, 0);">**</span><span style="color: rgb(0, 0, 0);"> device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">dev </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">malloc</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>sizeof</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
    	
    	</span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello Stub: failed to alloc space"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    		</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 0, 0);">EFAULT</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    	memset</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>sizeof</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
    	dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">tag </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> HARDWARE_DEVICE_TAG</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">version </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">close </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> hello_device_close</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">set_val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> hello_set_val</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">get_val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> hello_get_val</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">((</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">fd </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">DEVICE_NAME</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> O_RDWR</span><span style="color: rgb(102, 102, 0);">))</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello Stub: failed to open /dev/hello -- %s."</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> strerror</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">errno</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">free</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    		</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 0, 0);">EFAULT</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">device </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    	LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello Stub: open /dev/hello successfully."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span>


    DEVICE_NAME定义为"/dev/hello"。由于设备文件是在内核驱动里面通过device_create创建的,而device_create创建的设备文件默认只有root用户可读写,而hello_device_open一般是由上层APP来调用的,这些APP一般不具有root权限,这时候就导致打开设备文件失败:


    Hello Stub: failed to open /dev/hello -- Permission denied.
    


    解决办法是类似于Linux的udev规则,打开Android源代码工程目录下,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:


    /dev/hello 0666 root root
    


    3、定义自定义的api函数



    这里定义hello_device_close、hello_set_val和hello_get_val这三个函数:


    <span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_device_close</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> hello_device </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">device</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		close</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">fd</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    		free</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    	
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_set_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello Stub: set value %d to device."</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    	write</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">fd</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>sizeof</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_get_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> dev</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello Stub: error val pointer"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    		</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 0, 0);">EFAULT</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
    	read</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">dev</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">fd</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>sizeof</strong></span><span style="color: rgb(102, 102, 0);">(*</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
    
    	LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello Stub: get value %d from device"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    
    	</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span>


    三、在hello目录下新建Android.mk文件


    <span style="color: rgb(0, 0, 0);">LOCAL_PATH </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> $</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">call </span><span style="color: rgb(170, 34, 255); "><strong>my</strong></span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 0, 0);">dir</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    include $</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">CLEAR_VARS</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    LOCAL_MODULE_TAGS </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> optional
    LOCAL_PRELINK_MODULE </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>false</strong></span><span style="color: rgb(0, 0, 0);">
    LOCAL_MODULE_PATH </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> $</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">TARGET_OUT_SHARED_LIBRARIES</span><span style="color: rgb(102, 102, 0);">)/</span><span style="color: rgb(0, 0, 0);">hw
    LOCAL_SHARED_LIBRARIES </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> liblog
    LOCAL_SRC_FILES </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> hello</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">c
    LOCAL_MODULE </span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);"> hello</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(170, 34, 255); "><strong>default</strong></span><span style="color: rgb(0, 0, 0);">
    include $</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">BUILD_SHARED_LIBRARY</span><span style="color: rgb(102, 102, 0);">)</span>


    注意,LOCAL_MODULE的定义规则,hello后面跟有default,hello.default能够保证我们的模块总能被硬象抽象层加载到。



    四、编译、重新打包Android系统镜像system.img


    $:~/Android$ mmm hardware/libhardware/modules/hello
    


    编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到hello.default.so文件了。


    $:USER-NAME@MACHINE-NAME:~/Android$ make snod
    


    重新打包后,system.img就包含我们定义的硬件抽象层模块hello.default了。







    [编写jni]



    虽然上一节我们在Android系统为我们自己的硬件增加了一个硬件抽象层模块,但是现在Java应用程序还不能访问到我们的硬件。我们还必须编写JNI方法和在Android的Application Frameworks层增加API接口,才能让上层Application访问我们的硬件。在这一节中,我们将首先完成jni接口的编写。







    一、新建com_android_server_HelloService.cpp文件



    进入到frameworks/base/services/jni目录,新建com_android_server_HelloService.cpp文件:


    <span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> LOG_TAG </span><span style="color: rgb(187, 68, 68);">"HelloService"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"jni.h"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"JNIHelp.h"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"android_runtime/AndroidRuntime.h"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><utils/misc.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><utils/Log.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hardware.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hello.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><stdio.h></span><span style="color: rgb(0, 0, 0);"> 
    
    </span><span style="color: rgb(170, 34, 255); "><strong>namespace</strong></span><span style="color: rgb(0, 0, 0);"> android  
    </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(0, 136, 0);">/*在硬件抽象层中定义的硬件访问结构体,参考<hardware/hello.h>*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> hello_device </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> NULL</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
    
        </span><span style="color: rgb(0, 136, 0);">/*通过硬件抽象层定义的硬件访问接口设置硬件寄存器val的值*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> hello_setVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jobject clazz</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jint value</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> value</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: set value %d to device."</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
                LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: device is not open."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
              
            hello_device</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">set_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    
        </span><span style="color: rgb(0, 136, 0);">/*通过硬件抽象层定义的硬件访问接口读取硬件寄存器val的值*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> jint hello_getVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jobject clazz</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
                LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: device is not open."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
            hello_device</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">get_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
              
            LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: get value %d from device."</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
          
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    
        </span><span style="color: rgb(0, 136, 0);">/*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>inline</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_device_open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">**</span><span style="color: rgb(0, 0, 0);"> device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">methods</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> HELLO_HARDWARE_MODULE_ID</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(102, 102, 0);">**)</span><span style="color: rgb(0, 0, 0);">device</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    
        </span><span style="color: rgb(0, 136, 0);">/*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> jboolean hello_init</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jclass clazz</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 0, 102);">hello_module_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
              
            LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: initializing......"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hw_get_module</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">HELLO_HARDWARE_MODULE_ID</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">**)&</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
                LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: hello Stub found."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device_open</span><span style="color: rgb(102, 102, 0);">(&(</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">),</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
                    LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: hello device is open."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                    </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
                LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: failed to open hello device."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
            LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: failed to get hello stub module."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">        
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    
        </span><span style="color: rgb(0, 136, 0);">/*JNI方法表*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">JNINativeMethod</span><span style="color: rgb(0, 0, 0);"> method_table</span><span style="color: rgb(102, 102, 0);">[]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(187, 68, 68);">"init_native"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"()Z"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">hello_init</span><span style="color: rgb(102, 102, 0);">},</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(187, 68, 68);">"setVal_native"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"(I)V"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">hello_setVal</span><span style="color: rgb(102, 102, 0);">},</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(187, 68, 68);">"getVal_native"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"()I"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">hello_getVal</span><span style="color: rgb(102, 102, 0);">},</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">  
    
        </span><span style="color: rgb(0, 136, 0);">/*注册JNI方法*/</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> register_android_server_HelloService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">env</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> jniRegisterNativeMethods</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"com/android/server/HelloService"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> method_table</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> NELEM</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">method_table</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(102, 102, 0);">};</span>


    注意文件的命名方法,com_android_server前缀表示的是包名,表示硬件服务HelloService是放在frameworks/base/services/java目录下的com/android/server目录的,即存在一个命名为com.android.server.HelloService的类。这里,我们暂时略去HelloService类的描述,在下一篇文章中,我们将回到HelloService类来。简单地说,HelloService是一个提供Java接口的硬件访问服务类。



    在这个cpp文件中,我们主要是做了以下事情:



    1、包括头文件



    (尤其是在hal层所定义的头文件)


    <span style="color: rgb(0, 136, 0);">#define</span><span style="color: rgb(0, 0, 0);"> LOG_TAG </span><span style="color: rgb(187, 68, 68);">"HelloService"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"jni.h"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"JNIHelp.h"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"android_runtime/AndroidRuntime.h"</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><utils/misc.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><utils/Log.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hardware.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><hardware/hello.h></span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(0, 136, 0);">#include</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);"><stdio.h></span>


    2、编写jni接口



    通过对hal中函数的调用,编写jni接口(这里只是简单地进行了一层包装):



    • 注意,linux driver -- hal -- jni, jni与linux driver并无直接关系,即jni的函数接口与linux driver不一定完全一一对应,很简单的一个例子便是在linux driver中可能只有一个ioctl函数,可是在hal层却通过对ioctl的调用实现了get,set,exchange等多个功能.
      <span style="color: rgb(0, 136, 0);">/*通过硬件抽象层定义的硬件访问接口设置硬件寄存器val的值*/</span><span style="color: rgb(0, 0, 0);">  
      </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> hello_setVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jobject clazz</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jint value</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
          </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> value</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
          LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: set value %d to device."</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
          </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
              LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: device is not open."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
              </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
          </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
                
          hello_device</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">set_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
      </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
      
      </span><span style="color: rgb(0, 136, 0);">/*通过硬件抽象层定义的硬件访问接口读取硬件寄存器val的值*/</span><span style="color: rgb(0, 0, 0);">  
      </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> jint hello_getVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jobject clazz</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
          </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
          </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(!</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
              LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: device is not open."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
              </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
          </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
          hello_device</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">get_val</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                
          LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: get value %d from device."</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            
          </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
      </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  </span>



    3、定义jni加载函数,注册jni方法表


    <span style="color: rgb(0, 136, 0);">/*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>inline</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> hello_device_open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hello_device_t</span><span style="color: rgb(102, 102, 0);">**</span><span style="color: rgb(0, 0, 0);"> device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">methods</span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">open</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> HELLO_HARDWARE_MODULE_ID</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_device_t</span><span style="color: rgb(102, 102, 0);">**)</span><span style="color: rgb(0, 0, 0);">device</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    
    </span><span style="color: rgb(0, 136, 0);">/*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> jboolean hello_init</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> jclass clazz</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 0, 102);">hello_module_t</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
              
        LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: initializing......"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hw_get_module</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">HELLO_HARDWARE_MODULE_ID</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>struct</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">hw_module_t</span><span style="color: rgb(102, 102, 0);">**)&</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: hello Stub found."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>if</strong></span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">hello_device_open</span><span style="color: rgb(102, 102, 0);">(&(</span><span style="color: rgb(170, 34, 255); "><strong>module</strong></span><span style="color: rgb(102, 102, 0);">-></span><span style="color: rgb(0, 0, 0);">common</span><span style="color: rgb(102, 102, 0);">),</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">&</span><span style="color: rgb(0, 0, 0);">hello_device</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
                LOGI</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: hello device is open."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
                </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 102, 102);">0</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
            LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: failed to open hello device."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
        LOGE</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"Hello JNI: failed to get hello stub module."</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">-</span><span style="color: rgb(0, 102, 102);">1</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">        
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  
    
    </span><span style="color: rgb(0, 136, 0);">/*JNI方法表*/</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>const</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">JNINativeMethod</span><span style="color: rgb(0, 0, 0);"> method_table</span><span style="color: rgb(102, 102, 0);">[]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(187, 68, 68);">"init_native"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"()Z"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">hello_init</span><span style="color: rgb(102, 102, 0);">},</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(187, 68, 68);">"setVal_native"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"(I)V"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">hello_setVal</span><span style="color: rgb(102, 102, 0);">},</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(187, 68, 68);">"getVal_native"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"()I"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*)</span><span style="color: rgb(0, 0, 0);">hello_getVal</span><span style="color: rgb(102, 102, 0);">},</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(102, 102, 0);">};</span><span style="color: rgb(0, 0, 0);">  
    
    </span><span style="color: rgb(0, 136, 0);">/*注册JNI方法*/</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> register_android_server_HelloService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">env</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
            </span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> jniRegisterNativeMethods</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">env</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"com/android/server/HelloService"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> method_table</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> NELEM</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">method_table</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  </span>


    其中,上面示例中的jni加载函数hello_init是通过hal中定义的hello_device_open函数实现的。在hello_init函数中,通过Android硬件抽象层提供的hw_get_module方法来加载模块ID为HELLO_HARDWARE_MODULE_ID的硬件抽象层模块,其中,HELLO_HARDWARE_MODULE_ID是在<hardware/hello.h>中定义的。Android硬件抽象层会根据HELLO_HARDWARE_MODULE_ID的值在Android系统的/system/lib/hw目录中找到相应的模块,然后加载起来,并且返回hw_module_t接口给调用者使用。在jniRegisterNativeMethods函数中,第二个参数的值必须对应HelloService所在的包的路径,即com.android.server.HelloService。







    二、修改onload.cpp,使系统启动时自动加载JNI方法调用表



    修改frameworks/base/services/jni/onload.cpp:



    1、在namespace android增加register_android_server_HelloService函数声明:


    <span style="color: rgb(170, 34, 255); "><strong>namespace</strong></span><span style="color: rgb(0, 0, 0);"> android </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//......</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> register_android_server_HelloService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JNIEnv</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);">env</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span>


    2、在JNI_onLoad增加register_android_server_HelloService函数调用:


    <span style="color: rgb(170, 34, 255); "><strong>extern</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"C"</span><span style="color: rgb(0, 0, 0);"> jint JNI_onLoad</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">JavaVM</span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> vm</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(102, 102, 0);">*</span><span style="color: rgb(0, 0, 0);"> reserved</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//......</span><span style="color: rgb(0, 0, 0);">
    register_android_server_HelloService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">env</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//......</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">}</span>


    这样,在Android系统初始化时,就会自动加载该JNI方法调用表。







    三、修改Android.mk文件,添加编译路径



    打开frameworks/base/services/jni/Android.mk,在LOCAL_SRC_FILES变量中增加一行:


    <span style="color: rgb(0, 0, 0);">LOCAL_SRC_FILES</span><span style="color: rgb(102, 102, 0);">:=</span><span style="color: rgb(0, 0, 0);">     com_android_server_AlarmManagerService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_BatteryService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_InputManager</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_LightsService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_PowerManagerService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_SystemServer</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_UsbService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_VibratorService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_location_GpsLocationProvider</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp     com_android_server_HelloService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp </span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">
        onload</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">cpp</span>






    四、编译和重新生成system.img


    $:mmm frameworks/base/services/jni
    $:make snod
    


    这样,重新打包的system.img镜像文件就包含我们刚才编写的JNI方法了







    [编写Framework接口]



    在Android系统中,硬件服务一般是运行在一个独立的进程中为各种应用程序提供服务。因此,调用这些硬件服务的应用程序与这些硬件服务之间的通信需要通过代理来进行。



    一、定义通信接口



    1、新增接口文件



    进入到frameworks/base/core/java/android/os目录,新增IHelloService.aidl接口定义文件:


    <span style="color: rgb(170, 34, 255); "><strong>package</strong></span><span style="color: rgb(0, 0, 0);"> android</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">os</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">  
           
    </span><span style="color: rgb(170, 34, 255); "><strong>interface</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> setVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">  
        </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> getVal</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">  
    </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">  </span>


    IHelloService接口主要提供了设备和获取硬件寄存器val的值的功能,分别通过setVal和getVal两个函数来实现。



    2、添加编译路径



    返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加IHelloService.aidl源文件:


    <span style="color: rgb(0, 0, 0);">LOCAL_SRC_FILES </span><span style="color: rgb(102, 102, 0);">+=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">/
       /</span><span style="color: rgb(102, 102, 0);">/......</span><span style="color: rgb(0, 0, 0);">
       core</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">java</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">android</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">os</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(102, 0, 102);">IVibratorService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">aidl </span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">
       core</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">java</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">android</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">os</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">aidl </span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">
       core</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">java</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">android</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">service</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">urlrenderer</span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(102, 0, 102);">IUrlRendererService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">aidl </span><span style="color: rgb(102, 102, 0);">/</span><span style="color: rgb(0, 0, 0);">
       </span><span style="color: rgb(0, 136, 0);">//.....</span>


    3、编译接口文件


    $:mmm frameworks/base
    


    这样,就会根据IHelloService.aidl生成相应的IHelloService.Stub接口。







    二、建立java文件,编写Framework接口



    进入到frameworks/base/services/java/com/android/server目录,新增HelloService.java文件:


    <span style="color: rgb(170, 34, 255); "><strong>package</strong></span><span style="color: rgb(0, 0, 0);"> com</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">android</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">server</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>import</strong></span><span style="color: rgb(0, 0, 0);"> android</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">content</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(102, 0, 102);">Context</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>import</strong></span><span style="color: rgb(0, 0, 0);"> android</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">os</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>import</strong></span><span style="color: rgb(0, 0, 0);"> android</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">util</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(102, 0, 102);">Slog</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>public</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>class</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">HelloService</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>extends</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(102, 0, 102);">Stub</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>private</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>final</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">String</span><span style="color: rgb(0, 0, 0);"> TAG </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"HelloService"</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 0, 102);">HelloService</span><span style="color: rgb(102, 102, 0);">()</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		init_native</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>public</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> setVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		setVal_native</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">	
    	</span><span style="color: rgb(170, 34, 255); "><strong>public</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> getVal</span><span style="color: rgb(102, 102, 0);">()</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    		</span><span style="color: rgb(170, 34, 255); "><strong>return</strong></span><span style="color: rgb(0, 0, 0);"> getVal_native</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    	
    	</span><span style="color: rgb(170, 34, 255); "><strong>private</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>native</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>boolean</strong></span><span style="color: rgb(0, 0, 0);"> init_native</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
        	</span><span style="color: rgb(170, 34, 255); "><strong>private</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>native</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> setVal_native</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    	</span><span style="color: rgb(170, 34, 255); "><strong>private</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>static</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>native</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> getVal_native</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(102, 102, 0);">};</span>






    三、在ServerThread::run函数中增加加载代码



    修改同目录的SystemServer.java文件:


    <span style="color: rgb(0, 102, 102);">@Override</span><span style="color: rgb(0, 0, 0);">
         </span><span style="color: rgb(170, 34, 255); "><strong>public</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> run</span><span style="color: rgb(102, 102, 0);">()</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
         </span><span style="color: rgb(0, 136, 0);">//.....</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>try</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
                      </span><span style="color: rgb(102, 0, 102);">Slog</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">TAG</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"DiskStats Service"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                      </span><span style="color: rgb(102, 0, 102);">ServiceManager</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">addService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"diskstats"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>new</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">DiskStatsService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">context</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>catch</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">Throwable</span><span style="color: rgb(0, 0, 0);"> e</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
                      </span><span style="color: rgb(102, 0, 102);">Slog</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">e</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">TAG</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"Failure starting DiskStats Service"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> e</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    
                </span><span style="color: rgb(0, 136, 0);">//start:增加加载代码</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(170, 34, 255); "><strong>try</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
                      </span><span style="color: rgb(102, 0, 102);">Slog</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">TAG</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"Hello Service"</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                      </span><span style="color: rgb(102, 0, 102);">ServiceManager</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">addService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"hello"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>new</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">HelloService</span><span style="color: rgb(102, 102, 0);">());</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>catch</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">Throwable</span><span style="color: rgb(0, 0, 0);"> e</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
                      </span><span style="color: rgb(102, 0, 102);">Slog</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">e</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">TAG</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(187, 68, 68);">"Failure starting Hello Service"</span><span style="color: rgb(102, 102, 0);">,</span><span style="color: rgb(0, 0, 0);"> e</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
                </span><span style="color: rgb(0, 136, 0);">//end</span><span style="color: rgb(0, 0, 0);">
         </span><span style="color: rgb(0, 136, 0);">//......</span><span style="color: rgb(0, 0, 0);">
         </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">      </span>






    四、编译、重新打包system.img


    $:mmm frameworks/base/services/java
    $:make snod
    


    这样,重新打包后的system.img系统镜像文件就在Application Frameworks层中包含了我们自定义的硬件服务了,并且会在系统启动的时候会自动加载HelloService,这样应用程序就可以通过Java接口来访问Hello硬件服务了。







    [App访问]


    <span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>import</strong></span><span style="color: rgb(0, 0, 0);"> android</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">os</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>private</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(0, 0, 0);"> helloService </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>null</strong></span><span style="color: rgb(102, 102, 0);">;</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(0, 102, 102);">@Override</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(170, 34, 255); "><strong>public</strong></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(170, 34, 255); "><strong>void</strong></span><span style="color: rgb(0, 0, 0);"> onCreate</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(102, 0, 102);">Bundle</span><span style="color: rgb(0, 0, 0);"> savedInstanceState</span><span style="color: rgb(102, 102, 0);">)</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 102, 0);">{</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
    	helloService </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(102, 0, 102);">IHelloService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(102, 0, 102);">Stub</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">asInterface</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">
    		</span><span style="color: rgb(102, 0, 102);">ServiceManager</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">getService</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(187, 68, 68);">"hello"</span><span style="color: rgb(102, 102, 0);">));</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
        </span><span style="color: rgb(102, 102, 0);">}</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(170, 34, 255); "><strong>int</strong></span><span style="color: rgb(0, 0, 0);"> val </span><span style="color: rgb(102, 102, 0);">=</span><span style="color: rgb(0, 0, 0);"> helloService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">getVal</span><span style="color: rgb(102, 102, 0);">();</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span><span style="color: rgb(0, 0, 0);">
    helloService</span><span style="color: rgb(102, 102, 0);">.</span><span style="color: rgb(0, 0, 0);">setVal</span><span style="color: rgb(102, 102, 0);">(</span><span style="color: rgb(0, 0, 0);">val</span><span style="color: rgb(102, 102, 0);">);</span><span style="color: rgb(0, 0, 0);">
    </span><span style="color: rgb(0, 136, 0);">//...</span>






    参考资料:



    在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序



    在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口







    http://www.hovercool.com/en/%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97

  • 相关阅读:
    电子商务:不只是一个网站那么简单
    sqlserver中使用查询分析器的一点小技巧
    以成败论英雄
    SEO 工具集合
    网页中常用JAVASCRIPT技巧
    调用样式表(基础)
    【转载】fontsize:100%有什么作用
    Div+CSS常见错误
    css简单的总结
    【转载】网页尺寸规范
  • 原文地址:https://www.cnblogs.com/liang123/p/6325428.html
Copyright © 2011-2022 走看看