[OpenWrt-Devel] [PATCH] usbgadget: Add new package

Michael Heimpold mhei at heimpold.de
Sat Feb 2 04:09:40 EST 2019


Hi Petr,

Am Dienstag, 29. Januar 2019, 15:30:35 CET schrieb Petr Štetiar:
> This package allows easier configuration of USB gadgets via standard UCI
> interface. So far only CDC/ACM has been implemented and tested.
> 

thanks for working on this. A few idea inside...

> Signed-off-by: Petr Štetiar <ynezz at true.cz>
> ---
>  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'
I think we could use defaults from /etc/device_info file in case
manufacturer and product are not set in config. This allow overriding the 
values by user and have sane defaults.

> +	option serial_number '007'

Here too, I think we should provide a hook in the script to obtain a serial
e.g. from U-Boot environment by default, or from a OTP register or so, but
allow overriding it by user.

> +	option usb_vid '0xbeef'
> +	option usb_pid '0x1234'
I would use Linux' Foundation defaults so that drivers on the USB host side
can match (in case the functions classes are not used, I admit that I'm not
really sure about this because I tested only Linux USB host systems so far.)

> +	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

I would prefer to have "config gadget <a-gadget-name>" to configure one
(multi-function) gadget and then have individual "config function <...>" 
section to configure one function for a referenced gadget.
I think the functions are orthogonal and thus "acm+rndis" looks like 
wrong approach for me.
I know this will make the init script much more complicated.

Regards, Michael


> 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
> +}





_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list