求助 使用libusb ,调用libusb_claim_interface 返回-6 如何禁止系统自动加载

空持百千偈 2021-05-25 05:25:53
操作系统: UOS 20
为了在用户权限下访问USB设备 添加了rules文件
SUBSYSTEM=="usb", ATTRS{idVendor}=="1111", MODE="0666"


示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "libusb.h"

libusb_context *g_stUsbCtx = 0;
struct libusb_device **pDevList = 0, *pCurDev = 0;
struct libusb_device_handle *pDevHandle = 0;
int main()
{
int iRtn = 0;
if (g_stUsbCtx == 0)
{
iRtn = libusb_init(&g_stUsbCtx);
if (iRtn < 0)
{
iRtn = -1;
return iRtn;
}
}
iRtn = libusb_get_device_list(g_stUsbCtx, &pDevList);
if (iRtn < 0)
{
iRtn = -1;
return iRtn;
}
pDevHandle = libusb_open_device_with_vid_pid(g_stUsbCtx,0x1111,0x2222);
if (pDevHandle <= 0)
{
iRtn = -1;
libusb_close(pDevHandle);
pDevHandle = 0;
return iRtn;
}


uint8_t ucInterfaceIndex = 0;
iRtn = libusb_claim_interface(pDevHandle, ucInterfaceIndex);

if ((iRtn != LIBUSB_SUCCESS) && (ucInterfaceIndex == 0))
{
iRtn = libusb_detach_kernel_driver(pDevHandle, ucInterfaceIndex);

iRtn = libusb_claim_interface(pDevHandle, ucInterfaceIndex);
}
iRtn = libusb_release_interface(pDevHandle, ucInterfaceIndex);


//close
if (pDevHandle)
{

if (ucInterfaceIndex != (uint8_t)(-1))
{
libusb_detach_kernel_driver(pDevHandle, ucInterfaceIndex);
}
libusb_close(pDevHandle);
pDevHandle = 0;
}
return 0;
}



UOS 系统会自动加载我的USB设备 ,导致 第一次 libusb_claim_interface必定失败,需要调用 libusb_detach_kernel_driver 来卸载设备.
这个时候问题就来了,每次调用libusb_detach_kernel_driver ,系统都会弹框提示 “设备已拔出” 。
有没有办法让它不提示,最好是让系统不自动加载我的设备,或者在不需要卸载的情况下发指令。

