input subsystem 笔记1

 

花了一个下午整理的资料,有些地方还不是很明白,先做下记录以后再详细整理。

 

/drivers/input/input.c
1.input.c --> core
	--> static int __init input_init(void)                              // 输入子系统初始化
		-->> err = class_register(&input_class);                        // 注册一个类
		-->> err = register_chrdev(INPUT_MAJOR, "input", &input_fops);  // 注册一个字符设备
		
			#define INPUT_MAJOR		13
			static const struct file_operations input_fops = {
				.owner = THIS_MODULE,
				.open = input_open_file,  
			};

疑问:file_operations input_fops 里面只有open函数,没有其他操作。如何读呢?? 是不是在input_open_file函数内进行了操作?

2. input_open_file
     > 	struct input_handler *handler = input_table[iminor(inode) >> 5];  // 根据打开文件的次设备号,得到一个input_handler。
	 >  new_fops = fops_get(handler->fops)   // 获取handler里的fops
	 >  file->f_op = new_fops;               // 将新的new_fops赋值给file->f_op,这样看来原来的input_fops只不过是个中转站,
	                                                // 在这里获取的fops才是真正需要操作的函数指针,以后的工作读写等操作都是从这个fops获取。
	 >  err = new_fops->open(inode, file);   // 调用open函数,file->f_op这个里面写有读函数。
	 
// input_handler的定义:
struct input_handler {
	const struct file_operations *fops;
	......
};

总结: 应用程序如何读?
   app read:  首先打开文件,调用input_fops 中的.open = input_open_file 函数, 
                  然后在input_open_file函数中获取新的new_fops,并打开这个文件句柄,然后调用这个文件中的read函数。
				  
找寻源头:
    input_table数组从那里来???				  
	   > static struct input_handler *input_table[8];  // 静态变量
	   > 函数int input_register_handler(struct input_handler *handler)    // 注册函数由谁注册???
            > input_table[handler->minor >> 5] = handler;  // 存入input_table 数组当中
			
问题: input_register_handler 由谁注册??  -->> 下一层sofeware注册到核心层input.c			
input_register_handler(struct input_handler *handler)			
	

sofeware层:evdev.c, keyboard.c, mousedev.c
比如evdev.c:
	static int __init evdev_init(void)
		> input_register_handler(&evdev_handler);  //evdev向core层input.c注册evdev_handler处理器。
		
		evdev_handler处理器原型:
			static struct input_handler evdev_handler = {
				.event =	evdev_event,
				.connect =	evdev_connect,
				.disconnect =	evdev_disconnect,
				.fops =		&evdev_fops,
				.minor =	EVDEV_MINOR_BASE,
				.name =		"evdev",
				.id_table =	evdev_ids,
			};
		
				evdev_fops:
					static const struct file_operations evdev_fops = {
						.owner =	THIS_MODULE,
						.read =		evdev_read,
						.write =	evdev_write,
						.poll =		evdev_poll,
						.open =		evdev_open,
						.release =	evdev_release,
						.unlocked_ioctl = evdev_ioctl,
						.fasync =	evdev_fasync,
						.flush =	evdev_flush
					};
		
		        EVDEV_MINOR_BASE = 64; // #define EVDEV_MINOR_BASE	64
					.minor =	EVDEV_MINOR_BASE =64;
					
		        前面调用input_register_handler函数中
					input_table[handler->minor >> 5] = handler;  // 存入input_table 数组当中
				替换minor则为:64/32 =2; 即将handler存入input_table[2]中。
					input_table[2] = handler;  // 注意:这个handler= &evdev_handler

----------------------------------------------------------------------------------------------------
再来看上面的流程:
	app读:
		struct input_handler *handler = input_table[iminor(inode) >> 5];  // 根据打开文件的次设备号,得
		                                                                     到一个input_handler。
		参照前面分析: 打开evdev设备
			> input_table[iminor(inode) >> 5] = input_table[2] =&evdev_handler // 而evdev_handler结构中含有fops指针,指向文件操作函数。
			> struct input_handler *handler = & evdev_handler->evdev_fops      // 到这里就把要操作的设备的函数指针传进来了,
			这里面包括各种函数,包括Open,read 等,利用这个就可以打开设备,操作设备了。

