8-输入子系统

输入子系统:
  • 输入设备是嵌入式系统必不可少的组成部分,输入设备的类型和物理形态多种多样,如鼠标,键盘,触摸屏等等,尽管形式多样,
    但是他们都有一个共通性,即一个输入设备发生一个输入事件(如鼠标点击一下,按键按一个键等等),内核收到该输入事件后对其处理,
    并将处理结果汇报给应用层。根据这些特性,linux内核构建出了输入子系统框架,供linux驱动开发者们使用。
  • 优点:
    • 1.减少驱动开发者的工作量
    • 2.移植性,可读性强。
输入子系统框架:
  • 输入设备层--->输入核心层--->输入事件处理层---->应用层
  • 此框架类似一个在内核中的纵向结构,以一个输入事件为关键,将输入事件逐层上报,直至应用层。
输入子系统重要数据结构:
  • 1.input_dev: 表示一个输入设备
  • 2.input_handle:是连接输入设备和输入事件处理的数据结构
  • 3.input_handler:是表示一个输入事件处理的数据结构
  • 4.input_event: 是表示一个输入事件
输入系统的函数:
  • 1.input_allocate_device
  • 2.input_register_device
  • 3.input_free_device
  • 4.input_unregister_device
  • 5.input_event;
  • 6.input_sync:

/**
 * struct input_dev - represents an input device
 * @name: name of the device
 * @phys: physical path to the device in the system hierarchy
 * @uniq: unique identification code for the device (if device has it)
 * @id: id of the device (struct input_id)
 * @propbit: bitmap of device properties and quirks
 * @evbit: bitmap of types of events supported by the device (EV_KEY,
 *  EV_REL, etc.)
 * @keybit: bitmap of keys/buttons this device has
 * @relbit: bitmap of relative axes for the device
 * @absbit: bitmap of absolute axes for the device
 * @mscbit: bitmap of miscellaneous events supported by the device
 * @ledbit: bitmap of leds present on the device
 * @sndbit: bitmap of sound effects supported by the device
 * @ffbit: bitmap of force feedback effects supported by the device
 * @swbit: bitmap of switches present on the device
 * @hint_events_per_packet: average number of events generated by the
 *  device in a packet (between EV_SYN/SYN_REPORT events). Used by
 *  event handlers to estimate size of the buffer needed to hold
 *  events.
 * @keycodemax: size of keycode table
 * @keycodesize: size of elements in keycode table
 * @keycode: map of scancodes to keycodes for this device
 * @getkeycode: optional legacy method to retrieve current keymap.
 * @setkeycode: optional method to alter current keymap, used to implement
 *  sparse keymaps. If not supplied default mechanism will be used.
 *  The method is being called while holding event_lock and thus must
 *  not sleep
 * @ff: force feedback structure associated with the device if device
 *  supports force feedback effects
 * @repeat_key: stores key code of the last key pressed; used to implement
 *  software autorepeat
 * @timer: timer for software autorepeat
 * @rep: current values for autorepeat parameters (delay, rate)
 * @mt: pointer to multitouch state
 * @absinfo: array of &struct input_absinfo elements holding information
 *  about absolute axes (current value, min, max, flat, fuzz,
 *  resolution)
 * @key: reflects current state of device's keys/buttons
 * @led: reflects current state of device's LEDs
 * @snd: reflects current state of sound effects
 * @sw: reflects current state of device's switches
 * @open: this method is called when the very first user calls
 *  input_open_device(). The driver must prepare the device
 *  to start generating events (start polling thread,
 *  request an IRQ, submit URB, etc.)
 * @close: this method is called when the very last user calls
*  input_close_device().
 * @flush: purges the device. Most commonly used to get rid of force
 *  feedback effects loaded into the device when disconnecting
 *  from it
 * @event: event handler for events sent _to_ the device, like EV_LED
 *  or EV_SND. The device is expected to carry out the requested
 *  action (turn on a LED, play sound, etc.) The call is protected
 *  by @event_lock and must not sleep
 * @grab: input handle that currently has the device grabbed (via
 *  EVIOCGRAB ioctl). When a handle grabs a device it becomes sole
 *  recipient for all input events coming from the device
 * @event_lock: this spinlock is is taken when input core receives
 *  and processes a new event for the device (in input_event()).
 *  Code that accesses and/or modifies parameters of a device
 *  (such as keymap or absmin, absmax, absfuzz, etc.) after device
 *  has been registered with input core must take this lock.
 * @mutex: serializes calls to open(), close() and flush() methods
 * @users: stores number of users (input handlers) that opened this
 *  device. It is used by input_open_device() and input_close_device()
 *  to make sure that dev->open() is only called when the first
 *  user opens device and dev->close() is called when the very
 *  last user closes the device
 * @going_away: marks devices that are in a middle of unregistering and
 *  causes input_open_device*() fail with -ENODEV.
 * @dev: driver model's view of this device
 * @h_list: list of input handles associated with the device. When
 *  accessing the list dev->mutex must be held
 * @node: used to place the device onto input_dev_list
 * @num_vals: number of values queued in the current frame
 * @max_vals: maximum number of values queued in a frame
 * @vals: array of values queued in the current frame
 * @devres_managed: indicates that devices is managed with devres framework
 *  and needs not be explicitly unregistered or freed.
 */
