usb-gadget: Migrate the repository and make non-board-specific

This migrates the imx-board-gadget stuff into usb-gadget and makes it
non-board-specific.

Change-Id: I8ce91eb6c277704079dc861ec0148b06229af58f
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..b1d4667
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+usb-gadget (0.0.1-1) beaker; urgency=medium
+
+  * Initial release.
+
+ -- AIY Projects <support-aiyprojects@google.com>  Wed, 25 Jul 2018 23:28:51 +0000
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..a28b961
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,13 @@
+Source: usb-gadget
+Section: devel
+Priority: optional
+Maintainer: AIY Team <support-aiyprojects@google.com>
+Build-Depends: debhelper (>= 9)
+Standards-Version: 3.9.8
+Homepage: https://aiyprojects.withgoogle.com/
+
+Package: usb-gadget
+Version: 0.0.1
+Architecture: all
+Depends: ${shlibs:Depends}, ${misc:Depends}, dnsmasq, openssh-server, network-manager
+Description: Sets up USB gadget ethernet and TTY for a given USB port
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..a7f0048
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,27 @@
+Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: usb-gadget
+Source: <https://aiyprojects.googlesource.com/>
+
+Files: *
+Copyright: Copyright 2018 Google, LLC <support-aiyprojects@google.com>
+License: Apache-2.0
+
+Files: debian/*
+Copyright: 2018 Google, LLC <support-aiyprojects@google.com>
+License: Apache-2.0
+
+License: Apache-2.0
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ .
+ https://www.apache.org/licenses/LICENSE-2.0
+ .
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ .
+ On Debian systems, the complete text of the Apache version 2.0 license
+ can be found in "/usr/share/common-licenses/Apache-2.0".
diff --git a/debian/postinst b/debian/postinst
new file mode 100644
index 0000000..b1502ba
--- /dev/null
+++ b/debian/postinst
@@ -0,0 +1,40 @@
+#!/bin/sh
+# postinst script for usb-gadget
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <postinst> `configure' <most-recently-configured-version>
+#        * <old-postinst> `abort-upgrade' <new version>
+#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
+#          <new-version>
+#        * <postinst> `abort-remove'
+#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
+#          <failed-install-package> <version> `removing'
+#          <conflicting-package> <version>
+# for details, see https://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+
+case "$1" in
+    configure)
+      systemctl daemon-reload || true
+      systemctl enable usb-gadget || true
+    ;;
+
+    abort-upgrade|abort-remove|abort-deconfigure)
+    ;;
+
+    *)
+        echo "postinst called with unknown argument \`$1'" >&2
+        exit 1
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0
diff --git a/debian/preinst b/debian/preinst
new file mode 100644
index 0000000..27d176a
--- /dev/null
+++ b/debian/preinst
@@ -0,0 +1,41 @@
+#!/bin/sh
+# preinst script for test
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <new-preinst> `install'
+#        * <new-preinst> `install' <old-version>
+#        * <new-preinst> `upgrade' <old-version>
+#        * <old-preinst> `abort-upgrade' <new-version>
+# for details, see https://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+
+divert_dnsmaq_conf() {
+  dpkg-divert --package usb-gadget --add --rename \
+      --divert /etc/dnsmasq.conf.orig /etc/dnsmasq.conf
+}
+
+
+case "$1" in
+    install|upgrade)
+    	divert_dnsmaq_conf
+    ;;
+
+    abort-upgrade)
+    ;;
+
+    *)
+        echo "preinst called with unknown argument \`$1'" >&2
+        exit 1
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0
diff --git a/debian/prerm b/debian/prerm
new file mode 100644
index 0000000..c0d9355
--- /dev/null
+++ b/debian/prerm
@@ -0,0 +1,47 @@
+#!/bin/sh
+# prerm script for usb-gadget
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <prerm> `remove'
+#        * <old-prerm> `upgrade' <new-version>
+#        * <new-prerm> `failed-upgrade' <old-version>
+#        * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
+#        * <deconfigured's-prerm> `deconfigure' `in-favour'
+#          <package-being-installed> <version> `removing'
+#          <conflicting-package> <version>
+# for details, see https://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+
+revert_dnsmasq_conf () {
+  rm /etc/dnsmasq.conf
+  dpkg-divert --package usb-gadget --remove --rename \
+    --divert /etc/dnsmasq.conf.orig /etc/dnsmasq.conf
+}
+
+case "$1" in
+    remove|upgrade|deconfigure)
+      revert_dnsmasq_conf
+      systemctl disable usb-gadget || true
+      systemctl stop usb-gadget || true
+      systemctl restart dnsmasq || true
+    ;;
+
+    failed-upgrade)
+    ;;
+
+    *)
+        echo "prerm called with unknown argument \`$1'" >&2
+        exit 1
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..747699d
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,27 @@
+#!/usr/bin/make -f
+# See debhelper(7) (uncomment to enable)
+# output every command that modifies files on the build system.
+export DH_VERBOSE = 1
+
+
+# see FEATURE AREAS in dpkg-buildflags(1)
+#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
+
+# see ENVIRONMENT in dpkg-buildflags(1)
+# package maintainers to append CFLAGS
+#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
+# package maintainers to append LDFLAGS
+#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
+
+
+%:
+	dh $@
+
+override_dh_systemd_enable:
+	dh_systemd_enable --name=usb-gadget
+
+# dh_make generated override targets
+# This is example for Cmake (See https://bugs.debian.org/641051 )
+#override_dh_auto_configure:
+#	dh_auto_configure -- #	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)
+
diff --git a/debian/usb-gadget-getty@.service b/debian/usb-gadget-getty@.service
new file mode 100644
index 0000000..bd3a53d
--- /dev/null
+++ b/debian/usb-gadget-getty@.service
@@ -0,0 +1,35 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Gadget Getty on %I
+Documentation=man:agetty(8) man:systemd-getty-generator(8)
+Documentation=http://0pointer.de/blog/projects/serial-console.html
+BindsTo=dev-%i.device
+After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service
+After=rc-local.service
+
+# If additional gettys are spawned during boot then we should make
+# sure that this is synchronized before getty.target, even though
+# getty.target didn't actually pull it in.
+Before=getty.target
+IgnoreOnIsolate=yes
+
+[Service]
+ExecStart=-/sbin/agetty -w --keep-baud 115200,38400,9600 %I $TERM
+Type=idle
+Restart=always
+UtmpIdentifier=%I
+TTYPath=/dev/%I
+TTYReset=yes
+TTYVHangup=yes
+KillMode=process
+IgnoreSIGPIPE=no
+SendSIGHUP=yes
+
+[Install]
+WantedBy=getty.target
diff --git a/debian/usb-gadget.install b/debian/usb-gadget.install
new file mode 100644
index 0000000..4e66675
--- /dev/null
+++ b/debian/usb-gadget.install
@@ -0,0 +1,2 @@
+usr /
+etc /
diff --git a/debian/usb-gadget.service b/debian/usb-gadget.service
new file mode 100644
index 0000000..df3e375
--- /dev/null
+++ b/debian/usb-gadget.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=USB Gadget
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/usr/bin/usb-gadget-start.sh
+ExecStop=/usr/bin/usb-gadget-stop.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/etc/dnsmasq.d/99-no-default-dns b/etc/dnsmasq.d/99-no-default-dns
new file mode 100644
index 0000000..295f531
--- /dev/null
+++ b/etc/dnsmasq.d/99-no-default-dns
@@ -0,0 +1,2 @@
+# Prevent advertising as an authoritative DNS server
+dhcp-option=6
diff --git a/etc/dnsmasq.d/99-no-default-route b/etc/dnsmasq.d/99-no-default-route
new file mode 100644
index 0000000..377bc22
--- /dev/null
+++ b/etc/dnsmasq.d/99-no-default-route
@@ -0,0 +1,6 @@
+# Prevent advertising our device as the default gateway.
+#
+# This prevents mis-routing of packets to the device, which causes it to act
+# like a black hole.
+
+dhcp-option=3
diff --git a/usr/bin/usb-gadget-start.sh b/usr/bin/usb-gadget-start.sh
new file mode 100755
index 0000000..5ec05a9
--- /dev/null
+++ b/usr/bin/usb-gadget-start.sh
@@ -0,0 +1,67 @@
+#!/bin/bash
+
+# Remove the aiy ethernet connection
+nmcli connection delete usb0 || true
+
+# Setup the USB Gadget device
+CONFIGFS_PATH=/sys/kernel/config
+USB_GADGET_PATH=$CONFIGFS_PATH/usb_gadget
+AIY_GADGET_PATH=$USB_GADGET_PATH/g1
+mkdir -p $AIY_GADGET_PATH
+
+echo 0x0106 > $AIY_GADGET_PATH/idProduct
+echo 0x04e8 > $AIY_GADGET_PATH/idVendor
+mkdir -p $AIY_GADGET_PATH/strings/0x409
+hostname > $AIY_GADGET_PATH/strings/0x409/serialnumber
+echo Google,LLC > $AIY_GADGET_PATH/strings/0x409/manufacturer
+echo "Mendel" > $AIY_GADGET_PATH/strings/0x409/product
+
+mkdir -p $AIY_GADGET_PATH/configs/c.1
+echo 500 > $AIY_GADGET_PATH/configs/c.1/MaxPower
+mkdir -p $AIY_GADGET_PATH/configs/c.1/strings/0x409
+echo "Conf 1" > $AIY_GADGET_PATH/configs/c.1/strings/0x409/configuration
+
+mkdir $AIY_GADGET_PATH/functions/acm.0
+ln -s  $AIY_GADGET_PATH/functions/acm.0 $AIY_GADGET_PATH/configs/c.1
+
+mkdir $AIY_GADGET_PATH/functions/ecm.0
+ln -s $AIY_GADGET_PATH/functions/ecm.0 $AIY_GADGET_PATH/configs/c.1
+echo "02:22:78:0d:f6:df" > $AIY_GADGET_PATH/functions/ecm.0/dev_addr
+
+UDC=$(echo -n $(ls /sys/class/udc/|head -c -1 -n 1))
+echo $UDC
+
+sleep 1
+echo $UDC > $AIY_GADGET_PATH/UDC
+
+# Setup the ttyGS0 terminal
+systemctl enable usb-gadget-getty@ttyGS0.service
+systemctl start usb-gadget-getty@ttyGS0.service
+
+sleep 3
+
+# Set a static ip for the gadget ethernet
+$(nmcli con show | grep -q usb0)
+aiy_conn_exist=$?
+if [[ "$aiy_conn_exist" -eq 0 ]]; then
+  echo "usb0 connection already exists"
+else
+  nmcli con add con-name usb0 ifname usb0 type ethernet ip4 192.168.100.2/24
+fi
+
+nmcli con up id usb0
+
+# Setup the dhcp server and ssh
+
+cat << EOF > /etc/dnsmasq.conf
+# Configuration file for dnsmasq.
+port=0
+interface=usb0
+domain=local
+dhcp-range=192.168.100.50,192.168.100.99,255.255.255.0,12h
+dhcp-lease-max=150
+log-dhcp
+EOF
+
+systemctl restart dnsmasq
+systemctl restart sshd
diff --git a/usr/bin/usb-gadget-stop.sh b/usr/bin/usb-gadget-stop.sh
new file mode 100755
index 0000000..95bc0c6
--- /dev/null
+++ b/usr/bin/usb-gadget-stop.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+$(nmcli con show | grep -q usb0)
+aiy_conn_exist=$?
+if [[ "$aiy_conn_exist" -eq 0 ]]; then
+  nmcli con delete usb0
+else
+  echo "aiy-usb0 does not exist"
+fi
+
+systemctl disable usb-gadget-getty@ttyGS0.service
+systemctl stop usb-gadget-getty@ttyGS0.service
+
+CONFIGFS_PATH=/sys/kernel/config
+USB_GADGET_PATH=$CONFIGFS_PATH/usb_gadget
+AIY_GADGET_PATH=$USB_GADGET_PATH/g1
+
+echo "" > $AIY_GADGET_PATH/UDC
+rm -rf $AIY_GADGET_PATH/configs/c.1/acm.0
+rm -rf $AIY_GADGET_PATH/configs/c.1/ecm.0
+
+rmdir $AIY_GADGET_PATH/configs/c.1/strings/0x409
+rmdir $AIY_GADGET_PATH/configs/c.1
+rmdir $AIY_GADGET_PATH/functions/acm.0
+rmdir $AIY_GADGET_PATH/functions/ecm.0
+
+rmdir $AIY_GADGET_PATH/strings/0x409
+rmdir $AIY_GADGET_PATH