=====================================================================================================

另一层: 设备层
	前面有evdev_handler处理器原型:
		static struct input_handler evdev_handler = {
			.event =	evdev_event,
			.connect =	evdev_connect,
			.disconnect =	evdev_disconnect,
			.fops =		&evdev_fops,
			.minor =	EVDEV_MINOR_BASE,
			.name =		"evdev",
			.id_table =	evdev_ids,   
		};
	说明:
		.id_table =	evdev_ids,  // evdev_ids表示handler支持哪些设备,如果在设备层注册了一个新的设备,则匹配该evdev_handler
								// 是否支持该设备,如果支持,则调用该evdev_handler下的evdev_connect的函数。	

注册输入设备:
设备层向核心层注册函数: input_register_device
	drivers/input/input.c
		> int input_register_device(struct input_dev *dev)
			// 加入链表结构	
			> list_add_tail(&dev->node, &input_dev_list);	
			
			// 对于每一个input_handler,都调用input_attach_handler函数。
			> list_for_each_entry(handler, &input_handler_list, node)
				input_attach_handler(dev, handler); // 根据input_handler的id_table判断是否支持这个input_dev
				
				
				
注册input_handler:
调用 input_register_handler:
	drivers/input/input.c
	// 先将handler放入数组
	> input_table[handler->minor >> 5] = handler;  // 存入input_table 数组当中
	
	// 放入链表
	> list_add_tail(&handler->node, &input_handler_list);
		
	// 对于每个input_dev, 调用input_attach_handler
	> list_for_each_entry(dev, &input_dev_list, node)
		 input_attach_handler(dev, handler);	// 根据input_handler的id_table判断是否支持这个input_dev
		
		
函数input_attach_handler(dev, handler) 什么作用?? 用来匹配input_dev和handler,只有匹配成功,才能进行下一步关联操作。
	
	// 匹配handler->id 和 dev->id 中的数据。
	> id = input_match_device(handler->id_table, dev);
	
     	// 定义了一个 input_device_id,该结构体表示设备的标识,标识中存储了设备的信息。
		> 原型: static const struct input_device_id *input_match_device(const struct input_device_id *id,
							struct input_dev *dev)
							
		// 匹配成功,则调用connect函数。
		> error = handler->connect(handler, dev, id);
		
input_device_id	结构体原型:
struct input_device_id {

	kernel_ulong_t flags;

	__u16 bustype;
	__u16 vendor;
	__u16 product;
	__u16 version;

