Android11(RK3568)自定义服务制作(1)-Kernel驱动

Android11(RK3568)自定义服务制作(1)-Kernel驱动,第1张

Android11(RK3568)自定义服务制作(1)-Kernel驱动

最近在移植自己公司的产品进入新的Android系统中,需要用到自定义服务来调用自己的驱动。其实从Android4.0以来就没有再做过驱动开发,现在发现整个android11后,无论是驱动层还是JNI调用,HAL层到上层APP都有了很大的改动,最近也差了很多资料,发现各有各的问题。现在就我整理了一份从驱动到JNI到自定义service,其实还涉及到selinux的改写跟最后APP的调用,做个完整记录,为需要android11自定义服务的用户提供一份方便。本专栏分4个部分来写,从低到高。让你使用RK3568平台来实现自定义Service。

1.驱动篇

Android系统的内核从8.0开始就进入5时代。大量的平台都采用了最新的kernel5.0以上的版本。kernel5.0开始跟4.0的差距比较大,很多原来的写法已经不再适用了。

首先在kernel中的/arch/arm64/configs/firefly_defconfig中添加

# hello mod by sommerjiang
CONFIG_HELLO=y

再在/drivers/misc/下面的Kconfig添加一下的内容:

source "drivers/misc/hello/Kconfig"

Makefile中添加

obj-$(CONFIG_HELLO) += hello/

再在/drivers/misc/添加hello文件夹,并添加hello.c hello.h Kconfig Makefile四个文件,内容分别为

hello.c

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include 
  
#include "hello.h"  

//#define use_proc_hello
  
  
static int hello_major = 0;  
static int hello_minor = 0;  
  
  
static struct class* hello_class = NULL;  
static struct hello_android_dev* hello_dev = NULL;  
  
  
static int hello_open(struct inode* inode, struct file* filp);  
static int hello_release(struct inode* inode, struct file* filp);  
static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);  
static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);  
  
  
static struct file_operations hello_fops = {  
    .owner = THIS_MODULE,  
    .open = hello_open,  
    .release = hello_release,  
    .read = hello_read,  
    .write = hello_write,   
};  
  ;
  
static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr,  char* buf);  
static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);  
  
  //sys/class/hello
static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); 

      
    static int hello_open(struct inode* inode, struct file* filp) {  
        struct hello_android_dev* dev;          
          
          
        dev = container_of(inode->i_cdev, struct hello_android_dev, dev);  
        filp->private_data = dev;  
          
        return 0;  
    }  
      
      
    static int hello_release(struct inode* inode, struct file* filp) {  
        return 0;  
    }  
      
      
    static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {  
        ssize_t err = 0;  
        struct hello_android_dev* dev = filp->private_data;          
      
          
        
      
        if(count < sizeof(dev->val)) {  
            goto out;  
        }          
      
          
        if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) {  
            err = -EFAULT;  
            goto out;  
        }  
      
        err = sizeof(dev->val);  
      
    out:  
        //up(&(dev->sem));  
        return err;  
    }  
      
      
    static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {  
        struct hello_android_dev* dev = filp->private_data;  
        ssize_t err = 0;          
      
          
                
      
        if(count != sizeof(dev->val)) {  
            goto out;          
        }          
      
          
        if(copy_from_user(&(dev->val), buf, count)) {  
            err = -EFAULT;  
            goto out;  
        }  
      
        err = sizeof(dev->val);  
      
    out:  
        //ly up(&(dev->sem));  
        return err;  
    }  

  
static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) {  
    int val = 0;          
  
      
           
  
    val = dev->val;          
    //lyup(&(dev->sem));          
  
    return snprintf(buf, PAGE_SIZE, "%dn", val);  
}  
  
  
static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count) {  
    int val = 0;          
  
              
    val = simple_strtol(buf, NULL, 10);          
  
              
           
  
    dev->val = val;          
    // ly up(&(dev->sem));  
  
    return count;  
}  
  
  
static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {  
    struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);          
  
    return __hello_get_val(hdev, buf);  
}  
  
  
static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {   
    struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);    
      
    return __hello_set_val(hdev, buf, count);  
}

//old method for proc
#if 0
      
    static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {  
        if(off > 0) {  
            *eof = 1;  
            return 0;  
        }  
      
        return __hello_get_val(hello_dev, page);  
    }  
      
      
    static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) {  
        int err = 0;  
        char* page = NULL;  
      
        if(len > PAGE_SIZE) {  
            printk(KERN_alert"The buff is too large: %lu.n", len);  
            return -EFAULT;  
        }  
      
        page = (char*)__get_free_page(GFP_KERNEL);  
        if(!page) {                  
            printk(KERN_alert"Failed to alloc page.n");  
            return -ENOMEM;  
        }          
      
          
        if(copy_from_user(page, buff, len)) {  
            printk(KERN_alert"Failed to copy buff from user.n");                  
            err = -EFAULT;  
            goto out;  
        }  
      
        err = __hello_set_val(hello_dev, page, len);  
      
    out:  
        free_page((unsigned long)page);  
        return err;  
    }  
