libusb FAQ

Can libusb be used on the USB device side, e.g. Linux Gadget Device?

No.
libusb only provides an API for writing software on the host. Of course, if the device also acts as a USB host then libusb could still be useful, but only for the host part of the device.

https://github.com/libusb/libusb/wiki/FAQ#Can_libusb_be_used_on_the_USB_device_side_eg_Linux_Gadget_Device

libusb主要用于HOST端,device(gadget)端看起来支持度不行,device端可以通过HID节点进行读写

https://blog.csdn.net/drivermonkey/article/details/43635083

/*****************************************************************
* Author:		DriverMonkey
* Mail:		bookworepeng@Hotmail.com
* Phone:		18575593141
* QQ:		196568501
* Blog:		http://blog.csdn.net/drivermonkey
* Date:		02/07/2015
 *****************************************************************/
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
#define HID_MAX_PACKET_SIZE 64
#define HID_BUFFER_SIZE (10*1024*1024)// 10M bytes
 
typedef struct 
{
	char null_array[HID_MAX_PACKET_SIZE];
}buffer_offset_size_t;
 
 
static char hid_read_buffer[HID_BUFFER_SIZE];
static char hid_write_buffer[HID_BUFFER_SIZE];
 
static int hid_file_handle = 0;
static const char* hid_file_name = "/dev/hidg0";
 
static int hid_init(void);
static int hid_read(void* buffer, int buffer_size);
static int hid_write(void* buffer, int buffer_size);
 
/*****************************************************************
 * Function name: 			main
 * Author:				DriverMonkey
 * Function Description: 	main function just for hid temp testing
 * Input argv:
 * Output argv:
 * Return:
 * Be careful:
 *****************************************************************/
int main(void)
{
	int read_size = 0;
	int test_count = 100;
	hid_init();
 
	while(test_count--)
	{
		memset(hid_read_buffer, 0x00, HID_BUFFER_SIZE);
		read_size = hid_read(hid_read_buffer, HID_BUFFER_SIZE);
		//printf("getting report::%s \n", hid_read_buffer);
 
		memset(hid_write_buffer, 0x00, HID_BUFFER_SIZE);
		strcpy(hid_write_buffer, hid_read_buffer);
		hid_write(hid_write_buffer, read_size);
	}
	
	return 0;
}
/*****************************************************************
 * Function name: 			hid_init
 * Author:				DriverMonkey
 * Function Description: 	init HID
 * Input argv:
 * Output argv:
 * Return:				>= 0 - no error
 						< 0 - reading error
 * Be careful: Must be called befoore HID be used!
 *****************************************************************/
static int hid_init(void)
{
	if ((hid_file_handle = open(hid_file_name, O_RDWR, 0666)) < 0)
	{
		perror(hid_file_name);
		return hid_file_handle;
	}else
	{
		return hid_file_handle;
	}
}
 
/*****************************************************************
 * Function name: 			hid_read
 * Author:				DriverMonkey
 * Function Description: 	Read data form hid driver
 * Input argv:				buffer_size - buffer size 
 * Output argv:			buffer - buffer to save reading out data
 * Return:				>= 0 - read size
 						< 0 - reading error
 * Be careful: 
 *****************************************************************/
static int hid_read(void* buffer, int buffer_size)
{
	if(buffer == NULL)
	{
		perror("hid_read::pointer error!");
		return -1;
	}
	return read(hid_file_handle, buffer, buffer_size);
}
 
/*****************************************************************
 * Function name: 			hid_read
 * Author:				DriverMonkey
 * Function Description: 	Read data form hid driver
 * Input argv:				buffer_size - buffer size 
 * Output argv:			buffer - buffer to save reading out data
 * Return:				>= 0 - no error
 						< 0 - reading error
 * Be careful: 
 *****************************************************************/
