Skip to content

Commit c83042d

Browse files
build usb modules
Signed-off-by: Yaroslav Borbat <yaroslav.borbat@flant.com>
1 parent 1d5c139 commit c83042d

File tree

12 files changed

+581
-1
lines changed

12 files changed

+581
-1
lines changed

images/usb-modules/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Out-of-tree build: no kernel .config needed.
3+
# Use: make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
4+
5+
obj-m += usbip-core.o
6+
usbip-core-y := usbip_common.o usbip_event.o
7+
8+
obj-m += vhci-hcd.o
9+
vhci-hcd-y := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
10+
11+
obj-m += usbip-host.o
12+
usbip-host-y := stub_dev.o stub_main.o stub_rx.o stub_tx.o
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# Apply patches from patches/ to each kernel version tree in .out/
17+
# Usage: ./apply-patches.sh [out-dir]
18+
# Default out-dir is .out (relative to script directory)
19+
20+
set -e
21+
22+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23+
OUT_DIR="${1:-.out}"
24+
PATCHES_DIR="${SCRIPT_DIR}/patches"
25+
26+
cd "$SCRIPT_DIR"
27+
OUT_DIR="$(realpath "$OUT_DIR")"
28+
29+
if [[ ! -d "$OUT_DIR" ]]; then
30+
echo "Error: output directory '$OUT_DIR' does not exist" >&2
31+
exit 1
32+
fi
33+
34+
if [[ ! -d "$PATCHES_DIR" ]]; then
35+
echo "Error: patches directory '$PATCHES_DIR' does not exist" >&2
36+
exit 1
37+
fi
38+
39+
for version_dir in "$OUT_DIR"/*/; do
40+
[[ -d "$version_dir" ]] || continue
41+
version="$(basename "$version_dir")"
42+
for patch in "$PATCHES_DIR"/*.patch; do
43+
[[ -f "$patch" ]] || continue
44+
echo "Applying $(basename "$patch") to $version..."
45+
if ! patch -d "$version_dir" -p0 --forward --silent < "$patch"; then
46+
if patch -d "$version_dir" -p0 --reverse --check --silent < "$patch" 2>/dev/null; then
47+
echo " (already applied, skipping)"
48+
else
49+
echo " FAILED" >&2
50+
exit 1
51+
fi
52+
fi
53+
done
54+
done
55+
56+
echo "Done."
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# Build usbip-core, usbip-host, vhci-hcd for the running kernel and put .ko into a tmp dir.
17+
# Needs: kernel headers (e.g. linux-headers-$(uname -r)), make, gcc. No full kernel source.
18+
#
19+
# Usage:
20+
# ./build-usbip-modules.sh [OUTPUT_DIR]
21+
# OUTPUT_DIR defaults to /tmp/usbip-modules-$(uname -r)
22+
# USBIP_SRC defaults to the directory where this script lives (driver source).
23+
#
24+
# Env:
25+
# KVER - kernel version to build for (default: uname -r)
26+
# USBIP_SRC - path to driver source (must contain .c and Makefile.standalone)
27+
# OUTPUT_DIR - output directory (overrides optional argument)
28+
#
29+
# Minimal deps: bash, make, gcc, kernel headers package (e.g. linux-headers-${KVER}).
30+
31+
set -e
32+
33+
KVER="${KVER:-$(uname -r)}"
34+
for base in /lib/modules /usr/lib/modules; do
35+
[[ -d "${base}/${KVER}/build" || -L "${base}/${KVER}/build" ]] || continue
36+
KBUILD="${base}/${KVER}/build"
37+
break
38+
done
39+
KBUILD="${KBUILD:-/lib/modules/${KVER}/build}"
40+
USBIP_SRC="${USBIP_SRC:-$(cd "$(dirname "$0")" && pwd)}"
41+
OUTPUT_DIR="${OUTPUT_DIR:-${1:-/tmp/usbip-modules-${KVER}}}"
42+
43+
if [[ ! -d "$KBUILD" && ! -L "$KBUILD" ]]; then
44+
echo "build-usbip-modules: kernel build dir not found for ${KVER}" >&2
45+
echo "Install kernel headers (kernel-devel or linux-headers-${KVER}) on the host." >&2
46+
exit 1
47+
fi
48+
49+
# Resolve symlink so make sees the real path (must be visible in container)
50+
if [[ -L "$KBUILD" ]]; then
51+
KBUILD=$(readlink -f "$KBUILD")
52+
fi
53+
if [[ ! -d "$KBUILD" ]]; then
54+
echo "build-usbip-modules: build is a symlink but its target is not visible in the container." >&2
55+
echo " resolved path: $KBUILD" >&2
56+
echo "Mount the kernel build tree from the host, e.g.:" >&2
57+
echo " -v /usr/src/kernels:/usr/src/kernels:ro" >&2
58+
exit 1
59+
fi
60+
61+
if [[ ! -f "$USBIP_SRC/Makefile" ]]; then
62+
echo "build-usbip-modules: $USBIP_SRC/Makefile not found" >&2
63+
exit 1
64+
fi
65+
66+
make -C "$KBUILD" M="$USBIP_SRC" CC="${CC:-gcc}" modules
67+
68+
mkdir -p "$OUTPUT_DIR"
69+
cp -f "$USBIP_SRC"/usbip-core.ko "$USBIP_SRC"/usbip-host.ko "$USBIP_SRC"/vhci-hcd.ko "$OUTPUT_DIR/"
70+
71+
echo "Built for ${KVER}; modules in: ${OUTPUT_DIR}"
72+
ls -la "$OUTPUT_DIR"/*.ko

images/usb-modules/build.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -e
18+
19+
KVER=$(uname -r | cut -d. -f1,2)
20+
USBIP_SRC="/src/linux/$KVER/drivers/usb/usbip" ./build-usbip-modules.sh "$@"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--- drivers/usb/usbip/vhci.h
2+
+++ drivers/usb/usbip/vhci.h
3+
@@ -73,19 +73,11 @@ enum hub_speed {
4+
};
5+
6+
/* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
7+
-#ifdef CONFIG_USBIP_VHCI_HC_PORTS
8+
-#define VHCI_HC_PORTS CONFIG_USBIP_VHCI_HC_PORTS
9+
-#else
10+
-#define VHCI_HC_PORTS 8
11+
-#endif
12+
+#define VHCI_HC_PORTS 16
13+
14+
/* Each VHCI has 2 hubs (USB2 and USB3), each has VHCI_HC_PORTS ports */
15+
#define VHCI_PORTS (VHCI_HC_PORTS*2)
16+
17+
-#ifdef CONFIG_USBIP_VHCI_NR_HCS
18+
-#define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS
19+
-#else
20+
-#define VHCI_NR_HCS 1
21+
-#endif
22+
+#define VHCI_NR_HCS 4
23+
24+
#define MAX_STATUS_NAME 16
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
This directory contains patches used to build the following out-of-tree kernel modules:
3+
4+
- `usbip-core`
5+
- `usbip-host`
6+
- `vhci-hcd`
7+
8+
---
9+
10+
## 001-vhci-increase-ports-and-controllers.patch
11+
12+
This patch modifies the default configuration of the `vhci-hcd` (Virtual Host Controller Interface) driver.
13+
14+
### Changes
15+
16+
- Sets the number of ports per virtual hub to **16** (hardcoded).
17+
- Sets the number of virtual host controllers to **4** (hardcoded).
18+
- Removes dependency on:
19+
- `CONFIG_USBIP_VHCI_HC_PORTS`
20+
- `CONFIG_USBIP_VHCI_NR_HCS`
21+
22+
### Resulting Capacity
23+
24+
Each VHCI controller provides:
25+
- 2 hubs (USB 2.0 and USB 3.0)
26+
- 16 ports per hub
27+
28+
With 4 controllers total:
29+
30+
4 controllers × 2 hubs × 16 ports = **128 ports**
31+
32+
This allows up to **128 USB devices** to be attached simultaneously via USB/IP (subject to kernel and system limitations).
33+
34+
> Note: The number of ports and controllers is now fixed at compile time and no longer configurable via kernel config options.

images/usb-modules/tools/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module tools
2+
3+
go 1.25.0
4+
5+
require (
6+
github.com/spf13/cobra v1.10.2
7+
github.com/spf13/pflag v1.0.9
8+
)
9+
10+
require github.com/inconshreveable/mousetrap v1.1.0 // indirect

images/usb-modules/tools/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
2+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
6+
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
7+
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
8+
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9+
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)