usb鼠標(biāo)驅(qū)動(dòng)注解及測(cè)試
鼠標(biāo)驅(qū)動(dòng)可分為幾個(gè)部分:驅(qū)動(dòng)加載部分、probe部分、open部分、urb回調(diào)函數(shù)處理部分。
下文陰影部分為注解。
一、驅(qū)動(dòng)加載部分
static int __init usb_mouse_init(void)
{
int retval = usb_register(usb_mouse_driver);//注冊(cè)鼠標(biāo)驅(qū)動(dòng)
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,
};
如果注冊(cè)成功的話,將會(huì)調(diào)用usb_mouse_probe。那么什么時(shí)候才算注冊(cè)成功呢?
和其它驅(qū)動(dòng)注冊(cè)過(guò)程一樣,只有在其對(duì)應(yīng)的“總線”上發(fā)現(xiàn)匹配的“設(shè)備”才會(huì)調(diào)用probe??偩€匹配的方法和具體總線相關(guān),如:platform_bus_type中是判斷驅(qū)動(dòng)名稱和平臺(tái)設(shè)備名稱是否相同;那如何確認(rèn)usb總線的匹配方法呢?
Usb設(shè)備是注冊(cè)在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;
/* check for generic driver, which we don't match any device with */
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),也就是說(shuō)通過(guò)比對(duì)“dev中intf信息”和“usb_drv->id_table信息”,如果匹配則說(shuō)明驅(qū)動(dòng)所對(duì)應(yīng)的設(shè)備已經(jīng)添加到總線上了,所以接下了就會(huì)調(diào)用drv中的probe方法注冊(cè)u(píng)sb設(shè)備驅(qū)動(dòng)。
usb_mouse_id_table的定義為:
static struct usb_device_id usb_mouse_id_table[] = {
{ USB_INTERFACE_INFO(3, 1, 2) },
{ } /* Terminating entry */
};
#define USB_INTERFACE_INFO(cl,sc,pr)
.match_flags = USB_DEVICE_ID_MATCH_INT_INFO,
.bInterfaceClass = (cl),
.bInterfaceSubClass = (sc),
.bInterfaceProtocol = (pr)
鼠標(biāo)設(shè)備遵循USB人機(jī)接口設(shè)備(HID),在HID規(guī)范中規(guī)定鼠標(biāo)接口類碼為:
接口類:0x03
接口子類:0x01
接口協(xié)議:0x02
這樣分類的好處是設(shè)備廠商可以直接利用標(biāo)準(zhǔn)的驅(qū)動(dòng)程序。除了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)
匹配的過(guò)程為:
usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_host_interface *intf;
struct usb_device *dev;
/* proc_connectinfo in devio.c may call us with id == NULL. */
if (id == NULL)
return NULL;
intf = interface->cur_altsetting;
dev = interface_to_usbdev(interface);
/* It is important to check that id->driver_info is nonzero,
since an entry that is all zeroes except for a nonzero
id->driver_info is the way to create an entry that
indicates that the driver want to examine every
device and 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;
/* No need to test id->bcdDevice_lo != 0, since 0 is never greater than any unsigned number. */
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;
//遵循的協(xié)議
if ((id->match_flags USB_DEVICE_ID_MATCH_INT_PROTOCOL)
(id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
continue;
return id;
}
return NULL;
}
從中可以看出,只有當(dāng)設(shè)備的接口類、接口子類、接口協(xié)議匹配鼠標(biāo)驅(qū)動(dòng)時(shí)鼠標(biāo)驅(qū)動(dòng)才會(huì)調(diào)用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;
/* 以下是網(wǎng)絡(luò)的一段對(duì)cur_altsettin的解釋,下面就借花獻(xiàn)佛。usb 設(shè)備有一個(gè)configuration 的概念,表示配置,一個(gè)設(shè)備可以有多個(gè)配置,但只能同時(shí)激活一個(gè),如:一些設(shè)備可以下載固件,或可以設(shè)置不同的全局模式,就像手機(jī)可以被設(shè)定為靜音模式或響鈴模式一樣。而這里又有一個(gè)setting,咋一看有些奇怪,這兩個(gè)詞不是一回事嗎.還是拿我們最熟悉的手機(jī)來(lái)打比方,configuration 不說(shuō)了,setting,一個(gè)手機(jī)可能各種配置都確定了,是振動(dòng)還是鈴聲已經(jīng)確定了,各種功能都確定了,但是聲音的大小還可以變吧,通常手機(jī)的音量是一格一格的變動(dòng),大概也就5,6 格,那么這個(gè)可以算一個(gè)setting 吧.這里cur_altsetting 就是表示的當(dāng)前的這個(gè)setting,或者說(shuō)設(shè)置??梢圆榭丛a中usb_interface 結(jié)構(gòu)定義的說(shuō)明部分。從說(shuō)明中可以看到一個(gè)接口可以有多種setting*/
if (interface->desc.bNumEndpoints != 1)
return -ENODEV;
/*根據(jù)HID規(guī)則,期望鼠標(biāo)只有一個(gè)端點(diǎn)即中斷端點(diǎn)bNumEndpoints 就是接口描述符中的成員,表示這個(gè)接口有多少個(gè)端點(diǎn),不過(guò)這其中不包括0 號(hào)端點(diǎn),0號(hào)端點(diǎn)是任何一個(gè)usb 設(shè)備都必須是提供的,這個(gè)端點(diǎn)專門用于進(jìn)行控制傳輸,即它是一個(gè)控制端點(diǎn).正因?yàn)槿绱?所以即使一個(gè)設(shè)備沒有進(jìn)行任何設(shè)置,usb 主機(jī)也可以開始跟它進(jìn)行一些通信,因?yàn)榧词共恢榔渌亩它c(diǎn),但至少知道它一定有一個(gè)0號(hào)端點(diǎn),或者說(shuō)一個(gè)控制端點(diǎn)。
*/
endpoint = interface->endpoint[0].desc;//端點(diǎn)0描述符,此處的0表示中斷端點(diǎn)
if (!(endpoint->bEndpointAddress 0x80))
return -ENODEV;
/*先看bEndpointAddress,這個(gè)struct usb_endpoint_descriptor 中的一個(gè)成員,是8個(gè)bit,或者說(shuō)1 個(gè)byte,其中bit7 表示的是這個(gè)端點(diǎn)的方向,0 表示OUT,1 表示IN,OUT 與IN 是對(duì)主機(jī)而言。OUT 就是從主機(jī)到設(shè)備,IN 就是從設(shè)備到主機(jī)。而宏*USB_DIR_IN 來(lái)自
*include/linux/usb_ch9.h
* USB directions
* This bit flag is used in endpoint descriptors' bEndpointAddress field.
* It's also one of three fields in control requests bRequestType.
*#define USB_DIR_OUT 0 /* to device */
*#define USB_DIR_IN 0x80 /* to host */
*/
if ((endpoint->bmAttributes 3) != 3)? //判斷是否是中斷類型
return -ENODEV;
/* bmAttributes 表示屬性,總共8位,其中bit1和bit0 共同稱為Transfer Type,即傳輸類型,即00 表示控制,01 表示等時(shí),10 表示批量,11 表示中斷*/
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);//構(gòu)造中斷端點(diǎn)的輸入pipe
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
/*跟蹤usb_maxpacket
usb_maxpacket(struct usb_device *udev, int pipe, int is_out)
{
struct usb_host_endpoint *ep;
unsigned epnum = usb_pipeendpoint(pipe);
/*
得到的自然就是原來(lái)pipe 里邊的15 至18 位.一個(gè)pipe 的15 位至18 位是endpoint 號(hào),(一共16 個(gè)endpoint,)所以很顯然,這里就是得到endpoint 號(hào)
*/
if (is_out) {
WARN_ON(usb_pipein(pipe));
ep = udev->ep_out[epnum];
} else {
WARN_ON(usb_pipeout(pipe));
}
if (!ep)
return 0;
/* NOTE:? only 0x07ff bits are for packet size... */
return le16_to_cpu(ep->desc.wMaxPacketSize);
}
*/
//返回對(duì)應(yīng)端點(diǎn)能夠傳輸?shù)淖畲蟮臄?shù)據(jù)包,鼠標(biāo)的返回的最大數(shù)據(jù)包為4個(gè)字節(jié),
第0個(gè)字節(jié):bit 0、1、2、3、4分別代表左、右、中、SIDE、EXTRA鍵的按下情況
第1個(gè)字節(jié):表示鼠標(biāo)的水平位移
第2個(gè)字節(jié):表示鼠標(biāo)的垂直位移
第3個(gè)字節(jié):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);
/*
申請(qǐng)用于urb用于數(shù)據(jù)傳輸?shù)?U>內(nèi)存,注意:這里將返回“mouse->data”和“mouse->data_dma”
mouse->data:記錄了用于普通傳輸用的內(nèi)存指針
mouse->data_dma:記錄了用于DMA傳輸?shù)膬?nèi)存指針
如果是DMA 方式的傳輸,那么usb core 就應(yīng)該使用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);
//設(shè)置input系統(tǒng)響應(yīng)按鍵和REL(相對(duì)結(jié)果)事件
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);
//設(shè)置input系統(tǒng)響應(yīng)的碼表及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);
/*
usb_to_input_id(const struct usb_device *dev, struct input_id *id)
{
id->bustype = BUS_USB;
id->vendor = le16_to_cpu(dev->descriptor.idVendor);
id->product = le16_to_cpu(dev->descriptor.idProduct);
id->version = le16_to_cpu(dev->descriptor.bcdDevice);
}
struct usb_device 中有一個(gè)成員struct usb_device_descriptor,而struct usb_device_descriptor 中的成員__u16 bcdDevice,表示的是制造商指定的產(chǎn)品的版本號(hào),制造商id 和產(chǎn)品id 來(lái)標(biāo)志一個(gè)設(shè)備.bcdDevice 一共16 位,是以bcd碼的方式保存的信息,也就是說(shuō),每4 位代表一個(gè)十進(jìn)制的數(shù),比如0011 0110 1001 0111 就代表的3697.
業(yè)內(nèi)為每家公司編一個(gè)號(hào),這樣便于管理,比如三星的編號(hào)就是0x0839,那么三星的產(chǎn)品中就會(huì)在其設(shè)備描述符中idVendor 的烙上0x0839.而三星自己的每種產(chǎn)品也會(huì)有個(gè)編號(hào),和Digimax 410 對(duì)應(yīng)的編號(hào)就是0x000a,而bcdDevice_lo 和bcdDevice_hi 共同組成一個(gè)具體設(shè)備的編號(hào)(device release
number),bcd 就意味著這個(gè)編號(hào)是二進(jìn)制的格式.
*/
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);
/*
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,
void *context,
int interval)
{
spin_lock_init(urb->lock);
urb->dev = dev;
urb->pipe = pipe;
urb->transfer_buffer = transfer_buffer;//如果不使用DMA傳輸方式,則使用這個(gè)緩沖指針。如何用DMA則使用transfer_DMA,這個(gè)值會(huì)在后面單獨(dú)給URB賦
urb->transfer_buffer_length = buffer_length;
urb->complete = complete;
urb->context = context;
if (dev->speed == USB_SPEED_HIGH)
urb->interval = 1 (interval - 1);
else
urb->interval = interval;
urb->start_frame. = -1;
}
此處只是構(gòu)建好一個(gè)urb,在open方法中會(huì)實(shí)現(xiàn)向usb core遞交urb
*/
mouse->irq->transfer_dma = mouse->data_dma;
mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
/*
#define URB_NO_TRANSFER_DMA_MAP 0x0004? //urb->transfer_dma valid on submit
#define URB_NO_SETUP_DMA_MAP??? 0x0008? //urb->setup_dma valid on submit
, 這里是兩個(gè)DMA 相關(guān)的flag,一個(gè)是URB_NO_SETUP_DMA_MAP,而另一個(gè)是
URB_NO_TRANSFER_DMA_MAP.注意這兩個(gè)是不一樣的,前一個(gè)是專門為控制傳輸準(zhǔn)備的,因?yàn)橹挥锌刂苽鬏斝枰羞@么一個(gè)setup 階段需要準(zhǔn)備一個(gè)setup packet。
transfer_buffer 是給各種傳輸方式中真正用來(lái)數(shù)據(jù)傳輸?shù)?而setup_packet 僅僅是在控制傳輸中發(fā)送setup 的包,控制傳輸除了setup 階段之外,也會(huì)有數(shù)據(jù)傳輸階段,這一階段要傳輸數(shù)據(jù)還是得靠transfer_buffer,而如果使用dma 方式,那么就是使用transfer_dma.
因?yàn)檫@里使用了mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP,所以應(yīng)該給urb的transfer_dma賦值。所以用了:
mouse->irq->transfer_dma = mouse->data_dma;
*/
input_register_device(mouse->dev);
//向input系統(tǒng)注冊(cè)input設(shè)備
printk(KERN_INFO "input: %s on %s", mouse->name, path);
usb_set_intfdata(intf, mouse);
/*
usb_set_intfdata().的結(jié)果就是使得
%intf->dev->driver_data= mouse,而其它函數(shù)中會(huì)調(diào)用usb_get_intfdata(intf)的作用就是把mouse從中取出來(lái)
*/
return 0;
}
三、open部分
當(dāng)應(yīng)用層打開鼠標(biāo)設(shè)備時(shí),usb_mouse_open將被調(diào)用
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中構(gòu)建好的中斷urb,注意:此處是成功遞交給usb core以后就返回,而不是等到從設(shè)備取得鼠標(biāo)數(shù)據(jù)。
return 0;
}
四、urb回調(diào)函數(shù)處理部分
當(dāng)出現(xiàn)傳輸錯(cuò)誤或獲取到鼠標(biāo)數(shù)據(jù)后,urb回調(diào)函數(shù)將被執(zhí)行
static void usb_mouse_irq(struct urb *urb, struct pt_regs *regs)
{
struct usb_mouse *mouse = urb->context;
//在usb_fill_int_urb中有對(duì)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;
}
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系統(tǒng)報(bào)告key事件,分別是鼠標(biāo)LEFT、RIGHT、MIDDLE、SIDE、EXTRA鍵,
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)中的value非0時(shí)表示按下,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系統(tǒng)報(bào)告rel事件,分別是x方向位移、y方向位移、wheel值
input_sync(dev);
//最后需要向事件接受者發(fā)送一個(gè)完整的報(bào)告。這是input系統(tǒng)的要求。
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);
}
五、應(yīng)用層測(cè)試代碼編寫
在應(yīng)用層編寫測(cè)試鼠標(biāo)的測(cè)試程序,在我的系統(tǒng)中,鼠標(biāo)設(shè)備為/dev/input/event3. 測(cè)試代碼如下:
/*
* usb_mouse_test.c
*by lht
*/
#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");
exit(0);
}
printf ("mouse opened, fd=%d",fd);
while(1)
{
printf("...............................................");
count=read(fd, ev_mouse, sizeof(struct input_event));
for(i=0;i(int)count/sizeof(struct input_event);i++)
{
printf("type=%d",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",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",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
}
}
}
close (fd);
return 0;
}
運(yùn)行結(jié)果如下:
根據(jù)type、code、value的值,可以判斷出鼠標(biāo)的狀態(tài),具體值參考include/linux/input.h
評(píng)論