Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB: Replace OHCI USB host library #438

Merged
merged 10 commits into from
May 26, 2021
Merged

USB: Replace OHCI USB host library #438

merged 10 commits into from
May 26, 2021

Conversation

Ryzee119
Copy link
Contributor

@Ryzee119 Ryzee119 commented Mar 24, 2021

This PR replaces the legacy 'hacked' together linux usb stack currently in nxdk with a new library.

Note: CI will fail on the sdl samples because I havent update the hal/input.c code yet It should work now, but I have submoduled in own SDL2 fork. XboxDev/nxdk-sdl#38 will need to be merged and SDL2 submodule update prior to merge.

This library was developed by OpenNuvoton (https://github.com/OpenNuvoton) who are a large manufacturer of ARM based embedded microcontrollers. This library was intended for use on their range of chips with OHCI hardware. I specifically imported it from https://github.com/OpenNuvoton/N9H30_emWin_NonOS/tree/master/Library/UsbHostLib with some modifications to make it xbox friendly. The largest change is the separation between virtual and physical memory which is not really a concept on microcontrollers.

The library has some advantages over the current implementation:

  1. A more permissive license (Apache 2.0)
  2. A full range of inbuilt USB class drivers. I have tested a range of these on my personal repo https://github.com/Ryzee119/nxdk_ohci_test. These samples are just quick hacks for testing. Not considered 'production ready'.
  3. Proper OHCI interrupt management so that USB hot-plugging should work properly.
  4. The XID driver can properly distinguish between various Xinput devices (Duke, Steel Battalion, Xremote)
  5. Proper interrupt out writes, so rumble should work with no problem.
  6. Some very useful features for developers is support for CDC class USB-Serial adaptors for improved 'printf debugging'.

**I have thoroughly tested this on my own repo projects. Some outstanding aspects: **

  1. Untested on 1.0 xboxes with daughterboard. I would expect this to have no issues though. Has been tested ok!
  2. PR does not include changes to hal/input.c, consequently sdl2 joystick/gamecontroller will break. I am happy to do this work but would like some feedback on the preferred APIs going forward. sdl_joystick driver rewritten. All old features plus more. (Rumble, hotplugging etc)
  3. Currently the ohci library is included as a submodule to my personal repo. This should either be migrated here or commited directly into nxdk (or it can stay linked to my repo, I dont mind) Its here https://github.com/XboxDev/libusbohci.
  4. The mass storage class driver has been tested, but disabled in this PR due to the fact it is hardcoded to use FATFS. I think this requirement should be removed to allow other filesystem types (FATX etc). I can do this in a follow up PR. FATFS code removed. This class driver just supports standard SCSI needed for MSC. User filesystem drivers need to be linked in.

@dracc
Copy link
Contributor

dracc commented Mar 24, 2021

Amazing! 🎉
I'll test this tonight.

samples/usb/main.c Outdated Show resolved Hide resolved
@Teufelchen1
Copy link
Contributor

Misses adjustment of the README.md

samples/usb/main.c Outdated Show resolved Hide resolved
lib/usb/Makefile Outdated Show resolved Hide resolved
@Ryzee119
Copy link
Contributor Author

Ryzee119 commented Mar 27, 2021

Ive made a sdl_joystick driver using this new stack. It does not use hal/input.c at all and instead directly calls the usb HID APIs, so hal/input.c can be completely removed.

I've tested this on SDLPopX, OpenTyrian, Commander keen, Xenium-Tools, sdl_gamecontroller sample and NevolutionX
https://github.com/Ryzee119/nxdk-sdl/tree/usbh

It also supports native Xbox360, Xbone controllers too!

To test replace your lib/sdl/SDL2 with this fork.

cd lib/sdl/SDL2
git remote add ryzee https://github.com/Ryzee119/nxdk-sdl.git
git checkout ryzee usbh

@@ -97,4 +121,4 @@ int main(void)
}

return 0;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Misses Newline 😏

