RK android13光感适配HAL层调用

简介: RK android13光感适配HAL层调用

首先看\hardware\rockchip\sensor\st LightSensor.cpp

它里面有三个ioctl的调用

    int32_t ret = ioctl(dev_fd, LIGHTSENSOR_IOCTL_GET_ENABLED, &flags);
    ret = ioctl(dev_fd, LIGHTSENSOR_IOCTL_SET_RATE, &ms);
    err = ioctl(dev_fd, LIGHTSENSOR_IOCTL_ENABLE, &flags);

ioctl是与驱动层交互,这就需要在驱动层实现miscdevice,然后支持这三个ioctrl

驱动层实现

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/input.h>

int32_t ls_state_ret = -1;

#define LIGHTSENSOR_IOCTL_MAGIC 'l'
#define LIGHTSENSOR_IOCTL_GET_ENABLED _IOR(LIGHTSENSOR_IOCTL_MAGIC, 1, int *)
#define LIGHTSENSOR_IOCTL_ENABLE _IOW(LIGHTSENSOR_IOCTL_MAGIC, 2, int *)
#define LIGHTSENSOR_IOCTL_SET_RATE _IOW(LIGHTSENSOR_IOCTL_MAGIC, 3, short)


static int lightsensor_open(struct inode *inode, struct file *file)
{
    // do nothing for now
    return 0;
}

static long lightsensor_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{    
    
    switch (cmd) {
        
        case LIGHTSENSOR_IOCTL_GET_ENABLED:         
            pr_err("LIGHTSENSOR_IOCTL_GET_ENABLED ret = %d\n", ls_state_ret);
            copy_to_user((void __user *)arg, &ls_state_ret, sizeof(ls_state_ret));
            break;
        case LIGHTSENSOR_IOCTL_ENABLE:
            // enable/disable the sensor
            {
                int value = 0;
                void __user *argp = (void __user *)arg;
                copy_from_user(&value, (void __user*)argp, sizeof(value));
                    pr_err("lightsensor_ioctl value = %d\n", value);
                mutex_lock(&ps_data->io_lock);
                    ls_state_ret = stk3x1x_enable_als(ps_data, value);
                mutex_unlock(&ps_data->io_lock);
                    pr_err("lightsensor_ioctl ret = %d\n", ls_state_ret);
                copy_to_user((void __user *)arg, &ls_state_ret, sizeof(ls_state_ret));
            }
            break;
        case LIGHTSENSOR_IOCTL_SET_RATE:
            {
                short ms = 0;
                uint64_t value = 0;
                void __user *argp = (void __user *)arg;
                copy_from_user(&ms, (void __user*)argp, sizeof(ms));
                #ifdef STK_DEBUG_PRINTF
                    printk(KERN_ERR "%s: set als poll delay=%lld\n", __func__, ms);
                #endif
                    value = ms * 1000 * 1000;
                    if (value < MIN_ALS_POLL_DELAY_NS) {
                        printk(KERN_ERR "%s: delay is too small\n", __func__);
                        value = MIN_ALS_POLL_DELAY_NS;
                    }
                    mutex_lock(&ps_data->io_lock);
                    if (value != ktime_to_ns(ps_data->als_poll_delay))
                        ps_data->als_poll_delay = ns_to_ktime(value);
                #ifdef STK_ALS_FIR
                    ps_data->fir.number = 0;
                    ps_data->fir.idx = 0;
                    ps_data->fir.sum = 0;
                #endif
                    mutex_unlock(&ps_data->io_lock);
            }
            break;
        default:
            return -EINVAL;
    }
    return 0;
}

static const struct file_operations lightsensor_fops = {
    .owner          = THIS_MODULE,
    .unlocked_ioctl = lightsensor_ioctl,
    .compat_ioctl   = lightsensor_ioctl,
    .open           = lightsensor_open,
};

static struct miscdevice lightsensor_dev = {
    .minor = MISC_DYNAMIC_MINOR,
    .name  = "lightsensor",
    .fops  = &lightsensor_fops,
};

probe中注册
err = misc_register(&lightsensor_dev);

    if (err) {
        pr_err("misc_register failed: %d\n", err);
    }

顺便做了一下HAL层光感代码调试