static int hid_write(void* buffer, int buffer_size)
{
	int return_v = 0;
	int writting_count = buffer_size / HID_MAX_PACKET_SIZE;
	int remainding_size = buffer_size % HID_MAX_PACKET_SIZE;
	buffer_offset_size_t* buffer_offset = (buffer_offset_size_t*)buffer;
	
	if(buffer == NULL)
	{
		perror("hid_write::pointer error!");
		return -1;
	}
 
	while(writting_count--)
	{
		return_v = write(hid_file_handle, buffer_offset,HID_MAX_PACKET_SIZE);
		if(return_v < 0)
		{
			perror("hid_write::writting error!");
			return return_v;
		}
		buffer_offset++;
	}
 
	return_v = write(hid_file_handle, buffer_offset, remainding_size);
 
	return return_v;
}

 

 

07-23
### libusb 使用教程与文档概述 libusb 是一个用于访问和控制 USB 设备的跨平台库,支持多种操作系统,包括 Linux、Windows 和 macOS。它提供了一套统一的 API 接口,使得开发者可以编写与平台无关的 USB 通信程序。以下内容涵盖了 libusb 的使用方法、文档资源以及常见问题的解决方案。 #### 1. libusb 安装与配置 在 Linux 系统中,可以通过源码安装 libusb。通常需要下载 `libusb-1.0.9.tar.bz2` 和 `libusb-compat-0.1.4.tar.bz2`,其中后者用于兼容旧版本的 libusb 接口[^1]。安装步骤如下: ```bash tar -xjf libusb-1.0.9.tar.bz2 cd libusb-1.0.9 ./configure make sudo make install ``` 对于依赖管理,可以使用包管理器安装,例如在 Debian/Ubuntu 上: ```bash sudo apt install libusb-1.0-0 ``` 在 Windows 上,可以通过 vcpkg 或 MSYS2 安装 libusb。 #### 2. libusb API 使用示例 libusb 提供了丰富的 API 用于设备查找、配置和数据传输。以下是一个简单的示例代码,展示如何列出所有连接的 USB 设备: ```c #include <libusb-1.0/libusb.h> #include <stdio.h> int main() { libusb_context *ctx = NULL; libusb_device **devs; ssize_t cnt; libusb_init(&ctx); cnt = libusb_get_device_list(ctx, &devs); for (ssize_t i = 0; i < cnt; i++) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(devs[i], &desc); if (r < 0) { fprintf(stderr, "Failed to get device descriptor\n"); continue; } printf("Device %d: ID %04x:%04x\n", (int)i, desc.idVendor, desc.idProduct); } libusb_free_device_list(devs, 1); libusb_exit(ctx); return 0; } ``` 编译时需要链接 libusb: ```bash gcc -o list_usb list_usb.c -lusb-1.0 ``` #### 3. libusb 文档与学习资源 官方文档主要集中在 [libusb.info](http://libusb.info) 网站上,该网站提供了完整的 API 文档、开发指南和示例代码。对于开发者来说,理解 libusb 的异步 I/O 模型、设备生命周期管理和错误处理机制是非常重要的[^1]。 #### 4. 常见问题与解决方案 - **设备无法识别**:确保设备驱动程序正确安装,并且设备处于活动状态。可以通过 `lsusb` 命令检查设备是否被系统识别[^4]。 - **权限问题**:在 Linux 上,可能需要将用户添加到 `plugdev` 组以获得访问 USB 设备的权限。 - **API 调用失败**:检查返回值以获取错误信息,使用 `libusb_error_name()` 函数可以将错误码转换为可读字符串。 - **兼容性问题**:旧版本的 libusb(如 0.1.x)与新版本(1.0.x)存在 API 差异,建议使用 `libusb-compat` 包来维持兼容性[^1]。 #### 5. 高级开发与集成 对于嵌入式系统,如 STM32 微控制器,可以使用轻量级 USB 设备栈实现与 libusb 的集成。该项目提供了跨平台兼容性、易于集成的 HAL 库支持以及详细的文档和示例代码,帮助开发者快速上手[^3]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值