108 lines · bash
1#!/bin/bash2 3################################################################################4# This is free and unencumbered software released into the public domain.5#6# Anyone is free to copy, modify, publish, use, compile, sell, or7# distribute this software, either in source code form or as a compiled8# binary, for any purpose, commercial or non-commercial, and by any9# means.10#11# In jurisdictions that recognize copyright laws, the author or authors12# of this software dedicate any and all copyright interest in the13# software to the public domain. We make this dedication for the benefit14# of the public at large and to the detriment of our heirs and15# successors. We intend this dedication to be an overt act of16# relinquishment in perpetuity of all present and future rights to this17# software under copyright law.18#19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,20# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.22# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR23# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,24# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR25# OTHER DEALINGS IN THE SOFTWARE.26#27# For more information, please refer to <https://unlicense.org/>28################################################################################29 30################################################################################31# This is a sample script which shows how to use vUDC with ConfigFS gadgets32################################################################################33 34# Stop script on error35set -e36 37################################################################################38# Create your USB gadget39# You may use bare ConfigFS interface (as below)40# or libusbgx or gt toool41# Instead of ConfigFS gadgets you may use any of legacy gadgets.42################################################################################43CONFIGFS_MOUNT_POINT="/sys/kernel/config"44GADGET_NAME="g1"45ID_VENDOR="0x1d6b"46ID_PRODUCT="0x0104"47 48cd ${CONFIGFS_MOUNT_POINT}/usb_gadget49# Create a new USB gadget50mkdir ${GADGET_NAME}51cd ${GADGET_NAME}52 53# This gadget contains one function - ACM (serial port over USB)54FUNC_DIR="functions/acm.ser0"55mkdir ${FUNC_DIR}56 57# Just one configuration58mkdir configs/c.159ln -s ${FUNC_DIR} configs/c.160 61# Set our gadget identity62echo ${ID_VENDOR} > idVendor63echo ${ID_PRODUCT} > idProduct64 65################################################################################66# Load vudc-module if vudc is not available67# You may change value of num param to get more than one vUDC instance68################################################################################69[[ -d /sys/class/udc/usbip-vudc.0 ]] || modprobe usbip-vudc num=170 71################################################################################72# Bind gadget to our vUDC73# By default we bind to first one but you may change this if you would like74# to use more than one instance75################################################################################76echo "usbip-vudc.0" > UDC77 78################################################################################79# Let's now run our usbip daemon in a USB device mode80################################################################################81usbipd --device &82 83################################################################################84# Now your USB gadget is available using USB/IP protocol.85# To prepare your client, you should ensure that usbip-vhci module is inside86# your kernel. If it's not then you can load it:87#88# $ modprobe usbip-vhci89#90# To check availability of your gadget you may try to list devices exported91# on a remote server:92#93# $ modprobe usbip-vhci94# $ usbip list -r $SERVER_IP95# Exportable USB devices96# ======================97# usbipd: info: request 0x8005(6): complete98# - 127.0.0.199# usbip-vudc.0: Linux Foundation : unknown product (1d6b:0104)100# : /sys/devices/platform/usbip-vudc.0101# : (Defined at Interface level) (00/00/00)102#103# To attach this device to your client you may use:104#105# $ usbip attach -r $SERVER_IP -d usbip-vudc.0106#107################################################################################108