Today I have enabled the USB gadget support for file storage. The intention is to be able to export files via the USB device interface to a PC.
The file storage gadget must be enabled at the kernel config menu:
USB support -> Support for USB gadgets -> File-backed storage gadget
Note that only one USB gadget may be enabled at the same time. If multiple gadgets must be supported, all of them must be configured as modules, so I had to remove built-in support for ethernet gadget from the kernel. Switching the USB function requires removing and installing the proper modules.
The module for file storage is g_file_storage.o and is installed this way:
insmod g_file_storage.o file=/results.bin stall=0
The 'stall' argument is necessary for the USB disk to be properly detected by windows. Linux does not require this argument and the drive can be mounted without problem. If 'stall' is not set to zero and the gadget is connected to a windows PC, the following messages appear:
g_file_storage pxa2xx_udc: full speed config #1
udc: pxa2xx_ep_disable, ep1in-bulk not enabled
udc: pxa2xx_ep_disable, ep2out-bulk not enabled
udc: USB reset
udc: USB reset
repeating every few seconds.
For the 'stall' option to be available, it is necessary to enable the 'file-backed storage gadget in test mode' option in the kernel configuration.
Multiple files may be specified when the gadget module is installed, thus creating multiple drives visible to the remote host.
Any volume size may be created but it seems that Windows assigns floppy drive letters if the volume size is similar to a floppy device size. I have tested 720KB and 1440KB only.
The volume may be declared as read-only by using the "ro=1" parameter at the insmod.
The backend file may be either a disk partition or a file image.
An initial filesystem image can be created this way:
# dd if=/dev/zero of=results.bin bs=512 count=2880
# mkdosfs results.bin
Then, loop-mount the image file and populate the filsystem. Here is where problems came: if a process writes a new file to the loop filesystem, the host side of the USB connection (where the file browser runs) does not see the new file, even if the file browser is refreshed. The only workaround is to unplug/plug again the USB cable. This happens even if a 'sync' command is run on the tester device.
Also, some inconsistences happen if the USB host side writes to the device. The device doesn't see the new files, and vice-versa.
In conclusion, it is quite a good method to export only file from a Linux device, but with some limitations on "live" filesystems.