ioscanner
Posts: 4
Joined: Tue Aug 13, 2019 6:13 pm

USB Host module

Tue Aug 13, 2019 6:22 pm

Does anyone know of a USB Host/Slave module that works with the Pi? I don't want to use a Pi zero. I want to build it all into one case. I will use USB gadget to provide mouse, keyboard and provide mass_storage. With the Pi 4 that gives me one port I can use for storage, but need another to provide mouse and keyboard.

Thanks in advance!

User avatar
rpdom
Posts: 14742
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: USB Host module

Tue Aug 13, 2019 7:41 pm

I believe there is a module that allows the Pi to appear as multiple slave USB devices on the one USB connection.

trejan
Posts: 409
Joined: Tue Jul 02, 2019 2:28 pm

Re: USB Host module

Tue Aug 13, 2019 7:44 pm

You want 3 separate USB device ports so the RPi can be a mouse, keyboard and storage device? Why not use the composite gadget and use a single USB port?

ioscanner
Posts: 4
Joined: Tue Aug 13, 2019 6:13 pm

Re: USB Host module

Tue Aug 13, 2019 8:45 pm

I was thinking two ports. I am not sure you can do g_mass_storage with HID emulation at the same time. I see you can do g_multi for g_mass and g_ether. I will test and see if it can be done all at once. If it can that will be great!

Now the other issues is I would like to build the same design using a Raspberry Pi 3B+, but no OTG support on that model so I still need to find a USB host/slave adapter that works. The HDMI to CSI2 bridge works with both 3 and 4 models, so that is not an issue.

trejan
Posts: 409
Joined: Tue Jul 02, 2019 2:28 pm

Re: USB Host module

Tue Aug 13, 2019 9:09 pm

ioscanner wrote:
Tue Aug 13, 2019 8:45 pm
I was thinking two ports. I am not sure you can do g_mass_storage with HID emulation at the same time. I see you can do g_multi for g_mass and g_ether. I will test and see if it can be done all at once. If it can that will be great!
If you want to use the g_multi kernel module then you'll need to modify it as it is fixed function. It is considered a legacy driver and the "proper" way to configure a gadget is now using configfs. You can use the libusbg library to help you. It should be able to do do what you want though with mass storage + 2x HID.
ioscanner wrote:
Tue Aug 13, 2019 8:45 pm
Now the other issues is I would like to build the same design using a Raspberry Pi 3B+, but no OTG support on that model so I still need to find a USB host/slave adapter that works.
The MAX3421E is a SPI attached USB host/device interface and does work with the RPi. I've no idea if you can do a composite device with it though and it is limited to USB Full Speed i.e. 12Mbps. I suspect all of the SPI attached USB interfaces will be similar.

I assume you need USB host ports and/or the onboard Ethernet? If not then the 3A+ would give you access to the SoC USB controller.

ioscanner
Posts: 4
Joined: Tue Aug 13, 2019 6:13 pm

Re: USB Host module

Tue Aug 13, 2019 9:48 pm

I am using configfs and libcomposite for mouse and keyboard. I was trying to use g_mass_storage and correct it will not allow both at the same time. Has anyone tried to do configfs with all 3 (mouse, keyboard and mass_storage) at the same time?

On the Pi4 I am using the type-c port. Has anyone found a type-c usb hub that works? So we can power the Pi and connect to a target PC to control mouse, keyboard and provide mass_storage? For now I just have the Pi4 directly connected getting power from the PC. But if the PC is rebooted the Pi4 will reboot.

User avatar
thagrol
Posts: 1705
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: USB Host module

Wed Aug 14, 2019 11:56 am

ioscanner wrote:
Tue Aug 13, 2019 9:48 pm
I am using configfs and libcomposite for mouse and keyboard. I was trying to use g_mass_storage and correct it will not allow both at the same time. Has anyone tried to do configfs with all 3 (mouse, keyboard and mass_storage) at the same time?
You won't be able to use libcomposite and g_mass_storage at the same time but you should be able to provide the same functionality as g_mass-storage via libcomposite. See https://www.isticktoit.net/?p=1383
On the Pi4 I am using the type-c port. Has anyone found a type-c usb hub that works? So we can power the Pi and connect to a target PC to control mouse, keyboard and provide mass_storage? For now I just have the Pi4 directly connected getting power from the PC. But if the PC is rebooted the Pi4 will reboot.
Assuming you intend to use the hub between the PC and he 4B with the PC still acting as the host you should be aware that there is a know issue with the pull resistorss on the USB C port that can cause smart/E matked cables to refuse to provide power.

There is also no power delivery negotiation present so the most you're likely to get from the hub/host is 900mA (assuming the port/hub is standards compliant). Way below the recommended 3A.

Power the 4B seperately via the 5v and ground pins on the GPIO header though it may be adviseable to modifly your USB cable to the host (cut the +5V wire) to avoid issues with power coming into the 4B from two sources.
This space unintentionally left blank.

ioscanner
Posts: 4
Joined: Tue Aug 13, 2019 6:13 pm

Re: USB Host module

Thu Aug 15, 2019 6:40 pm

I got libcomposite working. Seems I have to do the setup of mouse, keyboard and storage at the same time for all to run at once. Then I can turn off and on Lun.0 with the file I want to share.

As for the power. That works. I can just modify a USB cable and connect it to the GPIO 5v side. I thought it would only use for providing power but if it allows me to provide power to the board that works perfect.

Thank you!

User avatar
thagrol
Posts: 1705
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: USB Host module

Thu Aug 15, 2019 9:57 pm

ioscanner wrote:
Thu Aug 15, 2019 6:40 pm
I got libcomposite working. Seems I have to do the setup of mouse, keyboard and storage at the same time for all to run at once. Then I can turn off and on Lun.0 with the file I want to share.
Be careful doing that. Without a secondary coms channel between the USB host and your code there is no way to know when it is safe to do this. All but the most basic of USB hosts will be using read and write caching, all the mass storage drive sees is a requiest to read or write N bytes starting at address A and has no idea if further writes are coming. Even that little data doesn't appear to be exposed to userland.

Turning off Lun 0 while the host has pending reads isn't likely to cause problems on the Pi end but likely will to the host. Turning it off during writes or when the host hasn't flushed its write cache is likely to cause problems on both ends upto and including file/SD card corruptuion on the Pi end.

If you're expecting/intending to have simultaneous read/write access to the shared storage from both linux on the Pi and the USB host you should rethink things as a mass_storage gadget can't cope reliably or well with this. Put simply: both sides use read/write caching, both sides expect exclusive access, and neither side tells the other what it's doing or whether it has more data to write in its cache. Any writes made on one side will not be seen without unmounting and remounting on the other.

Lastly, I'm concerned about "with the file I want to share". The mass storage gadget appears as a block device (hdd etc) to the host. Any file you give it will be treated as such so you can't give it, say, a text file and expect the host to see it as such. The file parameter given to g_mass_storage/libcomposite can be either a partition (e.g. /dev/sda1) or a file (e.g. /foo/bar) but the file will be treated as if it were a partition so will need to be formatted in a way the host can understand just the same as a USB stick or HDD would need to be.

Appologies if I've misinterpreted your post and you're aware of the above.
This space unintentionally left blank.

Return to “HATs and other add-ons”