USB驱动——鼠标驱动程序(中断传输)

    本文以 usbmouse.c 为例,简单分析usb鼠标驱动程序。

static int __init usb_mouse_init(void)
{
	int retval = usb_register(&usb_mouse_driver);
	if (retval == 0)
		printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
				DRIVER_DESC "\n");
	return retval;
}
    使用 usb_register 注册到内核的驱动程序都是对应于一个usb设备的接口(逻辑设备)。

static struct usb_driver usb_mouse_driver = {
	.name		= "usbmouse",
	.probe		= usb_mouse_probe,
	.disconnect	= usb_mouse_disconnect,
	.id_table	= usb_mouse_id_table,
};
    id_table 中指明了这个驱动程序支持哪些类型的接口

static struct usb_device_id usb_mouse_id_table [] = {
	{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
		USB_INTERFACE_PROTOCOL_MOUSE) },
	{ }	/* Terminating entry */
};
    USB_INTERFACE_INFO ,通过接口信息来匹配,内核里还有一些其它的宏,例如 USB_DEVICE ,使用它可以根据厂商ID、设备ID来匹配,让你的驱动程序仅仅支持某一个厂家的某一款设备。还有一些其它的匹配方法不再赘述。

    匹配成功之后,便会调用 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;
	
	/* 输入设备 */
	struct input_dev *input_dev;
	int pipe, maxp;
	int error = -ENOMEM;
	
	/* 获取接口的当前设置 */
	interface = intf->cur_altsetting;
	
	/* 获得当前设置的第一个端点的描述符,usb鼠标除了端点0就这一个端点 */
	endpoint = &interface->endpoint[0].desc;

	/* 获得端点对应的管道 */
	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
	
	/* 返回这个端点的传输最大包大小 */
	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
	
	/* 输入子系统 */
	input_dev = input_allocate_device();
	
	/* 用一个 usb_mouse 结构来保存 urb buffer irq 等信息 */
	mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);	

	/* 分配空间 */
	mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma);
	
	/* 分配urb */
	mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
	
	mouse->usbdev = dev;
	mouse->dev = input_dev;
	
	/* 一些设备信息的拷贝,无关紧要 */
	if (dev->manufacturer)
		strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));

	if (dev->product) {
		if (dev->manufacturer)
			strlcat(mouse->name, " ", sizeof(mouse->name));
		strlcat(mouse->name, dev->product, sizeof(mouse->name));
	}

	if (!strlen(mouse->name))
		snprintf(mouse->name, sizeof(mouse->name),
			 "USB HIDBP Mouse %04x:%04x",
			 le16_to_cpu(dev->descriptor.idVendor),
			 le16_to_cpu(dev->descriptor.idProduct));

	usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
	strlcat(mouse->phys, "/input0", sizeof(mouse->phys));

	/* 输入子系统的一些设置 */
	input_dev->name = mouse->name;
	input_dev->phys = mouse->phys;
	usb_to_input_id(dev, &input_dev->id);
	input_dev->dev.parent = &intf->dev;
	
	/* 设置支持哪些类型的事件。以及那些事件 */
	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
	input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
		BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
	input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
	input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
		BIT_MASK(BTN_EXTRA);
	input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);

	input_set_drvdata(input_dev, mouse);
	
	/* 输入设备的Open close 函数,open函数中会提交urb */
	input_dev->open = usb_mouse_open;
	input_dev->close = usb_mouse_close;
	
	/* 填充中断传输类型的urb */
	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;
	
	/* 注册输入设备 */
	error = input_register_device(mouse->dev);

	usb_set_intfdata(intf, mouse);
	return 0;
}
static int usb_mouse_open(struct input_dev *dev)
{
	struct usb_mouse *mouse = input_get_drvdata(dev);

	mouse->irq->dev = mouse->usbdev;
	if (usb_submit_urb(mouse->irq, GFP_KERNEL))
		return -EIO;

	return 0;
}
    open 输入设备的时候,urb 就被提交了。每次鼠标有信息要提交给usb主机时,就会调用到 
static void usb_mouse_irq(struct urb *urb)
{
	struct usb_mouse *mouse = urb->context;
	signed char *data = mouse->data;
	struct input_dev *dev = mouse->dev;
	int status;

	switch (urb->status) {
	case 0:			/* success */
		break;
	case -ECONNRESET:	/* unlink */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	/* -EPIPE:  should clear the halt */
	default:		/* error */
		goto resubmit;
	}
        /* 根据data里的内容,上报不同事件 */
	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_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, GFP_ATOMIC);// 重新提交 urb
	if (status)
		err ("can't resubmit intr, %s-%s/input0, status %d",
				mouse->usbdev->bus->bus_name,
				mouse->usbdev->devpath, status);
}






已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页