求助 使用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 ,系统都会弹框提示 “设备已拔出” 。
有没有办法让它不提示,最好是让系统不自动加载我的设备,或者在不需要卸载的情况下发指令。

谢谢各位大佬!
...全文
8349 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

1,323

社区成员

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

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