成人性生交大片免费看视频r_亚洲综合极品香蕉久久网_在线视频免费观看一区_亚洲精品亚洲人成人网在线播放_国产精品毛片av_久久久久国产精品www_亚洲国产一区二区三区在线播_日韩一区二区三区四区区区_亚洲精品国产无套在线观_国产免费www

主頁 > 知識庫 > Linux輸入子系統(tǒng)框架原理解析

Linux輸入子系統(tǒng)框架原理解析

熱門標(biāo)簽:信貸電銷機(jī)器人系統(tǒng) 400 電話 申請費(fèi)用 山東電信外呼系統(tǒng)靠譜嗎 云南云電銷機(jī)器人招商 長沙回?fù)芡夂粝到y(tǒng) 江蘇自動外呼系統(tǒng)一般多少錢 比較穩(wěn)定的外呼系統(tǒng) ai電話機(jī)器人營銷 鸚鵡螺號航海地圖標(biāo)注時(shí)間

input輸入子系統(tǒng)框架

linux輸入子系統(tǒng)(linux input subsystem)從上到下由三層實(shí)現(xiàn),分別為:輸入子系統(tǒng)事件處理層(EventHandler)、輸入子系統(tǒng)核心層(InputCore)和輸入子系統(tǒng)設(shè)備驅(qū)動層。

一個(gè)輸入事件,如鼠標(biāo)移動,鍵盤按鍵按下,joystick的移動等等通過 input driver -> Input core -> Event handler -> userspace 到達(dá)用戶空間傳給應(yīng)用程序。

【注意】keyboard.c不會在/dev/input下產(chǎn)生節(jié)點(diǎn),而是作為ttyn終端(不包括串口終端)的輸入。

驅(qū)動層

對于輸入子系統(tǒng)設(shè)備驅(qū)動層而言,主要實(shí)現(xiàn)對硬件設(shè)備的讀寫訪問,中斷設(shè)置,并把硬件產(chǎn)生的事件轉(zhuǎn)換為核心層定義的規(guī)范提交給事件處理層。將底層的硬件輸入轉(zhuǎn)化為統(tǒng)一事件形式,想輸入核心(Input Core)匯報(bào)。

輸入子系統(tǒng)核心層

對于核心層而言,為設(shè)備驅(qū)動層提供了規(guī)范和接口。設(shè)備驅(qū)動層只要關(guān)心如何驅(qū)動硬件并獲得硬件數(shù)據(jù)(例如按下的按鍵數(shù)據(jù)),然后調(diào)用核心層提供的接口,核心層會自動把數(shù)據(jù)提交給事件處理層。它承上啟下為驅(qū)動層提供輸入設(shè)備注冊與操作接口,如:input_register_device;通知事件處理層對事件進(jìn)行處理;在/Proc下產(chǎn)生相應(yīng)的設(shè)備信息。

事件處理層

對于事件處理層而言,則是用戶編程的接口(設(shè)備節(jié)點(diǎn)),并處理驅(qū)動層提交的數(shù)據(jù)處理。主要是和用戶空間交互(Linux中在用戶空間將所有的設(shè)備都當(dāng)作文件來處理,由于在一般的驅(qū)動程序中都有提供fops接口,以及在/dev下生成相應(yīng)的設(shè)備文件nod,這些操作在輸入子系統(tǒng)中由事件處理層完成)。

/dev/input目錄下顯示的是已經(jīng)注冊在內(nèi)核中的設(shè)備編程接口,用戶通過open這些設(shè)備文件來打開不同的輸入設(shè)備進(jìn)行硬件操作。

事件處理層為不同硬件類型提供了用戶訪問及處理接口。例如當(dāng)我們打開設(shè)備/dev/input/mice時(shí),會調(diào)用到事件處理層的Mouse Handler來處理輸入事件,這也使得設(shè)備驅(qū)動層無需關(guān)心設(shè)備文件的操作,因?yàn)镸ouse Handler已經(jīng)有了對應(yīng)事件處理的方法。

輸入子系統(tǒng)由內(nèi)核代碼drivers/input/input.c構(gòu)成,它的存在屏蔽了用戶到設(shè)備驅(qū)動的交互細(xì)節(jié),為設(shè)備驅(qū)動層和事件處理層提供了相互通信的統(tǒng)一界面。

由上圖可知輸入子系統(tǒng)核心層提供的支持以及如何上報(bào)事件到input event drivers。

作為輸入設(shè)備的驅(qū)動開發(fā)者,需要做以下幾步:

  • 在驅(qū)動加載模塊中,設(shè)置你的input設(shè)備支持的事件類型
  • 注冊中斷處理函數(shù),例如鍵盤設(shè)備需要編寫按鍵的抬起、放下,觸摸屏設(shè)備需要編寫按下、抬起、絕對移動,鼠標(biāo)設(shè)備需要編寫單擊、抬起、相對移動,并且需要在必要的時(shí)候提交硬件數(shù)據(jù)(鍵值/坐標(biāo)/狀態(tài)等等)
  • 將輸入設(shè)備注冊到輸入子系統(tǒng)中