struct input_dev {
    const char *name;   //输入设备的名称标识
    const char *phys;
    const char *uniq;
    struct input_id id; //输入设备的id

    unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];

    unsigned long evbit[BITS_TO_LONGS(EV_CNT)];     //表示输入设备支持的设备类型
    unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];  //表示按键设备支持的具体输入事件类型
    unsigned long relbit[BITS_TO_LONGS(REL_CNT)];  //表示相对位移设备支持的具体输入事件类型
    unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];  //表示绝对位移设备支持的具体输入事件类型
    unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];  //表示杂项设备支持的具体输入事件类型
    unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];  //表示led设备支持的具体输入事件类型
  unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];    //表示声音设备支持的具体输入事件类型
    unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];    //表示力反馈设备支持的具体输入事件类型
    unsigned long swbit[BITS_TO_LONGS(SW_CNT)];    //表示开关设备支持的具体输入事件类型

    unsigned int hint_events_per_packet;

    unsigned int keycodemax;
    unsigned int keycodesize;
    void *keycode;

    int (*setkeycode)(struct input_dev *dev,
              const struct input_keymap_entry *ke,
              unsigned int *old_keycode);
    int (*getkeycode)(struct input_dev *dev,
              struct input_keymap_entry *ke);

    struct ff_device *ff;

    unsigned int repeat_key;
    struct timer_list timer;

    int rep[REP_CNT];

    struct input_mt *mt;

    struct input_absinfo *absinfo;

    unsigned long key[BITS_TO_LONGS(KEY_CNT)];
    unsigned long led[BITS_TO_LONGS(LED_CNT)];
    unsigned long snd[BITS_TO_LONGS(SND_CNT)];
    unsigned long sw[BITS_TO_LONGS(SW_CNT)];

    int (*open)(struct input_dev *dev);
    void (*close)(struct input_dev *dev);
    int (*flush)(struct input_dev *dev, struct file *file);
    int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);

    struct input_handle __rcu *grab;

    spinlock_t event_lock;
    struct mutex mutex;

    unsigned int users;
    bool going_away;

    struct device dev;

    struct list_head    h_list;
    struct list_head    node;

    unsigned int num_vals;
    unsigned int max_vals;
    struct input_value *vals;

    bool devres_managed;
};

#define BUS_PCI         0x01
#define BUS_ISAPNP      0x02
#define BUS_USB         0x03
#define BUS_HIL         0x04
#define BUS_BLUETOOTH       0x05
#define BUS_VIRTUAL     0x06

#define BUS_ISA         0x10
#define BUS_I8042       0x11
#define BUS_XTKBD       0x12
#define BUS_RS232       0x13
#define BUS_GAMEPORT        0x14
#define BUS_PARPORT     0x15
#define BUS_AMIGA       0x16
#define BUS_ADB         0x17
#define BUS_I2C         0x18
#define BUS_HOST        0x19
#define BUS_GSC         0x1A
#define BUS_ATARI       0x1B
#define BUS_SPI         0x1C

struct input_id {
    __u16 bustype;   //总线类型
    __u16 vendor;    //厂商
    __u16 product;   //产品编号
    __u16 version;   //版本号
};

对位图数据结构操作的API:
/************************************************************************/
static inline void
set_bit(unsigned long nr, volatile void * addr)
函数功能:对位图的某一个位,置1
返回值:  无
参数1:   要设置的位的编号
参数2:   要设置的位图
/************************************************************************/

/************************************************************************/
static inline void
clear_bit(unsigned long nr, volatile void * addr)
函数功能: 对位图的某一个位,置0
返回值:   无
参数1:   要设置的位的编号
参数2:   要设置的位图
/************************************************************************/

/**
 * 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
 * @events: event sequence 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
 * @legacy_minors: set to %true by drivers using legacy minor ranges
 * @minor: beginning of range of 32 legacy 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);
    void (*events)(struct input_handle *handle,
               const struct input_value *vals, unsigned int count);
    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);
//正是这个回调函数,实现了input_handler,input_dev,和input_handle三者之间的联系,他们由之前的独立的3个数据结构变成
    相互联系的数据结构



    void (*disconnect)(struct input_handle *handle);
    void (*start)(struct input_handle *handle);

    bool legacy_minors;
    int minor;
    const char *name;

    const struct input_device_id *id_table;

    struct list_head    h_list;
    struct list_head    node;
};


/**
 * struct input_handle - links input device with an input handler
 * @private: handler-specific data
 * @open: counter showing whether the handle is 'open', i.e. should deliver
 *  events from its device
 * @name: name given to the handle by handler that created it
 * @dev: input device the handle is attached to
 * @handler: handler that works with the device through this handle
 * @d_node: used to put the handle on device's list of attached handles
 * @h_node: used to put the handle on handler's list of handles from which
 *  it gets events
 */
