CLI를 통해 USB 포트를 분리했다가 다시 연결


17

무작위로 작동을 멈추는 마우스가 있습니다. 해결책은 간단합니다. 플러그를 뽑았다가 다시 꽂습니다. 커맨드 라인을 통해이 작업을 수행 할 수있는 방법이 있습니까? 커맨드 라인을 통해 수행하면 몇 가지 장점이 있습니다.

  1. 커넥터가 마모되지 않습니다.
  2. 더 빠릅니다.
  3. 책상 아래에서 기어 다니는 문제를 해결합니다.
  4. 가장 중요 : 실수로 다른 것을 분리하지 못하게합니다.

또한이 작업을 수행하는 방법이 궁금합니다.

OS는 데비안 8입니다.

감사!


1
정확히 같은 질문은 아니지만 대답은 적용 가능해야합니다. 논리적으로 연결이 끊긴 USB 장치를 다시 연결하는 방법?
Gilles 'SO- 악마 중지

아마 unix.stackexchange.com/a/166601/117599 가 당신이 찾고있는 것입니다.
phk

답변:


13

다음을 저장하십시오 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>ID를 대체 하십시오 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


1
이렇게하면 장치의 전원이 효과적으로 재설정되므로 플러그를 뽑았다가 다시 꽂지 않아도 완전히 재설정됩니까?
Jarryd

이것은 매력처럼 작동했습니다.
웨지 마틴

프로그램이 직렬 USB 장치 (예 : / dev / ttydev0가 /etc/udev/rules.d/mystuff.rules에 지정된대로 / dev / myserialdevice에서 심볼릭 링크 됨)를 연 경우 장치가 어떤 이유로 중단되는 경우 그런 다음 위와 같이 ioctl ()을 사용하여 재설정해야합니까, 아니면 단순히 닫고 다시 열면 충분합니까?
Per Lindberg

1
@Jarryd는 링크 에서 Alan의 설명을 참조하십시오 .Note however, that reset followed by re-enumeration is _not_ the same thing as power-cycle followed by reconnect and re-enumeration.
ckujau

2

/ubuntu/645/how-do-you-reset-a-usb-device-from-the-command-line 에서 답변을 기반으로 전체 프로세스를 단순화하는 Python 스크립트를 만들었습니다. .

아래 스크립트를 reset_usb.py로 저장하거나 https://github.com/mcarans/resetusb/ 를 리포지토리로 복제 하십시오 .

용법:

python reset_usb.py help :이 도움말보기

sudo python reset_usb.py list : 모든 USB 장치 목록

sudo python reset_usb.py path / dev / bus / usb / XXX / YYY : / dev / bus / usb / XXX / YYY 경로를 사용하여 USB 장치 재설정

sudo python reset_usb.py search "search terms": list에서 반환 한 검색 문자열 내에서 검색어를 사용하여 USB 장치를 검색하고 일치하는 장치를 재설정

sudo python reset_usb.py listpci : 모든 PCI USB 장치 나열

sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : /sys/bus/pci/drivers/.../XXXX:XX 경로를 사용하여 PCI USB 장치를 재설정합니다 : 트리플 엑스

sudo python reset_usb.py searchpci "search terms": listpci에서 반환 한 검색 문자열 내에서 검색어를 사용하여 PCI USB 장치를 검색하고 일치하는 장치를 재설정

#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl

instructions = '''
Usage: python reset_usb.py help : Show this help
       sudo python reset_usb.py list : List all USB devices
       sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
       sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
       sudo python reset_usb.py listpci : List all PCI USB devices
       sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
       sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device       
       '''


if len(sys.argv) < 2:
    print(instructions)
    sys.exit(0)

option = sys.argv[1].lower()
if 'help' in option:
    print(instructions)
    sys.exit(0)


def create_pci_list():
    pci_usb_list = list()
    try:
        lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
        pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
        for pci_device in pci_devices:
            device_dict = dict()
            categories = pci_device.split(os.linesep)
            for category in categories:
                key, value = category.split('\t')
                device_dict[key[:-1]] = value.strip()
            if 'USB' not in device_dict['Class']:
                continue
            for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
                slot = device_dict['Slot']
                if slot in dirs:
                    device_dict['path'] = os.path.join(root, slot)
                    break
            pci_usb_list.append(device_dict)
    except Exception as ex:
        print('Failed to list pci devices! Error: %s' % ex)
        sys.exit(-1)
    return pci_usb_list