///////////////////////////////////////////////////////////////////分割線/////////////////////////////////////////////////////////////////////////////////

輸入核心提供了底層輸入設(shè)備驅(qū)動程序所需的API,如分配/釋放一個(gè)輸入設(shè)備:

struct input_dev *input_allocate_device(void);
void input_free_device(struct input_dev *dev);

/**
 * input_allocate_device - allocate memory for new input device
 *
 * Returns prepared struct input_dev or NULL.
 *
 * NOTE: Use input_free_device() to free devices that have not been
 * registered; input_unregister_device() should be used for already
 * registered devices.
 */
struct input_dev *input_allocate_device(void)
{
  struct input_dev *dev;
     /*分配一個(gè)input_dev結(jié)構(gòu)體,并初始化為0*/ 
  dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  if (dev) {
    dev->dev.type = &input_dev_type;/*初始化設(shè)備的類型*/ 
    dev->dev.class = &input_class; /*設(shè)置為輸入設(shè)備類*/ 
    device_initialize(&dev->dev);/*初始化device結(jié)構(gòu)*/ 
    mutex_init(&dev->mutex); /*初始化互斥鎖*/ 
    spin_lock_init(&dev->event_lock); /*初始化事件自旋鎖*/ 
    INIT_LIST_HEAD(&dev->h_list);/*初始化鏈表*/ 
    INIT_LIST_HEAD(&dev->node); /*初始化鏈表*/ 

    __module_get(THIS_MODULE);/*模塊引用技術(shù)加1*/ 
  }

  return dev;
}

注冊/注銷輸入設(shè)備用的接口如下:

int __must_check input_register_device(struct input_dev *);
void input_unregister_device(struct input_dev *);

/**
 * input_register_device - register device with input core
 * @dev: device to be registered
 *
 * This function registers device with input core. The device must be
 * allocated with input_allocate_device() and all it's capabilities
 * set up before registering.
 * If function fails the device must be freed with input_free_device().
 * Once device has been successfully registered it can be unregistered
 * with input_unregister_device(); input_free_device() should not be
 * called in this case.
 */
int input_register_device(struct input_dev *dev)
{
    //定義一些函數(shù)中將用到的局部變量
  static atomic_t input_no = ATOMIC_INIT(0);
  struct input_handler *handler;
  const char *path;
  int error;
  //設(shè)置 input_dev 所支持的事件類型,由 evbit 成員來表示。具體類型在后面歸納。
  /* Every input device generates EV_SYN/SYN_REPORT events. */
  __set_bit(EV_SYN, dev->evbit);

  /* KEY_RESERVED is not supposed to be transmitted to userspace. */
  __clear_bit(KEY_RESERVED, dev->keybit);

  /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
  input_cleanse_bitmasks(dev);

   //初始化 timer 定時(shí)器,用來處理重復(fù)點(diǎn)擊按鍵。(去抖)
  /*
   * If delay and period are pre-set by the driver, then autorepeating
   * is handled by the driver itself and we don't do it in input.c.
   */
  init_timer(&dev->timer);
    //如果 rep[REP_DELAY] 和 [REP_PERIOD] 沒有設(shè)值,則賦默認(rèn)值。為了去抖。
  if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
    dev->timer.data = (long) dev;
    dev->timer.function = input_repeat_key;
    dev->rep[REP_DELAY] = 250;
    dev->rep[REP_PERIOD] = 33;
  }
   //檢查下列兩個(gè)函數(shù)是否被定義,沒有被定義則賦默認(rèn)值。
  if (!dev->getkeycode)
    dev->getkeycode = input_default_getkeycode;//得到指定位置鍵值

  if (!dev->setkeycode)
    dev->setkeycode = input_default_setkeycode;//設(shè)置指定位置鍵值
    //設(shè)置 input_dev 中 device 的名字為 inputN
    //將如 input0 input1 input2 出現(xiàn)在 sysfs 文件系統(tǒng)中
  dev_set_name(&dev->dev, "input%ld",
       (unsigned long) atomic_inc_return(&input_no) - 1);
    //將 input->dev 包含的 device 結(jié)構(gòu)注冊到 Linux 設(shè)備模型中。
  error = device_add(&dev->dev);
  if (error)
    return error;
  //打印設(shè)備的路徑并輸出調(diào)試信息
  path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  printk(KERN_INFO "input: %s as %s\n",
    dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  kfree(path);

  error = mutex_lock_interruptible(&input_mutex);
  if (error) {
    device_del(&dev->dev);
    return error;
  }
    //將 input_dev 加入 input_dev_list 鏈表中(這個(gè)鏈表中包含有所有 input 設(shè)備)
  list_add_tail(&dev->node, &input_dev_list);

  list_for_each_entry(handler, &input_handler_list, node)
   //調(diào)用 input_attatch_handler()函數(shù)匹配 handler 和 input_dev。
    //這個(gè)函數(shù)很重要,在后面單獨(dú)分析。
    input_attach_handler(dev, handler);

  input_wakeup_procfs_readers();

  mutex_unlock(&input_mutex);

  return 0;
}

