Linux 混杂设备、外部中断和输入子系统

混杂设备也是一种字符设备,主设备号固定为10。相对于普通字符设备驱动,它不需要自己去生成设备文件。 

1、声明使用的头文件 

#include <linux/miscdevice.h> 

2、定义一个混杂设备:
static struct miscdevice miscDevice = {
 .minor = MISC_DYNAMIC_MINOR,      //自动分配从设备号  .name = "设备名称",
 .fops = &dev_fops,                              //设备文件操作指针 };


3、注册混杂设备:
misc_register(&miscDevice)                 //成功返回0 4、注销混杂设备:
misc_deregister(&miscDevice);


在驱动中使用外部中断 

1、声明头文件
#include <linux/interrupt.h> 

#include <mach/irqs.h> 

2、申明中断处理程序
static irqreturn_t handler(int irq,void *dev_ID)

{  

... ....
 return IRQ_RETVAL(IRQ_HANDLED); 

}
中断处理程序不能用户空间发送或接收数据,以及使用引起阻塞或调度的函数。 在中断处理函数中分配内存要使用GFP_ATOMIC标志,避免中断处理函数进入睡眠。

 3、注册中断
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev) 

//成功返回返回0 irq:中断号

handler:中断处理程序 

flags:与中断相关的标志
 IRQF_TRIGGER_RISING:上升沿触发 

 IRQF_TRIGGER_FALLING:下降沿触发  

IRQF_TRIGGER_HIGH:高电平触发 

 IRQF_TRIGGER_LOW:低电平触发
 IRQF_SAMPLE_RANDOM:为系统随机发生器提供支持  

IRQF_SHARED:中断可在设备间共享 

 IRQF_DISABLED:是否快速中断 name:中断名称
dev:主要用于共享中断,可通过该参数向中断处理程序传递设备号或其它参数 

4、注销中断: 

free_irq(unsigned int irq) 

5、其它:
 disable_irq(unsigned int irq)  

enable_irq(unsigned int irq)


输入子系统 input子系统: 

1、头文件:
#include <asm/bitops.h>

 #include <linux/input.h>

 2、申明
static struct input_dev *input_Dev 

3、初始化
input_Dev = input_allocate_device(); 

input_Dev->name = "名称";
input_Dev->id.bustype = BUS_HOST; //总线类型  

BUS_PCI   

BUS_ISAPNP  

BUS_USB  

BUS_HIL
 BUS_BLUETOOTH 

 BUS_VIRTUAL 

 BUS_ISA  BUS_I8042 

 BUS_XTKBD  

BUS_RS232  

BUS_GAMEPORT  

BUS_PARPORT

BUS_AMIGA  

BUS_ADB  

BUS_I2C  

BUS_HOST  

BUS_GSC  

BUS_ATARI
input_Dev->id.vendor = 供应商代码;

 input_Dev->id.version = 版本;
set_bit(EV_KEY,input_Dev->evbit); //支持按键事件类型 

 EV_KEY :按键  

EV_REL :相对坐标  

EV_ABS:绝对坐标  

EV_SND:声音  

EV_FF:力反馈  ... ...  


set_bit(KEY_A,input_Button->keybit); //设置支持按键A 

4、注册输入设备
input_register_device(struct input_dev *dev); 

5、报告输入事件
input_report_key(struct input_dev *dev, unsigned int code, int value) 

code:事件代码,可在input.h中查询相关值 

value:事件值,如果是按键类型,按下为1,松开为0 

事件同步,告知input core,驱动已发出一次完整的报告。

 input_sync((struct input_dev *dev) 

5、注销输入设备
input_unregister_device(struct input_dev *dev); 释放设备内存
input_free_device(struct input_dev *dev);

原文地址:https://www.cnblogs.com/pengdonglin137/p/3612195.html