struct input_handle {

    void *private;

    int open;
    const char *name;

    struct input_dev *dev;
    struct input_handler *handler;

    struct list_head    d_node;
 struct list_head    h_node;
};


 * 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)
  • 函数功能: 申请一个输入设备的内存
  • 返回值: 返回成功申请的输入设备的指针,失败则返回NULL
  • 参数1: 无

/**
 * 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.
 *
 * Note that this function is also used to register managed input devices
 * (ones allocated with devm_input_allocate_device()). Such managed input
 * devices need not be explicitly unregistered or freed, their tear down
 * is controlled by the devres infrastructure. It is also worth noting
 * that tear down of managed input devices is internally a 2-step process:
 * registered managed input device is first unregistered, but stays in
 * memory and can still handle input_event() calls (although events will
 * not be delivered anywhere). The freeing of managed input device will
 * happen later, when devres stack is unwound to the point where device
 * allocation was made.
 */
  • int input_register_device(struct input_dev *dev)
  • 函数功能: 向内核注册一个输入设备
  • 返回值: 成功为0.失败返回负数
  • 参数1: 要注册的输入设备

/**
 * input_unregister_device - unregister previously registered device
 * @dev: device to be unregistered
 *
 * This function unregisters an input device. Once device is unregistered
 * the caller should not try to access it as it may get freed at any moment.
 */
  • void input_unregister_device(struct input_dev *dev)
  • 函数功能: 向内核注销一个输入设备
  • 返回值: 无
  • 参数1: 要注销的输入设备

/**
 * input_free_device - free memory occupied by input_dev structure
 * @dev: input device to free
 *
 * This function should only be used if input_register_device()
 * was not called yet or if it failed. Once device was registered
 * use input_unregister_device() and memory will be freed once last
 * reference to the device is dropped.
 *
 * Device should be allocated by input_allocate_device().
 *
 * NOTE: If there are references to the input device then memory
 * will not be freed until last reference is dropped.
 */
  • void input_free_device(struct input_dev *dev)
  • 函数功能: 释放一个输入设备的内存
  • 返回值: 无
  • 参数1: 要释放的输入设备

/**
 * input_event() - report new input event
 * @dev: device that generated the event
 * @type: type of the event
 * @code: event code
 * @value: value of the event
 *
 * This function should be used by drivers implementing various input
 * devices to report input events. See also input_inject_event().
 *
 * NOTE: input_event() may be safely used right after input device was
 * allocated with input_allocate_device(), even before it is registered
 * with input_register_device(), but the event will not reach any of the
 * input handlers. Such early invocation of input_event() may be used
 * to 'seed' initial state of a switch or initial position of absolute
 * axis, etc.
 */
  • void input_event(struct input_dev *dev,unsigned int type, unsigned int code, int value)
  • 函数功能: 汇报一个输入事件
  • 返回值: 无
  • 参数1: 产生输入事件的设备
  • 参数2: 产生输入事件的设备类型
  • 参数3: 设备类型的具体的输入事件(码值)。
  • 参数4: 输入事件的变化值。

  • static inline void input_sync(struct input_dev *dev)
  • 函数功能: 用于汇报输入事件的同步,即表示一次输入事件的汇报结束。
  • 返回值: 无
  • 参数1: 汇报输入事件的设备

/*
 * The event structure itself
 */

struct input_event {
    struct timeval time;
    __u16 type;
    __u16 code;
    __s32 value;
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,816评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,729评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,300评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,780评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,890评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,084评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,151评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,912评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,355评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,666评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,809评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,504评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,150评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,121评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,628评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,724评论 2 351

推荐阅读更多精彩内容

  • 1:InputChannel提供函数创建底层的Pipe对象 2: 1)客户端需要新建窗口 2)new ViewRo...
    自由人是工程师阅读 5,284评论 0 18
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,638评论 18 139
  • 事件层 该函数返回一个指向 input_dev 类型的指针,该结构体是一个输入设备结构体,包含了输入设备的一些相关...
    季风落地窗阅读 676评论 0 3
  • 串口操作 串口操作需要的头文件 #include /*标准输入输出定义*/ #include /*标准函数库定...
    旅行家John阅读 1,299评论 0 3
  • 10.27 有降温的趋势,对自己说早安! 我哥买了蛋糕,我说吃了会胖。我哥直接说:吃饱了才有力气减肥。 于是,我毫...
    行动中的分享静阅读 569评论 4 3