	// 定义数组?
	kernel_ulong_t evbit[INPUT_DEVICE_ID_EV_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t keybit[INPUT_DEVICE_ID_KEY_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t relbit[INPUT_DEVICE_ID_REL_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t absbit[INPUT_DEVICE_ID_ABS_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t mscbit[INPUT_DEVICE_ID_MSC_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t ledbit[INPUT_DEVICE_ID_LED_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t sndbit[INPUT_DEVICE_ID_SND_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t ffbit[INPUT_DEVICE_ID_FF_MAX / BITS_PER_LONG + 1];
	kernel_ulong_t swbit[INPUT_DEVICE_ID_SW_MAX / BITS_PER_LONG + 1];

	kernel_ulong_t driver_info;
};

input_match_device函数用来与input_dev和handler进行匹配,handler的id_table表中定义了其支持的input_dev设备.
input_match_device函数中有:
static const struct input_device_id *input_match_device(const struct input_device_id *id,
							struct input_dev *dev)
{
	......
	MATCH_BIT(evbit,  EV_MAX);    // 比较匹配?
	MATCH_BIT(keybit, KEY_MAX);
	MATCH_BIT(relbit, REL_MAX);
	MATCH_BIT(absbit, ABS_MAX);
	MATCH_BIT(mscbit, MSC_MAX);
	MATCH_BIT(ledbit, LED_MAX);
	MATCH_BIT(sndbit, SND_MAX);
	MATCH_BIT(ffbit,  FF_MAX);
	MATCH_BIT(swbit,  SW_MAX);
}
		
		
总结:注册input_dev 或 input_handler时,会两两比较左边的input_dev和右边的input_handler,
根据input_handler的id_table判断这个input_handler能否支持这个input_dev,
如果能支持,则调用input_handler的connect函数建立连接。

-------------------------------------------------------------------------------------------------

如何建立连接??? 例子:evdev
1. 分配一个input_handle结构体

2. 在evdev_connect函数内定义一个结构体struct evdev *evdev, 该结构体中包含一个结构体
   struct input_handle handle,而结构体struct input_handle同时包含两个结构体:
	   struct input_dev *dev;
	   struct input_handler *handler;
   通过这两个结构体将dev和handler联系起来了,这里handle就相当于一个中间轴,连接着两端。
   
	input_handle.dev = input_dev ;  // 指向左边的input_dev
	input_handle.handler = input_handler; // 指向右边的input_handler
	
3. 注册
   通过各自的链表,找到对应的handler或dev的中间轴input_handle,从而将handlerhe和dev对应起来。
	input_handler->h_list =  &input_handle;
	input_dev->h_list     =  &input_handle;

	
函数:evdev_connect
	// 分配一个input_handle 结构体。
	evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
	
	// 设置
	函数原型:
	static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
			 const struct input_device_id *id)
	{
		......
		
		struct evdev *evdev;
		
		evdev->exist = 1;
		evdev->minor = minor;
		evdev->handle.dev = dev;
		evdev->handle.name = evdev->name;
		evdev->handle.handler = handler;
		evdev->handle.private = evdev;
		sprintf(evdev->name, "event%d", minor);   // 会自动建立的设备节点的名称。
		
		
		error = input_register_handle(&evdev->handle);  // 注册
	}
		// 注册
		error = input_register_handle(&evdev->handle);

			int input_register_handle(struct input_handle *handle)
			{
				struct input_handler *handler = handle->handler;
				
				//list_add_tail(*new, *head)
				
				// 即将 &handle->d_node 挂在&handle->dev->h_list下。
				list_add_tail(&handle->d_node, &handle->dev->h_list);
				
				// 这里handler = handle->handler,即将&handle->h_node 挂在&handle->handler->h_list下。
				list_add_tail(&handle->h_node, &handler->h_list);

				if (handler->start)
					handler->start(handle);

				return 0;
			}
		
		
struct evdev {
	int exist;
	int open;
	int minor;
	char name[16];
	struct input_handle handle;
	wait_queue_head_t wait;
	struct evdev_client *grab;
	struct list_head client_list;
};

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

struct input_handler {
	void *private;
	void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
	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;
	const struct input_device_id *blacklist;
	struct list_head	h_list;
	struct list_head	node;
};		  


struct input_dev {

	void *private;

	const char *name;
	const char *phys;
	const char *uniq;
	struct input_id id;

	unsigned long evbit[NBITS(EV_MAX)];
	unsigned long keybit[NBITS(KEY_MAX)];
	unsigned long relbit[NBITS(REL_MAX)];
	unsigned long absbit[NBITS(ABS_MAX)];
	unsigned long mscbit[NBITS(MSC_MAX)];
	unsigned long ledbit[NBITS(LED_MAX)];
	unsigned long sndbit[NBITS(SND_MAX)];
	unsigned long ffbit[NBITS(FF_MAX)];
	unsigned long swbit[NBITS(SW_MAX)];

	unsigned int keycodemax;
	unsigned int keycodesize;
	void *keycode;
	int (*setkeycode)(struct input_dev *dev, int scancode, int keycode);
	int (*getkeycode)(struct input_dev *dev, int scancode, int *keycode);

	struct ff_device *ff;

	unsigned int repeat_key;
	struct timer_list timer;

	int state;

	int sync;

	int abs[ABS_MAX + 1];
	int rep[REP_MAX + 1];

	unsigned long key[NBITS(KEY_MAX)];
	unsigned long led[NBITS(LED_MAX)];
	unsigned long snd[NBITS(SND_MAX)];
	unsigned long sw[NBITS(SW_MAX)];

	int absmax[ABS_MAX + 1];
	int absmin[ABS_MAX + 1];
	int absfuzz[ABS_MAX + 1];
	int absflat[ABS_MAX + 1];

	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 *grab;

	struct mutex mutex;	/* serializes open and close operations */
	unsigned int users;

	struct class_device cdev;
	union {			/* temporarily so while we switching to struct device */
		struct device *parent;
	} dev;

	struct list_head	h_list;
	struct list_head	node;
};


怎么读按键???
app: read
------------------
	...
	> 最终会调用函数:evdev_read();
	static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
	{
		.....
		// 环形缓冲区。 如果是NONBLOCK非阻塞方式打开,则读不到数据马上返回。
		if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK))
			return -EAGAIN;
			
		// 如果是阻塞方式,读不到数据,则进入休眠模式。
		retval = wait_event_interruptible(evdev->wait,client->head != client->tail || !evdev->exist);
	}
	
谁来唤醒???
evdev_event:在哪里定义??
      --->> static struct input_handler evdev_handler = {
	                                                         .event =	evdev_event,
															......
														}
	// 唤醒读休眠的进程													
	> wake_up_interruptible(&evdev->wait);
	
evdev_event被谁调用???
猜:应该是硬件相关的代码,input_dev那层调用的。
在设备的中断服务程序里,确定时间是什么,然后调用相应的input_handler的event函数来处理。

参考/drivers/input/keyboard/gpio_keys.c:

static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
	> input_event(input, type, button->code, !!state);
	> input_sync(input);
	
	(1) void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
		
		// 遍历dev->h_list链表中的handle函数,如果handle打开,则调用handler中的event事件,对于evdev设备,
		// 则调用 evdev_handler.event = evdev_event 函数。
		list_for_each_entry(handle, &dev->h_list, d_node)
		if (handle->open)
			handle->handler->event(handle, type, code, value);  // 如果是evdev设备,则这里调用evdev_handler中的evdev_event函数。

evdev中对struct input_handler的具体定义:
static struct input_handler evdev_handler = {
	.event =	evdev_event,
	.connect =	evdev_connect,
	.disconnect =	evdev_disconnect,
	.fops =		&evdev_fops,
	.minor =	EVDEV_MINOR_BASE,
	.name =		"evdev",
	.id_table =	evdev_ids,
};

struct input_handler 结构体原型:
struct input_handler {

	void *private;

	void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
	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;
	const struct input_device_id *blacklist;

	struct list_head	h_list;
	struct list_head	node;
};

-------------------------------------------------------------------------------------------
怎么写符合输入子系统的驱动程序??
1. 分配一个input_dev结构体
2. 设置
3. 注册
4. 硬件相关的代码,比如在中断服务程序里上报事件。

参考/drivers/input/keyboard/gpio_keys.c
	static int __devinit gpio_keys_probe(struct platform_device *pdev)
	
	    // 1. 分配一个input_dev结构体
		input = input_allocate_device();

		// 2. 设置
		input->name = pdev->name;
		input->phys = "gpio-keys/input0";
		input->dev.parent = &pdev->dev;

		input->id.bustype = BUS_HOST;
		input->id.vendor = 0x0001;
		input->id.product = 0x0001;
		input->id.version = 0x0100;
		
		// 3. 注册
		error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
			 button->desc ? button->desc : "gpio_keys",
			 pdev);
			 
		// 4. 硬件相关的代码,比如在中断服务程序里上报事件。	 
		static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
			// 中断服务程序里上报事件。
			> input_event(input, type, button->code, !!state);
			> input_sync(input);


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值