#endif      
      
#ifdef use_proc_hello
    static void hello_create_proc(void) {  
        //struct proc_dir_entry* entry;  
          
        //entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);  
	//proc_create(HELLO_DEVICE_PROC_NAME, 0, NULL, &hello_fops);
        if(!proc_create(HELLO_DEVICE_PROC_NAME, 0666, NULL, &hello_fops)) {  
 		printk("error ");
        }  
    }  
  #endif    
      
    static void hello_remove_proc(void) {  
        remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);  
    }  

      
    static int  __hello_setup_dev(struct hello_android_dev* dev) {  
        int err;  
        dev_t devno = MKDEV(hello_major, hello_minor);  //jiang zhu 
      
        memset(dev, 0, sizeof(struct hello_android_dev));  
      
        cdev_init(&(dev->dev), &hello_fops);  
        dev->dev.owner = THIS_MODULE;  
        dev->dev.ops = &hello_fops;          
      
          
        err = cdev_add(&(dev->dev),devno, 1);  
        if(err) {  
            return err;  
        }          
      
          
        //init_MUTEX(&(dev->sem));  
	// ly sema_init(&(dev->sem),1);
        dev->val = 0;  
      
        return 0;  
    }  
      
      
    static int __init hello_init(void){   
        int err = -1;  
        dev_t dev = 0;  
        struct device* temp = NULL;  
      
        printk(KERN_alert"Initializing hello device.n");          
      
          
        err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);  
        if(err < 0) {  
            printk(KERN_alert"Failed to alloc char dev region.n");  
            goto fail;  
        }  
      
        hello_major = MAJOR(dev);  
        hello_minor = MINOR(dev);          
      
          
        hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);  
        if(!hello_dev) {  
            err = -ENOMEM;  
            printk(KERN_alert"Failed to alloc hello_dev.n");  
            goto unregister;  
        }          
      
          
        err = __hello_setup_dev(hello_dev);  
        if(err) {  
            printk(KERN_alert"Failed to setup dev: %d.n", err);  
            goto cleanup;  
        }          
      
          
        hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);  
        if(IS_ERR(hello_class)) {  
            err = PTR_ERR(hello_class);  
            printk(KERN_alert"Failed to create hello class.n");  
            goto destroy_cdev;  
        }          
      
          
        temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);  
        if(IS_ERR(temp)) {  
            err = PTR_ERR(temp);  
            printk(KERN_alert"Failed to create hello device.");  
            goto destroy_class;  
        }          
      
          
        err = device_create_file(temp, &dev_attr_val);  
        if(err < 0) {  
            printk(KERN_alert"Failed to create attribute val.");                  
            goto destroy_device;  
        }  
      
        dev_set_drvdata(temp, hello_dev);          
      
#ifdef use_proc_hello
          
        hello_create_proc();  
#endif      
        printk(KERN_alert"Succedded to initialize hello device.n");  
        return 0;  
      
    destroy_device:  
        device_destroy(hello_class, dev);  
      
    destroy_class:  
        class_destroy(hello_class);  
      
    destroy_cdev:  
        cdev_del(&(hello_dev->dev));  
      
    cleanup:  
        kfree(hello_dev);  
      
    unregister:  
        unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);  
      
    fail:  
        return err;  
    }  
      
      
    static void __exit hello_exit(void) {  
        dev_t devno = MKDEV(hello_major, hello_minor);  
      
        printk(KERN_alert"Destroy hello device.n");          
      
          
        hello_remove_proc();          
      
          
        if(hello_class) {  
            device_destroy(hello_class, MKDEV(hello_major, hello_minor));  
            class_destroy(hello_class);  
        }          
      
          
        if(hello_dev) {  
            cdev_del(&(hello_dev->dev));  
            kfree(hello_dev);  
        }          
      
          
        unregister_chrdev_region(devno, 1);  
    }  
      
    MODULE_LICENSE("GPL");  
    MODULE_DEscriptION("First Android Driver");  
      
    module_init(hello_init);  
    module_exit(hello_exit);  

hello.h

#ifndef _HELLO_ANDROID_H_  
#define _HELLO_ANDROID_H_  
  
#include   
#include   
  
#define HELLO_DEVICE_NODE_NAME  "hello"  
#define HELLO_DEVICE_FILE_NAME  "hello"  
#define HELLO_DEVICE_PROC_NAME  "hello"  
#define HELLO_DEVICE_CLASS_NAME "hello"  
  
struct hello_android_dev {  
    int val;  
    struct semaphore sem;  
    struct cdev dev;  
};  
  
#endif 

Kconfig

config HELLO
        tristate "First Android Driver"
        default n
        help
        This is the first android driver.

Makefile

obj-$(CONFIG_HELLO) += hello.o

这样编译完后就可以烧录到内核里面。完成驱动的编写。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5624453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存