Linux 平台上的usb驱动开发,主要有内核驱动的开发和基于libusb的无驱设计。
- libusb 介绍
libusb 设计了一系列的外部API 为应用程序所调用,通过这些API应用程序可以操作硬件,从libusb的源代码可以看出,这些API 调用了内核的底层接口,和kernel driver中所用到的函数所实现的功能差不多,只是libusb更加接近USB 规范。使得libusb的使用也比开发内核驱动相对容易的多。
- 下载及安装测试
- 下载:http://sourceforge.net/projects/libusb/files/libusb-1.0/
- 查看libusb里面的INSTALL文件,了解配置安装过程:
- 到根目录下,输入`./configure'命令,配置系统。
- 输入`make' 编译文件
- 输入`make install' ,安装文件
- 编译/examples/listdevs.c文件。测试能否识别usb设备并列出设备。
- libusb 的外部接口介绍:
- 初始化设备接口:这些接口也可以称为核心函数,它们主要用来初始化并寻找相关设备。
- usb_init
函数定义: void usb_init(void);
从函数名称可以看出这个函数是用来初始化相关数据的,这个函数大家只要记住必须调用就行了,而且是一开始就要调用的.
- usb_find_busses
函数定义: int usb_find_busses(void);
寻找系统上的usb总线,任何usb设备都通过usb总线和计算机总线通信。进而和其他设备通信。此函数返回总线数。
- usb_find_devices
函数定义: int usb_find_devices(void);
寻找总线上的usb设备,这个函数必要在调用usb_find_busses()后使用。以上的三个函数都是一开始就要用到的,此函数返回设备数量。
- usb_get_busses
函数定义: struct usb_bus *usb_get_busses(void);
这个函数返回总线的列表,在高一些的版本中已经用不到了,这在下面的实例中会有讲解
- usb_open
函数定义: usb_dev_handle *usb_open(struct *usb_device dev);
打开要使用的设备,在对硬件进行操作前必须要调用usb_open 来打开设备,这里大家看到有两个结构体 usb_dev_handle 和 usb_device 是我们在开发中经常碰到的,有必要把它们的结构看一看。在libusb 中的usb.h和usbi.h中有定义。
这里我们不妨理解为返回的 usb_dev_handle 指针是指向设备的句柄,而行参里输入就是需要打开的设备。
- usb_close
函数定义: int usb_close(usb_dev_handle *dev);
与usb_open相对应,关闭设备,是必须调用的, 返回0成功,<0 失败。
- usb_set_configuration
函数定义: int usb_set_configuration(usb_dev_handle *dev, int configuration);
设置当前设备使用的configuration,参数configuration 是你要使用的configurtation descriptoes中的bConfigurationValue, 返回0成功,<0失败( 一个设备可能包含多个configuration,比如同时支持高速和低速的设备就有对应的两个configuration,详细可查看usb标准)
- usb_set_altinterface
函数定义: int usb_set_altinterface(usb_dev_handle *dev, int alternate);
和名字的意思一样,此函数设置当前设备配置的interface descriptor,参数alternate是指interface descriptor中的bAlternateSetting。返回0成功,<0失败
- usb_resetep
函数定义: int usb_resetep(usb_dev_handle *dev, unsigned int ep);
复位指定的endpoint,参数ep 是指bEndpointAddress,。这个函数不经常用,被下面介绍的usb_clear_halt函数所替代。
- usb_clear_halt
函数定义: int usb_clear_halt (usb_dev_handle *dev, unsigned int ep);
复位指定的endpoint,参数ep 是指bEndpointAddress。这个函数用来替代usb_resetep
- usb_reset
函数定义: int usb_reset(usb_dev_handle *dev);
这个函数现在基本不怎么用,不过这里我也讲一下,和名字所起的意思一样,这个函数reset设备,因为重启设备后还是要重新打开设备,所以用usb_close就已经可以满足要求了。
- usb_claim_interface
函数定义: int usb_claim_interface(usb_dev_handle *dev, int interface);
注册与操作系统通信的接口,这个函数必须被调用,因为只有注册接口,才能做相应的操作。Interface 指 bInterfaceNumber. (下面介绍的usb_release_interface 与之相对应,也是必须调用的函数)
- usb_release_interface
函数定义: int usb_release_interface(usb_dev_handle *dev, int interface);
注销被usb_claim_interface函数调用后的接口,释放资源,和usb_claim_interface对应使用。
- usb_control_msg
函数定义:int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
从默认的管道发送和接受控制数据
- usb_get_string
函数定义: int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, size_t buflen);
usb_get_string_simple
函数定义: int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen);
- usb_get_descriptor
函数定义: int usb_get_descriptor(usb_dev_handle *dev, unsigned char type, unsigned char index, void *buf, int size);
- usb_get_descriptor_by_endpoint
函数定义: int usb_get_descriptor_by_endpoint(usb_dev_handle *dev, int ep, unsigned char type, unsigned char index, void *buf, int size);
- usb_bulk_write
函数定义: int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);
- usb_interrupt_read
函数定义: int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);
- usb_bulk_write
函数定义: int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);
- usb_interrupt_read
函数定义: int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);
基本上libusb所经常用到的函数就有这些了,和usb协议确实很接近吧。下面我们实例在介绍一个应用。