1#!/bin/bash
2
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, or
7# distribute this software, either in source code form or as a compiled
8# binary, for any purpose, commercial or non-commercial, and by any
9# means.
10#
11# In jurisdictions that recognize copyright laws, the author or authors
12# of this software dedicate any and all copyright interest in the
13# software to the public domain. We make this dedication for the benefit
14# of the public at large and to the detriment of our heirs and
15# successors. We intend this dedication to be an overt act of
16# relinquishment in perpetuity of all present and future rights to this
17# 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 OF
21# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23# 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 OR
25# OTHER DEALINGS IN THE SOFTWARE.
26#
27# For more information, please refer to <http://unlicense.org/>
28################################################################################
29
30################################################################################
31# This is a sample script which shows how to use vUDC with ConfigFS gadgets
32################################################################################
33
34# Stop script on error
35set -e
36
37################################################################################
38# Create your USB gadget
39# You may use bare ConfigFS interface (as below)
40# or libusbgx or gt toool
41# 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_gadget
49# Create a new USB gadget
50mkdir ${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 configuration
58mkdir configs/c.1
59ln -s ${FUNC_DIR} configs/c.1
60
61# Set our gadget identity
62echo ${ID_VENDOR} > idVendor
63echo ${ID_PRODUCT} > idProduct
64
65################################################################################
66# Load vudc-module if vudc is not available
67# You may change value of num param to get more than one vUDC instance
68################################################################################
69[[ -d /sys/class/udc/usbip-vudc.0 ]] || modprobe usbip-vudc num=1
70
71################################################################################
72# Bind gadget to our vUDC
73# By default we bind to first one but you may change this if you would like
74# to use more than one instance
75################################################################################
76echo "usbip-vudc.0" > UDC
77
78################################################################################
79# Let's now run our usbip daemon in a USB device mode
80################################################################################
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 inside
86# your kernel. If it's not then you can load it:
87#
88# $ modprobe usbip-vhci
89#
90# To check availability of your gadget you may try to list devices exported
91# on a remote server:
92#
93# $ modprobe usbip-vhci
94# $ usbip list -r $SERVER_IP
95# Exportable USB devices
96# ======================
97# usbipd: info: request 0x8005(6): complete
98#  - 127.0.0.1
99# usbip-vudc.0: Linux Foundation : unknown product (1d6b:0104)
100#            : /sys/devices/platform/usbip-vudc.0
101#            : (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.0
106#
107################################################################################
108