*/
void device_connection_callback(UDEV_T *udev, int status)
{
debugPrint("Device connected on port %u (PID: %04x VID: %04x)\n", udev->port_num,
Copy link
Member

Choose a reason for hiding this comment

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

What do these ports look like? If they are USB ports and not gamepad ports, then that's relevant for the message, in my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are the number of the USB hub port. I have updated the wording here.


usbh_core_init();
usbh_install_conn_callback(&device_connection_callback, &device_disconnect_callback);
debugPrint("Plug and unplug USB devices to test\n");
Copy link
Member

@JayFoxRox JayFoxRox Apr 15, 2021

Choose a reason for hiding this comment

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

This should be a bit more explicit.
People might assume that this will test their gamepad like that XDK sample that everybody loves (with the 3D gamepad).
This, however, merely seems to show plug and unplug events?

 debugPrint("Plug and unplug USB devices to print USB device VID and PID\n"); 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the debug print here to make it clearer what it is doing


void device_disconnect_callback(UDEV_T *udev, int status)
{
debugPrint("Device disconnected on port %u (PID: %04x VID: %04x)\n", udev->port_num,
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think I might prefer order 1. VID, 2. PID; but I'm not sure what other common USB tools use. (Idea is to visually group them by manufacturer first.. so that should come first)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me. Updated this

@@ -0,0 +1,5 @@
XBE_TITLE = nxdk\ sample\ -\ usb
Copy link
Member

@JayFoxRox JayFoxRox Apr 15, 2021

Choose a reason for hiding this comment

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

nit: I think just calling this sample "usb" is not ideal; but I have trouble coming up with my own name.

Just to clarify, I mean both: XBE title, but more importantly, pathname

(I'd assume there'll be different USB samples eventually, at least some of which will likely deserve a "usb-" prefix)

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps call it hotplugging?

Copy link
Contributor Author

@Ryzee119 Ryzee119 Apr 16, 2021

Choose a reason for hiding this comment

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

Agree that it needs a better name. This sample really just shows the absolute basic core init so it atleast builds during ci tests. People might get drawn into it for gamecontroller stuff when they should be using SDL_gamecontroller.
I am working on an 'all-in-one` usb sample. https://github.com/Ryzee119/nxdk_ohci_test/blob/xbox/main.c, advantage being it includes all source files in ci runs to catch issues better.

lib/usb/Makefile Outdated

.PHONY: clean-usb
clean-usb:
$(VE)rm -f $(USBH_OBJS) $(NXDK_DIR)/lib/libnxdk_usb.lib
Copy link
Member

Choose a reason for hiding this comment

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

I think this Makefile should be part of the USB submodule, unless that submodule remains platform unspecific (which, to be honest, sounds like a good idea to me; but only if we can attract other stakeholders / convince upstream to submodule it)

Other maintainers might have different thoughts on this matter though.

.gitmodules Outdated
@@ -31,3 +31,6 @@
[submodule "lib/sdl/SDL2_image"]
path = lib/sdl/SDL2_image
url = https://github.com/libsdl-org/SDL_image.git
[submodule "lib/usb/ohci_usbhostlib"]
path = lib/usb/ohci_usbhostlib
url = https://github.com/Ryzee119/ohci_usbhostlib.git
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@Ryzee119 Ryzee119 Apr 16, 2021

Choose a reason for hiding this comment

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

  • I created a self PR, happy to discuss stack specifics there. Xbox port: Ryzee119/libusbohci#2
  • Timing branch was a initial attempt to minimise/remove some of the blocking delays in the stack. Have not spent any time on this yet. afair the whole stack is non-block except during device connection/removal/suspend stuff
  • Master branch is/was intended to be a clean copy of upsteam to track diff.

Copy link
Member

Choose a reason for hiding this comment

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

I created a self PR, happy to discuss stack specifics there. Ryzee119/libusbohci#1

Please add this information to the PR description - I assume it could be more important than the discussion on this repository.

What are your plans for https://github.com/Ryzee119/nxdk_ohci_test/tree/master?

Master branch is/was intended to be a clean copy of upsteam to track diff.

I should probably have made a separate comment, but I was asking about "nxdk_ohci_test" and not "ohci_usbhostlib"; trying to get an idea of the existing ecosystem.

Copy link
Contributor Author

@Ryzee119 Ryzee119 Apr 19, 2021

Choose a reason for hiding this comment

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

What are your plans for https://github.com/Ryzee119/nxdk_ohci_test/tree/master?

I think it could make a good nxdk sample if/when i clean it up to it suitable quality. Probably something I will do in the near future. See the 'xbox' branch for something a bit better quality.

@espes
Copy link
Contributor

espes commented May 11, 2021

This is fantastic!

Currently the ohci library is included as a submodule to my personal repo. This should either be migrated here or commited directly into nxdk (or it can stay linked to my repo, I dont mind)

I've invited you to the xboxdev org if you want to move it

@Ryzee119 Ryzee119 force-pushed the usbh branch 2 times, most recently from eaabadd to f835fe6 Compare May 19, 2021 04:22
@JayFoxRox
Copy link
Member

This PR was merged in a bad state - XboxDev/nxdk-sdl@60ff410 is not part of the XboxDev git repo. GitHub only finds it because it's part of the same fork tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants