Skip to content

Commit

Permalink
Merge pull request #13382 from chrysn-pull-requests/usb-check-unifica…
Browse files Browse the repository at this point in the history
…tion

usb: Warn on test-ID usage in a unified location
  • Loading branch information
fjmolinas authored Jun 30, 2020
2 parents d132ef4 + 85d7042 commit 1004fb3
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 96 deletions.
3 changes: 3 additions & 0 deletions Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ include $(RIOTMAKE)/vars.inc.mk
# Include build targets for selected tools
include $(RIOTMAKE)/tools/targets.inc.mk

# Checks and defaults for USB Vendor and Product ID
include $(RIOTMAKE)/usb-codes.inc.mk

# Warn if the selected board and drivers don't provide all needed features:
ifneq (, $(filter all flash, $(if $(MAKECMDGOALS), $(MAKECMDGOALS), all)))
EXPECT_ERRORS :=
Expand Down
20 changes: 20 additions & 0 deletions dist/usb_id_testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file contains all codes that should trigger the usb_id_check warning
# about a test PID being used

1209 0001
1209 0002
1209 0003
1209 0004
1209 0005
1209 0006
1209 0007
1209 0008
1209 0009
1209 000a
1209 000b
1209 000c
1209 000d
1209 000e
1209 000f
1209 0010
1209 7d01
5 changes: 0 additions & 5 deletions examples/usbus_minimal/Kconfig

This file was deleted.

28 changes: 2 additions & 26 deletions examples/usbus_minimal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ USEMODULE += usbus
USEMODULE += auto_init_usbus

# USB device vendor and product ID
export DEFAULT_VID = 1209
export DEFAULT_PID = 7D00
USB_VID ?= $(DEFAULT_VID)
USB_PID ?= $(DEFAULT_PID)
USB_VID ?= $(USB_VID_TESTING)
USB_PID ?= $(USB_PID_TESTING)

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1
Expand All @@ -28,25 +26,3 @@ QUIET ?= 1
SHOULD_RUN_KCONFIG ?=

include $(RIOTBASE)/Makefile.include

# Set USB VID/PID via CFLAGS if not being set via Kconfig
ifndef CONFIG_USB_VID
CFLAGS += -DCONFIG_USB_VID=0x$(USB_VID)
else
USB_VID = $(patsubst 0x%,%,$(CONFIG_USB_VID))
endif

ifndef CONFIG_USB_PID
CFLAGS += -DCONFIG_USB_PID=0x$(USB_PID)
else
USB_PID = $(patsubst 0x%,%,$(CONFIG_USB_PID))
endif

.PHONY: usb_id_check
usb_id_check:
@if [ $(USB_VID) = $(DEFAULT_VID) -o $(USB_PID) = $(DEFAULT_PID) ] ; then \
$(COLOR_ECHO) "$(COLOR_RED)Private testing pid.codes USB VID/PID used!, do not use it outside of test environments!$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)MUST NOT be used on any device redistributed, sold or manufactured, VID/PID is not unique!$(COLOR_RESET)" 1>&2 ; \
fi

all: | usb_id_check
33 changes: 33 additions & 0 deletions makefiles/usb-codes.inc.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Set USB VID/PID via CFLAGS if not being set via Kconfig
ifndef CONFIG_USB_VID
ifdef USB_VID
CFLAGS += -DCONFIG_USB_VID=0x$(USB_VID)
endif
else
USB_VID = $(patsubst 0x%,%,$(CONFIG_USB_VID))
endif

ifndef CONFIG_USB_PID
ifdef USB_PID
CFLAGS += -DCONFIG_USB_PID=0x$(USB_PID)
endif
else
USB_PID = $(patsubst 0x%,%,$(CONFIG_USB_PID))
endif