而對于所有的輸入事件,內(nèi)核都用統(tǒng)一的數(shù)據(jù)結(jié)構(gòu)來描述,這個(gè)數(shù)據(jù)結(jié)構(gòu)是input_event

/*
 * The event structure itself
 */

struct input_event {
  struct timeval time; //<輸入事件發(fā)生的時(shí)間
  __u16 type;     //<輸入事件的類型
  __u16 code;     //<在輸入事件類型下的編碼
  __s32 value;     //<code的值
};

輸入事件的類型--input_event.type

/*
 * Event types
 */

#define EV_SYN      0x00 //< 同步事件
#define EV_KEY      0x01 //< 按鍵事件
#define EV_REL      0x02 //<相對坐標(biāo)(如:鼠標(biāo)移動,報(bào)告相對最后一次位置的偏移) 
#define EV_ABS      0x03 //< 絕對坐標(biāo)(如:觸摸屏或操作桿,報(bào)告絕對的坐標(biāo)位置) 
#define EV_MSC      0x04 //< 其它 
#define EV_SW       0x05 //<開關(guān) 
#define EV_LED      0x11 //<按鍵/設(shè)備燈 
#define EV_SND      0x12 //<聲音/警報(bào) 
#define EV_REP      0x14 //<重復(fù) 
#define EV_FF       0x15 //<力反饋 
#define EV_PWR      0x16 //<電源 
#define EV_FF_STATUS   0x17 //<力反饋狀態(tài) 
#define EV_MAX      0x1f //< 事件類型最大個(gè)數(shù)和提供位掩碼支持 
#define EV_CNT      (EV_MAX+1)

Linux輸入子系統(tǒng)提供了設(shè)備驅(qū)動層上報(bào)輸入事件的函數(shù)

報(bào)告輸入事件用的接口如下:

/* 報(bào)告指定type、code的輸入事件 */
void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
/* 報(bào)告鍵值 */
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
{
  input_event(dev, EV_KEY, code, !!value);
}
/* 報(bào)告相對坐標(biāo) */
static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value)
{
  input_event(dev, EV_REL, code, value);
}
/* 報(bào)告絕對坐標(biāo) */
static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
{
  input_event(dev, EV_ABS, code, value);
}
...

當(dāng)提交輸入設(shè)備產(chǎn)生的輸入事件之后,需要調(diào)用下面的函數(shù)來通知輸入子系統(tǒng),以處理設(shè)備產(chǎn)生的完整事件:

void input_sync(struct input_dev *dev);

【例子】驅(qū)動實(shí)現(xiàn)——報(bào)告結(jié)束input_sync()同步用于告訴input core子系統(tǒng)報(bào)告結(jié)束,觸摸屏設(shè)備驅(qū)動中,一次點(diǎn)擊的整個(gè)報(bào)告過程如下:

input_reprot_abs(input_dev,ABS_X,x); //x坐標(biāo)
input_reprot_abs(input_dev,ABS_Y,y); // y坐標(biāo)
input_reprot_abs(input_dev,ABS_PRESSURE,1);
input_sync(input_dev);//同步結(jié)束

【例子】按鍵中斷程序

//按鍵初始化
static int __init button_init(void)
{//申請中斷
  if(request_irq(BUTTON_IRQ,button_interrupt,0,”button”,NUll))
    return –EBUSY;
  set_bit(EV_KEY,button_dev.evbit); //支持EV_KEY事件
  set_bit(BTN_0,button_dev.keybit); //支持設(shè)備兩個(gè)鍵
  set_bit(BTN_1,button_dev.keybit); //
  input_register_device(&button_dev);//注冊input設(shè)備
}
/*在按鍵中斷中報(bào)告事件*/
Static void button_interrupt(int irq,void *dummy,struct pt_regs *fp)
{
  input_report_key(&button_dev,BTN_0,inb(BUTTON_PORT0));//讀取寄存器BUTTON_PORT0的值
  input_report_key(&button_dev,BTN_1,inb(BUTTON_PORT1));
  input_sync(&button_dev);
}

【小結(jié)】input子系統(tǒng)仍然是字符設(shè)備驅(qū)動程序,但是代碼量減少很多,input子系統(tǒng)只需要完成兩個(gè)工作:初始化和事件報(bào)告(這里在linux中是通過中斷來實(shí)現(xiàn)的)。

Event Handler層解析

Input輸入子系統(tǒng)數(shù)據(jù)結(jié)構(gòu)關(guān)系圖

input_handler結(jié)構(gòu)體

struct input_handle;

