浅析linux下键盘设备工作和注册流程 -tty串口﹑hid鼠标键盘和usb盘相关 - ...

来源:百度文库 编辑:神马文学网 时间:2024/04/19 06:31:44
浅析linux下键盘设备工作和注册流程

浅析linux下键盘设备工作和注册流程

【浅析linux下鼠标驱动的实现】
 input_init()=>
=>
class_register(&input_class);注册input类
input_proc_init();创建proc下的目录和文件
register_chrdev(INPUT_MAJOR, "input", &input_fops);注册驱动程序到cdev_map上,以待驱动设备.

drivers\input\keyboard\pxa3xx_keypad.c为我们的keyboard设备,
pxa3xx_keypad_probe=>
request_irq(IRQ_ENHROT, &enhanced_rotary_interrupt,
            IRQF_DISABLED, "Enhanced Rotary", (void *)keypad);注册快捷键中断
request_irq(IRQ_KEYPAD, pxa3xx_keypad_interrupt, IRQF_DISABLED,pdev->name, keypad);注册中断
static irqreturn_t pxa3xx_keypad_interrupt(int irq, void *dev_id)
{
    struct pxa3xx_keypad *keypad = dev_id;
    uint32_t kpc = keypad_readl(KPC);

    if (kpc & KPC_MI)
        pxa3xx_keypad_scan_matrix(keypad);

    if (kpc & KPC_DI)
        pxa3xx_keypad_scan_direct(keypad);

    return IRQ_HANDLED;
}
在irq中如果读到了key,那么会直接调用
input_report_key(keypad->input_dev,lookup_matrix_keycode(keypad, row, col),
                new_state[col] & (1 << row));
