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

Android 下的 LED 控制(下)——Linux 驅動層解析

編輯:關於 Android 程式設計

這一篇接著聊 Linux 側的實現。上一篇已經確認:JNI 通過打開 /dev/led 這個設備節點,再用 ioctl 來控制 LED 的亮滅與蜂鳴器的鳴響。那麼問題就來了——Linux 為什麼會接受 ioctl 的控制?這個函數最終是怎麼把指令落到硬件上的?走到這一步,其實已經和 Android 沒有關係了,純粹是 Linux 驅動的範疇。

最初我以為板上的 LED 驅動是動態加載的模塊(*.ko),系統起來之後再 insmod 進內核。翻系統配置文件時才發現並非如此。看一下 Android 初始化腳本對 LED 的配置,文件路徑是 CQA83TAndroid_v2.1.0_bv3/android/device/softwinner/octopus-f1/init.sun8i.rc:

這裡改的只是設備節點的權限,但恰恰說明了問題:Android 部分開始啟動時,LED 驅動就已經在 Linux 內核裡待命了。如果還不放心,可以對比一下同一份腳本對其他設備的配置:

由此可以確認,LED 驅動是在內核階段完成加載的。至於具體在哪一步加載,先按下不表,下面直接看源碼。

LED 屬於字符設備,源碼位於 Linux 內核的 drivers/char/led.c:

 #include <linux/types.h> #include <linux/delay.h> #include <linux/platform_device.h> #include <linux/init.h> #include <linux/input.h> #include <linux/irq.h> #include <linux/interrupt.h> #include <linux/jiffies.h> #include <linux/module.h> #include <linux/gpio.h> #include <linux/input/matrix_keypad.h> #include <linux/slab.h> #include <linux/miscdevice.h> #include <linux/printk.h> #include <linux/kernel.h> #include <mach/irqs.h> #include <mach/hardware.h> #include <mach/sys_config.h> #define LED_IOCTL_SET_ON 1 #define LED_IOCTL_SET_OFF 0 static script_item_u led_val[5]; static script_item_value_type_e led_type; static struct semaphore lock; //led_open static int led_open(struct inode *inode, struct file *file) { if (!down_trylock(&lock)) return 0; else return -EBUSY; } //led_close static int led_close(struct inode *inode, struct file *file) { up(&lock); return 0; } //led_ioctl static long led_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { unsigned int n; n = (unsigned int)arg; switch (cmd) { case LED_IOCTL_SET_ON: if (n < 1) return -EINVAL; if(led_val[n-1].gpio.gpio != -1) { __gpio_set_value(led_val[n-1].gpio.gpio, 1); printk("led%d on !\n", n); } break; case LED_IOCTL_SET_OFF: default: if (n < 1) return -EINVAL; if(led_val[n-1].gpio.gpio != -1) { __gpio_set_value(led_val[n-1].gpio.gpio, 0); printk("led%d off !\n", n); } break; } return 0; } //led_gpio static int __devinit led_gpio(void) { int i = 0; char gpio_num[10]; for(i =1 ; i < 6; i++) { sprintf(gpio_num, "led_gpio%d", i); led_type= script_get_item("led_para", gpio_num, &led_val[i-1]); if(SCIRPT_ITEM_VALUE_TYPE_PIO != led_type) { printk("led_gpio type fail !"); led_val[i-1].gpio.gpio = -1; continue; } if(0 != gpio_request(led_val[i-1].gpio.gpio, NULL)) { printk("led_gpio gpio_request fail !"); led_val[i-1].gpio.gpio = -1; continue; } if (0 != gpio_direction_output(led_val[i-1].gpio.gpio, 0)) { printk("led_gpio gpio_direction_output fail !"); led_val[i-1].gpio.gpio = -1; continue; } } return 0; } //file_operations static struct file_operations leds_ops = { .owner = THIS_MODULE, .open = led_open, .release = led_close, .unlocked_ioctl = led_ioctl, }; //miscdevice static struct miscdevice leds_dev = { .minor = MISC_DYNAMIC_MINOR, .name = "led", .fops = &leds_ops, }; //led_remove static int __devexit led_remove(struct platform_device *pdev) { return 0; } //led_probe static int __devinit led_probe(struct platform_device *pdev) { int led_used; script_item_u val; script_item_value_type_e type; int err; printk("led_para!\n"); type = script_get_item("led_para", "led_used", &val); if (SCIRPT_ITEM_VALUE_TYPE_INT != type) { printk("%s script_get_item \"led_para\" led_used = %d\n", __FUNCTION__, val.val); return -1; } led_used = val.val; printk("%s script_get_item \"led_para\" led_used = %d\n", __FUNCTION__, val.val); if(!led_used) { printk("%s led_used is not used in config, led_used=%d\n", __FUNCTION__,led_used); return -1; } err = led_gpio(); if (err) return -1; sema_init(&lock, 1); err = misc_register(&leds_dev); printk("======= cqa83 led initialized ================\n"); return err; } //platform_device struct platform_device led_device = { .name = "led", }; //platform_driver static struct platform_driver led_driver = { .probe = led_probe, .remove = __devexit_p(led_remove), .driver = { .name = "led", .owner = THIS_MODULE, }, }; //led_init static int __init led_init(void) { if (platform_device_register(&led_device)) { printk("%s: register gpio device failed\n", __func__); } if (platform_driver_register(&led_driver)) { printk("%s: register gpio driver failed\n", __func__); } return 0; } //led_exit static void __exit led_exit(void) { platform_driver_unregister(&led_driver); } module_init(led_init); module_exit(led_exit); MODULE_DESCRIPTION("Led Driver"); MODULE_LICENSE("GPL v2");

