嵌入式linux驱动-usb实现按键功能笔记

一、开发环境

1、内核:Linux 2.6.22.6;

2、JZ2440v3

3、ubuntu 9.10

二、过程


三、程序

#include 
#include 
#include 
#include 
#include 
#include 

static struct input_dev *usb_key_dev;
static char *usb_buff;
static dma_addr_t usb_buff_phys;
static int len;
static struct urb *usb_key_urb;

static void usbmouse_as_key_irq(struct urb *urb)
{
	int i;
	static int cnt;
	static unsigned char pre_val;

	/*

	printk("data cnt = %d ",++cnt);
	for(i=0;idescriptor.bcdUSB);
	printk("VID = %x \n",dev->descriptor.idVendor);
	printk("PID = %x \n",dev->descriptor.idProduct);*/

	interface = intf->cur_altsetting;
	endpoint = &interface->endpoint[0].desc;

	//1.分配一个input_dev 结构体
	usb_key_dev=input_allocate_device();

	//2.设置
		//2.1能长生那类事件
		set_bit(EV_KEY,usb_key_dev->evbit);
		set_bit(EV_REP,usb_key_dev->evbit);

		//2.2 能产生这类事件的那些事件
		set_bit(KEY_L,usb_key_dev->keybit);
		set_bit(KEY_S,usb_key_dev->keybit);
		set_bit(KEY_ENTER,usb_key_dev->keybit);

	//3.注册
	input_register_device(usb_key_dev);
	//4.硬件相关的操作
		//数据传输3要素:源、目的、长度
			//源:usb设备的某个端点
			pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
			//长度:
			len=endpoint->wMaxPacketSize;
			//目的:		
			 usb_buff = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &usb_buff_phys);
		//使用3要素
			//分配usb request block
			usb_key_urb=usb_alloc_urb(0, GFP_KERNEL);
			//使用3要素设置urb
			usb_fill_int_urb(usb_key_urb, dev, pipe, usb_buff,len,usbmouse_as_key_irq,NULL , endpoint->bInterval);
			usb_key_urb->transfer_dma = usb_buff_phys;
			usb_key_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
		//使用urb
		usb_submit_urb(usb_key_urb, GFP_KERNEL);

			

	return 0;
}
static void usbmouse_as_key_disconnect(struct usb_interface *intf)
{
	struct usb_device *dev = interface_to_usbdev(intf);
	printk("disconnect usb mouse \n");
	usb_kill_urb(usb_key_urb);
	usb_free_urb(usb_key_urb);
	usb_buffer_free(dev, len,usb_buff,usb_buff_phys);
	input_unregister_device(usb_key_dev);
	input_free_device(usb_key_dev);

}

static struct usb_device_id usbmouse_as_key_id_table [] = {
	{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
		USB_INTERFACE_PROTOCOL_MOUSE) },
	{ }	/* Terminating entry */
};



//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	= usbmouse_as_key_id_table,
};

static int  usbmouse_as_key_init(void)
{
	//2.注册usb_driver
	 usb_register(&usbmouse_as_key_driver);

	return 0;
}

static void  usbmouse_as_key_exit(void)
{
//卸载usb_driver
	usb_deregister(&usbmouse_as_key_driver);
}

module_init(usbmouse_as_key_init);
module_exit(usbmouse_as_key_exit);

MODULE_LICENSE("GPL");


你可能感兴趣的:(嵌入式linux驱动)