/**
 * struct input_handler - implements one of interfaces for input devices
 * @private: driver-specific data
 * @event: event handler. This method is being called by input core with
 *  interrupts disabled and dev->event_lock spinlock held and so
 *  it may not sleep
 * @filter: similar to @event; separates normal event handlers from
 *  "filters".
 * @match: called after comparing device's id with handler's id_table
 *  to perform fine-grained matching between device and handler
 * @connect: called when attaching a handler to an input device
 * @disconnect: disconnects a handler from input device
 * @start: starts handler for given handle. This function is called by
 *  input core right after connect() method and also when a process
 *  that "grabbed" a device releases it
 * @fops: file operations this driver implements
 * @minor: beginning of range of 32 minors for devices this driver
 *  can provide
 * @name: name of the handler, to be shown in /proc/bus/input/handlers
 * @id_table: pointer to a table of input_device_ids this driver can
 *  handle
 * @h_list: list of input handles associated with the handler
 * @node: for placing the driver onto input_handler_list
 *
 * Input handlers attach to input devices and create input handles. There
 * are likely several handlers attached to any given input device at the
 * same time. All of them will get their copy of input event generated by
 * the device.
 *
 * The very same structure is used to implement input filters. Input core
 * allows filters to run first and will not pass event to regular handlers
 * if any of the filters indicate that the event should be filtered (by
 * returning %true from their filter() method).
 *
 * Note that input core serializes calls to connect() and disconnect()
 * methods.
 */
struct input_handler {

  void *private;

  void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
  bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
  bool (*match)(struct input_handler *handler, struct input_dev *dev);
  int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
  void (*disconnect)(struct input_handle *handle);
  void (*start)(struct input_handle *handle);

  const struct file_operations *fops;
  int minor;
  const char *name;

  const struct input_device_id *id_table;

  struct list_head  h_list;
  struct list_head  node;
};

【例子】以evdev.c中的evdev_handler為例:

static struct input_handler evdev_handler = {
        .event = evdev_event, //<向系統(tǒng)報(bào)告input事件,系統(tǒng)通過read方法讀取
        .connect = evdev_connect, //<和input_dev匹配后調(diào)用connect構(gòu)建
        .disconnect = evdev_disconnect,
        .fops = &evdev_fops, //<event設(shè)備文件的操作方法
        .minor = EVDEV_MINOR_BASE, //<次設(shè)備號基準(zhǔn)值
        .name = "evdev",
        .id_table = evdev_ids, //<匹配規(guī)則
    };

輸入設(shè)備驅(qū)動的簡單案例

documentation/input/input-programming.txt文件,講解了編寫輸入設(shè)備驅(qū)動程序的核心步驟。

Programming input drivers
~~~~~~~~~~~~~~~~~~~~~~~~~

1. Creating an input device driver
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1.0 The simplest example
~~~~~~~~~~~~~~~~~~~~~~~~

Here comes a very simple example of an input device driver. The device has
just one button and the button is accessible at i/o port BUTTON_PORT. When
pressed or released a BUTTON_IRQ happens. The driver could look like:

#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>

#include <asm/irq.h>
#include <asm/io.h>

static struct input_dev *button_dev;

static irqreturn_t button_interrupt(int irq, void *dummy)
{
  input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
  input_sync(button_dev);
  return IRQ_HANDLED;
}

static int __init button_init(void)
{
  int error;

  if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
        printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
        return -EBUSY;
    }

  button_dev = input_allocate_device();
  if (!button_dev) {
    printk(KERN_ERR "button.c: Not enough memory\n");
    error = -ENOMEM;
    goto err_free_irq;
  }

  button_dev->evbit[0] = BIT_MASK(EV_KEY);
  button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);

  error = input_register_device(button_dev);
  if (error) {
    printk(KERN_ERR "button.c: Failed to register device\n");
    goto err_free_dev;
  }

  return 0;

 err_free_dev:
  input_free_device(button_dev);
 err_free_irq:
  free_irq(BUTTON_IRQ, button_interrupt);
  return error;
}

static void __exit button_exit(void)
{
    input_unregister_device(button_dev);
  free_irq(BUTTON_IRQ, button_interrupt);
}

module_init(button_init);
module_exit(button_exit);

1.1 What the example does
~~~~~~~~~~~~~~~~~~~~~~~~~

First it has to include the <linux/input.h> file, which interfaces to the
input subsystem. This provides all the definitions needed.

In the _init function, which is called either upon module load or when
booting the kernel, it grabs the required resources (it should also check
for the presence of the device).

Then it allocates a new input device structure with input_allocate_device()
and sets up input bitfields. This way the device driver tells the other
parts of the input systems what it is - what events can be generated or
accepted by this input device. Our example device can only generate EV_KEY
type events, and from those only BTN_0 event code. Thus we only set these
two bits. We could have used

  set_bit(EV_KEY, button_dev.evbit);
  set_bit(BTN_0, button_dev.keybit);

as well, but with more than single bits the first approach tends to be
shorter.

Then the example driver registers the input device structure by calling

  input_register_device(&button_dev);

This adds the button_dev structure to linked lists of the input driver and
calls device handler modules _connect functions to tell them a new input
device has appeared. input_register_device() may sleep and therefore must
not be called from an interrupt or with a spinlock held.

While in use, the only used function of the driver is

  button_interrupt()

which upon every interrupt from the button checks its state and reports it
via the

  input_report_key()

