1*4882a593Smuzhiyun#!/bin/bash 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun################################################################################ 4*4882a593Smuzhiyun# This is free and unencumbered software released into the public domain. 5*4882a593Smuzhiyun# 6*4882a593Smuzhiyun# Anyone is free to copy, modify, publish, use, compile, sell, or 7*4882a593Smuzhiyun# distribute this software, either in source code form or as a compiled 8*4882a593Smuzhiyun# binary, for any purpose, commercial or non-commercial, and by any 9*4882a593Smuzhiyun# means. 10*4882a593Smuzhiyun# 11*4882a593Smuzhiyun# In jurisdictions that recognize copyright laws, the author or authors 12*4882a593Smuzhiyun# of this software dedicate any and all copyright interest in the 13*4882a593Smuzhiyun# software to the public domain. We make this dedication for the benefit 14*4882a593Smuzhiyun# of the public at large and to the detriment of our heirs and 15*4882a593Smuzhiyun# successors. We intend this dedication to be an overt act of 16*4882a593Smuzhiyun# relinquishment in perpetuity of all present and future rights to this 17*4882a593Smuzhiyun# software under copyright law. 18*4882a593Smuzhiyun# 19*4882a593Smuzhiyun# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20*4882a593Smuzhiyun# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21*4882a593Smuzhiyun# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22*4882a593Smuzhiyun# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 23*4882a593Smuzhiyun# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 24*4882a593Smuzhiyun# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25*4882a593Smuzhiyun# OTHER DEALINGS IN THE SOFTWARE. 26*4882a593Smuzhiyun# 27*4882a593Smuzhiyun# For more information, please refer to <https://unlicense.org/> 28*4882a593Smuzhiyun################################################################################ 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun################################################################################ 31*4882a593Smuzhiyun# This is a sample script which shows how to use vUDC with ConfigFS gadgets 32*4882a593Smuzhiyun################################################################################ 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun# Stop script on error 35*4882a593Smuzhiyunset -e 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun################################################################################ 38*4882a593Smuzhiyun# Create your USB gadget 39*4882a593Smuzhiyun# You may use bare ConfigFS interface (as below) 40*4882a593Smuzhiyun# or libusbgx or gt toool 41*4882a593Smuzhiyun# Instead of ConfigFS gadgets you may use any of legacy gadgets. 42*4882a593Smuzhiyun################################################################################ 43*4882a593SmuzhiyunCONFIGFS_MOUNT_POINT="/sys/kernel/config" 44*4882a593SmuzhiyunGADGET_NAME="g1" 45*4882a593SmuzhiyunID_VENDOR="0x1d6b" 46*4882a593SmuzhiyunID_PRODUCT="0x0104" 47*4882a593Smuzhiyun 48*4882a593Smuzhiyuncd ${CONFIGFS_MOUNT_POINT}/usb_gadget 49*4882a593Smuzhiyun# Create a new USB gadget 50*4882a593Smuzhiyunmkdir ${GADGET_NAME} 51*4882a593Smuzhiyuncd ${GADGET_NAME} 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun# This gadget contains one function - ACM (serial port over USB) 54*4882a593SmuzhiyunFUNC_DIR="functions/acm.ser0" 55*4882a593Smuzhiyunmkdir ${FUNC_DIR} 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun# Just one configuration 58*4882a593Smuzhiyunmkdir configs/c.1 59*4882a593Smuzhiyunln -s ${FUNC_DIR} configs/c.1 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun# Set our gadget identity 62*4882a593Smuzhiyunecho ${ID_VENDOR} > idVendor 63*4882a593Smuzhiyunecho ${ID_PRODUCT} > idProduct 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun################################################################################ 66*4882a593Smuzhiyun# Load vudc-module if vudc is not available 67*4882a593Smuzhiyun# You may change value of num param to get more than one vUDC instance 68*4882a593Smuzhiyun################################################################################ 69*4882a593Smuzhiyun[[ -d /sys/class/udc/usbip-vudc.0 ]] || modprobe usbip-vudc num=1 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun################################################################################ 72*4882a593Smuzhiyun# Bind gadget to our vUDC 73*4882a593Smuzhiyun# By default we bind to first one but you may change this if you would like 74*4882a593Smuzhiyun# to use more than one instance 75*4882a593Smuzhiyun################################################################################ 76*4882a593Smuzhiyunecho "usbip-vudc.0" > UDC 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun################################################################################ 79*4882a593Smuzhiyun# Let's now run our usbip daemon in a USB device mode 80*4882a593Smuzhiyun################################################################################ 81*4882a593Smuzhiyunusbipd --device & 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun################################################################################ 84*4882a593Smuzhiyun# Now your USB gadget is available using USB/IP protocol. 85*4882a593Smuzhiyun# To prepare your client, you should ensure that usbip-vhci module is inside 86*4882a593Smuzhiyun# your kernel. If it's not then you can load it: 87*4882a593Smuzhiyun# 88*4882a593Smuzhiyun# $ modprobe usbip-vhci 89*4882a593Smuzhiyun# 90*4882a593Smuzhiyun# To check availability of your gadget you may try to list devices exported 91*4882a593Smuzhiyun# on a remote server: 92*4882a593Smuzhiyun# 93*4882a593Smuzhiyun# $ modprobe usbip-vhci 94*4882a593Smuzhiyun# $ usbip list -r $SERVER_IP 95*4882a593Smuzhiyun# Exportable USB devices 96*4882a593Smuzhiyun# ====================== 97*4882a593Smuzhiyun# usbipd: info: request 0x8005(6): complete 98*4882a593Smuzhiyun# - 127.0.0.1 99*4882a593Smuzhiyun# usbip-vudc.0: Linux Foundation : unknown product (1d6b:0104) 100*4882a593Smuzhiyun# : /sys/devices/platform/usbip-vudc.0 101*4882a593Smuzhiyun# : (Defined at Interface level) (00/00/00) 102*4882a593Smuzhiyun# 103*4882a593Smuzhiyun# To attach this device to your client you may use: 104*4882a593Smuzhiyun# 105*4882a593Smuzhiyun# $ usbip attach -r $SERVER_IP -d usbip-vudc.0 106*4882a593Smuzhiyun# 107*4882a593Smuzhiyun################################################################################ 108