usb设备驱动程序(一)

代码:

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

#include 


static struct usb_device_id myuvc_ids[] = {
	/* Generic USB Video Class */
		{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },/*vdieocontral interface*/
		{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 2, 0) },
		{}
};
static int myuvc_probe(struct usb_interface *intf,const struct usb_device_id *id)   //usb_interface->usb_host_interface
{
	int m,j
	struct usb_device *udev = interface_to_usbdev(intf); /*可在结构体usb_device获得设备描述符:struct usb_device_descriptor descriptor;*/
	struct usb_device_descriptor *descriptor = &udev->descriptor;
	/*获取IAD*/
	struct usb_interface_assoc_descriptor *assoc_desc;
	assoc_desc = udev->actconfig->intf_assoc[0];
	/*视频流接口中包括一个接口和与其对应的多个转换设置接口(Alternate Setting)。打印接口描述符*/
	struct usb_interface_descriptor	*interface;
	struct usb_endpoint_descriptor	*endpoint;
	for (j=0;jnum_altsetting;j++)
		{
			interface = &intf->altsetting[j].desc;
			/*打印端点描述符*/
			for (m=0; mbNumEndpoints;m++)
				{
					endpoint = intf->altsetting[j].endpoint[m].desc;
				}
			
		}
	/*打印uvc内自己定义的自定义描述符*/
	//static int uvc_parse_control(struct uvc_device *dev)
	//struct usb_host_interface *alts = dev->intf->cur_altsetting;
	//unsigned char *buffer = alts->extra;
	//int buflen = alts->extralen;
	
	unsigned char *buffer = intf->cur_altsetting->extra;/*每个buffer的第一个元素 表示这个buffer的长度*/
	int buflen = intf->cur_altsetting->extralen; /*extralen存的是buf的总长度*/
	printk("buflen %d\n",buflen);




	
	printk("bNumConfigurations %d\n",descriptor->bNumConfigurations);
	static int count;
	printk("connect %d\n",count++);
	return 0;
};
static void myuvc_disconnect(struct usb_interface *intf)
{
	static int count;
	printk("disconnect %d\n",count++);
};


struct usb_driver myuvc_driver = {
		.name		= "my_uvcvideo",
		.probe		= myuvc_probe,
		.disconnect	= myuvc_disconnect,
		//.suspend	= uvc_suspend,
		//.resume		= uvc_resume,
		//.reset_resume	= uvc_reset_resume,
		.id_table	= myuvc_ids,
		//.supports_autosuspend = 1,
};



static int  myuvc_init(void)
{
	int ret ;
	ret = usb_register(&myuvc_driver);
	if (ret < 0) {
		return ret;
	}
	return 0;
}

static void  myuvc_cleanup(void)
{
	usb_deregister(&myuvc_driver);
}

module_init(myuvc_init);
module_exit(myuvc_cleanup);
MODULE_LICENSE("GPL");














1.卸载原有的驱动

sudo rmmod uvcvideo  //有些没有就不用卸载
sudo insmod myuvc.ko

2.插拔usb设备,并用dmesg 查看信息

dmesg 命令主要用来显示内核信息。使用 dmesg 可以有效诊断机器硬件故障或者添加硬件出现的问题。另外,使用 dmesg 可以确定您的服务器安装了哪些硬件。每次系统重启,系统都会检查所有硬件并将信息记录下来。执行/bin/dmesg 命令可以查看该记录,开机信息亦保存在/var/log目录中,名称为dmesg的文件里。

3.输出

你可能感兴趣的:(linux)