This package allows easier configuration of USB gadgets via standard UCI
interface. So far only CDC/ACM has been implemented and tested.

Signed-off-by: Petr Štetiar <[email protected]>
---
 package/utils/usbgadget/Makefile               | 33 ++++++++++
 package/utils/usbgadget/files/usbgadget.config | 21 ++++++
 package/utils/usbgadget/files/usbgadget.init   | 90 ++++++++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 package/utils/usbgadget/Makefile
 create mode 100644 package/utils/usbgadget/files/usbgadget.config
 create mode 100644 package/utils/usbgadget/files/usbgadget.init

diff --git a/package/utils/usbgadget/Makefile b/package/utils/usbgadget/Makefile
new file mode 100644
index 0000000..e45bfe4
--- /dev/null
+++ b/package/utils/usbgadget/Makefile
@@ -0,0 +1,33 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=usbgadget
+PKG_RELEASE:=1
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/usbgadget
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=Utility for USB gadgets configuration
+endef
+
+define Package/usbgadget/conffiles
+/etc/config/usbgadget
+endef
+
+define Package/usbgadget/description
+ This package contains a small script which could be used for
+ configuration of USB gadgets over configfs kernel interface.
+endef
+
+define Build/Compile
+endef
+
+define Package/usbgadget/install
+       $(INSTALL_DIR) $(1)/etc/init.d
+       $(INSTALL_DIR) $(1)/etc/config
+       $(INSTALL_BIN) ./files/usbgadget.init $(1)/etc/init.d/usbgadget
+       $(INSTALL_DATA) ./files/usbgadget.config $(1)/etc/config/usbgadget
+endef
+
+$(eval $(call BuildPackage,usbgadget))
diff --git a/package/utils/usbgadget/files/usbgadget.config 
b/package/utils/usbgadget/files/usbgadget.config
new file mode 100644
index 0000000..65f6b5c
--- /dev/null
+++ b/package/utils/usbgadget/files/usbgadget.config
@@ -0,0 +1,21 @@
+config gadget
+       option type 'acm'
+       option name 'gadget-acm'
+       option manufacturer 'OpenWrt'
+       option product 'OpenWrt USB CDC/ACM'
+       option serial_number '007'
+       option usb_vid '0xbeef'
+       option usb_pid '0x1234'
+       option udc_dev 'ci_hdrc.0'
+       option disabled 1
+
+config gadget
+       option type 'acm+rndis'
+       option name 'gadget-acm-rndis'
+       option manufacturer 'OpenWrt'
+       option product 'OpenWrt USB CDC/ACM+RNDIS'
+       option serial_number '009'
+       option usb_vid '0xbeef'
+       option usb_pid '0x9abc'
+       option udc_dev 'ci_hdrc.0'
+       option disabled 1
diff --git a/package/utils/usbgadget/files/usbgadget.init 
b/package/utils/usbgadget/files/usbgadget.init
new file mode 100644
index 0000000..b49b6f3
--- /dev/null
+++ b/package/utils/usbgadget/files/usbgadget.init
@@ -0,0 +1,90 @@
+#!/bin/sh /etc/rc.common
+
+START=97
+
+load_gadget() {
+       local name
+       local type
+       local manufacturer
+       local product
+       local serial_number
+       local usb_vid
+       local usb_pid
+       local udc_dev
+       local disabled
+
+       config_get disabled $1 disabled
+       [ "$disabled" = "1" ] && return
+
+       config_get usb_vid $1 usb_vid
+       config_get usb_pid $1 usb_pid
+       config_get udc_dev $1 udc_dev
+       [ -z "$usb_vid" -o -z "$usb_pid" -o -z "$udc_dev" ] && return
+
+       config_get type $1 type "acm"
+       config_get name $1 name "${usb_vid}-acm"
+       config_get manufacturer $1 manufacturer "OpenWrt"
+       config_get product $1 product "OpenWrt USB ${type}"
+       config_get serial_number $1 serial_number "1922"
+
+       local path="/sys/kernel/config/usb_gadget/$name"
+       mkdir -p "$path"
+       echo "$usb_vid" > "$path/idVendor"
+       echo "$usb_pid" > "$path/idProduct"
+
+       local strings="$path/strings/0x409"
+       mkdir -p "$strings"
+       echo "$product" > "$strings/product"
+       echo "$manufacturer" > "$strings/manufacturer"
+       echo "$serial_number" > "$strings/serialnumber"
+
+       mkdir -p "$path/configs/$name.1"
+
+       case "$type" in
+       "acm")
+               mkdir -p "$path/functions/acm.0"
+               ln -sf "$path/functions/acm.0" "$path/configs/$name.1"
+               ;;
+       "acm+rndis")
+               mkdir -p "$path/functions/acm.0"
+               mkdir -p "$path/functions/rndis.0"
+               ln -sf "$path/functions/acm.0" "$path/configs/$name.1"
+               ln -sf "$path/functions/rndis.0" "$path/configs/$name.1"
+               ;;
+       esac
+
+       echo "$udc_dev" > "$path/UDC"
+}
+
+unload_gadget() {
+       local name
+       local usb_vid
+       local udc_dev
+       local disabled
+
+       config_get disabled $1 disabled
+       [ "$disabled" = "1" ] && return
+
+       config_get usb_vid $1 usb_vid
+       config_get udc_dev $1 udc_dev
+       [ -z "$usb_vid" -o -z "$udc_dev" ] && return
+
+       config_get name $1 name "${usb_vid}-acm"
+       [ -d "/sys/kernel/config/usb_gadget/$name" ] || return
+       echo '' > "/sys/kernel/config/usb_gadget/$name/UDC" > /dev/null
+}
+
+stop() {
+       [ -e /sys/kernel/config/usb_gadget ] || exit 0
+       config_load usbgadget
+       config_foreach unload_gadget gadget
+}
+
+start() {
+       grep -q configfs /proc/modules || exit 0
+       grep -q configfs /proc/mounts || mount -t configfs none 
/sys/kernel/config
+       [ -e /sys/kernel/config/usb_gadget ] || exit 0
+
+       config_load usbgadget
+       config_foreach load_gadget gadget
+}
-- 
1.9.1


_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to