Skip to content

Conversation

chrysn
Copy link
Member

@chrysn chrysn commented Jan 16, 2020

Contribution description

This adds a check for whether any custom user code (ie. anything but built-in peripherals) uses USB, and if nothing does, allows for 1209/7D00 to be the default VID/PID pair.

It follows the action plan of #12273 (comment), with the simplification that it operates per compilation unit, and thus the ifdef/define combination can be inside the include guard where those things usually are.

Alternatives

If the build system can tell RIOT's compilation units apart from application compilation units or packages, it could set USB_H_USER_IS_RIOT on all modules. That would significantly reduce the number of files this touches. (But then again, any helpers for creating custom HID devices would need to unset the flag again, so meh).

The name USB_H_USER_IS_RIOT is up for bikeshedding.

Testing procedure

  • Pick a board with USB support (eg. samr21-xpro).
  • In any example that does not do anything USB-related, add the usbus_cdc_ecm, auto_init_usbus and auto_init_gnrc_netif modules without setting CONFIG_USB_VID. (stdio_cdc_acm would work the same).
  • The example builds fine, and now has an additional Ethernet interface.
  • For comparison, take an example that does anything custom USB, like usbus_uart_adapter from USBUS: CDC ACM to UART functionality with example. #12268
  • In that example, remove the CONFIG_USB_PID settings (incidentally, if you right now merge master into that PR, currently that is already the case as the renaming of USB_CONFIG_PID to CONFIG_USB_PID is not in there yet).
  • Building this will result in an error message that you need to set a VID/PID, and recommends the 1209/7D01 pair reserved for RIOT demos, tests and experiments.

Issues/PRs references

This is a first step in properly addressing #12273.

A follow-up (preferably in an own PR, but if you prefer I can pull it in here as well) should move the usb_id_check that's currently replicated across examples into common make infrastructure. In the end, the only USB related lines in a USB example makefile should be USB_VID = 1209 / USB_PID = 7D00 (or ${RIOT_TESTING_[VP]ID}) and the appropriate USEMODULE. Boards that get USB by default (eg. because their default stdio is CDC-ACM, or because gnrc_netdev_default pulls in CEC-ECM) already don't need anything that says "USB" in their Makefiles with this PR.

@benpicco benpicco added Area: USB Area: Universal Serial Bus Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation labels Jan 22, 2020
Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the USB_H_USER_IS_RIOT... It leaves a lot of open potential for errors. Also, are there other users for usb.h than RIOT? Other than that I just have some style nits about the config macros.

@chrysn
Copy link
Member Author

chrysn commented Jan 28, 2020

Also, are there other users for usb.h than RIOT?

The USB subsystem is public API, so there can be, and #12268 adds an example that does access those as an application. bergzand's work on USB audio has not hit the repository AFAICT, but is another likely scenario.

Not sure about the USB_H_USER_IS_RIOT... It leaves a lot of open potential for errors

I'm not trying to make this malice-proof; it's all C after all and people can define what they like. The main goal here is to make the user experience smooth for unaware USB users (no blinky-blinky-get-your-own-code just because my board happens to use USB UART) without letting the RIOT VID/PID pair degenerate into "could be anything really as long as it's RIOT based" codes that more resemble the test pairs than something recognizable.

For that, USB code blocksenabled by USEMODULE (etc, but especially the choice of a board) need to distinguish themselves from USB code blocks introduced by the user. A define looks like the right choice for me to make that distinction; naming is flexible and could just as well be RIOT_BUILTIN.

review comments

Thanks, will work them in with the next batch.

@miri64
Copy link
Member

miri64 commented Jan 28, 2020

The USB subsystem is public API, so there can be, and #12268 adds an example that does access those as an application. bergzand's work on USB audio has not hit the repository AFAICT, but is another likely scenario.

Ah, so "RIOT" in this case means system internals, not RIOT applications?

@chrysn
Copy link
Member Author

chrysn commented Jan 28, 2020

Yes. Code that is part of the operating system. So maybe RIOT_SYSTEM_INTERNAL_USB?

@miri64
Copy link
Member

miri64 commented Jan 28, 2020

Yes. Code that is part of the operating system. So maybe RIOT_SYSTEM_INTERNAL_USB?

USB_H_USER_IS_RIOT_INTERNAL?

@chrysn
Copy link
Member Author

chrysn commented Jan 28, 2020

Sounds good, will be in next patch series along with fixes for my stylistic missteps.

@chrysn
Copy link
Member Author

chrysn commented Jan 30, 2020

Updated, reflecting the style nits and the guard rename.

