Linux下的USB总线驱动(03)——USB鼠标驱动 usbmouse.c

USB鼠标驱动 usbmouse.c

原文链接:http://www.linuxidc.com/Linux/2012-12/76197p7.htm

drivers/hid/usbhid/usbmouse.c

下面我们分析下USB鼠标驱动,鼠标输入HID类型,其数据传输采用中断URB,鼠标端点类型为IN。我们先看看这个驱动的模块加载部分。

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驱动,我们跟踪看看被注册的usb_mouse_driver

static struct usb_driver usb_mouse_driver = {
	.name		= "usbmouse",           //驱动名
	.probe		= usb_mouse_probe,
	.disconnect	= usb_mouse_disconnect,
	.id_table	= usb_mouse_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 */
};

MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
再细细看看USB_INTERFACE_INFO宏的定义

/**
 * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces
 * @cl: bInterfaceClass value
 * @sc: bInterfaceSubClass value
 * @pr: bInterfaceProtocol value
 *
 * This macro is used to create a struct usb_device_id that matches a
 * specific class of interfaces.
 */
#define USB_INTERFACE_INFO(cl, sc, pr) \
	.match_flags = USB_DEVICE_ID_MATCH_INT_INFO, \
	.bInterfaceClass = (cl), \
	.bInterfaceSubClass = (sc), \
	.bInterfaceProtocol = (pr)

根据宏,我们知道,我们设置的支持项包括接口类,接口子类,接口协议三个匹配项。

主要看看usb_driver中定义的probe函数

static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *dev = interface_to_usbdev(intf);//由接口获取usb_dev
	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;                   //获取设置

	if (interface->desc.bNumEndpoints != 1)             //鼠标端点只有1个
		return -ENODEV;

	endpoint = &interface->endpoint[0].desc;            //获取端点描述符
	if (!usb_endpoint_is_int_in(endpoint))              //检查该端点是否是中断输入端点
		return -ENODEV;

	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);  //建立中断输入端点
	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));      //端点能传输的最大数据包(Mouse为4个)

	mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);   //分配usb_mouse结构体
	input_dev = input_allocate_device();                     //分配input设备空间
	if (!mouse || !input_dev)
		goto fail1;

	mouse->data = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &mouse->data_dma); //分配缓冲区
	if (!mouse->data)
		goto fail1;

	mouse->irq = usb_alloc_urb(0, GFP_KERNEL);                              //分配urb
	if (!mouse->irq)
		goto fail2;

	mouse->usbdev = dev;         //填充mouse的usb_device结构体
	mouse->dev = input_dev;      //填充mouse的input结构体

	if (dev->manufacturer)       //复制厂商ID
		strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));

	if (dev->product) {          //复制产品ID
		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)); //获取usb_mouse的设备节点

	input_dev->name = mouse->name;                        //将鼠标名赋给内嵌input结构体
	input_dev->phys = mouse->phys;                        //将鼠标设备节点名赋给内嵌input结构体
	usb_to_input_id(dev, &input_dev->id);                 //将usb_driver的支持项拷贝给input
	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);      //表明相对坐标包括X坐标和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);                           //将mouse设为input的私有数据

	input_dev->open = usb_mouse_open;                              //input设备的open操作函数
	input_dev->close = usb_mouse_close;

	usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
			 (maxp > 8 ? 8 : maxp),
			 usb_mouse_irq, mouse, endpoint->bInterval);   //填充urb
	mouse->irq->transfer_dma = mouse->data_dma;
	mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;         //使用transfer_dma

	error = input_register_device(mouse->dev);                     //注册input设备
	if (error)
		goto fail3;

	usb_set_intfdata(intf, mouse);
	return 0;

fail3:	
	usb_free_urb(mouse->irq);
fail2:	
	usb_free_coherent(dev, 8, mouse->data, mouse->data_dma);
fail1:	
	input_free_device(input_dev);
	kfree(mouse);
	return error;
}

在探讨probe实现的功能时,我们先看看urb填充函数usb_fill_int_urb

