|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +#set -xv |
| 4 | + |
| 5 | +SELF=${SELF:-$(basename $0)} |
| 6 | + |
| 7 | +source /usr/local/lib/utils |
| 8 | +source usb_gadget |
| 9 | + |
| 10 | +# The composite gadget directory |
| 11 | +GADGET=/sys/kernel/config/usb_gadget/FunKey |
| 12 | + |
| 13 | +# USB VID for Intel |
| 14 | +ID_VENDOR="0x8087" |
| 15 | + |
| 16 | +# USB PID for Multifunction Composite Gadget |
| 17 | +ID_PRODUCT="0x011e" |
| 18 | + |
| 19 | +# Get the CPU serial number |
| 20 | +SERIAL="$(grep Serial /proc/cpuinfo | sed 's/Serial\s*: \(\w*\)/\1/')" |
| 21 | + |
| 22 | +# Initialize the ADB |
| 23 | +init_adb() { |
| 24 | + |
| 25 | + # Don't proceed if existing gadget is present |
| 26 | + if [ -e ${GADGET} ]; then |
| 27 | + return 0 |
| 28 | + fi |
| 29 | + |
| 30 | + # Get the legacy drivers out of the way |
| 31 | + modprobe -r g_ether |
| 32 | + modprobe -r g_mass_storage |
| 33 | + |
| 34 | + # Load the libcomposite USB driver, configfs and various other drivers |
| 35 | + modprobe libcomposite |
| 36 | + modprobe usb_f_serial |
| 37 | + modprobe usb_f_fs |
| 38 | + modprobe usb_f_acm |
| 39 | + |
| 40 | + # USB Device Controller Driver |
| 41 | + local udc_driver=$(ls /sys/class/udc | cut -f1 | head -n 1) |
| 42 | + |
| 43 | + # Create our gadget directory |
| 44 | + mkdir ${GADGET} |
| 45 | + mkdir ${GADGET}/strings/0x409 |
| 46 | + mkdir ${GADGET}/configs/FunKey.1 |
| 47 | + mkdir ${GADGET}/configs/FunKey.1/strings/0x409 |
| 48 | + mkdir ${GADGET}/functions/acm.GS0 |
| 49 | + mkdir ${GADGET}/functions/ffs.adb |
| 50 | + |
| 51 | + # USB VID and PID |
| 52 | + echo ${ID_VENDOR} > ${GADGET}/idVendor |
| 53 | + echo ${ID_PRODUCT} > ${GADGET}/idProduct |
| 54 | + |
| 55 | + # Device String Descriptiors |
| 56 | + echo "Intel" > ${GADGET}/strings/0x409/manufacturer |
| 57 | + echo "FunKey S" > ${GADGET}/strings/0x409/product |
| 58 | + echo ${SERIAL} > ${GADGET}/strings/0x409/serialnumber |
| 59 | + |
| 60 | + # Configuration |
| 61 | + |
| 62 | + # Maximum power is 120 mA |
| 63 | + echo 120 > ${GADGET}/configs/FunKey.1/MaxPower |
| 64 | + |
| 65 | + # Configuration String Descriptors |
| 66 | + echo "ADB+CDC" > ${GADGET}/configs/FunKey.1/strings/0x409/configuration |
| 67 | + |
| 68 | + # Add the ACM function to the FunKey.1 configuration |
| 69 | + ln -s ${GADGET}/functions/acm.GS0 ${GADGET}/configs/FunKey.1 |
| 70 | + |
| 71 | + # Add the FunctionFS function to the FunKey.1 configuration |
| 72 | + ln -s ${GADGET}/functions/ffs.adb ${GADGET}/configs/FunKey.1 |
| 73 | + |
| 74 | + # Create the function filesystem |
| 75 | + mkdir /dev/usb-ffs |
| 76 | + mkdir /dev/usb-ffs/adb |
| 77 | + |
| 78 | + # Mount the ADB function filesystem |
| 79 | + mount -t functionfs adb /dev/usb-ffs/adb |
| 80 | + |
| 81 | + # Bring up the loopback network |
| 82 | + ifup lo |
| 83 | + |
| 84 | + # Launch the ADB daemon |
| 85 | + adbd >/dev/null & |
| 86 | + |
| 87 | + # Sleeping is required to wait for the UDC to come up |
| 88 | + sleep 5 |
| 89 | + |
| 90 | + # Bind the USB Gadget |
| 91 | + echo ${udc_driver} > ${GADGET}/UDC |
| 92 | + return 0 |
| 93 | +} |
| 94 | + |
| 95 | +# Deinitialize the ADB |
| 96 | +deinit_adb() { |
| 97 | + |
| 98 | + # Unbind the device |
| 99 | + echo > ${GADGET}/UDC |
| 100 | + |
| 101 | + # Kill the ADB daemon |
| 102 | + killall adbd |
| 103 | + |
| 104 | + # Bring down the local network |
| 105 | + ifdown lo |
| 106 | + |
| 107 | + # Unmount the ADB function filesystem |
| 108 | + umount /dev/usb-ffs/adb |
| 109 | + |
| 110 | + # Delete the function filesystem |
| 111 | + rmdir /dev/usb-ffs/adb |
| 112 | + rmdir /dev/usb-ffs |
| 113 | + |
| 114 | + # Remove functions from configurations |
| 115 | + rm ${GADGET}/configs/FunKey.1/acm.GS0 |
| 116 | + rm ${GADGET}/configs/FunKey.1/ffs.adb |
| 117 | + |
| 118 | + # Remove string directories in configurations |
| 119 | + rmdir ${GADGET}/configs/FunKey.1/strings/0x409 |
| 120 | + |
| 121 | + # Remove configurations |
| 122 | + rmdir ${GADGET}/configs/FunKey.1 |
| 123 | + |
| 124 | + # Remove functions |
| 125 | + rmdir ${GADGET}/functions/acm.GS0 |
| 126 | + rmdir ${GADGET}/functions/ffs.adb |
| 127 | + |
| 128 | + # Remove strings |
| 129 | + rmdir ${GADGET}/strings/0x409 |
| 130 | + |
| 131 | + # Finallyy remove the gadget |
| 132 | + rmdir ${GADGET} |
| 133 | + |
| 134 | + # Unload the kernel modules |
| 135 | + modprobe -r usb_f_serial usb_f_fs usb_f_acm |
| 136 | +} |
| 137 | + |
| 138 | +case "$1" in |
| 139 | + |
| 140 | + start) |
| 141 | + deinit_usb_gadget |
| 142 | + init_adb |
| 143 | + ;; |
| 144 | + |
| 145 | + stop) |
| 146 | + deinit_adb |
| 147 | + init_usb_gadget |
| 148 | + ;; |
| 149 | + |
| 150 | + *) |
| 151 | + die 15 "Usage $0 {start|stop}" |
| 152 | + ;; |
| 153 | +esac |
| 154 | +exit $? |
0 commit comments