光感设置inputevent是 set_bit(EV_ABS, HAL层需要把它从/dev/input/eventx中解析出来,里面设置一系列文件的编历与查找,
只看代码的话,不容易看懂,把代码截取出来,放IDE中去调试,会方便很多。
下面的是代码展示的是从 /dev/input/eventx 找到光感数据的文件 “lightsensor-level”。顺便学习一下readdir方法的使用。

#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/select.h>

#include <linux/input.h>
#include <iostream>
#include <string.h>
#include <memory>


/*
 * 1: is target
 * 0: is not target
 * other: error
 */
static int is_target_dev(const char* fname, const char *inputName)
{
    int ret = 0;
    int fd = -1;
    char devname[PATH_MAX];
    char name[80];

    sprintf(devname, "/sys/class/input/%s/device/name", fname);
    fd = open(devname, O_RDONLY);

    if (fd >= 0) {
        if (read(fd, name, 79) < 1)
            name[0] = '\0';

        if (!strncmp(name, inputName, strlen(inputName)))
            ret = 1;

        close(fd);
    }

    return ret;
}

char input_name[PATH_MAX];
int openInput(const char *inputName)
{
    int fd = -1;
    const char *dirname = "/dev/input";
    char devname[PATH_MAX];
    char *filename;
    DIR *dir;
    struct dirent *de;
    dir = opendir(dirname);
    if(dir == NULL)
        return -1;
    strcpy(devname, dirname);
    filename = devname + strlen(devname); // filename 是devname的首地址往后移动devname长度个字节
    *filename++ = '/';  // 这里相当于用filename直接来操作 devname
    //*filename++ = '\0';
    std::cout << "devname  ---> " << devname << std::endl;
    printf("filename = %s\n ", filename);
    while((de = readdir(dir))) {
        if(de->d_name[0] == '.' &&
           (de->d_name[1] == '\0' ||
            (de->d_name[1] == '.' && de->d_name[2] == '\0')))
            continue;

        printf("de->d_name = %s\n", de->d_name);
        //if (!is_target_dev(de->d_name, inputName))
        //    continue;
        strcpy(filename, de->d_name); //filename 是指向 devname的指针,改变filename,devname也会更着一起改变

        printf("devname = %s\n", devname);
        fd = open(devname, O_RDONLY);

        if (fd >= 0) {
            strcpy(input_name, filename);
            printf("input_name = %s\n", input_name);
            break;
        }
    }
    closedir(dir);


    return fd;
}

int main(int argc, char *argv[]){

    int data_fd = openInput("lightsensor-level");

    return -1;
}
相关文章
|
6月前
|
安全 Android开发
Android HAL 层
Android HAL 层
52 1
|
6月前
|
传感器 Java Android开发
Android HAL深入探索(1): 架构概述
Android HAL深入探索(1): 架构概述
585 1
|
6月前
|
Android开发 C++
Android P HAL层添加HIDL实例
Android P HAL层添加HIDL实例
114 0
|
6月前
|
编解码 调度 Android开发
Android音频框架之一 详解audioPolicy流程及HAL驱动加载与配置
Android音频框架之一 详解audioPolicy流程及HAL驱动加载与配置
611 0
|
6月前
|
Android开发
Android RIL 动态切换 4G 模块适配
Android RIL 动态切换 4G 模块适配
173 0
|
6月前
|
物联网 Android开发
Android Ble蓝牙App(四)UI优化和描述符
Android Ble蓝牙App(四)UI优化和描述符
147 0
|
6月前
|
传感器 监控 物联网
Android Ble蓝牙App(三)特性和属性
Android Ble蓝牙App(三)特性和属性
100 0
|
Java Android开发
RK3326 android10.0(Q) 音频通道 tinyalsa_hal层 bug 记录
RK3326 android10.0(Q) 音频通道 tinyalsa_hal层 bug 记录
472 0
|
监控 Android开发
[RK3568 Android11]Framework层获取和处理按键事件流程总结
[RK3568 Android11]Framework层获取和处理按键事件流程总结
138 0
[RK3568 Android11]Framework层获取和处理按键事件流程总结
|
Linux Android开发
[RK3568 Android11] Binder通信整体框架
[RK3568 Android11] Binder通信整体框架
105 0
 [RK3568 Android11] Binder通信整体框架