2.USB鼠标驱动 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) },
{ }
};
再细细看看USB_INTERFACE_INFO宏的定义
#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_device
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); //建立中断输入端点
//返回端点能传输的最大的数据包,鼠标的返回的最大数据包为4个字节
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL); //分配mouse结构体
input_dev = input_allocate_device(); //分配input设备空间
if (!mouse || !input_dev)
goto fail1;
mouse->data = usb_buffer_alloc(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)) //拷贝产品ID
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结构体
input_dev->phys = mouse->phys; //将鼠标设备节点名赋给内嵌input结构体
usb_to_input_id(dev, &input_dev->id); //将usb_driver的支持项拷贝给input
input_dev->dev.parent = &intf->dev;
//evbit表明支持按键事件(EV_KEY)和相对坐标事件(EV_REL)
input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
//keybit表明按键值包括左键、右键和中键
input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
//relbit表明相对坐标事件值包括X坐标和Y坐标
input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
//keybit表明除了左键、右键和中键,还支持其他按键
input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
BIT_MASK(BTN_EXTRA);
//relbit表明除了X坐标和Y坐标,还支持中键滚轮的滚动值
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_buffer_free(dev, 8, mouse->data, mouse->data_dma);
fail1:
input_free_device(input_dev);
kfree(mouse);
return error;
}
其实上面这个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; //获取urb指针
if (usb_submit_urb(mouse->irq, GFP_KERNEL)) //提交urb
return -EIO;
return 0;
}
好了,当用户层open打开这个USB鼠标后,我们就已经将urb提交给了USB核心,那么根据USB数据处理流程知道,当处理完毕后,USB核心会通知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: //成功
break;
case -ECONNRESET: //未连接
case -ENOENT:
case -ESHUTDOWN:
return;
default:
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); //鼠标SIDE
input_report_key(dev, BTN_EXTRA, data[0] & 0x10); //鼠标EXTRA
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请求块,这样又会调用新的回调函数,周而复始。在回调函数中