18c2ecf20Sopenharmony_ci#!/bin/bash
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci################################################################################
48c2ecf20Sopenharmony_ci# This is free and unencumbered software released into the public domain.
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Anyone is free to copy, modify, publish, use, compile, sell, or
78c2ecf20Sopenharmony_ci# distribute this software, either in source code form or as a compiled
88c2ecf20Sopenharmony_ci# binary, for any purpose, commercial or non-commercial, and by any
98c2ecf20Sopenharmony_ci# means.
108c2ecf20Sopenharmony_ci#
118c2ecf20Sopenharmony_ci# In jurisdictions that recognize copyright laws, the author or authors
128c2ecf20Sopenharmony_ci# of this software dedicate any and all copyright interest in the
138c2ecf20Sopenharmony_ci# software to the public domain. We make this dedication for the benefit
148c2ecf20Sopenharmony_ci# of the public at large and to the detriment of our heirs and
158c2ecf20Sopenharmony_ci# successors. We intend this dedication to be an overt act of
168c2ecf20Sopenharmony_ci# relinquishment in perpetuity of all present and future rights to this
178c2ecf20Sopenharmony_ci# software under copyright law.
188c2ecf20Sopenharmony_ci#
198c2ecf20Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
208c2ecf20Sopenharmony_ci# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
218c2ecf20Sopenharmony_ci# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
228c2ecf20Sopenharmony_ci# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
238c2ecf20Sopenharmony_ci# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
248c2ecf20Sopenharmony_ci# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
258c2ecf20Sopenharmony_ci# OTHER DEALINGS IN THE SOFTWARE.
268c2ecf20Sopenharmony_ci#
278c2ecf20Sopenharmony_ci# For more information, please refer to <https://unlicense.org/>
288c2ecf20Sopenharmony_ci################################################################################
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci################################################################################
318c2ecf20Sopenharmony_ci# This is a sample script which shows how to use vUDC with ConfigFS gadgets
328c2ecf20Sopenharmony_ci################################################################################
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci# Stop script on error
358c2ecf20Sopenharmony_ciset -e
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci################################################################################
388c2ecf20Sopenharmony_ci# Create your USB gadget
398c2ecf20Sopenharmony_ci# You may use bare ConfigFS interface (as below)
408c2ecf20Sopenharmony_ci# or libusbgx or gt toool
418c2ecf20Sopenharmony_ci# Instead of ConfigFS gadgets you may use any of legacy gadgets.
428c2ecf20Sopenharmony_ci################################################################################
438c2ecf20Sopenharmony_ciCONFIGFS_MOUNT_POINT="/sys/kernel/config"
448c2ecf20Sopenharmony_ciGADGET_NAME="g1"
458c2ecf20Sopenharmony_ciID_VENDOR="0x1d6b"
468c2ecf20Sopenharmony_ciID_PRODUCT="0x0104"
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cicd ${CONFIGFS_MOUNT_POINT}/usb_gadget
498c2ecf20Sopenharmony_ci# Create a new USB gadget
508c2ecf20Sopenharmony_cimkdir ${GADGET_NAME}
518c2ecf20Sopenharmony_cicd ${GADGET_NAME}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci# This gadget contains one function - ACM (serial port over USB)
548c2ecf20Sopenharmony_ciFUNC_DIR="functions/acm.ser0"
558c2ecf20Sopenharmony_cimkdir ${FUNC_DIR}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci# Just one configuration
588c2ecf20Sopenharmony_cimkdir configs/c.1
598c2ecf20Sopenharmony_ciln -s ${FUNC_DIR} configs/c.1
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci# Set our gadget identity
628c2ecf20Sopenharmony_ciecho ${ID_VENDOR} > idVendor
638c2ecf20Sopenharmony_ciecho ${ID_PRODUCT} > idProduct
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci################################################################################
668c2ecf20Sopenharmony_ci# Load vudc-module if vudc is not available
678c2ecf20Sopenharmony_ci# You may change value of num param to get more than one vUDC instance
688c2ecf20Sopenharmony_ci################################################################################
698c2ecf20Sopenharmony_ci[[ -d /sys/class/udc/usbip-vudc.0 ]] || modprobe usbip-vudc num=1
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci################################################################################
728c2ecf20Sopenharmony_ci# Bind gadget to our vUDC
738c2ecf20Sopenharmony_ci# By default we bind to first one but you may change this if you would like
748c2ecf20Sopenharmony_ci# to use more than one instance
758c2ecf20Sopenharmony_ci################################################################################
768c2ecf20Sopenharmony_ciecho "usbip-vudc.0" > UDC
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci################################################################################
798c2ecf20Sopenharmony_ci# Let's now run our usbip daemon in a USB device mode
808c2ecf20Sopenharmony_ci################################################################################
818c2ecf20Sopenharmony_ciusbipd --device &
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci################################################################################
848c2ecf20Sopenharmony_ci# Now your USB gadget is available using USB/IP protocol.
858c2ecf20Sopenharmony_ci# To prepare your client, you should ensure that usbip-vhci module is inside
868c2ecf20Sopenharmony_ci# your kernel. If it's not then you can load it:
878c2ecf20Sopenharmony_ci#
888c2ecf20Sopenharmony_ci# $ modprobe usbip-vhci
898c2ecf20Sopenharmony_ci#
908c2ecf20Sopenharmony_ci# To check availability of your gadget you may try to list devices exported
918c2ecf20Sopenharmony_ci# on a remote server:
928c2ecf20Sopenharmony_ci#
938c2ecf20Sopenharmony_ci# $ modprobe usbip-vhci
948c2ecf20Sopenharmony_ci# $ usbip list -r $SERVER_IP
958c2ecf20Sopenharmony_ci# Exportable USB devices
968c2ecf20Sopenharmony_ci# ======================
978c2ecf20Sopenharmony_ci# usbipd: info: request 0x8005(6): complete
988c2ecf20Sopenharmony_ci#  - 127.0.0.1
998c2ecf20Sopenharmony_ci# usbip-vudc.0: Linux Foundation : unknown product (1d6b:0104)
1008c2ecf20Sopenharmony_ci#            : /sys/devices/platform/usbip-vudc.0
1018c2ecf20Sopenharmony_ci#            : (Defined at Interface level) (00/00/00)
1028c2ecf20Sopenharmony_ci#
1038c2ecf20Sopenharmony_ci# To attach this device to your client you may use:
1048c2ecf20Sopenharmony_ci#
1058c2ecf20Sopenharmony_ci# $ usbip attach -r $SERVER_IP -d usbip-vudc.0
1068c2ecf20Sopenharmony_ci#
1078c2ecf20Sopenharmony_ci################################################################################
108