call to the input system. There is no need to check whether the interrupt
routine isn't reporting two same value events (press, press for example) to
the input system, because the input_report_* functions check that
themselves.

Then there is the

  input_sync()

call to tell those who receive the events that we've sent a complete report.
This doesn't seem important in the one button case, but is quite important
for for example mouse movement, where you don't want the X and Y values
to be interpreted separately, because that'd result in a different movement.

1.2 dev->open() and dev->close()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In case the driver has to repeatedly poll the device, because it doesn't
have an interrupt coming from it and the polling is too expensive to be done
all the time, or if the device uses a valuable resource (eg. interrupt), it
can use the open and close callback to know when it can stop polling or
release the interrupt and when it must resume polling or grab the interrupt
again. To do that, we would add this to our example driver:

static int button_open(struct input_dev *dev)
{
  if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
        printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
        return -EBUSY;
    }

    return 0;
}

static void button_close(struct input_dev *dev)
{
    free_irq(IRQ_AMIGA_VERTB, button_interrupt);
}

static int __init button_init(void)
{
  ...
  button_dev->open = button_open;
  button_dev->close = button_close;
  ...
}

Note that input core keeps track of number of users for the device and
makes sure that dev->open() is called only when the first user connects
to the device and that dev->close() is called when the very last user
disconnects. Calls to both callbacks are serialized.

The open() callback should return a 0 in case of success or any nonzero value
in case of failure. The close() callback (which is void) must always succeed.

1.3 Basic event types
~~~~~~~~~~~~~~~~~~~~~

The most simple event type is EV_KEY, which is used for keys and buttons.
It's reported to the input system via:

  input_report_key(struct input_dev *dev, int code, int value)

See linux/input.h for the allowable values of code (from 0 to KEY_MAX).
Value is interpreted as a truth value, ie any nonzero value means key
pressed, zero value means key released. The input code generates events only
in case the value is different from before.

In addition to EV_KEY, there are two more basic event types: EV_REL and
EV_ABS. They are used for relative and absolute values supplied by the
device. A relative value may be for example a mouse movement in the X axis.
The mouse reports it as a relative difference from the last position,
because it doesn't have any absolute coordinate system to work in. Absolute
events are namely for joysticks and digitizers - devices that do work in an
absolute coordinate systems.

Having the device report EV_REL buttons is as simple as with EV_KEY, simply
set the corresponding bits and call the

  input_report_rel(struct input_dev *dev, int code, int value)

function. Events are generated only for nonzero value.

However EV_ABS requires a little special care. Before calling
input_register_device, you have to fill additional fields in the input_dev
struct for each absolute axis your device has. If our button device had also
the ABS_X axis:

  button_dev.absmin[ABS_X] = 0;
  button_dev.absmax[ABS_X] = 255;
  button_dev.absfuzz[ABS_X] = 4;
  button_dev.absflat[ABS_X] = 8;

Or, you can just say:

  input_set_abs_params(button_dev, ABS_X, 0, 255, 4, 8);

This setting would be appropriate for a joystick X axis, with the minimum of
0, maximum of 255 (which the joystick *must* be able to reach, no problem if
it sometimes reports more, but it must be able to always reach the min and
max values), with noise in the data up to +- 4, and with a center flat
position of size 8.

If you don't need absfuzz and absflat, you can set them to zero, which mean
that the thing is precise and always returns to exactly the center position
(if it has any).

1.4 BITS_TO_LONGS(), BIT_WORD(), BIT_MASK()
~~~~~~~~~~~~~~~~~~~~~~~~~~

These three macros from bitops.h help some bitfield computations:

  BITS_TO_LONGS(x) - returns the length of a bitfield array in longs for
        x bits
  BIT_WORD(x)   - returns the index in the array in longs for bit x
  BIT_MASK(x)   - returns the index in a long for bit x

1.5 The id* and name fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The dev->name should be set before registering the input device by the input
device driver. It's a string like 'Generic button device' containing a
user friendly name of the device.

The id* fields contain the bus ID (PCI, USB, ...), vendor ID and device ID
of the device. The bus IDs are defined in input.h. The vendor and device ids
are defined in pci_ids.h, usb_ids.h and similar include files. These fields
should be set by the input device driver before registering it.

The idtype field can be used for specific information for the input device
driver.

The id and name fields can be passed to userland via the evdev interface.

1.6 The keycode, keycodemax, keycodesize fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These three fields should be used by input devices that have dense keymaps.
The keycode is an array used to map from scancodes to input system keycodes.
The keycode max should contain the size of the array and keycodesize the
size of each entry in it (in bytes).

Userspace can query and alter current scancode to keycode mappings using
EVIOCGKEYCODE and EVIOCSKEYCODE ioctls on corresponding evdev interface.
When a device has all 3 aforementioned fields filled in, the driver may
rely on kernel's default implementation of setting and querying keycode
mappings.

1.7 dev->getkeycode() and dev->setkeycode()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getkeycode() and setkeycode() callbacks allow drivers to override default
keycode/keycodesize/keycodemax mapping mechanism provided by input core
and implement sparse keycode maps.

