📄 usbmouse.c.bak
字号:
//You know, usb device driver is according to the interface.
#include <linux/kernel.h>
#include <linux/slab.h> //kmalloc,kfree
#include <linux/module.h>
#inlcude <linux/usb.h>
#include <linux/init.h>
#define DEBUG
#define MOU_INTF_CL 3 //mouse interface class
#define MOU_INTF_SC 1 //interface sub class
#define MOU_INTF_PR 2 //interface protocal
struct usb_mouse{
signed char mouse_data[8];
char name[128];
struct usb_device * usbdev;
struct urb mouse_urb;
int open;
};
MODULE_AUTHOR("Haijian CAO");
MODULE_DESCRIPTION("USB HID Boot Protocol mouse driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("V1.0");
static struct usb_device_id usb_mouse_id_table[]={
{USB_INTERFACE_INFO(MOU_INTF_CL,MOU_INTF_SC,MOU_INTF_PR)},
{}
};
MODULE_DEVICE_TABLE(usb,usb_mouse_id_table); //Export the id_table
static struct usb_driver usb_mouse_driver={
name: "chj_usb_mouse",
probe: usb_mouse_probe,
disconnect: usb_mouse_disconnect,
id_table: usb_mouse_id_table,
};
/*************************************************************************
Probe function.
Return a struct usb_mouse pointer or a NULL that indicate error.
*************************************************************************/
static void * usb_mouse_probe(struct usb_device *udev,unsigned int ifnum,
const struct usb_device_id *id)
{
struct usb_interface *iface;
struct usb_interface_descriptor * iface_desc;
struct usb_endpoint_descriptor *enp_desc;
struct usb_mouse * mouse;
int pipe,maxp;
char * buf;
#ifdef DEBUG
printk(KERN_INFO "I have entered the probe function!\n");
return NULL;
#endif
iface =&udev->actconfig->interface[ifnum];
iface_desc=&iface->altsetting[iface->act_altsetting]; //get the active
//interface description.
if(iface_desc->bNumEndpoints!=1) return NULL; //There is only one enp.
enp_desc=iface_desc->endpoint; //get the endpoint
//Now make sure that the type of the endpoint is appropriate
if(!(endpoint->bEndpointAddress&
USB_ENDPOINT_DIR_MASK)) return NULL; //Is an IN endpoint
if((endpoint->bmAttributes&USB_ENDPOINT_XFER_MASK)!=
USB_ENDPOINT_XFER_INT) return NULL; //Is an Interrupt endpoint
//I don't know why not involving the idVendor 0x056a mouse
if(udev->descriptor.idVendor==0x056a) return NULL;
//Create a proper pipe(in,interrupt,low)
pipe=usb_rcvintpipe(udev,endpoint->bEndpointAddress);
//Get the value of the max_packet
maxp=usb_maxpacket(udev,pipe,usb_pipeout(pipe));
usb_set_idle(udev,iface_desc->bInterfaceNumber,0,0);
//Now fill the struct usb_mouse
if(!(mouse=kmalloc(sizeof(struct usb_mouse),GFP_KERNEL))) return NULL;
memset(mouse,0,sizeof(struct usb_mouse));
if(!(buf=kmalloc(63,GFP_KERNEL)))
{
kfree(mouse);
return NULL;
}
if(dev->descriptor.iManufacturer&&usb_string(dev,
dev->descriptor.iManufacturer,buf,63)>0)
strcat(mouse->name,buf);
if(dev->descriptor.iProduct&&usb_string(dev,
dev->descriptor.iProduct,buf,63)>0)
strcat(mouse->name,buf);
kfree(buf);
FILL_INT_URB(&mouse->mouse_urb,udev,pipe,mouse->data,maxp>8?8:maxp,
usb_mouse_irq,mouse,enp_desc->bInterval);
printk(KERN_INFO "Device: %s\nThe ifnum is: %d\n",mouse->name,ifnum);
if (usb_submit_urb(&mouse->mouse_urb)) return -EIO;
return mouse;
}
static void usb_mouse_irq(struct urb *urb)
{
signed char *data=urb->context->mouse_data;
int i;
if (urb->status) return; //some error
printk(KERN_INFO "Mouse Event!\nData=[");
for (i=0;i<=7;i++)
{
printk(KERN_INFO "0x%02x,",data[i]);
}
printk(KERN_INFO "]!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -