作業系統: 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 ,系統都會彈框提示 “設備已拔出” 。
有沒有辦法讓它不提示,最好是讓系統不自動加載我的設備,或者在不需要卸載的情況下發指令。
謝謝各位大佬!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/285066.html
標籤:驅動程序開發區
上一篇:Virtualbox安裝完ubantu后一直在這兩個來回跳動,請大佬賜教!!
下一篇:xargs