def create_usb_list():
    device_list = list()
    try:
        lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
        usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
        for device_categories in usb_devices:
            if not device_categories:
                continue
            categories = device_categories.split(os.linesep)
            device_stuff = categories[0].strip().split()
            bus = device_stuff[1]
            device = device_stuff[3][:-1]
            device_dict = {'bus': bus, 'device': device}
            device_info = ' '.join(device_stuff[6:])
            device_dict['description'] = device_info
            for category in categories:
                if not category:
                    continue
                categoryinfo = category.strip().split()
                if categoryinfo[0] == 'iManufacturer':
                    manufacturer_info = ' '.join(categoryinfo[2:])
                    device_dict['manufacturer'] = manufacturer_info
                if categoryinfo[0] == 'iProduct':
                    device_info = ' '.join(categoryinfo[2:])
                    device_dict['device'] = device_info
            path = '/dev/bus/usb/%s/%s' % (bus, device)
            device_dict['path'] = path

            device_list.append(device_dict)
    except Exception as ex:
        print('Failed to list usb devices! Error: %s' % ex)
        sys.exit(-1)
    return device_list


if 'listpci' in option:
    pci_usb_list = create_pci_list()
    for device in pci_usb_list:
        print('path=%s' % device['path'])
        print('    manufacturer=%s' % device['SVendor'])
        print('    device=%s' % device['SDevice'])
        print('    search string=%s %s' % (device['SVendor'], device['SDevice']))
    sys.exit(0)

if 'list' in option:
    usb_list = create_usb_list()
    for device in usb_list:
        print('path=%s' % device['path'])
        print('    description=%s' % device['description'])
        print('    manufacturer=%s' % device['manufacturer'])
        print('    device=%s' % device['device'])
        print('    search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
    sys.exit(0)

if len(sys.argv) < 3:
    print(instructions)
    sys.exit(0)

option2 = sys.argv[2]

print('Resetting device: %s' % option2)


# echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
def reset_pci_usb_device(dev_path):
    folder, slot = os.path.split(dev_path)
    try:
        fp = open(os.path.join(folder, 'unbind'), 'wt')
        fp.write(slot)
        fp.close()
        fp = open(os.path.join(folder, 'bind'), 'wt')
        fp.write(slot)
        fp.close()
        print('Successfully reset %s' % dev_path)
        sys.exit(0)
    except Exception as ex:
        print('Failed to reset device! Error: %s' % ex)
        sys.exit(-1)


if 'pathpci' in option:
    reset_pci_usb_device(option2)


if 'searchpci' in option:
    pci_usb_list = create_pci_list()
    for device in pci_usb_list:
        text = '%s %s' % (device['SVendor'], device['SDevice'])
        if option2 in text:
            reset_pci_usb_device(device['path'])
    print('Failed to find device!')
    sys.exit(-1)


def reset_usb_device(dev_path):
    USBDEVFS_RESET = 21780
    try:
        f = open(dev_path, 'w', os.O_WRONLY)
        fcntl.ioctl(f, USBDEVFS_RESET, 0)
        print('Successfully reset %s' % dev_path)
        sys.exit(0)
    except Exception as ex:
        print('Failed to reset device! Error: %s' % ex)
        sys.exit(-1)


if 'path' in option:
    reset_usb_device(option2)


if 'search' in option:
    usb_list = create_usb_list()
    for device in usb_list:
        text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
        if option2 in text:
            reset_usb_device(device['path'])
    print('Failed to find device!')
    sys.exit(-1)

0

소프트웨어를 통해 항상 USB 스택을 재설정하고 USB 장치를 절전 ( 절전 ) 모드로 전환 할 수 있지만 + 5V 포트 전원에는 영향을 미치지 않습니다 (항상 켜져 있음).

USB 허브에 따라 실제 USB 포트의 전원을 실제로 끌 수 있습니다 (사이클).

"스마트"USB 허브 만 포트 당 전원주기를 허용합니다. 다음 은이를 제어 할 수있는 작은 프로젝트입니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.