既然 JNI 是通過 ioctl 下發控制,那麼源碼裡與之對應的自然是 led_ioctl。這個函數值得單獨拎出來看:

 //led_ioctl static long led_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { unsigned int n; n = (unsigned int)arg; switch (cmd) { case LED_IOCTL_SET_ON: if (n < 1) return -EINVAL; if(led_val[n-1].gpio.gpio != -1) { __gpio_set_value(led_val[n-1].gpio.gpio, 1); printk("led%d on !\n", n); } break; case LED_IOCTL_SET_OFF: default: if (n < 1) return -EINVAL; if(led_val[n-1].gpio.gpio != -1) { __gpio_set_value(led_val[n-1].gpio.gpio, 0); printk("led%d off !\n", n); } break; } return 0; }

開頭兩行先定義變量 n 並把 arg 賦給它,於是 n 就成了 LED 的編號。下面是一個 switch-case,判斷依據是形參 cmd,用到 LED_IOCTL_SET_ON 與 LED_IOCTL_SET_OFF 兩個宏——它們在文件開頭被定義為 1 和 0。cmd 的作用一目了然:決定 LED 該亮還是該滅。命中點亮分支就調用 __gpio_set_value 寫 1,命中熄滅分支則寫 0。

為什麼寫 1 亮、寫 0 滅?這得從硬件電路找答案:

看電路圖就很直觀了:LED 與蜂鳴器都是共地接法,只有 IO 口輸出高電平時,LED 才會點亮、蜂鳴器才會鳴叫。

接著看 __gpio_set_value 的實現,它在 drivers/gpio/gpiolib.c 中:

 /** * __gpio_set_value() - assign a gpio's value * @gpio: gpio whose value will be assigned * @value: value to assign * Context: any * * This is used directly or indirectly to implement gpio_set_value(). * It invokes the associated gpio_chip.set() method. */ void __gpio_set_value(unsigned gpio, int value) { struct gpio_chip *chip; chip = gpio_to_chip(gpio); /* Should be using gpio_set_value_cansleep() */ WARN_ON(chip->can_sleep); trace_gpio_value(gpio, 0, value); if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) _gpio_set_open_drain_value(gpio, chip, value); else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) _gpio_set_open_source_value(gpio, chip, value); else chip->set(chip, gpio - chip->base, value); } EXPORT_SYMBOL_GPL(__gpio_set_value);

逐行來看。

struct gpio_chip *chip;——這個結構體牽涉到 Linux 的 GPIO 驅動模型,先簡單交代背景。GPIO 是嵌入式系統裡最基礎也最常用的資源:點亮 LED、驅動蜂鳴器、輸出高低電平、檢測按鍵,都靠它。在 davinci 這類平台上,最底層的寄存器操作位於 arch/arm/mach-davinci 目錄下的 gpio.c,寫過單片機 MCU 的朋友對這種寄存器級驅動應該不陌生。

本質上 GPIO 驅動要做的就是讀狀態或寫狀態,邏輯並不複雜,但 Linux 在其上做了一層軟件分層:把各板卡對 GPIO 的操作抽象成統一接口,具體實現放在 gpiolib.c,內核配置時需要開啟 CONFIG_GENERIC_GPIO。

由於 GPIO 與硬件體系緊密耦合,Linux 用 gpio_chip 這個模型來統一描述:每塊板卡實現自己的控制模塊(request、free、input、output、get、set、irq 等),註冊進內核後會更新全局數組 gpio_desc[]。當有人請求某個 GPIO 時,就在這個數組中查到它,再調用對應 gpio_chip 的處理函數。也就是說,調用方只需帶著全局序號去 request、dataout、datain、free,最終會落到具體的硬件實現上。

而 GPIO 本身是一組引腳,由多個寄存器共同控制——數據狀態、輸入輸出方向、清零、中斷觸發沿等等,通常按 bank 分組,底層讀寫則依賴 __raw_writel()、__raw_writeb()、__raw_readl()、__raw_readb() 這幾個函數。