# Exported for the benefit of Kconfig
USB_VID_TESTING = 1209
USB_PID_TESTING = 7D01
usb_id_check:
@if grep --quiet --ignore-case "^$(USB_VID) $(USB_PID)$$" $(RIOTBASE)/dist/usb_id_testing; then \
$(COLOR_ECHO) "$(COLOR_RED)Private testing pid.codes USB VID/PID used!, do not use it outside of test environments!$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)MUST NOT be used on any device redistributed, sold or manufactured, VID/PID is not unique!$(COLOR_RESET)" 1>&2 ; \
fi
@if [ "$(USB_VID) $(subst D,d,$(USB_PID))" = "1209 7d00" ]; then \
$(COLOR_ECHO) "$(COLOR_RED)RIOT standard peripherals code (1209/7D00) cannot be set explicitly.$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)Unset USB_VID / USB_PID for the code to be picked automatically, or set$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)them to \$${USB_VID_TESTING} / \$${USB_PID_TESTING} during development.$(COLOR_RESET)" 1>&2 ; \
exit 1; \
fi
.PHONY: usb_id_check
all: | usb_id_check
2 changes: 1 addition & 1 deletion sys/include/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern "C" {
#define CONFIG_USB_PID INTERNAL_PERIPHERAL_PID
#else
#error Please configure your vendor and product IDs. For development, you may \
set CONFIG_USB_VID=0x1209 CONFIG_USB_PID=0x7D01.
set USB_VID=${USB_VID_TESTING} USB_PID=${USB_PID_TESTING}.
#endif
#endif

Expand Down
2 changes: 2 additions & 0 deletions sys/usb/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ endchoice
config USB_PID
hex "Product ID"
range 0x0000 0xFFFF
default 0x7D01
help
You must provide your own PID.

config USB_VID
hex "Vendor ID"
range 0x0000 0xFFFF
default 0x1209
help
You must provide your own VID.

Expand Down
17 changes: 2 additions & 15 deletions tests/usbus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@ FEATURES_PROVIDED += periph_usbdev
DISABLE_MODULE += auto_init_usbus

# USB device vendor and product ID
DEFAULT_VID = 1209
DEFAULT_PID = 7D00
USB_VID ?= $(DEFAULT_VID)
USB_PID ?= $(DEFAULT_PID)

CFLAGS += -DCONFIG_USB_VID=0x$(USB_VID) -DCONFIG_USB_PID=0x$(USB_PID)
USB_VID ?= $(USB_VID_TESTING)
USB_PID ?= $(USB_PID_TESTING)

include $(RIOTBASE)/Makefile.include

.PHONY: usb_id_check
usb_id_check:
@if [ $(USB_VID) = $(DEFAULT_VID) ] || [ $(USB_PID) = $(DEFAULT_PID) ] ; then \
$(COLOR_ECHO) "$(COLOR_RED)Private testing pid.codes USB VID/PID used!, do not use it outside of test environments!$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)MUST NOT be used on any device redistributed, sold or manufactured, VID/PID is not unique!$(COLOR_RESET)" 1>&2 ; \
fi

all: | usb_id_check
17 changes: 0 additions & 17 deletions tests/usbus_cdc_acm_stdio/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,4 @@ USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps

# USB device vendor and product ID
DEFAULT_VID = 1209
DEFAULT_PID = 7D00
USB_VID ?= $(DEFAULT_VID)
USB_PID ?= $(DEFAULT_PID)

CFLAGS += -DCONFIG_USB_VID=0x$(USB_VID) -DCONFIG_USB_PID=0x$(USB_PID)

include $(RIOTBASE)/Makefile.include

.PHONY: usb_id_check
usb_id_check:
@if [ $(USB_VID) = $(DEFAULT_VID) ] || [ $(USB_PID) = $(DEFAULT_PID) ] ; then \
$(COLOR_ECHO) "$(COLOR_RED)Private testing pid.codes USB VID/PID used!, do not use it outside of test environments!$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)MUST NOT be used on any device redistributed, sold or manufactured, VID/PID is not unique!$(COLOR_RESET)" 1>&2 ; \
fi

all: | usb_id_check
32 changes: 0 additions & 32 deletions tests/usbus_cdc_ecm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,4 @@ USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps

# USB device vendor and product ID
# pid.codes test VID/PID, not globally unique
export DEFAULT_VID = 1209
export DEFAULT_PID = 7D00
USB_VID ?= $(DEFAULT_VID)
USB_PID ?= $(DEFAULT_PID)

include $(RIOTBASE)/Makefile.include

# Set USB VID/PID via CFLAGS if not being set via Kconfig
ifndef CONFIG_USB_VID
CFLAGS += -DCONFIG_USB_VID=0x$(USB_VID)
else
USB_VID = $(patsubst 0x%,%,$(CONFIG_USB_VID))
endif

ifndef CONFIG_USB_PID
CFLAGS += -DCONFIG_USB_PID=0x$(USB_PID)
else
USB_PID = $(patsubst 0x%,%,$(CONFIG_USB_PID))
endif

# There is a Kconfig in the app folder, we need to indicate not to run it by default
SHOULD_RUN_KCONFIG ?=

.PHONY: usb_id_check
usb_id_check:
@if [ $(USB_VID) = $(DEFAULT_VID) -o $(USB_PID) = $(DEFAULT_PID) ] ; then \
$(COLOR_ECHO) "$(COLOR_RED)Private testing pid.codes USB VID/PID used!, do not use it outside of test environments!$(COLOR_RESET)" 1>&2 ; \
$(COLOR_ECHO) "$(COLOR_RED)MUST NOT be used on any device redistributed, sold or manufactured, VID/PID is not unique!$(COLOR_RESET)" 1>&2 ; \
fi

all: | usb_id_check

0 comments on commit 1004fb3

Please sign in to comment.