Android 驱动入门

Android Driver

Posted by LXG on September 24, 2021

液晶显示器

LCD液晶屏驱动详解

Linux设备分类

  • 字符设备-c
  • 块设备-b
  • 网络设备

ls -al dev/
crwxrwxrwx root     root     251,   3 2021-01-01 08:07 ttyS3


ls -al dev/block/
drwxr-xr-x root     root              2021-01-01 08:07 by-name
brw------- root     root     179,   0 2021-01-01 08:07 mmcblk0
brw------- system   system   179,   8 2021-01-01 08:07 mmcblk0p8
brw------- root     root     179,   9 2021-01-01 08:07 mmcblk0p9
drwxr-xr-x root     root              2021-01-01 08:07 platform
drwx------ root     root              2021-01-01 08:07 vold
brw------- root     root     253,   0 2021-01-01 08:07 zram0

Android 专用驱动

Ashmem: 匿名共享内存驱动

./kernel/drivers/staging/android/ashmem.c


ls -al dev/ashmem
crw-rw-rw- root     root      10,  51 2021-01-01 08:07 ashmem

Logger: 轻量级log驱动

drivers/staging/android/logger.c


ls -al dev/log/
crw-rw-rw- root     log       10,  49 2021-01-01 08:07 events
crw-rw-rw- root     log       10,  50 2021-01-01 08:07 main
crw-rw-rw- root     log       10,  48 2021-01-01 08:07 radio
crw-rw-rw- root     log       10,  47 2021-01-01 08:07 system

logger_system

Binder驱动

drivers/staging/android/binder.c


ls -al dev/binder
crw-rw-rw- root     root      10,  52 2021-01-01 08:07 binder

设备驱动

显示驱动

drivers/video/fbmem.c


crw------- root     root     247,   0 2021-01-01 08:07 lcd

ls -al dev/graphics/*
crw-rw---- system   system    29,   0 2021-01-01 08:00 fb0
crw-rw---- system   system    29,   1 2021-01-01 08:00 fb1
crw-rw---- root     graphics  29,   2 2021-01-01 08:00 fb2
crw-rw---- root     graphics  29,   3 2021-01-01 08:00 fb3
crw-rw---- root     graphics  29,   4 2021-01-01 08:00 fb4
crw-rw---- root     graphics  29,   5 2021-01-01 08:00 fb5
crw-rw---- root     graphics  29,   6 2021-01-01 08:00 fb6
crw-rw---- root     graphics  29,   7 2021-01-01 08:00 fb7


输入

drivers/input/input.c


ls -al dev/input/
crw-rw---- root     input     13,  64 2021-01-01 08:00 event0
crw-rw---- root     input     13,  65 2021-01-01 08:00 event1
crw-rw---- root     input     13,  66 2021-01-01 08:00 event2
crw-rw---- root     input     13,  67 2021-01-01 08:00 event3
crw-rw---- root     input     13,  68 2021-01-01 08:00 event4
crw-rw---- root     input     13,  63 2021-01-01 08:00 mice
crw-rw---- root     input     13,  32 2021-01-01 08:00 mouse0

摄像头驱动

drivers/media/video/v4l2-dev.c


ls -al dev/video0
crw-rw-rw- media    media     81,   0 2021-01-01 08:00 video0

音频驱动


ls -al dev/snd/*
crw-rw---- system   audio    116,   0 2021-01-01 08:00 controlC0
crw-rw---- media    media    116,  24 2021-01-01 08:00 pcmC0D0c
crw-rw---- media    media    116,  16 2021-01-01 08:00 pcmC0D0p
crw-rw---- system   audio    116,  33 2021-01-01 08:00 timer

Flash驱动


ls dev/block/
by-name
loop0
loop1
loop2
loop3
loop4
loop5
loop6
loop7
mmcblk0
mmcblk0boot0
mmcblk0boot1
mmcblk0p1
mmcblk0p10
mmcblk0p11
mmcblk0p12
mmcblk0p2
mmcblk0p3
mmcblk0p5
mmcblk0p6
mmcblk0p7
mmcblk0p8
mmcblk0p9
platform
vold
zram0

蓝牙驱动

蓝牙设备是网络设备,使用网络接口

./drivers/bluetooth

WLAN 驱动

WLAN设备是网络设备,使用网络接口

drivers/net/

USB 驱动


ls -al dev/usb*
lrwxrwxrwx root     root              2021-01-01 08:00 input1-1.5 -> /dev/input/event2
lrwxrwxrwx root     root              2021-01-01 08:00 tty1-1.1 -> /dev/ttyUSB0
crw-rw---- root     usb       10,  54 2021-01-01 08:00 usb_accessory

驱动模块的加载

lsmod 命令列出了当前系统中加载的模块

Module           Size      Used by
sunxi_schw       12559     0 - Live 0x00000000 (O)
cdc_ether        5099      0 - Live 0x00000000
rtl8152          37073     0 - Live 0x00000000
mcs7830          6292      0 - Live 0x00000000
qf9700           7805      0 - Live 0x00000000
asix             17150     0 - Live 0x00000000
usbnet           17692     4 cdc_ether,mcs7830,qf9700,asix, Live 0x00000000
sunxi_keyboard   3021      0 - Live 0x00000000
gt9xx_ts         28934     0 - Live 0x00000000
gc0328           11137     0 - Live 0x00000000
vfe_v4l2         445432    0 - Live 0x00000000
vfe_subdev       4523      2 gc0328,vfe_v4l2, Live 0x00000000
vfe_os           3951      2 vfe_v4l2,vfe_subdev, Live 0x00000000
cci              21738     1 gc0328, Live 0x00000000
videobuf_dma_contig 5567   1 vfe_v4l2, Live 0x00000000
videobuf_core    16520     2 vfe_v4l2,videobuf_dma_contig, Live 0x00000000
leds_sunxi       1359      0 - Live 0x00000000
mali             212802    15 - Live 0x00000000 (O)
lcd              69452     0 - Live 0x00000000
disp             992848    8 mali,lcd, Live 0x00000000
nand             280194    0 - Live 0x00000000 (O)

液晶显示器

screen_driver

调试LCD显示屏的过程,实际上就是调试发送端和接收端IC的过程

TFT-LCD常用的接口,TTL(RGB)、LVDS、EDP、MIPI

TTL接口的信号类型

LCD常用接口原理篇

RGB

rgb_lcd

LVDS

lvds_lcd

MIPI

mipi_lcd

Hello World

在Ubuntu上为Android系统编写Linux内核驱动程序

hello.h


#ifdef _HELLO_ANDROID_H_

#define _HELLO_ANDROID_H_

#include <linux/cdev.h>
#include <linux/semaphore.h>

#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

hello.c



#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/device.h>
#include <asm/uaccess.h>

#include "hello.h"

// 主设备和从设备号变量
static int hello_major = 0;
static int hello_minor = 0;

// 设备类别
static struct clsss* hello_calss = 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);

// 定义设备属性
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 (down_interruptible(&(dev->sem)))
    {
        return -ERESTARTSYS;
    }
    
    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* def = filp->private_data;
    ssize_t err = 0;

    if (down_interruptible(&(dev->sem)))
    {
        return -ERESTARTSYS;
    }

    if (count != sizeof(dev->val))
    {
        goto out;
    }

    if (copy_from_user(&(dev->val), buf, count))
    {
        err = -EFAULT;
        goto out;
    }
    
    err = sizeof(dev->val);

out:
    up(&(dev->sem));

    return err;
    
    
}

/*---------------------------------------------------------------------------------*/