/**
 * usb_fill_int_urb - macro to help initialize a interrupt urb
 * @urb: pointer to the urb to initialize.
 * @dev: pointer to the struct usb_device for this urb.
 * @pipe: the endpoint pipe
 * @transfer_buffer: pointer to the transfer buffer
 * @buffer_length: length of the transfer buffer
 * @complete_fn: pointer to the usb_complete_t function
 * @context: what to set the urb context to.
 * @interval: what to set the urb interval to, encoded like
 *	the endpoint descriptor's bInterval value.
 *
 * Initializes a interrupt urb with the proper information needed to submit
 * it to a device.
 *
 * Note that High Speed and SuperSpeed interrupt endpoints use a logarithmic
 * encoding of the endpoint interval, and express polling intervals in
 * microframes (eight per millisecond) rather than in frames (one per
 * millisecond).
 *
 * Wireless USB also uses the logarithmic encoding, but specifies it in units of
 * 128us instead of 125us.  For Wireless USB devices, the interval is passed
 * through to the host controller, rather than being translated into microframe
 * units.
 */
static inline void usb_fill_int_urb(struct urb *urb,
				    struct usb_device *dev,
				    unsigned int pipe,
				    void *transfer_buffer,
				    int buffer_length,
				    usb_complete_t complete_fn,
				    void *context,
				    int interval)
{
	urb->dev = dev;
	urb->pipe = pipe;
	urb->transfer_buffer = transfer_buffer;
	urb->transfer_buffer_length = buffer_length;
	urb->complete = complete_fn;
	urb->context = context;
	if (dev->speed == USB_SPEED_HIGH || dev->speed == USB_SPEED_SUPER)
		urb->interval = 1 << (interval - 1);
	else
		urb->interval = interval;
	urb->start_frame = -1;
}
其实probe主要是初始化usb设备和input设备,终极目标是为了完成urb的提交和input设备的注册。由于注册为input设备类型,那么当用户层open打开设备时候,最终会调用input中的open实现打开,我们看看input中open的实现

static int usb_mouse_open(struct input_dev *dev)
{
	struct usb_mouse *mouse = input_get_drvdata(dev);   //获取私有数据

	mouse->irq->dev = mouse->usbdev;                    //获取utb指针
	if (usb_submit_urb(mouse->irq, GFP_KERNEL))         //提交urb
		return -EIO;

	return 0;
}
当用户层open打开这个USB鼠标后,我们就已经将urb提交给了USB core,那么根据USB数据处理流程知道,当处理完毕后,USB core会通知USB设备驱动程序,这里我们是响应中断服务程序,这就相当于该URB的回调函数。我们在提交urb时候定义了中断服务程序 usb_mouse_irq,我们跟踪看看

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;                             //数据处理没成功,重新提交urb
	}

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

根据上面的中断服务程序,我们应该知道,系统是周期性地获取鼠标的事件信息,因此在URB回调函数的末尾再次提交URB请求块,这样又会调用新的回调函数,周而复始。在回调函数中提交URB只能是GFP_ATOMIC优先级,因为URB回调函数运行于中断上下文中禁止导致睡眠的行为。而在提交URB 过程中可能会需要申请内存、保持信号量,这些操作或许会导致USB内核睡眠。

最后我们再看看这个驱动的私有数据mouse的定义

struct usb_mouse {
	char name[128];             //名字
	char phys[64];              //设备节点
	struct usb_device *usbdev;  //内嵌usb_device设备
	struct input_dev *dev;      //内嵌input_dev设备
	struct urb *irq;            //urb结构体

	signed char *data;          //transfer_buffer缓冲区
	dma_addr_t data_dma;        //transfer _dma缓冲区
};

在上面这个结构体中,每一个成员的作用都应该很清楚了,尤其最后两个的使用区别和作用,前面也已经说过。

如果最终需要测试这个USB鼠标驱动,需要在内核中配置USB支持、对HID接口的支持、对OHCI HCD驱动的支持。另外,将驱动移植到开发板之后,由于采用的是input设备模型,所以还需要开发板带LCD屏才能测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值