10

libusbを使用しようとしていますが、次のエラーメッセージが表示されます。

usbfs:プロセス24665(myprogram)が使用前にインターフェース0を要求しませんでした

理由はよくわかりません。私が知る限り、図書館にある説明に従ってやっているからです。これが私のコードです:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <libusb.h>

int main(void)
{
    int result;
    struct libusb_device_descriptor desc;
    libusb_device **list;
    libusb_device *my_device = NULL;

    result = libusb_init(NULL);
    libusb_set_debug(NULL, 3);

    ssize_t count = libusb_get_device_list(NULL, &list);
    for (int i = 0; i < count; i++) {
        libusb_device *device = list[i];
        result = libusb_get_device_descriptor(device, &desc);
        if((desc.idVendor == 0x03f0) && (desc.idProduct == 0x241d)) {
            my_device = device;
            break;
         }
    }

    if(my_device != NULL) {
        libusb_device_handle *handle;
        result = libusb_open(my_device, &handle);
        int kernelActive = libusb_kernel_driver_active(handle, 0);
        if(kernelActive == 1) {
            result = libusb_detach_kernel_driver(handle, 0);
        }
        result = libusb_claim_interface (handle, 0);
        result = libusb_control_transfer(handle,0x21,34,0x0003,0,NULL,0,0);
        result = libusb_release_interface (handle, 0);

        if(kernelActive == 1) {
          result = libusb_attach_kernel_driver(handle, 0);
        }
        libusb_close(handle);
    }
    libusb_free_device_list(list, 1);
    libusb_exit(NULL);
    return EXIT_SUCCESS;
}

ご覧のとおり、転送前にインターフェースを要求します。(私は他のUSBデバイスでも同じコードを試しましたが、それが何か関係がある場合に備えてです。)

私はlibusb-1.0.9を使用しています。これは私が見つけた最新のリリースです。私はUbuntu12.04_64(Precise Pangolin)でこれを実行して ます

4

3 に答える 3

5

と同じ問題がありましたlibusb-1.0; 私はもともとこのシーケンスを持っていました:

libusb_init
libusb_open_device_with_vid_pid
libusb_reset_device
libusb_get_device
libusb_reset_device
libusb_set_configuration
libusb_claim_interface
libusb_set_interface_alt_setting 
libusb_get_device_descriptor
libusb_get_bus_number 
libusb_get_device_address
libusb_get_string_descriptor_ascii
if(libusb_kernel_driver_active.. ) 
  if(libusb_detach_kernel_driver.. ) 
libusb_bulk_transfer
...

...そしてそのために、最初に実行されたときに「要求されていないインターフェース」が生成されましlibusb_bulk_transferた(ただし、上に表示されていない後続のものは生成されませんでした)gdb。(ところで、そのエラーメッセージは/linux/drivers/usb/core/devio.cから来ています)

このページ: USB Hid の問題 · Yubico/yubikey-personalization Wiki · GitHublibusb-0.1は、対応する "detach_driver" 関数と呼ばれる修正を参照しています。そのため、コード内の「detach_driver」部分も移動し始めました-そして最後に、このシーケンスは「インターフェイスが要求されていません」というメッセージを取り除くようです:

libusb_init
libusb_open_device_with_vid_pid
if(libusb_kernel_driver_active.. ) 
  if(libusb_detach_kernel_driver.. ) 
libusb_reset_device
libusb_get_device
libusb_set_configuration
libusb_claim_interface
libusb_set_interface_alt_setting 
libusb_get_device_descriptor
libusb_get_bus_number
libusb_get_device_address
libusb_get_string_descriptor_ascii
libusb_bulk_transfer
...

どうやら、最初にドライバーが切り離されてからインターフェイスが要求された場合、エラーは生成されません。しかし、それはあなたがOPに持っているものでもあります-だから、OPの秘訣はdetach、 then set configuration、そしてその後claim interface...

これが役に立てば幸いです、
乾杯!

于 2012-11-13T19:03:58.037 に答える
4

calling libusb_set_debug(context, where_to)libusb からさらにデバッグ情報を取得してみてください。メッセージの where_to は整数です。

Level 0: no messages ever printed by the library (default)
Level 1: error messages are printed to stderr
Level 2: warning and error messages are printed to stderr
Level 3: informational messages are printed to stdout, warning and error messages are printed to stderr

これはかなり良いlibusb ドキュメントからのものです。

エラーメッセージが問題ないように見えるコードを実行しましたが、内部的には他のプロセスが排他的要求を持っていると報告したため、使用できませんでした。

于 2012-08-30T02:18:44.343 に答える
1

すべての結果値を確認する必要があります。そうすれば、何が問題なのかを簡単に見つけることができます。期待どおりの結果が返された場合は、すべての結果値を確認してください。

確認:

  • libusb_open は LIBUSB_SUCCESS を返しますか?
  • libusb_kernel_driver_active は、エラー コードではなく、0 または 1 を返しますか?
  • libusb_detach_kernel_driver は LIBUSB_SUCCESS を返しますか?
  • libusb_claim_interface は LIBUSB_SUCCESS を返しますか?
于 2012-07-04T11:48:49.813 に答える