5

I am using usbip and a raspberry pi to extend the range of a wireless keyboard to a computer that is just a tad too far away for the keyboard to work reliably on its own.

Sometimes the USB receiver of the keyboard is reconnected and used elsewhere, but when it is reconnected to the raspberry pi the USBIP connection is not automatically re-established.

How can I achieve automatic reconnection?

3 Answers 3

6

When a device that is bound and attached remotely is unplugged, the device is automatically detached on the client and unbound on the host. After that the state is the same as if it was never bound or attached.

The usbip commands for binding (on the host) and attaching (on the client) may be run repeatedly with the same arguments. While this issues an error message on already bound or attached devices, nothing bad happens! So one can just install background scripts that will repeatedly bind and attach the devices. Example scripts and systemd units are provided below. Be sure to change the Hostname and Port IDs to your needs.

Host

Skript /opt/usbip/usbip-bind:

#!/bin/bash SPOOL=/var/spool/usbip/bind if [[ $1 == "-q" ]] then exec &>/dev/null fi touch $SPOOL while [[ -e $SPOOL ]] do /usr/bin/usbip bind -b 1-1.2.1 /usr/bin/usbip bind -b 1-1.2.2 sleep 10 done /usr/bin/usbip unbind -b 1-1.2.1 /usr/bin/usbip unbind -b 1-1.2.2 exit 0 

Systemd unit /etc/systemd/system/usbip-bind.service:

[Unit] Description=USB-IP Bindings [Service] ExecStart=/opt/usbip/usbip-bind -q ExecStop=/bin/rm /var/spool/usbip/bind ; /bin/bash -c "while [[ -d /proc/"$MAINPID" ]]; do sleep 1; done" [Install] WantedBy=multi-user.target 

Be sure do make the directory /var/spool/usbip. Then enable and start the unit:

systemctl daemon-reload systemctl enable usbip-bind systemctl start usbip-bind 

Client

Skript /opt/usbip/usbip-attach:

#!/bin/bash SPOOL=/var/spool/usbip/attach if [[ $1 == "-q" ]] then exec &>/dev/null fi touch $SPOOL while [[ -e $SPOOL ]] do /usr/bin/usbip attach -r pi -b 1-1.2.1 /usr/bin/usbip attach -r pi -b 1-1.2.2 sleep 10 done /usr/bin/usbip detach -p 0 /usr/bin/usbip detach -p 1 exit 0 

Systemd unit /etc/systemd/system/usbip-attach.service:

[Unit] Description=USB-IP Attach Wants=network-online.target After=network-online.target [Service] ExecStart=/opt/usbip/usbip-attach -q ExecStop=/bin/rm /var/spool/usbip/attach ; /bin/bash -c "while [[ -d /proc/"$MAINPID" ]]; do sleep 1; done" [Install] WantedBy=multi-user.target 

Be sure do make the directory /var/spool/usbip. Then enable and start the unit:

systemctl daemon-reload systemctl enable usbip-attach systemctl start usbip-attach 

Now you may remove the device whenever needed and at most 20 seconds after plugging it back in the usbip connection is reestablished.

1
  • 1
    Binding on usbip host can be done using udev rules and a simple lsusb | grep "vendor_id:product_id" script Commented Jul 19, 2019 at 13:05
1

I tried this and it kind of work.. there are a few more this you have to do.

First:

You have to make the scipts runnable on both host and client

sudo chmod +x /opt/usbip/usbip-attach sudo chmod +x /opt/usbip/usbip-bind 

In my case usbip was not installed in /opt/usbip, I had to simply write just usbip in both host and client service file.

I also had to start the usbipd service. So on host i used this file instead:

[Unit] Description=USB-IP Bindings [Service] ExecStartPre=/usr/sbin/usbipd -D ExecStart=/opt/usbip/usbip-bind -q ExecStop=/bin/rm /var/spool/usbip/bind ; /bin/bash -c "while [[ -d /proc/"$MAINPID" ]]; do sleep 1; done" [Install] WantedBy=multi-user.target 

It starts the usbipd server first

and in both host and client files i had to remove /usr/bin/

After these changes it worked fine on my Raspberry Pi host and ubuntu 19.10 client

1

Although my answer is quite late, but maybe useful for the others: https://github.com/alpertsev/usbip-service-shell

1
  • 1
    Welcome to the site, and thank you for your contribution. Please note that link-only answers are discouraged, as the link may become invalid or the linked content change. Please edit your answer to include at least a summary of how to use the tool referenced in the link. Commented Jul 1, 2021 at 13:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.