// 读取寄存器内容到缓冲区中
static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) {
    int val = 0;

    if (down_interruptible(&(dev->sem)))
    {
        return -ERESTARTSYS;
    }

    val = dev->val;

    up(&(dev->sem));

    return snprintf(buf, PAGE_SIZE, "%d\n", 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);

    if (down_interruptible(&(dev->sem)))
    {
        return -ERESTARTSYS;
    }

    dev->val = val;
    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);
}

/*----------------------------------------------------------------------------------------*/


// 读取设备寄存器 val 的值,保存在page缓冲区中
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);
}

// 把缓冲区的值buff保存到设备寄存器val中去
static ssize_t hello_proc_white(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;
}


//创建/proc/hello文件
static void hello_create_proc(void) {
    struct proc_dir_entry* entry;

    entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);
    if (entry)
    {
        entry->owner = THIS_MODULE;
        entry->read_proc = hello_proc_read;
        entry->write_proc = hello_proc_white;
    }
    
}

// 删除/proc/hello文件
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);

    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;
    }

    //初始化信号量和寄存器val的值
    init_MUTEX(&(dev->sem));
    dev->val = 0;

    return 0;
}

// 模块儿加载方法
static init __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 设备结构体变量
    hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KEENEL);
    if (!hello_dev)
    {
        err = -ENOMEM;
        printk(KERN_ALERT"Failed to alloc dev: %d.\n", err);
        goto unregister;
    }

    //初始化设备
    err = __hello_setup_dev(hello_dev);
    if (err)
    {
        printk(KERN_ALERT"Failed to setup dev: %d.\n", err);
        goto cleanup;
    }
    
    //在/sys/class/ 目录下创建设备类别目录hello*/
    hello_calss = 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;
    }
    
    //在 /dev/目录和/sys/class/hello/目录下分别创建设备文件hello
    temp = device_create(hello_calss, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);
    if (IS_ERR(temp))
    {
        err = PTR_ERR(temp);
        printk(KERN_ALERT"Failed to create hello device.\n");
        goto destroy_calss;
    }

    //在/sys/class/hello/hello/目录下创建属性文件val
    err = device_create_file(temp, &dev_attr_val);
    if (err < 0)
    {
        printk(KERN_ALERT"Failed to create attribute val.\n");
        goto destroy_device;
    }
    
    dev_set_drvdata(temp, hello_dev);

    // 创建 proc/hello 文件
    hello_create_proc()

    printk(KERN_ALERT"Succedded to initialize hello device.\n");
    return 0;

destroy_device:
    device_destroy(hello_calss, 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");

    // 删除/proc/hello文件
    hello_remove_proc();

    //销毁设备类别和设备
    if (hello_class)
    {
        device_destroy(hello_calss, 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);