2014年1月22日 星期三

Linux Input Device - Touchscreen in Android 輸入子系統 part1

前言

在此只探討 kernel space 的 input device,並以 touchscreen driver "s3c2410_ts.c" 為例,說明如何在 driver 內增加一個 input device。

本篇文章主要為整理網路上的資源並結合自己的想法,期望讀者對於input device有更多的認識。

在介紹 input device 之前,我們必須要思考一個問題,為什麼需要 input device?


輸入裝置如mouse、joystick、touchscreen、keyboard 等等,當使用者使用裝置對設備進行輸入時,必須要有一定的規範以及架構來將輸入進來訊息儲存並轉換成某個特定的操作,譬如說在Android 系統中,使用者點擊touchscreen ,系統必須根據點擊位置判斷需要執行哪項的操作。

在Linux 的架構中kernel space必須對輸入的訊息做第一次的處裡,之後再由上層的service來讀取並進行第二次的處理,整個架構非常完整,畢竟 input device 是人與設備的一個訊息交換的媒介,肯定是很重要的。

Kernel Space 中的架構


input driver -> input core -> event handler -> user space

 接下來將逐一介紹

input driver

每個touchscreen 都必須要有一個driver來驅動硬體,driver也必須註冊一個input_dev來表明自己是屬於Input device。

舉例 /kernel/drivers/input/touchscreen/s3c2410_ts.c (只列出部分code)

static int __devinit s3c2410ts_probe(struct platform_device *pdev)
{
...
input_dev = input_allocate_device(); //先配置一個Input Device的空間
...
//對此input_dev進行初始化的設定
ts.input = input_dev;
ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); //touchscreen所接受的輸入類型(type),EV_KEY表示"按鍵與按鈕",EV_ABS表示"絕對值座標"

ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); //表示手指按下touchscreen

input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0); //設定X座標的最大最小值
input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0); //設定Y座標的最大最小值

ts.input->name = "S3C24XX TouchScreen";
ts.input->id.bustype = BUS_HOST;
ts.input->id.vendor = 0xDEAD;
ts.input->id.product = 0xBEEF;
ts.input->id.version = 0x0102;
 ...
ret = input_register_device(ts.input); //註冊此Input Device
 ...
}

driver 部分的初始化的過程到此算完成,必須寫在probe函式內是因為driver起來時就會call到,所以當然註冊input device也必須寫在這裡。

接下來要介紹,當使用者按下touchscreen時,driver如何回報給上層

static void touch_timer_fire(unsigned long data)
{
...
input_report_abs(ts.input, ABS_X, ts.xp); //回報X座標
input_report_abs(ts.input, ABS_Y, ts.yp); //回報Y座標
 input_report_key(ts.input, BTN_TOUCH, 1); //表示手指按下,第三個參數如果為0則表示手指離開touchscreen

input_sync(ts.input); //表示回報完成
...
}

這篇因著重在input device所以就不在driver其他部份多做說明。

下一篇將介紹剩餘的部分

第一次寫網誌,主要是想記錄自己在工作上所學習到的知識,如果有任何錯誤麻煩指教了^^

Ref:




2 則留言: