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;
};
“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值