static inline unsigned int lookup_matrix_keycode(
        struct pxa3xx_keypad *keypad, int row, int col)
{
    return keypad->matrix_keycodes[(row << 3) + col];
}
input_report_key(struct input_dev *dev, unsigned int code, int value)
dev为input_dev设备,我们的4*4键盘
code为标准PC键盘码值
value为按键动作,为1表示键盘按下,为0表示按键抬起
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
{
    input_event(dev, EV_KEY, code, !!value);
}
void input_event(struct input_dev *dev,
         unsigned int type, unsigned int code, int value)
{
    unsigned long flags;

    if (is_event_supported(type, dev->evbit, EV_MAX)) {
        spin_lock_irqsave(&dev->event_lock, flags);
        add_input_randomness(type, code, value);//因为按键的存在随机性,所以按键是给系统提供墒随机数的好来源.
        input_handle_event(dev, type, code, value);
        spin_unlock_irqrestore(&dev->event_lock, flags);
    }
}
static void input_handle_event(struct input_dev *dev,
                   unsigned int type, unsigned int code, int value)
{
    ...
        case EV_KEY:
        if (is_event_supported(code, dev->keybit, KEY_MAX) &&
            !!test_bit(code, dev->key) != value) {//这次来的是否为新的键值

            if (value != 2) {
                __change_bit(code, dev->key);//通过异或^操作,反转code对应的bitmap,如果value等于2,那么将忽略该按键
                if (value)
                    input_start_autorepeat(dev, code);//键盘按下,那么开启定时检测,这样可以出现连续输入的效果
            }

            disposition = INPUT_PASS_TO_HANDLERS;
        }
        break;
    ...
}
static void input_start_autorepeat(struct input_dev *dev, int code)
{
    if (test_bit(EV_REP, dev->evbit) &&
     dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
     dev->timer.data) {
        dev->repeat_key = code;
        mod_timer(&dev->timer,//重新启动定时器input_repeat_key,时间间隔msecs_to_jiffies(dev->rep[REP_DELAY])
             jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
    }
}

static void input_repeat_key(unsigned long data)
{
    struct input_dev *dev = (void *) data;
    unsigned long flags;

    spin_lock_irqsave(&dev->event_lock, flags);

    if (test_bit(dev->repeat_key, dev->key) &&
     is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {

        input_pass_event(dev, EV_KEY, dev->repeat_key, 2);//交给处理按键函数

        if (dev->sync) {
            /*
             * Only send SYN_REPORT if we are not in a middle
             * of driver parsing a new hardware packet.
             * Otherwise assume that the driver will send
             * SYN_REPORT once it's done.
             */
            input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
        }

        if (dev->rep[REP_PERIOD])
            mod_timer(&dev->timer, jiffies +
                    msecs_to_jiffies(dev->rep[REP_PERIOD]));
    }

    spin_unlock_irqrestore(&dev->event_lock, flags);
}

input_pass_event=>
handle->handler->event(handle, type, code, value);
就是kbd_handler的kbd_event=>kbd_keycode=>
atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNICODE, ¶m)
通知挂在keyboard链上所有等待键盘输入的应用程序,
通过register_keyboard_notifier()函数可以注册到键盘链上【gliethttp.Leith】,


input_dev = input_allocate_device();申请一个input设备空间
input_dev->open = pxa3xx_keypad_open;给这个空间填充方法
input_dev->close = pxa3xx_keypad_close;
input_dev->private = keypad;
set_bit(EV_KEY, input_dev->evbit);//键按下
set_bit(EV_REL, input_dev->evbit);//键释放
pxa3xx_keypad_build_keycode(keypad);//设备键盘映射码
该函数将根据pxa3xx_device_keypad设备下的matrix_key_map进行键控设置,
pxa_set_keypad_info(&jades_keypad_info)=>将jades_keypad_info登记为pdata;
#define MAX_MATRIX_KEY_NUM (8 * 8)
matrix_keycodes[MAX_MATRIX_KEY_NUM];表示为8*8键盘
keypad->matrix_keycodes[(row << 3) + col] = code;表示第row行的第col列处按键,代表code编码值,这个为我们内部使用.
set_bit(code, input_dev->keybit);//设置code为我们的键盘对操作系统可用的键盘值
if(pdata->direct_key_num) {
    for (i = 0; i < pdata->direct_key_num; i++) {
        set_bit(pdata->direct_key_map[i], input_dev->keybit);//快捷键单元
    }
}
set_bit(KEY_POWER, input_dev->keybit);//登记电源按键为系统可见按键
input_register_device(input_dev);=>//注册设该备devices_subsys总线上
int input_register_device(struct input_dev *dev)
{
    static atomic_t input_no = ATOMIC_INIT(0);
    struct input_handler *handler;
    const char *path;
    int error;

    __set_bit(EV_SYN, dev->evbit);

    /*
     * 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);
    if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
        dev->timer.data = (long) dev;
        dev->timer.function = input_repeat_key;//消抖处理函数,采用延时消抖
        dev->rep[REP_DELAY] = 500;//250;
        dev->rep[REP_PERIOD] = 66;//33;
    }

    if (!dev->getkeycode)
        dev->getkeycode = input_default_getkeycode;

    if (!dev->setkeycode)
        dev->setkeycode = input_default_setkeycode;
    //在/sys/class/input下创建以input0,input1为目录名的input类型设备
    snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
         "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);

    if (dev->cdev.dev)
        dev->dev.parent = dev->cdev.dev;

    error = device_add(&dev->dev);//将设备登记到设备总线上,之后将以目录和文件的形式呈现
    if (error)
        return error;

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

    list_add_tail(&dev->node, &input_dev_list);
//将设备放到input的链表上,该链表上存放着所有input类型的dev设备对象【gliethttp.Leith】
    list_for_each_entry(handler, &input_handler_list, node)
        input_attach_handler(dev, handler);
//从input_handler_list驱动链表上尝试匹配,是否有驱动该dev设备的driver驱动,如果有,那么将匹配的驱动绑定给dev设备,来驱动这个dev.
    input_wakeup_procfs_readers();

    mutex_unlock(&input_mutex);

    return 0;
}

drivers\char\keyboard.c
kbd_init()=>
input_register_handler(&kbd_handler); 注册键盘驱动到input_handler_list链表上

static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
    const struct input_device_id *id;
    int error;
//看看这个咚咚,是不是在黑名单里,如果在,那么就byebye了
【gliethttp.Leith】
    if (handler->blacklist && input_match_device(handler->blacklist, dev))
        return -ENODEV;

    id = input_match_device(handler->id_table, dev);
    if (!id)
        return -ENODEV;

    error = handler->connect(handler, dev, id);//ok,找到驱动该dev的driver,那么尝试连接
    if (error && error != -ENODEV)
        printk(KERN_ERR
            "input: failed to attach handler %s to device %s, "
            "error: %d\n",
            handler->name, kobject_name(&dev->dev.kobj), error);

    return error;
}

kbd_connect=>input_register_handle=>input_open_device=>pxa3xx_keypad_open配置键盘io口