usb驱动程序实现

1、代码如下



/*

 * drivers\hid\usbhid\usbmouse.c

 */



#include 

#include 

#include 

#include 

#include 

#include 





/* USB Device ID */

#define USB_VENDOR_ID_AIC                0xA69C



#ifndef CONFIG_USB_BT

#define USB_PRODUCT_ID_AIC               0x8800

#else

#define USB_PRODUCT_ID_AIC               0x8801

#endif



static struct usb_device_id aicwf_usb_id_table[] = {

	//{USB_DEVICE(0x8801,0x8800)},

	//{ }	/* Terminating entry */



#ifndef CONFIG_USB_BT

    {USB_DEVICE(USB_VENDOR_ID_AIC, USB_PRODUCT_ID_AIC)},

#else

    {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_AIC, USB_PRODUCT_ID_AIC, 0xff, 0xff, 0xff)},

#endif

    {}

};





static int usbmouse_as_key_probe(struct usb_interface *intf, const struct usb_device_id *id)

{

	struct usb_device *dev = interface_to_usbdev(intf);



	printk("found usbmouse!\n");



	printk("bcdUSB = %x\n", dev->descriptor.bcdUSB);

	printk("VID    = 0x%x\n", dev->descriptor.idVendor);

	printk("PID    = 0x%x\n", dev->descriptor.idProduct);

	

	return 0;

}



static void usbmouse_as_key_disconnect(struct usb_interface *intf)

{

	printk("disconnect usbmouse!\n");

}



/* 1. 分配/设置usb_driver */

static struct usb_driver usbmouse_as_key_driver = {

	.name		= "usbmouse_as_key_",

	.probe		= usbmouse_as_key_probe,

	.disconnect	= usbmouse_as_key_disconnect,

	.id_table	= aicwf_usb_id_table,

};





static int usbmouse_as_key_init(void)

{

	/* 2. 注册 */

	usb_register(&usbmouse_as_key_driver);

	return 0;

}



static void usbmouse_as_key_exit(void)

{

	usb_deregister(&usbmouse_as_key_driver);	

}



module_init(usbmouse_as_key_init);

module_exit(usbmouse_as_key_exit);



MODULE_LICENSE("GPL");

2、insmod 加载usbmouse_as_key.ko

dmesg 信息说明加载成功

[ 3928.470862] found usbmouse!
[ 3928.470888] bcdUSB = 200
[ 3928.470909] VID    = 0xa69c
[ 3928.470926] PID    = 0x8800

[ 3928.471201] kobject: 'drivers' (88a095c0): kobject_add_internal: parent: 'usbmouse_as_key', set: ''
[ 3928.471372] kobject: 'usbmouse_as_key_' (887cfe80): kobject_uevent_env
[ 3928.471433] kobject: 'usbmouse_as_key_' (887cfe80): fill_kobj_path: path = '/bus/usb/drivers/usbmouse_as_key_'
[ 3928.474580] usbcore: registered new interface driver usbmouse_as_key_

你可能感兴趣的:(Linux,内核)