参考:http://rainy0415.blog.163.com/blog/static/168511662010620320385/
参考:http://blog.csdn.net/hongtao_liu/article/details/4208957
如何测试Linux下的USB鼠标可用,usb mouse , /dev/input/event1
一句话:
cat /dev/input/event1 | hexdump
移动鼠标,然后终端就打印出你的鼠标坐标了。
首先usb键盘驱动的源代码位于一下目录:
drivers/usb/input/usbkbd.c
将usb键盘驱动编译进内核:
#make menuconfig
Device Drivers--->USB support---->USB HIDBP Keyboard (simple Boot) support
(注意:有可能默认设置USB键盘驱动是不可见的,需修改当前目录下的Kconfig文件,在此不做详细介绍,Kconfig语法有待进一步熟悉:))
保存设置后,重新编译内核:
#source setenv
#make uImage
uImage生成后位于目录:arch/arm/boot/uImage;
(或者直接将usb键盘驱动编译为驱动模块,进行加载也可);
启动系统后,确定usb键盘加载到了那个设备文件,一般为/dev/input/event0设备,可通过cat命令进行确认:
#cat /dev/input/event0
操作usb键盘,会有乱码出现;
然后应用层用这个程序来获取usb键盘的输入:
#include
#include
#include
#include
struct input_event buff;
int fd;
int read_nu;
int main(int argc, char *argv[])
{
fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0)
{
perror("can not open device usbkeyboard!");
exit(1);
}
int i = 0;
printf("--fd:%d--\n",fd);
while(1)
{
while(read(fd,&buff,sizeof(struct input_event))==0)
{
;
}
//if(buff.code > 40)
printf("type:%d code:%d value:%d\n",buff.type,buff.code,buff.value);
//#if 0
//i++;
//if(i > 12)
//{
//break;
//}
//#endif
}
close(fd);
return 1;
}
运行程序后,按下A键,可见如下输出:
--fd:3--
type:1 code:30 value:1
type:0 code:0 value:0
参考2.6.14版本中的driver/usb/input/usbmouse.c。鼠标驱动可分为几个部分:驱动加载部分、probe部分、open部分、urb回调函数处理部分。
下文阴影部分为注解。
一、驱动加载部分
static int __init usb_mouse_init(void)
{
int retval = usb_register(&usb_mouse_driver);//注册鼠标驱动
if (retval == 0)
info(DRIVER_VERSION ":" DRIVER_DESC);
return retval;
}
其中usb_mouse_driver的定义为:
static struct usb_driver usb_mouse_driver = {
.owner = THIS_MODULE,
.name = "usbmouse",
.probe = usb_mouse_probe,
.disconnect = usb_mouse_disconnect,
.id_table = usb_mouse_id_table,
};
如果注册成功的话,将会调用usb_mouse_probe。那么什么时候才算注册成功呢?
和其它驱动注册过程一样,只有在其对应的“总线”上发现匹配的“设备”才会调用probe。总线匹配的方法和具体总线相关,如:platform_bus_type中是判断驱动名称和平台设备名称是否相同;那如何确认usb总线的匹配方法呢?
Usb设备是注册在usb_bus_type总线下的。查看usb_bus_type的匹配方法。
struct bus_type usb_bus_type = {
.name = "usb",
.match = usb_device_match,
.hotplug = usb_hotplug,
.suspend = usb_generic_suspend,
.resume = usb_generic_resume,
};
其中usb_device_match定义了匹配方法
static int usb_device_match (struct device *dev, struct device_driver *drv)
{
struct usb_interface *intf;
struct usb_driver *usb_drv;
const struct usb_device_id *id;
if (drv == &usb_generic_driver)
return 0;
intf = to_usb_interface(dev);
usb_drv = to_usb_driver(drv);
id = usb_match_id (intf, usb_drv->id_table);
if (id)
return 1;
return 0;
}
可以看出usb的匹配方法是usb_match_id (intf, usb_drv->id_table),也就是说通过比对“dev中intf信息”和“usb_drv->id_table信息”,如果匹配则说明驱动所对应的设备已经添加到总线上了,所以接下了就会调用drv中的probe方法注册usb设备驱动。
usb_mouse_id_table的定义为:
static struct usb_device_id usb_mouse_id_table[] = {
{ USB_INTERFACE_INFO(3, 1, 2) },
{ }
};
#define USB_INTERFACE_INFO(cl,sc,pr) /
.match_flags = USB_DEVICE_ID_MATCH_INT_INFO, /
.bInterfaceClass = (cl), /
.bInterfaceSubClass = (sc), /
.bInterfaceProtocol = (pr)
鼠标设备遵循USB人机接口设备(HID),在HID规范中规定鼠标接口类码为:
接口类:0x03
接口子类:0x01
接口协议:0x02
这样分类的好处是设备厂商可以直接利用标准的驱动程序。除了HID类以外还有Mass storage、printer、audio等
#define USB_DEVICE_ID_MATCH_INT_INFO /
(USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS | USB_DEVICE_ID_MATCH_INT_PROTOCOL)
匹配的过程为:
usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_host_interface *intf;
struct usb_device *dev;
if (id == NULL)
return NULL;
intf = interface->cur_altsetting;
dev = interface_to_usbdev(interface);
for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
id->driver_info; id++) {
if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
continue;
if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
continue;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
(id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
continue;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
(id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
continue;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
(id->bDeviceClass != dev->descriptor.bDeviceClass))
continue;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
(id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
continue;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
(id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
continue;
//接口类
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
(id->bInterfaceClass != intf->desc.bInterfaceClass))
continue;
//接口子类
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
(id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
continue;
//遵循的协议
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
(id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
continue;
return id;
}
return NULL;
}
从中可以看出,只有当设备的接口类、接口子类、接口协议匹配鼠标驱动时鼠标驱动才会调用probe方法。
二、probe部分
static int usb_mouse_probe(struct usb_interface * intf, const struct usb_device_id * id)
{
struct usb_device * dev = interface_to_usbdev(intf);
struct usb_host_interface *interface;
struct usb_endpoint_descriptor *endpoint;
struct usb_mouse *mouse;
int pipe, maxp;
char path[64];
interface = intf->cur_altsetting;
if (interface->desc.bNumEndpoints != 1)
return -ENODEV;
endpoint = &interface->endpoint[0].desc;//端点0描述符,此处的0表示中断端点
if (!(endpoint->bEndpointAddress & 0x80))
return -ENODEV;
*#define USB_DIR_IN 0x80
*/
if ((endpoint->bmAttributes & 3) != 3)? //判断是否是中断类型
return -ENODEV;
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);//构造中断端点的输入pipe
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
if (is_out) {
WARN_ON(usb_pipein(pipe));
ep = udev->ep_out[epnum];
} else {
WARN_ON(usb_pipeout(pipe));
ep = udev->ep_in[epnum];
}
if (!ep)
return 0;
return le16_to_cpu(ep->desc.wMaxPacketSize);
}
*/
//返回对应端点能够传输的最大的数据包,鼠标的返回的最大数据包为4个字节,
第0个字节:bit 0、1、2、3、4分别代表左、右、中、SIDE、EXTRA键的按下情况
第1个字节:表示鼠标的水平位移
第2个字节:表示鼠标的垂直位移
第3个字节:REL_WHEEL位移
if (!(mouse = kmalloc(sizeof(struct usb_mouse), GFP_KERNEL)))
return -ENOMEM;
memset(mouse, 0, sizeof(struct usb_mouse));
mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
if (!mouse->data) {
kfree(mouse);
return -ENOMEM;
}
mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
if (!mouse->irq) {
usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
kfree(mouse);
return -ENODEV;
}
mouse->usbdev = dev;
mouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
//设置input系统响应按键和REL(相对结果)事件
mouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
mouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
mouse->dev.keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
mouse->dev.relbit[0] |= BIT(REL_WHEEL);
//设置input系统响应的码表及rel表
mouse->dev.private = mouse;
mouse->dev.open = usb_mouse_open;
mouse->dev.close = usb_mouse_close;
usb_make_path(dev, path, 64);
sprintf(mouse->phys, "%s/input0", path);
mouse->dev.name = mouse->name;
mouse->dev.phys = mouse->phys;
usb_to_input_id(dev, &mouse->dev.id);
mouse->dev.dev = &intf->dev;
if (dev->manufacturer)
strcat(mouse->name, dev->manufacturer);
if (dev->product)
sprintf(mouse->name, "%s %s", mouse->name, dev->product);
if (!strlen(mouse->name))
sprintf(mouse->name, "USB HIDBP Mouse x:x",
mouse->dev.id.vendor, mouse->dev.id.product);
usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
(maxp > 8 ? 8 : maxp),
usb_mouse_irq, mouse, endpoint->bInterval);
mouse->irq->transfer_dma = mouse->data_dma;
mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
input_register_device(&mouse->dev);
//向input系统注册input设备
printk(KERN_INFO "input: %s on %s/n", mouse->name, path);
usb_set_intfdata(intf, mouse);
return 0;
}
三、open部分
当应用层打开鼠标设备时,usb_mouse_open将被调用
static int usb_mouse_open(struct input_dev *dev)
{
struct usb_mouse *mouse = dev->private;
mouse->irq->dev = mouse->usbdev;
if (usb_submit_urb(mouse->irq, GFP_KERNEL))
return -EIO;
//向usb core递交了在probe中构建好的中断urb,注意:此处是成功递交给usb core以后就返回,而不是等到从设备取得鼠标数据。
return 0;
}
四、urb回调函数处理部分
当出现传输错误或获取到鼠标数据后,urb回调函数将被执行
static void usb_mouse_irq(struct urb *urb, struct pt_regs *regs)
{
struct usb_mouse *mouse = urb->context;
//在usb_fill_int_urb中有对urb->context赋值
signed char *data = mouse->data;
struct input_dev *dev = &mouse->dev;
int status;
switch (urb->status) {
case 0:
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
return;
default:
goto resubmit;
}
input_regs(dev, regs);
input_report_key(dev, BTN_LEFT, data[0] & 0x01);
input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
input_report_key(dev, BTN_SIDE, data[0] & 0x08);
input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
//向input系统报告key事件,分别是鼠标LEFT、RIGHT、MIDDLE、SIDE、EXTRA键,
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)中的value非0时表示按下,0表示释放
input_report_rel(dev, REL_X, data[1]);
input_report_rel(dev, REL_Y, data[2]);
input_report_rel(dev, REL_WHEEL, data[3]);
//向input系统报告rel事件,分别是x方向位移、y方向位移、wheel值
input_sync(dev);
//最后需要向事件接受者发送一个完整的报告。这是input系统的要求。
resubmit:
status = usb_submit_urb (urb, SLAB_ATOMIC);
//重新递交urb
if (status)
err ("can't resubmit intr, %s-%s/input0, status %d",
mouse->usbdev->bus->bus_name,
mouse->usbdev->devpath, status);
}
五、应用层测试代码编写
在应用层编写测试鼠标的测试程序,在我的系统中,鼠标设备为/dev/input/event3. 测试代码如下:
#include
#include
#include
#include
#include
int main (void)
{
int fd,i,count;
struct input_event ev_mouse[2];
fd = open ("/dev/input/event3",O_RDWR);
if (fd < 0) {
printf ("fd open failed/n");
exit(0);
}
printf ("/nmouse opened, fd=%d/n",fd);
while(1)
{
printf(".............................................../n");
count=read(fd, ev_mouse, sizeof(struct input_event));
for(i=0;i<(int)count/sizeof(struct input_event);i++)
{
printf("type=%d/n",ev_mouse[i].type);
if(EV_REL==ev_mouse[i].type)
{
printf("time:%ld.%d",ev_mouse[i].time.tv_sec,ev_mouse[i].time.tv_usec);
printf(" type:%d code:%d value:%d/n",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
}
if(EV_KEY==ev_mouse[i].type)
{
printf("time:%ld.%d",ev_mouse[i].time.tv_sec,ev_mouse[i].time.tv_usec);
printf(" type:%d code:%d value:%d/n",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
}
}
}
close (fd);
return 0;
}
运行结果如下:
根据type、code、value的值,可以判断出鼠标的状态,具体值参考include/linux/input.h
阅读(4160) | 评论(0) | 转发(0) |