162306a36Sopenharmony_ci#!/bin/bash
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci################################################################################
462306a36Sopenharmony_ci# This is free and unencumbered software released into the public domain.
562306a36Sopenharmony_ci#
662306a36Sopenharmony_ci# Anyone is free to copy, modify, publish, use, compile, sell, or
762306a36Sopenharmony_ci# distribute this software, either in source code form or as a compiled
862306a36Sopenharmony_ci# binary, for any purpose, commercial or non-commercial, and by any
962306a36Sopenharmony_ci# means.
1062306a36Sopenharmony_ci#
1162306a36Sopenharmony_ci# In jurisdictions that recognize copyright laws, the author or authors
1262306a36Sopenharmony_ci# of this software dedicate any and all copyright interest in the
1362306a36Sopenharmony_ci# software to the public domain. We make this dedication for the benefit
1462306a36Sopenharmony_ci# of the public at large and to the detriment of our heirs and
1562306a36Sopenharmony_ci# successors. We intend this dedication to be an overt act of
1662306a36Sopenharmony_ci# relinquishment in perpetuity of all present and future rights to this
1762306a36Sopenharmony_ci# software under copyright law.
1862306a36Sopenharmony_ci#
1962306a36Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2062306a36Sopenharmony_ci# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2162306a36Sopenharmony_ci# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2262306a36Sopenharmony_ci# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2362306a36Sopenharmony_ci# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2462306a36Sopenharmony_ci# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2562306a36Sopenharmony_ci# OTHER DEALINGS IN THE SOFTWARE.
2662306a36Sopenharmony_ci#
2762306a36Sopenharmony_ci# For more information, please refer to <https://unlicense.org/>
2862306a36Sopenharmony_ci################################################################################
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci################################################################################
3162306a36Sopenharmony_ci# This is a sample script which shows how to use vUDC with ConfigFS gadgets
3262306a36Sopenharmony_ci################################################################################
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci# Stop script on error
3562306a36Sopenharmony_ciset -e
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci################################################################################
3862306a36Sopenharmony_ci# Create your USB gadget
3962306a36Sopenharmony_ci# You may use bare ConfigFS interface (as below)
4062306a36Sopenharmony_ci# or libusbgx or gt toool
4162306a36Sopenharmony_ci# Instead of ConfigFS gadgets you may use any of legacy gadgets.
4262306a36Sopenharmony_ci################################################################################
4362306a36Sopenharmony_ciCONFIGFS_MOUNT_POINT="/sys/kernel/config"
4462306a36Sopenharmony_ciGADGET_NAME="g1"
4562306a36Sopenharmony_ciID_VENDOR="0x1d6b"
4662306a36Sopenharmony_ciID_PRODUCT="0x0104"
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cicd ${CONFIGFS_MOUNT_POINT}/usb_gadget
4962306a36Sopenharmony_ci# Create a new USB gadget
5062306a36Sopenharmony_cimkdir ${GADGET_NAME}
5162306a36Sopenharmony_cicd ${GADGET_NAME}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci# This gadget contains one function - ACM (serial port over USB)
5462306a36Sopenharmony_ciFUNC_DIR="functions/acm.ser0"
5562306a36Sopenharmony_cimkdir ${FUNC_DIR}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci# Just one configuration
5862306a36Sopenharmony_cimkdir configs/c.1
5962306a36Sopenharmony_ciln -s ${FUNC_DIR} configs/c.1
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci# Set our gadget identity
6262306a36Sopenharmony_ciecho ${ID_VENDOR} > idVendor
6362306a36Sopenharmony_ciecho ${ID_PRODUCT} > idProduct
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci################################################################################
6662306a36Sopenharmony_ci# Load vudc-module if vudc is not available
6762306a36Sopenharmony_ci# You may change value of num param to get more than one vUDC instance
6862306a36Sopenharmony_ci################################################################################
6962306a36Sopenharmony_ci[[ -d /sys/class/udc/usbip-vudc.0 ]] || modprobe usbip-vudc num=1
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci################################################################################
7262306a36Sopenharmony_ci# Bind gadget to our vUDC
7362306a36Sopenharmony_ci# By default we bind to first one but you may change this if you would like
7462306a36Sopenharmony_ci# to use more than one instance
7562306a36Sopenharmony_ci################################################################################
7662306a36Sopenharmony_ciecho "usbip-vudc.0" > UDC
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci################################################################################
7962306a36Sopenharmony_ci# Let's now run our usbip daemon in a USB device mode
8062306a36Sopenharmony_ci################################################################################
8162306a36Sopenharmony_ciusbipd --device &
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci################################################################################
8462306a36Sopenharmony_ci# Now your USB gadget is available using USB/IP protocol.
8562306a36Sopenharmony_ci# To prepare your client, you should ensure that usbip-vhci module is inside
8662306a36Sopenharmony_ci# your kernel. If it's not then you can load it:
8762306a36Sopenharmony_ci#
8862306a36Sopenharmony_ci# $ modprobe usbip-vhci
8962306a36Sopenharmony_ci#
9062306a36Sopenharmony_ci# To check availability of your gadget you may try to list devices exported
9162306a36Sopenharmony_ci# on a remote server:
9262306a36Sopenharmony_ci#
9362306a36Sopenharmony_ci# $ modprobe usbip-vhci
9462306a36Sopenharmony_ci# $ usbip list -r $SERVER_IP
9562306a36Sopenharmony_ci# Exportable USB devices
9662306a36Sopenharmony_ci# ======================
9762306a36Sopenharmony_ci# usbipd: info: request 0x8005(6): complete
9862306a36Sopenharmony_ci#  - 127.0.0.1
9962306a36Sopenharmony_ci# usbip-vudc.0: Linux Foundation : unknown product (1d6b:0104)
10062306a36Sopenharmony_ci#            : /sys/devices/platform/usbip-vudc.0
10162306a36Sopenharmony_ci#            : (Defined at Interface level) (00/00/00)
10262306a36Sopenharmony_ci#
10362306a36Sopenharmony_ci# To attach this device to your client you may use:
10462306a36Sopenharmony_ci#
10562306a36Sopenharmony_ci# $ usbip attach -r $SERVER_IP -d usbip-vudc.0
10662306a36Sopenharmony_ci#
10762306a36Sopenharmony_ci################################################################################
108