谢谢各位大佬!
...全文
5181 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
libusb1.0学习(一) 首先声明,这是看到国外论坛上的学习文章后,独立翻译过来作为笔记用,加入部分自我理解,并且全部原创。 介绍: libusb是一个开源库,可以帮助开发者在用户空间的层面上与UBS设备进行通讯。如果想了解更多,可以查看他们的主页:http://libusb.org/ 在其文档中,建议首先阅读USB2的规格说明:http://www.usb.org/developers/docs/,这可以帮助真正地了解USB是如何工作的。 libusb的安装: 你可以从官方的主页上获取源代码,并且编译安装。或者使用的发行版已经包含了软件包,可以很方便地安装。 如果已经安装完毕,请继续往下 通讯: 设备和客户端之间的通讯是个清楚的概念叫做使用管道。每个管道都是一个主机上的一个软件和设备上一个端点的通道。每个端点对于设备来说完成一部分特殊的目标,比如接受命令或者传输数据。一个全速设备最高可以拥有16个端点,然后低速的设备只拥有三个端点。 所有的USB设备当供电时都支持端口0。这个端口是默认的目标管道。当设备连接被检测到后,USBD软件会使用端口0来初始化设备,执行普通的(非特殊)配置,并且获得有设备提供的其他端点的信息。端点是以他们的端点数目(取决于设计的时间),总线宽带,访问频率,延迟和处理错误要求为特征区分的。 一旦设备里的端点识别并且配置完毕,管道就产生允许客户端软件与设备进行通讯。跟一个管道产生联系是以对总线的访问和带宽,传输的类型,传输的方向和最大数据负载大小为描述特征的。 USB定义了四种传输方式:控制传输,通常用来传输命令和状态操作;中断传输,通过设备初始化一些来自主机的请求;同步传输,用来传输投递关键事件的数据(比如视频和对话);批量传输,使用全部可以用的带宽但不是特定时间的。所有的数传使用相同格式的包装,包括控制信息,数据和错误效验区域。 这里有两种管道:消息管道和流管道。控制传输是使用消息管道。在消息管道中,在每个包中的数据部分对于USB系统软件是有意义的。 流管道被中断传输,同步传输和批量传输使用。在流管道中,在每个包中的数据部分对于USB是没有意义,仅仅在客户端软件和设备间传输。 同步接口: 同步接口允许你使用单独一个函数调用一个USB传输。当这个函数调用返回时,这个传输也已经完成并且返回结果供解析用。这种方式的优点是十分清晰的:你可以通过一个简单的函数调用做任何事。 尽管如此,这个接口还是有它的局限性。你的程序会进入休眠当在libusb_bulk_transfer()(当进行批量传输),直到传输完成。假如这需要花费三个小时,你的程序同样需要休眠同样的时间。实现将会在库里面结束,整体线程这期间是无用的。另一个问题是,当一个单独的传输线程结束,这里不存在可能多个端点和多个设备同时地进行I/O操作,除非你借助创造新的线程处理。另外的,当请求被提交后,这里没有机会可能取消传输。 设备和接口: 在libusb中,每个USB设备通过libusb_device和libusb_device_handle对象操作。libusb API 连接一个打开的设备至特定的接口。这意味着如果你在设备上请求多个接口,你必须同样多次打开设备来接受一个libusb_dev_handle,对应每个你想进行通讯的接口。不要忘记调用libusb_dev_handle。 这些意味着什么?这意味你在设备上操作以前,可以完成请求接口,同样,你可以在完成设备操作前,先释放接口。 每个设备都有自己独属的配置,比如vendor id,product id等。我们使用这些设置去发现需求的设备,并且通过这些配置来工作。首先我们写一个函数来查明这些配置并且打印出来,以便我们找到正确的一个;我们基本的操作如下: 1.通过调用libusb_init来初始化库,同时创建一个对话; 2.调用libusb_get_device_list来获得已经连接的设备的队列。这会创建一个libusb_device的数组,包含了所有连接到系统上的usb设备; 3.循环遍历所有的设备来检查他们的选项; 4.发现其中需要的一个,使用libusb_open或者libusb_open_device_with_vid_pid(当你知道这个设备vendor id和product id)来打开设备; 5.使用libusb_free_device_list清除使用libusb_get_device_list获得的队列; 6.通过libusb_claim_interface请求接口(需要你知道设备的接口数值); 7.操作想得到的I/O; 8.通过libusb_release_interface释放设备; 9.通过libusb_close将你之前打开的设备关闭; 10.通过libusb_exit来关闭对话; PS:英文需要苦练啊,一篇短的教程文章看的结结巴巴的 libusb1.0学习(二) 接学习一,学习二主要是看例程 ok,现在最简单的想法看看有多少信息包含在你的设备里,程序代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #include #include <libusb.h> using namespace std; void printdev(libusb_device *dev); //prototype of the function int main() { libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices libusb_context *ctx = NULL; //a libusb session int r; //for return values ssize_t cnt; //holding number of devices in list r = libusb_init(&ctx;); //initialize a library session if(r < 0) { cout<<"Init Error "<libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation cnt = libusb_get_device_list(ctx, &devs;); //get the list of devices if(cnt < 0) { cout<<"Get Device Error"<libusb_free_device_list(devs, 1); //free the list, unref the devices in it libusb_exit(ctx); //close the session return 0; } void printdev(libusb_device *dev) { libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc;); if (r < 0) { cout<<"failed to get device descriptor"<libusb_config_descriptor *config; libusb_get_config_descriptor(dev, 0, &config;); cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| "; const libusb_interface *inter; const libusb_interface_descriptor *interdesc; const libusb_endpoint_descriptor *epdesc; for(int i=0; i<(int)config->bNumInterfaces; i++) { inter = &config;->interface[i]; cout<<"Number of alternate settings: "<num_altsetting<<" | "; for(int j=0; jnum_altsetting; j++) { interdesc = &inter;->altsetting[j]; cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | "; cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | "; for(int k=0; k<(int)interdesc->bNumEndpoints; k++) { epdesc = &interdesc;->endpoint[k]; cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | "; cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | "; } } } cout<libusb_free_config_descriptor(config); } 写完之后,编译看看会出现什么。首先运行程序并检查设备,然后连上我自己的设备在执行程序。会发现有新的内容出现,可以根据vendor id和product id发现这正是我自己连上打开的设备。 注意:发现设备(调用libusb_get_device_list())会返回新的内存分配的设备队列。当你完成这个队列的使用后必须释放他。 Libusb同样需要知道当一切完成时清除队列的内容;设备的本身。 为处理这些问题,libusb提供了两个单独的条目: 一个释放队列本身的函数 一个针对设备内部的参考计数系统 新的设备由libusb_get_device_list()函数展示,都拥有一个参考计数1。你可以使用libubs_ref_device()和libusb_unref_device()增加或减少参考计数。当一个设备的参考计数为0时,该设备就被销毁 通过以上的信息,打开设备的基本流程可以视为如下步骤: 1. 使用libusb_get_device_list()发现设备; 2. 选择你想操作的设备,调用libusb_open(); 3. 在设备队列中unref所有的设备; 4. 释放已经发现的设备队列; 这个次序是十分重要的,在尝试打开设备之前,你不能够unreference设备,因此unreference操作有可能导致设备的销毁。 为了方便起见,libusb_free_device_list()函数包含一个参数,在释放队列本身前,该参数能够选择性地在队列中unreference所有设备。这包含了以上的步骤3和步骤4。 如果还有需要,可以去libusb1’s API(http://libusb.sourceforge.net/api-1.0/index.html)文档参考你需要的函数。 好了,现在你可以找到你需要的设备了。现在是打开设备,请求并且执行一个简单的I/O。如果你知道vendor ID和prouct ID,使用libusb_open_device_with_vid_pid。 另外需要注意的,如果内核(你的OS)已经连接到这个设备,你将无法请求到它。在这种情况下,你需要调用libusb_detach_kernel_drive来从内核中检测设备。如果你想知道内核是否可用的,使用libusb_kernel_drive_active,如果返回值为1,对于你的设备内核可以加载驱动。 批量传输 为了在你的设备上使用批量传输,你应该获得为你的USB设备获得一个设备句柄,并且你应该知道使用哪个端点(从之前设备说明获得)。 关于语法上的信息参考这里(http://libusb.sourceforge.net/api-1.0/group__syncio.html#gab8ae853ab492c22d707241dc26c8a805) 这里有个简单的例子包含所有我提到的相关部分: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #include #include <libusb.h> using namespace std; int main() { libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices libusb_device_handle *dev_handle; //a device handle libusb_context *ctx = NULL; //a libusb session int r; //for return values ssize_t cnt; //holding number of devices in list r = libusb_init(&ctx;); //initialize the library for the session we just declared if(r < 0) { cout<<"Init Error "<libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation cnt = libusb_get_device_list(ctx, &devs;); //get the list of devices if(cnt < 0) { cout<<"Get Device Error"<libusb_open_device_with_vid_pid(ctx, 5118, 7424); //these are vendorID and productID I found for my usb device if(dev_handle == NULL) cout<<"Cannot open device"<libusb_free_device_list(devs, 1); //free the list, unref the devices in it unsigned char *data = new unsigned char[4]; //data to write data[0]='a';data[1]='b';data[2]='c';data[3]='d'; //some dummy values int actual; //used to find out how many bytes were written if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached cout<<"Kernel Driver Active"<libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it cout<<"Kernel Driver Detached!"<libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1) if(r < 0) { cout<<"Cannot Claim Interface"<Claimed Interface"<"<libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual;, 0); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129 if(r == 0 && actual == 4) //we wrote the 4 bytes successfully cout<<"Writing Successful!"<libusb_release_interface(dev_handle, 0); //release the claimed interface if(r!=0) { cout<<"Cannot Release Interface"<Interface"<libusb_close(dev_handle); //close the device we opened libusb_exit(ctx); //needs to be called to end the delete[] data; //delete the allocated memory for data return 0; } 结尾:这个教程对于这个话题来说是十分简单的介绍,需要时间需联系同步传输,然后移动是异步的,这还有很多需要学习。 希望这些对你的初学有帮助。
驱动开发向来是内核开发中工作量最多的一块,随着USB设备的普及,大量的USB设备的驱动开发也成为驱动开发者手头上做的最多的事情。本文主要介绍Linux平台下基于libusb的驱动开发,希望能够给从事Linux驱动开发的朋友带来些帮助,更希望能够给其他平台上的无驱设计带来些帮助。文章是我在工作中使用libusb的一些总结,难免有错误,如有不当的地方,还请指正。 [1]   Linux 平台上的usb驱动开发,主要有内核驱动的开发和基于libusb的无驱设计。   对于内核驱动的大部分设备,诸如带usb接口的hid设备,linux本身已经自带了相关的驱动,我们只要操作设备文件便可以完成对设备大部分的操作,而另外一些设备,诸如自己设计的硬件产品,这些驱动就需要我们驱动工程师开发出相关的驱动了。内核驱动有它的优点,然而内核驱动在某些情况下会遇到如下的一些问题:   1 当使用我们产品的客户有2.4内核的平台,同时也有2.6内核的平台,我们要设计的驱动是要兼容两个平台的,就连makefile 我们都要写两个。   2 当我们要把linux移植到嵌入平台上,你会发现原先linux自 带的驱动移过去还挺大的,我的内核当然是越小越好拉,这样有必要么。这还不是最郁闷的地方,如果嵌入平台是客户的,客户要购买你的产品,你突然发现客户设 备里的系统和你的环境不一样,它没有你要的驱动了,你的程序运行不了,你会先想:“没关系,我写个内核驱动加载一下不就行了“。却发现客户连insmod加载模块的工具都没移植,那时你就看看老天,说声我怎么那么倒霉啊,客户可不想你动他花了n时间移植的内核哦   3 花了些功夫写了个新产品的驱动,挺有成就感啊,代码质量也是相当的有水准啊。正当你沉醉在你的代码中时,客服不断的邮件来了,“客户需要2.6.5内核的驱动,config文件我已经发你了” “客户需要双核的 2.6.18-smp 的驱动” “客户的平台是自己定制的是2.6.12-xxx “ 你恨不得把驱动的源代码给客户,这样省得编译了。你的一部分工作时间编译内核,定制驱动   有问题产生必然会有想办法解决问题的人, libusb的出现给我们带来了某些方便,即节约了我们的时间,也降低了公司的成本。 所以在一些情况下,就可以考虑使用libusb的无驱设计了。   下面我们就来详细讨论一下libusb, 并以写一个hid设备的驱动来讲解如何运用libusb,至于文章中涉及的usb协议的知识,限于篇幅,就不详细讲解了,相关的可自行查看usb相关协议。   一 libusb 介绍   libusb 设计了一系列的外部API 为应用程序所调用,通过这些API应用程序可以操作硬件,从libusb的源代码可以看出,这些API 调用了内核的底层接口,和kernel driver中所用到的函数所实现的功能差不多,只是libusb更加接近USB 规范。使得libusb使用也比开发内核驱动相对容易的多。   Libusb 的编译安装请查看Readme,这里不做详解   二 libusb 的外部接口   2.1 初始化设备接口   这些接口也可以称为核心函数,它们主要用来初始化并寻找相关设备。   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);   这个函数返回总线的列表,在高一些的版本中已经用不到了,这在下面的实例中会有讲解   2.2 操作设备接口   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对应使用。   2.3 控制传输接口   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);   2.4 批传输接口   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);   2.5 中断传输接口   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);

1,318

社区成员

发帖
与我相关
我的任务
社区描述
主要是开发驱动技术
社区管理员
  • 驱动程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