1.8 Key autorepeat
~~~~~~~~~~~~~~~~~~

... is simple. It is handled by the input.c module. Hardware autorepeat is
not used, because it's not present in many devices and even where it is
present, it is broken sometimes (at keyboards: Toshiba notebooks). To enable
autorepeat for your device, just set EV_REP in dev->evbit. All will be
handled by the input system.

1.9 Other event types, handling output events
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The other event types up to now are:

EV_LED - used for the keyboard LEDs.
EV_SND - used for keyboard beeps.

They are very similar to for example key events, but they go in the other
direction - from the system to the input device driver. If your input device
driver can handle these events, it has to set the respective bits in evbit,
*and* also the callback routine:

  button_dev->event = button_event;

int button_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
{
  if (type == EV_SND && code == SND_BELL) {
    outb(value, BUTTON_BELL);
    return 0;
  }
  return -1;
}

This callback routine can be called from an interrupt or a BH (although that
isn't a rule), and thus must not sleep, and must not take too long to finish.

input-programming.txt

該例子提供的案例代碼描述了一個(gè)button設(shè)備,產(chǎn)生的事件通過BUTTON_PORT引腳獲取,當(dāng)有按下/釋放發(fā)生時(shí),BUTTON_IRQ被觸發(fā),以下是驅(qū)動的源代碼:

#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>

#include <asm/irq.h>
#include <asm/io.h>

static struct input_dev *button_dev; /*輸入設(shè)備結(jié)構(gòu)體*/ 
/*中斷處理函數(shù)*/ 
static irqreturn_t button_interrupt(int irq, void *dummy)
{
  /*向輸入子系統(tǒng)報(bào)告產(chǎn)生按鍵事件*/ 
  input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);
  /*通知接收者,一個(gè)報(bào)告發(fā)送完畢*/ 
  input_sync(button_dev);
  return IRQ_HANDLED;
}
/*加載函數(shù)*/ 
static int __init button_init(void)
{
  int error;
  /*申請中斷處理函數(shù)*/ //返回0表示成功,返回-INVAL表示無效
  if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) {
        /*申請失敗,則打印出錯(cuò)信息*/ 
        printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
        return -EBUSY;
    }
  /*分配一個(gè)設(shè)備結(jié)構(gòu)體*/ 
  //將在 sys/class/input/input-n 下面創(chuàng)建設(shè)備屬性文件
  button_dev = input_allocate_device();
  if (!button_dev) {   /*判斷分配是否成功*/ 
    printk(KERN_ERR "button.c: Not enough memory\n");
    error = -ENOMEM;
    goto err_free_irq;
  }

  button_dev->evbit[0] = BIT_MASK(EV_KEY); /*設(shè)置按鍵信息*/ 
  button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);

  error = input_register_device(button_dev); /*注冊一個(gè)輸入設(shè)備*/ 
  if (error) {
    printk(KERN_ERR "button.c: Failed to register device\n");
    goto err_free_dev;
  }

  return 0;
 /*以下是錯(cuò)誤處理*/ 
 err_free_dev:
  input_free_device(button_dev);
 err_free_irq:
  free_irq(BUTTON_IRQ, button_interrupt);
  return error;
}
 /*卸載函數(shù)*/ 
static void __exit button_exit(void)
{
  input_unregister_device(button_dev); /*注銷按鍵設(shè)備*/ 
  free_irq(BUTTON_IRQ, button_interrupt);/*釋放按鍵占用的中斷線*/ 
}

module_init(button_init);
module_exit(button_exit);

