Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

漫談 Android 系統(6):硬件抽象層與 kernel 接口分析

編輯:關於 Android 程式設計

一、前言

近期在讀羅升陽的《Android 系統源代碼情景分析》。做 Android 系統的同學大概都繞不開這本書,而第二章講的硬件抽象層,正好能與實際工作相互印證。本文就沿着書中的思路,把硬件抽象層與 kernel 的關係梳理一遍,並用自己寫過的代碼做佐證。

二、kernel 暴露在用戶空間的接口

kernel 層通常需要向用戶空間提供一批接口,大體可分為三類:

  1. proc 文件系統接口
  2. 傳統設備文件系統接口
  3. devfs(sysfs)文件系統接口

其中前兩者一脈相承,配合起來才構成一套比較完整的文件系統接口;第三類則是設備模型視角下的展現方式。

2.1 proc 文件系統接口 + 傳統 file_operations

這類接口本質上是「傳統設備文件操作方法 + 傳統設備文件操作方法表 fops」的組合。老羅書中用了 freg.c 來說明,實際落地時還是需要結合自己的驅動去體會。

以我自己寫的 charger 相關代碼為例。在 probe 函數中直接調用一個創建接口的函數:

static int smbchg_probe(struct spmi_device *spmi)
{
    ...
    create_batt_voltage_proc_file();
    ...
}

來看它的定義:

void static create_batt_voltage_proc_file(void)
{
    batt_voltage_proc_file = proc_create(batt_voltage_PROC_FILE, 0644,
                                         NULL, &batt_voltage_fops);
    if (batt_voltage_proc_file) {
        printk("[Proc]%s sucessed!\n", __FUNCTION__);
    } else {
        printk("[Proc]%s failed!\n", __FUNCTION__);
    }
}

核心動作很單純:把 fops 掛上去。順藤摸瓜看 fops 的定義:

#define batt_voltage_PROC_FILE "batt_voltage_now"

static const struct file_operations batt_voltage_fops = {
    .owner   = THIS_MODULE,
    .open    = batt_voltage_proc_open,
    .read    = seq_read,
    .release = single_release,
};

fops 就出現在這裡。正是 proc_create(batt_voltage_PROC_FILE, 0644, NULL, &batt_voltage_fops) 這一行,把它掛到了 /proc/batt_voltage_now 這個節點上。

接着看 open:

static int batt_voltage_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, batt_voltage_proc_read, NULL);
}

它做的事情是註冊回調 batt_voltage_proc_read,真正的讀取邏輯在那裡:

static int batt_voltage_proc_read(struct seq_file *buf, void *v)
{
    int ret = -1;
    struct power_supply *psy;
    union power_supply_propval val;
    int voltage_now = 0;

    psy = get_psy_battery();
    ret = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
    if (!ret) {
        voltage_now = val.intval / 1000;
        seq_printf(buf, "%d\n", voltage_now);
    } else {
        printk("%s: can't get voltage_now, ret = %d!\n", __FUNCTION__, ret);
        voltage_now = ret;
    }
    return 0;
}

用戶空間 cat 一下這個節點,拿到的就是換算成毫伏之後的當前電壓值。

小結:這套機制的思路始終只有一件事——把 fops 掛到一個 proc 節點上,讓用戶空間能用最樸素的文件讀寫方式訪問內核數據。

2.2 devfs(sysfs)文件系統接口

再來看另一種風格,以 hall_sensor 為例:

static DEVICE_ATTR(status, 0664, show_action_status, store_action_status);

static struct attribute *hall_sensor_attrs[] = {
    &dev_attr_status.attr,
    NULL
};

static struct attribute_group hall_sensor_group = {
    .name  = "hall_sensor",
    .attrs = hall_sensor_attrs,
};

這段代碼把要架設的東西說得很清楚:用 DEVICE_ATTR 聲明一個屬性節點,並綁定讀與寫兩個操作函數 show_action_status 與 store_action_status,最後通過 attribute_group 掛到設備上。

/* sysfs DEVICE_ATTR part */
static ssize_t show_action_status(struct device *dev,
                                  struct device_attribute *attr,
                                  char *buf)
{
    if (!hall_sensor_dev)
        return sprintf(buf, "Hall sensor does not exist!\n");
    return sprintf(buf, "%d\n", hall_sensor_dev->status);
}

static ssize_t store_action_status(struct device *dev,
                                   struct device_attribute *attr,
                                   const char *buf,
                                   size_t count)
{
    int request;
    unsigned long flags;

    sscanf(buf, "%du", &request);

    spin_lock_irqsave(&hall_sensor_dev->mHallSensorLock, flags);
    if (!request)
        hall_sensor_dev->status = 0;
    else
        hall_sensor_dev->status = 1;
    spin_unlock_irqrestore(&hall_sensor_dev->mHallSensorLock, flags);

    log("[ATTR] status rewite value = %d\n", !hall_sensor_dev->status);
    return count;
}

具體業務細節不必糾結,關鍵在於它和 proc 那套路子異曲同工:都是在做「掛載」——把內核中的某個變量或操作,以文件節點的形式暴露出來。區別只在於挂載的載體不同:一個走 proc_create + fops,一個走 device attribute + attribute_group。

三、兩種方式的選型直觀對比

  • proc 接口:靈活度最高,read 函數完全由自己控制輸出格式,適合輸出計算結果、狀態快照這類「只讀且需要加工」的數據。
  • sysfs / devfs 接口:與設備模型深度綁定,節點自動出現在設備目錄下,屬性語義清晰,更適合表達設備的某個可讀寫屬性,也便於被 framework 層的 HAL 模塊統一掃描。

四、後續

本文只把 kernel 這一層的節點暴露方式做了初步介紹。hardware 抽象層如何封裝這些節點、framework 又如何通過 HAL 模塊調用下去,這條完整鏈路值得再單獨寫一篇——敬請期待。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved