[go: up one dir, main page]

Skip to content

Commit

Permalink
Enable / Disable support for USB Gadget (allows background updates)
Browse files Browse the repository at this point in the history
File correctly placed for installation
  • Loading branch information
bousqi committed Feb 10, 2019
1 parent f60e1bf commit 69ed6fb
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 26 deletions.
8 changes: 6 additions & 2 deletions install_usbgadget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ fi
echo "Installing USB gadget script..."
# Installing as a system service to enable it on boot
mkdir /usr/lib/systemd/system
cp resources/myusbgadget /usr/local/bin/myusbgadget
cp resources/myusbgadget /usr/local/bin/bridge_enable.sh
cp resources/myusbgadget /usr/local/bin/
cp resources/usbgadget_enable.sh /usr/local/bin/
cp resources/usbgadget_disable.sh /usr/local/bin/
cp resources/bridge_enable.sh /usr/local/bin/
chmod +x /usr/local/bin/myusbgadget
chmod +x /usr/local/bin/usbgadget_enable.sh
chmod +x /usr/local/bin/usbgadget_disable.sh
chmod +x /usr/local/bin/bridge_enable.sh
cp resources/myusbgadget.service /usr/lib/systemd/system/
systemctl enable myusbgadget
Expand Down
2 changes: 1 addition & 1 deletion bridge_enable.sh → resources/bridge_enable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables -A FORWARD -i wlan0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i usb0 -o wlan0 -j ACCEPT

#sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

65 changes: 45 additions & 20 deletions resources/myusbgadget
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,64 @@
# https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
# https://www.kernel.org/doc/Documentation/ABI/testing/configfs-usb-gadget

echo "Configuring USB Gadget..."

#remount
modprobe libcomposite

cd /sys/kernel/config/usb_gadget/
mkdir g && cd g

echo 0x243a > idVendor # Peugeot
echo 0x0001 > idProduct # Connect Apps
echo 0x0226 > bcdDevice # v2.26
echo 0x0200 > bcdUSB # USB 2.0
if [ -f /media/xfer/.EEM ]; then
echo 0x243a > idVendor # Peugeot
echo 0x0001 > idProduct # Connect Apps
echo 0x0226 > bcdDevice # v2.26
echo 0x0200 > bcdUSB # USB 2.0

# Composite support for Windows
#echo 0xEF > bDeviceClass
#echo 0x02 > bDeviceSubClass
#echo 0x01 > bDeviceProtocol

# Composite support for Windows
#echo 0xEF > bDeviceClass
#echo 0x02 > bDeviceSubClass
#echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
echo "CAFEDECA" > strings/0x409/serialnumber
echo "Aperture Science" > strings/0x409/manufacturer
echo "PSAKey" > strings/0x409/product
else
echo 0x8564 > idVendor # Transcend
echo 0x4000 > idProduct #
echo 0x0034 > bcdDevice # 0.34
echo 0x0200 > bcdUSB # USB 2.0

mkdir -p strings/0x409
echo "CAFEDECA" > strings/0x409/serialnumber
echo "Aperture Science" > strings/0x409/manufacturer
echo "PSAKey" > strings/0x409/product
mkdir -p strings/0x409
echo "000000000020" > strings/0x409/serialnumber
echo "TS-RDF5" > strings/0x409/manufacturer
echo "Transcend" > strings/0x409/product
fi

#mkdir -p functions/mass_storage.usb0 # mass storage
mkdir -p functions/eem.usb0 # network
echo "Enabling MassStorage function..."
mkdir -p functions/mass_storage.usb0 # mass storage
if [ -f /media/xfer/.EEM ]; then
echo "Enabling EEM function..."
mkdir -p functions/eem.usb0 # network
fi

mkdir -p configs/c.1
echo 500 > configs/c.1/MaxPower
#ln -s functions/mass_storage.usb0 configs/c.1/
ln -s functions/eem.usb0 configs/c.1/
ln -s functions/mass_storage.usb0 configs/c.1/
if [ -f /media/xfer/.EEM ]; then
ln -s functions/eem.usb0 configs/c.1/
fi

#echo /dev/mmcblk0p4 > /sys/kernel/config/usb_gadget/g/functions/mass_storage.usb0/lun.0/file
#echo 1 > functions/mass_storage.usb0/lun.0/removable
echo /dev/mmcblk0p3 > /sys/kernel/config/usb_gadget/g/functions/mass_storage.usb0/lun.0/file
echo 1 > functions/mass_storage.usb0/lun.0/removable

udevadm settle -t 5 || :
ls /sys/class/udc/ > UDC

# enables internet sharing
/usr/local/bin/bridge_enable.sh
if [ -f /media/xfer/.EEM ]; then
echo "Enabling Internet sharing..."
# enables internet sharing
/usr/local/bin/bridge_enable.sh
fi

7 changes: 4 additions & 3 deletions resources/myusbgadget.service
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[Unit]
Description=My USB gadget
After=systemd-modules-load.service apache2.service
Description=USB Gadget Service
After=systemd-modules-load.service
Before=networking.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/myusbgadget
ExecStart=/usr/local/bin/usbgadget_enable.sh
ExecStop=/usr/local/bin//usbgadget_disable.sh

[Install]
WantedBy=sysinit.target
11 changes: 11 additions & 0 deletions resources/usbgadget_disable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

if [ "$(whoami)" != "root" ]; then
echo "This script needs to be run as root. Please use sudo."
exit 1
fi

echo "Stopping USB Gadget..."

echo "" > /sys/kernel/config/usb_gadget/g/UDC
# sh -c "echo '' > /sys/kernel/config/usb_gadget/g/UDC"
16 changes: 16 additions & 0 deletions resources/usbgadget_enable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

if [ "$(whoami)" != "root" ]; then
echo "This script needs to be run as root. Please use sudo."
exit 1
fi

echo "Starting USB Gadget..."

if [ ! -d "/sys/kernel/config/usb_gadget/g/configs/c.1" ]; then
# usb gadget not yet created
/usr/local/bin/myusbgadget
fi

ls /sys/class/udc/ > /sys/kernel/config/usb_gadget/g/UDC

0 comments on commit 69ed6fb

Please sign in to comment.