标签:
参考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;
-
if (!(endpoint->bEndpointAddress & 0x80))
-
return -ENODEV;
-
-
-
-
-
-
-
-
-
*#define USB_DIR_IN 0x80
-
*/
-
-
if ((endpoint->bmAttributes & 3) != 3)?
-
return -ENODEV;
-
-
-
-
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
-
-
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);
-
}
-
*/
-
-
第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);
-
-
-
-
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);
-
-
-
-
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 %04x:%04x",
-
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);
-
-
-
-
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;
-
-
-
-
return 0;
-
}
四、urb回调函数处理部分
当出现传输错误或获取到鼠标数据后,urb回调函数将被执行
-
static void usb_mouse_irq(struct urb *urb, struct pt_regs *regs)
-
{
-
struct usb_mouse *mouse = 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);
-
-
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_sync(dev);
-
-
resubmit:
-
status = usb_submit_urb (urb, SLAB_ATOMIC);
-
-
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 <stdio.h>
-
#include <sys/types.h>
-
#include <unistd.h>
-
#include <fcntl.h>
-
#include <linux/input.h>
-
-
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;
-
}
Linux USB 驱动开发实例(二)—— USB 鼠标驱动注解及测试
标签:
原文地址:http://blog.csdn.net/qq_21593899/article/details/51726712