gpio_chip 的定義在 include/asm-generic/gpio.h:

 struct gpio_chip { const char *label; struct device *dev; struct module *owner; int (*request)(struct gpio_chip *chip, unsigned offset); void (*free)(struct gpio_chip *chip, unsigned offset); int (*direction_input)(struct gpio_chip *chip, unsigned offset); int (*get)(struct gpio_chip *chip, unsigned offset); int (*direction_output)(struct gpio_chip *chip, unsigned offset, int value); int (*set_debounce)(struct gpio_chip *chip, unsigned offset, unsigned debounce); void (*set)(struct gpio_chip *chip, unsigned offset, int value); int (*to_irq)(struct gpio_chip *chip, unsigned offset); void (*dbg_show)(struct seq_file *s, struct gpio_chip *chip); int base; u16 ngpio; const char *const *names; unsigned can_sleep:1; unsigned exported:1; #if defined(CONFIG_OF_GPIO) struct device_node *of_node; int of_gpio_n_cells; int (*of_xlate)(struct gpio_chip *gc, const struct of_phandle_args *gpiospec, u32 *flags); #endif #ifdef CONFIG_PINCTRL struct list_head pin_ranges; #endif };

回到 chip = gpio_to_chip(gpio);,同一文件下的實現是:

 /* caller holds gpio_lock *OR* gpio is marked as requested */ struct gpio_chip *gpio_to_chip(unsigned gpio) { return gpio_desc[gpio].chip; }

顯然它是從 gpio_desc 數組裡按序號取出對應的 chip。這個描述結構體就在同一文件中:

 struct gpio_desc { struct gpio_chip *chip; unsigned long flags; /* flag symbols are bit numbers */ #define FLAG_REQUESTED 0 #define FLAG_IS_OUT 1 #define FLAG_RESERVED 2 #define FLAG_EXPORT 3 #define FLAG_SYSFS 4 #define FLAG_TRIG_FALL 5 #define FLAG_TRIG_RISE 6 #define FLAG_ACTIVE_LOW 7 #define FLAG_OPEN_DRAIN 8 #define FLAG_OPEN_SOURCE 9 #define ID_SHIFT 16 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) #ifdef CONFIG_DEBUG_FS const char *label; #endif }; static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];

再看 WARN_ON(chip->can_sleep);——這裡是在表達一個約束:當前路徑設置的是不可休眠的 GPIO,若該 chip 標記為可休眠(can_sleep 為真),就應該改用 gpio_set_value_cansleep(),這行斷言用來提前暴露誤用。

trace_gpio_value(gpio, 0, value); 是給 GPIO 取值操作掛一個 trace 追蹤點,方便在 ftrace 中觀察 GPIO 的變化軌跡。

最後這段是真正寫值的地方:

 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
 _gpio_set_open_drain_value(gpio, chip, value);
 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
 _gpio_set_open_source_value(gpio, chip, value);
 else
 chip->set(chip, gpio - chip->base, value);

這裡牽涉到硬件上 GPIO 控制器的輸出示能方式。test_bit 做位測試,判斷 gpio_desc[gpio].flags 的 FLAG_OPEN_DRAIN 位是否為 1,也就是該 GPIO 是否工作於開漏模式;若不是,再判斷是否為開源模式;兩者皆非,就走最常規的 chip->set()。

兩個分支函數的實現如下:

 /* * _gpio_set_open_drain_value() - Set the open drain gpio's value. */ static void _gpio_set_open_drain_value(unsigned gpio, struct gpio_chip *chip, int value) { int err = 0; if (value) { err = chip->direction_input(chip, gpio - chip->base); if (!err) clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); } else { err = chip->direction_output(chip, gpio - chip->base, 0); if (!err) set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); } trace_gpio_direction(gpio, value, err); if (err < 0) pr_err("%s: Error in set_value for open drain gpio%d err %d\n", __func__, gpio, err); } /* * _gpio_set_open_source() - Set the open source gpio's value. */ static void _gpio_set_open_source_value(unsigned gpio, struct gpio_chip *chip, int value) { int err = 0; if (value) { err = chip->direction_output(chip, gpio - chip->base, 1); if (!err) set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); } else { err = chip->direction_input(chip, gpio - chip->base); if (!err) clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); } trace_gpio_direction(gpio, !value, err); if (err < 0) pr_err("%s: Error in set_value for open source gpio%d err %d\n", __func__, gpio, err); }

走到這裡,已經是直接操作 GPIO 的輸入輸出方向了。細心對比會發現一個有意思的差異:同樣是讓 value 為 1(輸出高),開漏分支用的是 chip->direction_input(),而開源分支用的是 chip->direction_output(..., 1)。這背後的邏輯應該與開漏電路「靠外部上拉才能出高」的特性有關,筆者在此處仍有疑惑,若有人清楚歡迎指教。

至此,Linux 下 GPIO 驅動模型大致摸了一遍,最大的感受就是——Linux 的層次實在是繁多。回到 led.c 本身還不難看出,這個驅動採用的是 platform 驅動機制,關於 platform 的細節留待專門文章再談。

文末再次感謝前輩們的無私分享。

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