qcom 8953 keychord系统(1) kernel层面

driver文件在drivers/input/misc里

module_init(keychord_init);
module_exit(keychord_exit);

static int __init keychord_init(void)
{
return misc_register(&keychord_misc);
}

看看register

int misc_register(struct miscdevice * misc)
{
dev_t dev;
int err = 0;


INIT_LIST_HEAD(&misc->list);


mutex_lock(&misc_mtx);


if (misc->minor == MISC_DYNAMIC_MINOR) {
int i = find_first_zero_bit(misc_minors, DYNAMIC_MINORS);
if (i >= DYNAMIC_MINORS) {
err = -EBUSY;
goto out;
}
misc->minor = DYNAMIC_MINORS - i - 1;
set_bit(i, misc_minors);
} else {
struct miscdevice *c;


list_for_each_entry(c, &misc_list, list) {
if (c->minor == misc->minor) {
err = -EBUSY;
goto out;
}
}
}


dev = MKDEV(MISC_MAJOR, misc->minor);


misc->this_device = device_create(misc_class, misc->parent, dev,  //建立设备节点
 misc, "%s", misc->name);
if (IS_ERR(misc->this_device)) {
int i = DYNAMIC_MINORS - misc->minor - 1;
if (i < DYNAMIC_MINORS && i >= 0)
clear_bit(i, misc_minors);
err = PTR_ERR(misc->this_device);
goto out;
}


/*
* Add it to the front, so that later devices can "override"
* earlier defaults
*/
list_add(&misc->list, &misc_list);
 out:
mutex_unlock(&misc_mtx);
return err;
}

接下来看看这个节点的ops

static const struct file_operations keychord_fops = {
.owner = THIS_MODULE,
.open = keychord_open,
.release = keychord_release,
.read = keychord_read,
.write = keychord_write,
.poll = keychord_poll,
};

先看这个open

static int keychord_open(struct inode *inode, struct file *file)
{
struct keychord_device *kdev;


kdev = kzalloc(sizeof(struct keychord_device), GFP_KERNEL);
if (!kdev)
return -ENOMEM;


spin_lock_init(&kdev->lock);
init_waitqueue_head(&kdev->waitq);    //初始化了这个waitq


kdev->input_handler.event = keychord_event;  //这个event会处理input keychord上报事件
kdev->input_handler.connect = keychord_connect;   //将自己的handler注册到全局handler list中去
kdev->input_handler.disconnect = keychord_disconnect;
kdev->input_handler.name = KEYCHORD_NAME;
kdev->input_handler.id_table = kdev->device_ids;


kdev->device_ids[0].flags = INPUT_DEVICE_ID_MATCH_EVBIT;
__set_bit(EV_KEY, kdev->device_ids[0].evbit);


file->private_data = kdev;


return 0;
}

看看connect里

static int keychord_connect(struct input_handler *handler,  //调用时就已经将参数传入,其实就open的时候的那个handler自身,在handler里最重要的就是event,也在open的同时把event赋值给了handler,所以真相大白
 struct input_dev *dev,
 const struct input_device_id *id)
{
int i, ret;
struct input_handle *handle;
struct keychord_device *kdev =container_of(handler, struct keychord_device, input_handler);

/*
* ignore this input device if it does not contain any keycodes
* that we are monitoring
*/
for (i = 0; i < KEY_MAX; i++) {
if (test_bit(i, kdev->keybit) && test_bit(i, dev->keybit))
break;
}
if (i == KEY_MAX)
return -ENODEV;


handle = kzalloc(sizeof(*handle), GFP_KERNEL);
if (!handle)
return -ENOMEM;


handle->dev = dev;  //这边连着device
handle->handler = handler;  //handle就是个连接器,这边连着handler
handle->name = KEYCHORD_NAME;
handle->private = kdev;


ret = input_register_handle(handle);   //注册到全局的handle list中 ,也就是让dev的handle list里有这个handle,让handler的handle list里也有这个handle。
if (ret)
goto err_input_register_handle;


ret = input_open_device(handle);
if (ret)
goto err_input_open_device;


pr_info("keychord: using input dev %s for fevent\n", dev->name);


return 0;


err_input_open_device:
input_unregister_handle(handle);
err_input_register_handle:
kfree(handle);
return ret;
}


而事件是怎么从kernel到userspace的呢

首先上层会调用

static ssize_t keychord_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
struct keychord_device *kdev = file->private_data;
__u16   id;
int retval;
unsigned long flags;


if (count < sizeof(id))
return -EINVAL;
count = sizeof(id);


if (kdev->head == kdev->tail && (file->f_flags & O_NONBLOCK))
return -EAGAIN;


retval = wait_event_interruptible(kdev->waitq,   //等待事件发生,这个waitq是在open的时候init的
kdev->head != kdev->tail);
if (retval)
return retval;

spin_lock_irqsave(&kdev->lock, flags);
/* pop a keychord ID off the queue */
id = kdev->buff[kdev->tail];
kdev->tail = (kdev->tail + 1) % BUFFER_SIZE;
spin_unlock_irqrestore(&kdev->lock, flags);


if (copy_to_user(buffer, &id, count))  //从kernel到usersapce
return -EFAULT;

return count;
}

而这个waitq的唤醒由

static void keychord_event(struct input_handle *handle, unsigned int type,
  unsigned int code, int value)
{
struct keychord_device *kdev = handle->private;
struct input_keychord *keychord;
unsigned long flags;
int i, got_chord = 0;


if (type != EV_KEY || code >= KEY_MAX)
return;


spin_lock_irqsave(&kdev->lock, flags);
/* do nothing if key state did not change */
if (!test_bit(code, kdev->keystate) == !value)
goto done;
__change_bit(code, kdev->keystate);
if (value)
kdev->key_down++;
else
kdev->key_down--;


/* don't notify on key up */
if (!value)
goto done;
/* ignore this event if it is not one of the keys we are monitoring */
if (!test_bit(code, kdev->keybit))
goto done;


keychord = kdev->keychords;
if (!keychord)
goto done;


/* check to see if the keyboard state matches any keychords */
for (i = 0; i < kdev->keychord_count; i++) {
if (check_keychord(kdev, keychord)) {
kdev->buff[kdev->head] = keychord->id;
kdev->head = (kdev->head + 1) % BUFFER_SIZE;
got_chord = 1;
break;
}
/* skip to next keychord */
keychord = NEXT_KEYCHORD(keychord);
}


done:
spin_unlock_irqrestore(&kdev->lock, flags);


if (got_chord) {
pr_info("keychord: got keychord id %d. Any tasks: %d\n",
keychord->id,
!list_empty_careful(&kdev->waitq.task_list));
wake_up_interruptible(&kdev->waitq);    //唤醒等待的read
}
}

这个kdev->input_handler.event = keychord_event; 在上层open了这个node之后就把event挂上去了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值