從這個(gè)簡單的例子中可以看到。

  • 在初始化函數(shù) button_init() 中注冊了一個(gè)中斷處理函數(shù),然后調(diào)用 input_allocate_device() 函數(shù)分配了一個(gè) input_dev 結(jié)構(gòu)體,并調(diào)用 input_register_device() 對其進(jìn)行注冊。
  • 在中斷處理函數(shù) button_interrupt() 中,實(shí)例將接收到的按鍵信息上報(bào)給 input 子系統(tǒng),從而通過 input子系統(tǒng),向用戶態(tài)程序提供按鍵輸入信息。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:運(yùn)城 齊齊哈爾 亳州 烏海 嘉興 澳門 衡陽 拉薩

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux輸入子系統(tǒng)框架原理解析》,本文關(guān)鍵詞  Linux,輸入,子系統(tǒng),框架,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux輸入子系統(tǒng)框架原理解析》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux輸入子系統(tǒng)框架原理解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    免费观看黄色网| 欧美牲交a欧美牲交aⅴ免费真| 免费无码国产v片在线观看| 欧美激情网站在线观看| 成人高h视频在线| 日韩av一区在线| 嗯用力啊快一点好舒服小柔久久| 婷婷亚洲婷婷综合色香五月| 久操视频在线观看免费| 奇米精品一区二区三区| 免费看污污视频| 91麻豆视频网站| 久操国产精品| 中文字幕1区2区3区| 中文字幕一区二区三区在线不卡| 日本精品人妻无码77777| 麻豆一区二区在线观看| 欧美乱偷一区二区三区在线| 国产在线91| 亚洲精品在线国产| 亚洲一区二区三区在线免费| 亚洲成人av一区二区三区| 成年人午夜视频| 91成人福利社区| 99在线视频观看| 国产一区二区免费在线| 亚洲v精品v日韩v欧美v专区| 色在线视频网| 色悠悠亚洲一区二区| 成人乱码一区二区三区av| 亚洲欧美日韩成人| **欧美日韩vr在线| 2019日本中文字幕| 欧美成人第一页| 美臀av在线| 中文字幕剧情在线观看| 日韩亚洲一区在线播放| 一区在线中文字幕| 性欧美xxxx视频在线观看| 无码人妻丰满熟妇区毛片| jizzjizz19| 91成人综合网| 亚洲国产一区二区在线播放| 欧美精品一卡二卡| 91杏吧porn蝌蚪| 国产+成+人+亚洲欧洲在线| 色乱码一区二区三区熟女| 亚洲国产欧美日韩精品| 国内精品久久久久久不卡影院| 68精品国产免费久久久久久婷婷| 欧美一区二区三区在线观看免费| 亚洲高清免费观看高清完整版| 波多野结衣视频网址| 成人国内精品久久久久一区| 午夜精品久久久久久久白皮肤| 特大巨黑人吊性xxx视频| 婷婷成人激情在线网| 亚洲精品一区二区三区网址| 国产农村妇女精品| 欧美在线激情网| 人人超碰91尤物精品国产| 亚洲第一男人av| 黄色精品一区二区| 99久久免费精品| 国产精品亚洲一区二区三区妖精| 69视频在线| 国产情侣呻吟对白高潮| 99热这里有精品| 国产乱妇无码大片在线观看| 91干在线观看| 97香蕉超级碰碰久久免费的优势| 国内外成人在线视频| 欧美日本在线一区| 91视频国产一区| 狠狠色综合日日| 婷婷激情综合五月天| 欧美色综合久久| 亚洲精品午夜| 精品不卡一区二区| 国产综合色香蕉精品| 久久久久久91亚洲精品中文字幕| 91在线网址| 91官网在线| 亚洲一区二区三区视频在线| 久久成人免费日本黄色| 九九视频免费观看| 国产v亚洲v天堂无码| 在线成人直播| 综合天堂av久久久久久久| 亚洲精品自产拍| 国产一区二区视频免费在线观看| 国产精品久久久久av蜜臀| 亚洲三级黄色片| 国产伦精品一区二区三区四区视频_| 91视频青青草| 国产日韩中文在线中文字幕| 首播影院在线观看免费观看电视| www视频在线播放| 欧美日韩一级黄| 亚洲不卡系列| 中国特黄一级片| 日韩aaaaaa| 6699嫩草久久久精品影院| 国产精品日本精品| 日韩欧美亚洲天堂| 成人免费av资源| 久操视频免费看| 国内精品伊人久久久久av影院| 亚洲一区视频在线| 视频精品一区二区三区| 6699嫩草久久久精品影院| 国产精品视频yy9299一区| 中文字幕在线看精品乱码| 欧美一区二区三区久久| 欧美激情精品久久久久久免费| 国产又粗又猛又色又| 成人免费av| 日本女优北野望在线电影| 一广人看www在线观看免费视频| 久久久www免费人成黑人精品| 在线观看日韩羞羞视频| 手机看片福利永久国产日韩| 精品国产一区二区三区不卡在线| 日本成人三级电影网站| 成年免费在线观看| 久久艹中文字幕| 日韩城人网站| 五月婷婷综合在线| 可以在线看的av| 午夜精品一区二区三级视频| 国产一区二区在线视频你懂的| 中国一级大黄大黄大色毛片| 成人做爰66片免费看网站| 欧美丝袜自拍制服另类| 日本aⅴ免费视频一区二区三区| 日本高清视频一区二区三区| 国产视频一区在线观看一区免费| 亚洲高清免费一级二级三级| 国产奶水涨喷在线播放| 亚洲图片在线| 交100部在线观看| 一本大道久久加勒比香蕉| 亚洲一区欧美在线| 四虎国产精品永久地址998| 四虎国产精品永久免费观看视频| 亚洲一区二区日韩| 免费观看成人av| 欧美特黄a级高清免费大片a级| 91色.com| 山东少妇露脸刺激对白在线| 黄色免费在线观看网站| 丰满人妻一区二区三区大胸| 久久这里只精品最新地址| 欧美成人午夜激情在线| 护士精品一区二区三区| 国产精品久久久久久久久毛片| 久久久久久久高潮| 亚洲国产精品毛片| 8mav在线| 久久久久亚洲av成人网人人软件| 国产精品免费网站| 欧美精品video| 中文字幕 视频一区| 日韩中文字幕一区| www.青青草| 欧美日韩第一视频| 9797在线看片亚洲精品| 国产精品羞羞答答在线| 国产无套粉嫩白浆在线2022年| 好想男人揉我下面好多水| 日韩欧美一中文字暮专区| 色综合伊人色综合网| 丁香婷婷激情网| 日韩电影在线免费| 色噜噜在线网| 成年女人毛片| 亚洲视频中文字幕在线观看| 毛片毛片毛片毛片| 日韩精品视频一区二区| 久久成人免费日本黄色| 欧美激情网站在线观看| av在线网址导航| 日韩欧美大片在线观看| 男人操女人免费| av在线免费一区| 久久国产精品久久久久久| 精品国产一二区| 中文有码在线播放| 香港三级日本三级| 亚洲精品国产精品国自产观看| 日本夜夜草视频网站| av超碰免费在线| 青青草原成人在线视频| 成年丰满熟妇午夜免费视频| 午夜在线观看视频网站| 亚洲精品二三区| www.com国产| 欧美一区二区三区爱爱| 国产精品一区二区三区成人| 欧美在线亚洲综合一区| 久久久久久三级| 99热免费观看| 欧美一区二区三区久久综| 视频一区视频二区中文| 欧美理论电影在线精品| 午夜精品一区二区三区在线观看| 韩剧1988免费观看全集| 日本午夜在线亚洲.国产| 手机在线观看av| 美女欧美视频在线观看免费| 秋霞午夜一区二区三区视频| 国产精品污www一区二区三区| 不卡一区在线观看| 高清国语自产拍免费一区二区三区| 日日摸夜夜爽人人添av| 中文字幕国产亚洲2019| www.亚洲资源| 精品国产一区二区三区久久久蜜月| 亚洲综合精品久久| 91大神福利视频在线| 精品97人妻无码中文永久在线| 亚洲一卡二卡三卡四卡五卡| 日韩av电影资源网| 日本三级久久| 国产亚洲精品美女久久久久| 一色道久久88加勒比一| 久久成人免费网站| 国产女主播在线直播| 日本女人性生活视频| 欧美在线网址| 中文人妻av久久人妻18| 黄色录像1级片| 中国字幕a在线看韩国电影| 久久精选视频| 欧日韩免费视频| 日本免费高清视频| 国产suv精品一区二区883| 国产 欧美 日韩 一区| 一级黄色免费片| 色悠久久久久综合欧美99| 亚洲精品高清无码视频| 中文字幕少妇| 女人扒开屁股爽桶30分钟| 茄子视频成人在线| 国产精品嫩草影院精东| 婷婷色中文字幕| 欧美伊人久久| 97人人爽人人澡人人精品| 97人妻精品一区二区三区| 337人体粉嫩噜噜噜| 日韩www视频| 免费av毛片在线看| 精品无码av一区二区三区不卡| 国产欧美在线观看视频| 国模吧视频一区| 99视频+国产日韩欧美| 国产精品情侣呻吟对白视频| 亚洲综合国产| 色婷婷.com| 自拍偷拍亚洲激情| 夜夜精品视频| 四虎影视18库在线影院| 免费播放片a高清在线观看| 水蜜桃在线免费观看| 九九热免费在线观看| 日本欧美大码aⅴ在线播放| 久久精品国产亚洲7777| 国产成人综合亚洲欧美在| 欧美性猛交bbbbb精品| 亚洲v片在线观看| 国产精品欧美在线| 玛雅亚洲电影| 97久久超碰国产精品电影| 国产女主播视频一区二区| 国产精品10p综合二区| 韩国av一区| 狠狠色丁香婷婷综合| 日韩一区二区三区免费播放| 经典一区二区| 国产日韩精品一区二区浪潮av| 久久久久久久九九九九| 日韩久久综合| 99爱视频在线观看| 欧美日韩精品一区二区三区视频| 亚洲免费观看高清完整版在线观看熊| 在线观看久久久久久| 成人精品久久av网站| av片免费播放| 一区二区三区高清视频在线观看| 国产毛片在线看| 亚洲精品一区二区三区四区| 国产做受高潮69| 欧美日韩不卡| 成人网视频在线观看| 亚洲性夜色噜噜噜7777| 亚洲在线成人精品| 高清毛片aaaaaaaaa片| 亚洲一区二区三区免费观看| 久久久久免费精品国产| 懂色av粉嫩av蜜乳av| 国产精品青青在线观看爽香蕉| 5252色成人免费视频| 人妻少妇一区二区三区| 国内自拍视频在线播放| 任你操视频在线观看| 国产精品男人的天堂| 亚洲视频电影图片偷拍一区| a级高清视频欧美日韩| 999精品视频一区二区三区| 亚洲人体在线| 欧美视频免费在线| 国产成人久久777777| 欧美日韩精品免费观看视频| 亚洲午夜女主播在线直播| 国内自拍第二页| 牛夜精品久久久久久久99黑人| 99精品在免费线偷拍| 91精品无人成人www| 久久艹在线视频| 国产视频网站在线| 亚洲第一区第二区| 妞干网视频在线观看| 色综合久久天天| 在线亚洲欧美| 成人黄色av免费在线观看| 中韩乱幕日产无线码一区| 日韩福利视频| 亚洲伦理一区二区|