這個文件是將input.c分析了一小半後打斷進入的,因爲在分析input.c的時候,發現這個文件只不過是一個函數集,類似於i2c-core.c的作用一樣,爲了避免重蹈分析i2c-core.c的痛苦,所以這裏先分析tsdev.c文件。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#define TSDEV_MINOR_BASE 128
#define TSDEV_MINORS 32 //次版本號
/* First 16 devices are h3600_ts compatible; second 16 are h3600_tsraw */
//開始的16個設備是H3600佔有
#define TSDEV_MINOR_MASK 15
#define TSDEV_BUFFER_SIZE 64 //緩存大小
//定義TS屏的大小: 240x320
#define CONFIG_INPUT_TSDEV_SCREEN_X 240
#define CONFIG_INPUT_TSDEV_SCREEN_Y 320
//定義屏幕大小,並開放模塊變量輸入接口,可以在加載模塊的時候指定屏幕大小
static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
module_param(xres, uint, 0);
MODULE_PARM_DESC(xres, "Horizontal screen resolution (can be negative for X-mirror)");
static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
module_param(yres, uint, 0);
MODULE_PARM_DESC(yres, "Vertical screen resolution (can be negative for Y-mirror)");
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//TS的事件結構體,用於記錄TS按下的事件
struct ts_event {
short pressure; //按下
short x;
short y; //x,y座標
short millisecs; //ms
};
//屏幕校準結構體,用於保存屏幕的計算參數
struct ts_calibration {
int xscale;
int xtrans;
int yscale;
int ytrans;
int xyswap;
};
//TS設備結構體,也是INPUT幾大類設備其中TS類的宿主結構體
struct tsdev {
int exist; //設備是否存在(connect寫1,dead的時候寫0)
int open; //0-handle沒打開,>0,有幾個client使用
int minor; //次版本號
char name[8]; //名字
struct input_handle handle; //句柄
wait_queue_head_t wait; //等待隊列
struct list_head client_list; //設備端鏈表
spinlock_t client_lock; //設備端鎖
struct mutex mutex; //互斥鎖
struct device dev; //設備
int x, y, pressure; //座標,動作
struct ts_calibration cal; //校準參數
};
//設備端結構體
struct tsdev_client {
struct fasync_struct *fasync; //異步操作的文件指針結構
struct list_head node; //鏈表
struct tsdev *tsdev; //設備指針(宿主)
struct ts_event buffer[TSDEV_BUFFER_SIZE]; //開個緩存保存操作事件
int head, tail; //事件隊列的頭,尾
spinlock_t buffer_lock; //操作緩存時用到的自旋鎖
int raw;
};
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#define IOC_H3600_TS_MAGIC 'f'
#define TS_GET_CAL _IOR(IOC_H3600_TS_MAGIC, 10, struct ts_calibration)
#define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
#define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOW(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOC(dir,type,nr,size) \
(((dir) << _IOC_DIRSHIFT) | \ //30
((type) << _IOC_TYPESHIFT) | \ //8
((nr) << _IOC_NRSHIFT) | \ //0
((size) << _IOC_SIZESHIFT)) //16
static struct tsdev *tsdev_table[TSDEV_MINORS/2]; //申請一組設備數組指針
static DEFINE_MUTEX(tsdev_table_mutex); //初始化一個互斥鎖
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
先從入口看起
module_init(tsdev_init);
module_exit(tsdev_exit);
static int __init tsdev_init(void)
{
//註冊的過程,其實就是把tsdev_handler掛進input_table表中,依據是minor的高三位
//然後將本handler掛進input_handler_list中
//最後取出掛在input_dev_list表中的每一個input設備,進行input_attach_handler探測,如果探測到handler上對應有dev,則調用hanlder的connect函數進行連接
return input_register_handler(&tsdev_handler);
}
static void __exit tsdev_exit(void)
{
//卸載函數就正好相反
//先依次從handler->h_list中取出每一個關聯在其上的handle設備,使之disconnect
//然後將handler從input_handler_list中斷開
//最後掛空input_table中對應的位置
input_unregister_handler(&tsdev_handler);
}
//這裏用handler來代替了驅動
static struct input_handler tsdev_handler = {
.event = tsdev_event, //事件函數
.connect = tsdev_connect, //連接函數
.disconnect = tsdev_disconnect, //失連函數(tsdev_exit時調用)
.fops = &tsdev_fops, //操作函數
.minor = TSDEV_MINOR_BASE, //次版本號
.name = "tsdev", //名字
.id_table = tsdev_ids, //ID表
};
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MODULE_DEVICE_TABLE(input, tsdev_ids);
其中tsdev_ids爲ID表
#define MODULE_DEVICE_TABLE(type,name) \
MODULE_GENERIC_TABLE(type##_device,name)
//如果不是module,則這個宏是空的
#define MODULE_GENERIC_TABLE(gtype,name) \
extern const struct gtype##_id __mod_##gtype##_table \
__attribute__ ((unused, alias(__stringify(name))))
展開來結構體就是input_device_id __mod_input_device_table
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
這一段是handler中的事件函數
//連接函數,註冊時,有設備掛上handler線上時調用
//連接的過程如下:
//1、在tsdev_table數組中找到一個空的位置
//2、新申請一塊tsdev內存,並關聯好相關信息,將其掛到tsdev_table中
//3、將tsdev->handle掛到本類的handler鏈表上,然後執行handler->start,目前這個成員沒有設置
static int tsdev_connect(
struct input_handler *handler,
struct input_dev *dev,
const struct input_device_id *id)
{
struct tsdev *tsdev;
int delta;
int minor;
int error;
//尋找表中第一個空的節點
for (minor = 0; minor < TSDEV_MINORS / 2; minor++)
if (!tsdev_table[minor])
break;
//沒有找到空的位置(從上面的循環結束條件可見,這裏能滿足嗎??)
if (minor == TSDEV_MINORS) {
printk(KERN_ERR "tsdev: no more free tsdev devices\n");
return -ENFILE;
}
//申請一塊內存,用於保存設置信息
tsdev = kzalloc(sizeof(struct tsdev), GFP_KERNEL);
if (!tsdev)
return -ENOMEM;
INIT_LIST_HEAD(&tsdev->client_list); //初始化表頭
spin_lock_init(&tsdev->client_lock); //初始化自旋鎖
mutex_init(&tsdev->mutex); //初始化互斥鎖
init_waitqueue_head(&tsdev->wait); //初始化等待隊列
//打印tsdev名
snprintf(tsdev->name, sizeof(tsdev->name), "ts%d", minor);
tsdev->exist = 1; //表示連接上了
tsdev->minor = minor; //次版本號
tsdev->handle.dev = dev; //設備
tsdev->handle.name = tsdev->name; //tsdev名
tsdev->handle.handler = handler; //所屬句柄
tsdev->handle.private = tsdev; //私有數據爲本身
/* Precompute the rough calibration matrix */
//這一段是計算X,Y的座標映射的過程
delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
if (delta == 0)
delta = 1;
//x座標的縮放比例(這個應該和後面計算座標有關)
tsdev->cal.xscale = (xres << 8) / delta;
//x座標起點位置, 我們推導一下公式:
// (min/屏幕大小) * (xres << 8) >> 8 à 計算出來的結果是最小值對應的屏幕座標
// 屏幕座標值 / 屏幕座標範圍 = x / xMax
tsdev->cal.xtrans = - ((dev->absmin [ABS_X] * tsdev->cal.xscale) >> 8);
delta = dev->absmax [ABS_Y] - dev->absmin [ABS_Y] + 1;
if (delta == 0)
delta = 1;
tsdev->cal.yscale = (yres << 8) / delta;
tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
//ts的名字作爲設備的總線號
strlcpy(tsdev->dev.bus_id, tsdev->name, sizeof(tsdev->dev.bus_id));
tsdev->dev.devt = MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor);//版本號
tsdev->dev.class = &input_class; //所屬類
tsdev->dev.parent = &dev->dev; //父設備
tsdev->dev.release = tsdev_free; //關聯的釋放函數(實現是kfree(tsdev))
device_initialize(&tsdev->dev); //將這個設備初始化
//註冊handle,實際也就是把tsdev->handle掛上handler鏈表中
error = input_register_handle(&tsdev->handle);
if (error)
goto err_free_tsdev;
//將tsdev結構體關到tsdev_table上
error = tsdev_install_chrdev(tsdev);
if (error)
goto err_unregister_handle;
//將設備註冊到設備鏈表上
error = device_add(&tsdev->dev);
if (error)
goto err_cleanup_tsdev;
return 0;
err_cleanup_tsdev:
tsdev_cleanup(tsdev);
err_unregister_handle:
input_unregister_handle(&tsdev->handle);
err_free_tsdev:
put_device(&tsdev->dev);
return error;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
看完了connect,我們就不難想到disconnect的操作了
static void tsdev_disconnect(struct input_handle *handle)
{
struct tsdev *tsdev = handle->private;
device_del(&tsdev->dev);
tsdev_cleanup(tsdev);
input_unregister_handle(handle);
put_device(&tsdev->dev);
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//disconnect時調用
static void tsdev_cleanup(struct tsdev *tsdev)
{
struct input_handle *handle = &tsdev->handle;
tsdev_mark_dead(tsdev); //這個函數只是將tsdev->exist = 0;
tsdev_hangup(tsdev); //異步通知信號給掛在線上的全部設備
tsdev_remove_chrdev(tsdev); //tsdev_table[tsdev->minor] = NULL;
/* tsdev is marked dead so noone else accesses tsdev->open */
if (tsdev->open)
input_close_device(handle); //如果執行了打開函數,則執行對應的關閉函數
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
事件函數,也就是終端設備通過INPUT接口報告的事件
static void tsdev_event(
struct input_handle *handle,
unsigned int type,
unsigned int code,
int value)
{
struct tsdev *tsdev = handle->private; //獲取私有數據,也就是所屬於的handler
struct input_dev *dev = handle->dev; //獲得設備
int wake_up_readers = 0;
switch (type) {
case EV_ABS: //絕對座標
switch (code) {
case ABS_X:
tsdev->x = value;
break;
case ABS_Y:
tsdev->y = value;
break;
case ABS_PRESSURE: //按下狀態
if (value > dev->absmax[ABS_PRESSURE])
value = dev->absmax[ABS_PRESSURE];
value -= dev->absmin[ABS_PRESSURE];
if (value < 0)
value = 0;
tsdev->pressure = value;
break;
}
break;
case EV_REL: //相對座標
switch (code) {
case REL_X:
tsdev->x += value;
if (tsdev->x < 0)
tsdev->x = 0;
else if (tsdev->x > xres)
tsdev->x = xres;
break;
case REL_Y:
tsdev->y += value;
if (tsdev->y < 0)
tsdev->y = 0;
else if (tsdev->y > yres)
tsdev->y = yres;
break;
}
break;
case EV_KEY: //按鍵
if (code == BTN_TOUCH || code == BTN_MOUSE) {
switch (value) {
//pressure也就是ABS命令中的ABS_PRESSURE
case 0:
tsdev->pressure = 0;
break;
case 1:
if (!tsdev->pressure)
tsdev->pressure = 1;
break;
}
}
break;
case EV_SYN: //同步事件,告訴設備動作完成
if (code == SYN_REPORT) {
tsdev_distribute_event(tsdev); //完成動作,實現函數見下面
wake_up_readers = 1;
}
break;
}
//喚醒休眠的進程
if (wake_up_readers)
wake_up_interruptible(&tsdev->wait);
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//在驅動報告SYN的時候被調用,作用是在本類設備鏈表中提取出每一個客戶端,然後調用tsdev_pass_event函數將TS事件通知給每一個客戶端處理。
static void tsdev_distribute_event(struct tsdev *tsdev)
{
struct tsdev_client *client;
struct timeval time;
int millisecs;
do_gettimeofday(&time); //獲取時間
millisecs = time.tv_usec / 1000; //單位換算成ms
//在本類設備鏈表中提取出每一個客戶端
//相應事件
list_for_each_entry_rcu(client, &tsdev->client_list, node)
tsdev_pass_event(tsdev, client,
tsdev->x, tsdev->y,
tsdev->pressure, millisecs);
}
由上可見,報告數據時只是設備參數,直到調用了SYN才真正執行事件
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//報告TS事件給指定的客戶端
static void tsdev_pass_event(
struct tsdev *tsdev, //設備
struct tsdev_client *client, //客戶端
int x, int y, //X,Y值
int pressure, //按鍵狀態
int millisecs) //時間
{
struct ts_event *event;
int tmp;
//自旋鎖
spin_lock(&client->buffer_lock);
//從事件隊列中取一個事件內存來保存新的事件
event = &client->buffer[client->head++];
client->head &= TSDEV_BUFFER_SIZE - 1;
//將X,Y轉換爲內部座標
//tsdev->cal參數在tsdev_connect函數執行時調用
if (!client->raw) {
x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
if (tsdev->cal.xyswap) { //如果XY需要互換
tmp = x; x = y; y = tmp;
}
}
//複製事件參數
event->millisecs = millisecs;
event->x = x;
event->y = y;
event->pressure = pressure;
spin_unlock(&client->buffer_lock);
//異步通知內核有事件發生
//具體原理請BAIDU“異步通知”
kill_fasync(&client->fasync, SIGIO, POLL_IN);
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
最後來看一下handler的操作函數
static const struct file_operations tsdev_fops = {
.owner = THIS_MODULE,
.open = tsdev_open,
.release = tsdev_release,
.read = tsdev_read,
.poll = tsdev_poll,
.fasync = tsdev_fasync,
.unlocked_ioctl = tsdev_ioctl,
};
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//我們先看Open
//小結一下打開的操作:
// tsdev設備引用計數加1
// 申請一塊內存保存新的client信息,同時把這個client掛到tsdev的設備鏈表上
// tsdev_open_device打開設備
//所以,我們不難知道release操作的實現,就是逆向上面三步
static int tsdev_open(struct inode *inode, struct file *file)
{
int i = iminor(inode) - TSDEV_MINOR_BASE;
struct tsdev_client *client;
struct tsdev *tsdev;
int error;
if (i >= TSDEV_MINORS)
return -ENODEV;
error = mutex_lock_interruptible(&tsdev_table_mutex); //互斥鎖
if (error)
return error;
//獲取設備結構體
tsdev = tsdev_table[i & TSDEV_MINOR_MASK];
if (tsdev)
get_device(&tsdev->dev); //設備的引用計數加1
mutex_unlock(&tsdev_table_mutex);
if (!tsdev)
return -ENODEV;
//申請一個客戶端結構體
client = kzalloc(sizeof(struct tsdev_client), GFP_KERNEL);
if (!client) {
error = -ENOMEM;
goto err_put_tsdev;
}
spin_lock_init(&client->buffer_lock);
client->tsdev = tsdev;
client->raw = i >= TSDEV_MINORS / 2;
tsdev_attach_client(tsdev, client); //將client設備掛載到tsdev的client_list鏈表上
//打開設備
error = tsdev_open_device(tsdev);
if (error)
goto err_free_client;
file->private_data = client; //將客戶端作爲文件的私有數據
return 0;
err_free_client:
tsdev_detach_client(tsdev, client);
kfree(client);
err_put_tsdev:
put_device(&tsdev->dev); //設備的引用計數減1
return error;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
static int tsdev_release(struct inode *inode, struct file *file)
{
struct tsdev_client *client = file->private_data;
struct tsdev *tsdev = client->tsdev;
tsdev_fasync(-1, file, 0);
tsdev_detach_client(tsdev, client); //斷開client鏈表
kfree(client); //釋放內存
tsdev_close_device(tsdev); //關閉設備
put_device(&tsdev->dev); //tsdev的引用減1
return 0;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
簡單看一下detach和attach是怎麼掛載client到tsdev上的:
其實就是鏈表操作,tsdev->client_list
static void tsdev_attach_client(struct tsdev *tsdev, struct tsdev_client *client)
{
spin_lock(&tsdev->client_lock);
list_add_tail_rcu(&client->node, &tsdev->client_list);
spin_unlock(&tsdev->client_lock);
synchronize_sched(); //RCU機制
}
static void tsdev_detach_client(struct tsdev *tsdev, struct tsdev_client *client)
{
spin_lock(&tsdev->client_lock);
list_del_rcu(&client->node);
spin_unlock(&tsdev->client_lock);
synchronize_sched();
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//在看看open和close
//這兩個函數也很簡單,核心就是打開和關閉tsdev的handle
static int tsdev_open_device(struct tsdev *tsdev)
{
int retval;
retval = mutex_lock_interruptible(&tsdev->mutex);
if (retval)
return retval;
if (!tsdev->exist)
retval = -ENODEV; //exist=0(不存在, connect的時候寫1,DEAD寫0)
else if (!tsdev->open++) { //open=0,沒有打開過,則打開句柄;如果已經打開,就把引用加1
retval = input_open_device(&tsdev->handle);
if (retval)
tsdev->open--; //打開失敗
}
mutex_unlock(&tsdev->mutex);
return retval;
}
static void tsdev_close_device(struct tsdev *tsdev)
{
mutex_lock(&tsdev->mutex);
//tsdev已連接(connect) 且這是本handler的最後一個使用client
if (tsdev->exist && !--tsdev->open)
input_close_device(&tsdev->handle); //關閉句柄
mutex_unlock(&tsdev->mutex);
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
在release client的時候我們看到了一個同步函數tsdev_fasync:
其實這個函數的核心是fasync_helper,這個是一個內核函數,這裏就先不挖了。
static int tsdev_fasync(int fd, struct file *file, int on)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
下一個是讀函數:
//讀操作,實際是讀tsdev的X,Y,KEY值,buffer中的數據格式是固定的
//如果當前沒有操作,則會用wait_event_interruptible來等待事件的產生
static ssize_t tsdev_read(
struct file *file,
char __user *buffer,
size_t count,
loff_t *ppos)
{
struct tsdev_client *client = file->private_data;
struct tsdev *tsdev = client->tsdev;
struct ts_event event;
int retval;
//隊列爲空 && 設備connect && 不允許塊操作
if (client->head == client->tail && tsdev->exist &&
(file->f_flags & O_NONBLOCK))
return -EAGAIN;
//等待事件中斷(隊列非空 或 tsdev死亡)
retval = wait_event_interruptible(tsdev->wait,
client->head != client->tail || !tsdev->exist);
if (retval)
return retval;
//tsdev沒有連接
if (!tsdev->exist)
return -ENODEV;
//運行到這裏,retval肯定初始爲0的
while (retval + sizeof(struct ts_event) <= count //
&& tsdev_fetch_next_event(client, &event)) {
//將事件複製給用戶層(這個事件是TS的X,Y,KEY)
if (copy_to_user(buffer + retval,
&event,
sizeof(struct ts_event)))
return -EFAULT;
retval += sizeof(struct ts_event);
}
return retval;
}
同樣,這個函數的調用引出了另外一個函數:
取下一個事件,事件是否爲空的判斷原理是判斷頭和尾索引是否相等(隊列的操作)
看到取事件的方法是讀client->buffer,我們回想起了tsdev_pass_event函數(IOCTL的SYNC最後執行的函數)所實現的代碼就是將REPORT的事件放入改buffer中
static int tsdev_fetch_next_event(
struct tsdev_client *client,
struct ts_event *event)
{
int have_event;
spin_lock_irq(&client->buffer_lock); //自旋鎖
have_event = client->head != client->tail; //判斷隊列是否爲空
if (have_event) { //不爲空
*event = client->buffer[client->tail++]; //取下一個事件
client->tail &= TSDEV_BUFFER_SIZE - 1;
}
spin_unlock_irq(&client->buffer_lock); //解鎖
return have_event;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
static unsigned int tsdev_poll(struct file *file, poll_table *wait)
{
struct tsdev_client *client = file->private_data;
struct tsdev *tsdev = client->tsdev;
poll_wait(file, &tsdev->wait, wait);
return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) |
(tsdev->exist ? 0 : (POLLHUP | POLLERR));
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//IOCTL命令只是設備和獲取校正參數的
static long tsdev_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
{
struct tsdev_client *client = file->private_data; //文件的私有數據
struct tsdev *tsdev = client->tsdev;
int retval = 0;
retval = mutex_lock_interruptible(&tsdev->mutex); //互斥
if (retval)
return retval;
//tsdev已死亡或未connect
if (!tsdev->exist) {
retval = -ENODEV;
goto out;
}
switch (cmd) {
case TS_GET_CAL: //獲取校正參數(縮放尺寸和偏移)
if (copy_to_user((void __user *)arg, &tsdev->cal,
sizeof (struct ts_calibration)))
retval = -EFAULT;
break;
case TS_SET_CAL: //設置校正參數
if (copy_from_user(&tsdev->cal, (void __user *)arg,
sizeof(struct ts_calibration)))
retval = -EFAULT;
break;
default:
retval = -EINVAL;
break;
}
out:
mutex_unlock(&tsdev->mutex); //解鎖
return retval;
}