If I may squash, I'll try to make the rest of Travis happy (currently it's hanging at my commit messages, won't get past that without the destructive steps).

@miri64
Copy link
Member

miri64 commented Jan 30, 2020

Yepp, feel free to squash :-)

@miri64
Copy link
Member

miri64 commented Jan 30, 2020

(and change the commit message length)

@miri64 miri64 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 30, 2020
@miri64
Copy link
Member

miri64 commented Jan 30, 2020

There is also a typo in this commit's message body

A new define, USB_H_USER_IS_RIOT_INTERNAL, is defined that may only be
set from within RIOT's own compilation units that deem themselves
standard RIOT peripherals. If all usb.h users in a program match that
requirement, a default VID/PID pair is set.

Due to the new composite check, the individual checks for VID/PID being
set become moot and are removed.
This list is probably incomplete as it was created experimentally.
As the whitelist define can be set per compilation unit in all
legitimate cases, the checks do not need to be run on every single usb.h
inclusion. This is done for two reaons:

* It is sufficient -- if any user C file includes usb.h, there's already
  a good chance that the user is doing something USB related manualy.
  (And conversely, the existing examples with boards that happen to pull
  in CDC-ACM or CDC-ECM do not include usb.h from an example C file).

* Defining the USB_H_USER_IS_RIOT around legitimate uses of the header
  by other headers would allow accidental sidestepping: If a user
  includes a legitimate usb.h using header (say, board.h) and just
  forgets to include usb.h on their own, their application that'd mess
  with USB would still work as usb.h is transitively included, and the
  check for custom includes does not trigger.
@chrysn chrysn force-pushed the usb-default-vidpid branch from 9dac020 to cca756d Compare January 30, 2020 14:07
Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. Tested with tests/usbus_cdc_acm_stdio on feather-nrf52840 with the following patch:

diff --git a/tests/usbus_cdc_acm_stdio/Makefile b/tests/usbus_cdc_acm_stdio/Makefile
index 4d07162137..f4f1a13784 100644
--- a/tests/usbus_cdc_acm_stdio/Makefile
+++ b/tests/usbus_cdc_acm_stdio/Makefile
@@ -7,21 +7,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
$ lsusb | grep 1209
Bus 003 Device 041: ID 1209:7d00 Generic 

BTW when removing the same lines in tests/usbus/Makefile, the include via usbdev_mock.c causes the error message to be triggered when the application is compiled, but I guess that is how it is intended.

In file included from /home/mlenders/Repositories/RIOT-OS/RIOT/drivers/include/periph/usbdev.h:78,
                 from /home/mlenders/Repositories/RIOT-OS/RIOT/tests/usbus/usbdev_mock.c:21:
/home/mlenders/Repositories/RIOT-OS/RIOT/sys/include/usb.h:39:2: error: #error Please configure your vendor and product IDs. For development, you may set CONFIG_USB_VID=0x1209 CONFIG_USB_PID=0x7D01.
 #error Please configure your vendor and product IDs. For development, you may \
  ^~~~~
make[1]: *** [/home/mlenders/Repositories/RIOT-OS/RIOT/Makefile.base:109: /home/mlenders/Repositories/RIOT-OS/RIOT/tests/usbus/bin/particle-xenon/application_tests_usbus/usbdev_mock.o] Error 1
make[1]: *** Waiting for unfinished jobs....
In file included from /home/mlenders/Repositories/RIOT-OS/RIOT/drivers/include/periph/usbdev.h:78,
                 from /home/mlenders/Repositories/RIOT-OS/RIOT/tests/usbus/main.c:25:
/home/mlenders/Repositories/RIOT-OS/RIOT/sys/include/usb.h:39:2: error: #error Please configure your vendor and product IDs. For development, you may set CONFIG_USB_VID=0x1209 CONFIG_USB_PID=0x7D01.
 #error Please configure your vendor and product IDs. For development, you may \
  ^~~~~

@chrysn
Copy link
Member Author

chrysn commented Jan 30, 2020

Thanks.

The removal of the config code is something that I'd like to do in a follow-up, but in one go with unifying usb_id_check into the generic make files. (In the end, only tests/usbus and examples like #12268 should have two lines of PID/VID setting).

The error case around mock is exactly as intended, yes; that does "custom USB stuff" and will need to retain explicit (testing) IDs.

@miri64 miri64 merged commit 8d749dc into RIOT-OS:master Jan 30, 2020
@chrysn chrysn deleted the usb-default-vidpid branch January 30, 2020 16:47
@leandrolanzieri leandrolanzieri added this to the Release 2020.04 milestone Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: USB Area: Universal Serial Bus CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants