How to change function of Linux USB composite gadget? - linux

We need to be able to change the mass storage function in a Linux composite USB gadget (to change the mounted directory). Currently the scripts teardown the UDC and restart it with the changed mount point.
The problem with this is that it disconnects the ACM device providing the command/control serial connection with a Windows PC.
Is there a way to just takedown/restart the mass storage portion of the composite device without stopping the entire UDC?
This is with kernel 4.1.9.

Related

How to create a simple Linux USB device to "echo" whatever is sent to it from the host?

As a web developer, I am new to USB and gadget development. I am creating a Windows Desktop application that communicates, using the Node-USB library, with a USB Device. To start, I am trying to create a mock USB device using Linux on my Raspberry Pi 4. I have set it up to boot as a USB device using this script, which is based on this guide, which sets the usb provider/vendor IDs, a rndis function, and more. Windows recognizes the device when plugging it into the computer, and I am able to open a connection to it using the NodeJS library.
I am now stuck on how to configure the device to set up a function that simply listens to data sent to it on a "bulk in" endpoint from the host, and echo it back automatically to the host, so that the Windows application receives it. I have been advised that there are many scripts available to set up a simple "demo" gadget like this on Linux, but I cannot find any.
It's not clear to me,
How to set up a usb function on the device to listen to a bulk "out" endpoint
How to set up a "driver" or a custom-made application to "listen" to the aforementioned function
How to make the listening application itself. The node-usb library says it's made for communicating with USB devices, not for devices itself, so it seems I can't use it?
Is there a simple bash script or built-in linux command that can automatically echo data to the host from the "in" endpoint?
I'd appreciate any guidance to point me in the right direction!
You need to write a USB function driver in kernel or use functionfs to do it in userspace. Either way you'll need to prepare USB descriptors which are presented to the host during enumeration (when your device is being plugged in) and then handle incoming USB requests on registered endpoints.
To setup one of the drivers already existing in kernel you can do it through configfs or using libusbgx wrapper library. There are also legacy function drivers which activate automatically after loading the module.
The node-usb library is wrapper for libusb, which is host-side library. So you're right, you cannot use it to create device-side function driver.
There is f_loopback function in kernel which does exactly what you need. This is probably the best starting point if you want to create your own USB function driver.

How to flash NUCLEO F401RE with command line

I am looking for a command line tool for flashing my NUCLEO card in ubuntu. I want this tools for flash my NUCLEO remotely with node.js. I already try st-flash and stm32flash but these tools don't work properly. My programs are generated by mbed online tool.
Do you have any suggestions ?
Thanks for your answers
You can update Nucleo boards simply by copying the mbed image to the mass storage device presented when you connect to the composite USB interface.
The image file is automatically written to the microcontroller flash by the integrated STLink device when the file is placed on the USB drive. This is a "write-only" device; the written file cannot be read back - if you interrogate the drive it always appears empty.
In Ubuntu you may have to explicitly mount the device. If it mounts automatically it will appear under /mnt (I guess).
Since it is a standard USB mass storage interface, you should not need any Nucleo or ST specific driver or software. It is a "drag & drop" programming interface.
If your board does not appear as a mass storage device, you may need to update the integrated STLink firmware as described at https://os.mbed.com/teams/ST-Americas-mbed-Team/wiki/Preparing-the-STM32-Nucleo-Board

C++ Detecting USB serial device plugged/unplugged

I need to detect when a USB serial device is plugged or unplugged on my embedded system and know what is the tty associated with it.
My system runs over a 2.6 Linux Kernel.
Since I don't have write access to udev rules, nowadays I'm trying to get this information from the file system, looking for modifications in /sys/bus/usb/devices directory. However, I'm facing some problems with this approach.
I know what is the Id BUS of the USB port connected (e.g 1-1.3). So, I search for the associated tty (looking for a directory in /sys/bus/usb/devices/<Id BUS>:1.0/tty/ - e.g. /sys/bus/usb/devices/1-1.3:1.0/tty/ttyACM0). This way I know that I should use /dev/ttyACM0 to communicate with my device.
But, sometimes, this device (/dev/ttyACM0) does not exist.
Is there any better way to get this information?
I even thought trying to get this information from the syslog, but I don't know whether this is a pretty good idea.
Edit:
Only to clarify, my system needs to be able to detect state changes in the USB bus, i.e. detecting when a new device is plugged (and getting the tty name linked to it) or an existing one is unplugged.
The system is monitoring up to N USB/serial devices, which are plugged to it using an USB HUB. During its normal execution new devices can be plugged, existing devices can be removed (or rebooted by a remote command - out of this scope). When a device is rebooted, it could receive a different tty from the previous one used before (e.g. ttyACM0 -> ttyACM3), since the kernel designates to it a tty which is free at the moment, and it is a big problem to me.
Netlink is the preferred mechanism for communication between kernel and userspace.
You would create a Netlink socket with family NETLINK_KOBJECT_UEVENT, listen on that socket and filter out messages that contain SUBSYSTEM=usb and ACTION=add for USB plug events or ACTION=remove for USB unplug events.
I wrote a USB abstraction library called libusbp. You should look at its port_name example, which shows how to use libusbp to get the serial port name (e.g. /dev/ttyACM0) for a USB serial device. Behind the scenes, libusbp gets this information using libudev.
Check if the virtual file is deleted using stat.
#include <sys/statvfs.h>
...
struct stat sb;
return (stat("/dev/ttyUSB0", &sb) == 0); // true if open, false otherwise

How to detect when a usb cable is connected/disconnected on the device side in Linux 2.6.37?

I have a embedded device that runs linux 2.6.37.
I want my application to know when the USB is connected.
Currently I can achieve this by pooling
/sys/devices/platform/musb/musb-hdrc.0/vbus.
However this approach does not distinguish between a USB charger or a USB host.
I found this udev approach but I don't think it's available in my version of the kernel. because I did not find any USB related nodes in my /dev. This discussing also shows that it might not be feasible, ether.
I also found linux hotplug and tried the netlink example, but I didn't see any output running the example when I connect/disconnect the USB cable.
What I want to do is to detect connection type on the device, when USB is connected, and prepare (unmount file system) and switch to g_file_storage if device is connected to a host, and do nothing if device is connect to a charger.
How shall I achieve this?
To achieve that, you can use the inotify(7) feature, available in all linux kernels to be awaken as soon as some device node gets created in /sys.
To know what type of device you have, you have to read the usb info from proper usb ioctl call (or if you are not a kernel interface expert, using the libusb interface) to get the device vendor, device id and device class fields coming from the device. Normally, the hotplug software gets informed on these clase of events (via a special socket). The most probably reason you don't get the device properly initialized is some misconfiguration in the config files for udev system, which normally has one entry for each possible device vendor/device id pair and allows it to load the appropiate device driver to control it. The process continues with the device driver module creating dynamically the actual devices, and they'll appear in the /dev/ filesystem as a consequence of some other kernel event to udevd.
Read apropiate documents in <linux_src>/Documentation (this directory directory belongs to the linux kernel source code, so you'll probably need to install it), and udevd(8) man pages to be able to add a new usb.
On 2.6.37 kernel, this could be done by polling
/sys/devices/platform/musb-omap2430.0/musb-hdrc.0/mode
If handshake with host is successful then it will read as "peripheral", if fail it'll be "idle".

How can BeagleBone Black be used as Mass Storage Device?

Is it possible to use the BB as Mass Storage Device?
I want it to be connected to an audio player that can read files from USB connectivity (such as USB flash drive) and act as data storage device containing one specific folder (and its sub-folders) from the file system (if possible, on a flash drive connected to the board.).
As the device specs says, it has connectivity of:
USB client for power & communications
USB host
Operating system will probably be Ubuntu but can be changed.
What drivers or configurations needs to be done in order to achieve this?
The latest images have already the mass storage usb gadget active, so a mass storage peripheral should be recognized by your system upon connection.
A quick google search reveals this discussion about a user trying to disable the USB MS gadget:
From the discussion, the files where the magic happens are:
Debian: /opt/scripts/boot/am335x_evm.sh
Ubuntu: /opt/scripts/boot/am335*
Armstrong: /usr/bin/g-ether-load.sh
In my Debian image the line you want to modify is:
modprobe g_multi file=${gadget_partition} cdrom=0 stall=0 removable=1 nofua=1 iSerialNumber=${SERIAL_NUMBER} iManufacturer=Circuitco iProduct=BeagleBone${BLACK} host_addr=${cpsw_1_mac}
and the corresponding $gadget_partition variable that is set just before that in order to customize the folder you want to expose.
Note that the g_multi gadget in its standard configuration presents 3 different devices: an ethernet interface, a mass storage peripheral and a serial interface. If you want to customize the parameters you can refer to the g_multi documentation (kernel.org)

Resources