CLIを介してUSBポートを取り外して再接続

CLIを介してUSBポートを取り外して再接続

マウスが突然動作を停止します。解決策は簡単です。プラグを抜いて再接続するだけです。コマンドラインでこれを行う方法はありますか?コマンドラインでこれを行うにはいくつかの利点があります。

  1. コネクタが摩耗しません。
  2. 急いで。
  3. テーブルの下に這う手間が少なくなりました。
  4. 最も重要なことは、誤って他のデバイスのプラグを抜くことを防ぐことです。

また、これをどうするのか気になります。

オペレーティングシステムはDebian 8です。

ありがとうございます!

ベストアンサー1

以下を保存してください。usbreset.c

/* usbreset -- send a USB port reset to a USB device */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/usbdevice_fs.h>


int main(int argc, char **argv)
{
    const char *filename;
    int fd;
    int rc;

    if (argc != 2) {
        fprintf(stderr, "Usage: usbreset device-filename\n");
        return 1;
    }
    filename = argv[1];

    fd = open(filename, O_WRONLY);
    if (fd < 0) {
        perror("Error opening output file");
        return 1;
    }

    printf("Resetting USB device %s\n", filename);
    rc = ioctl(fd, USBDEVFS_RESET, 0);
    if (rc < 0) {
        perror("Error in ioctl");
        return 1;
    }
    printf("Reset successful\n");

    close(fd);
    return 0;
}

端末で次のコマンドを実行します。

  1. コンパイラ:

    cc usbreset.c -o usbreset
    
  2. リセットするUSB​​デバイスのバスとデバイスIDを取得します。

    lsusb -t 
    
    Bus#  4  
    -Dev#   1 Vendor 0x1d6b Product 0x0001    
    -Dev#   3 Vendor 0x046b Product 0xff10
    
  3. コンパイルされたプログラムを実行可能にします。

    chmod +x usbreset
    
  4. sudo次のコマンドを実行して、見つかった権限とIDを<Bus>必要に応じて置き換えてプログラムを実行します。<Device>lsusb

    sudo ./usbreset /dev/bus/usb/004/003
    
    Resetting USB device /dev/bus/usb/004/003
    
    Reset successful
    

上記のプログラムのソース:http://marc.info/?l=linux-usb&m=121459435621262&w=2

おすすめ記事