基于TI Davinci处理器Linux系统的USB Gadget存储模块加载问题
Hey there! Let’s break down some practical, reliable alternatives to using the USB Gadget mass storage approach for pulling video files from your TI Davinci device to a host PC. I’ve tinkered with similar embedded media workflows, so these options are tailored to your setup:
1. USB Gadget: Switch to Virtual Serial/Ethernet (Instead of Mass Storage)
Instead of exposing your entire storage partition as a USB drive, leverage other USB Gadget functionalities to create a dedicated data channel:
- How to implement:
- Configure your kernel to support USB Gadget CDC-ACM (virtual serial port) or ECM (virtual Ethernet adapter).
- Set up a lightweight file transfer service on the Davinci:
- Use
dropbear(a tiny SSH/SFTP server) for secure transfers—perfect for embedded systems. - Or go simple with
tftpd-hpaif you don’t need encryption.
- Use
- On your host PC, once the USB connection is established, use
sftp user@<virtual-ip>:/path/to/video.mp4 ./ortftp -g -r video.mp4 <device-ip>to pull files. - Bonus: You can even add a custom module to your existing C program—when it gets a "transfer file" command via serial, it sends the file data over the USB virtual serial port, and a small host-side script saves it locally.
- Pros: No risk of accidental file deletion on the device, you can transfer individual files without mounting the entire partition, and it avoids conflicts with active video recording.
- Cons: Requires a bit of kernel configuration and setting up the lightweight service.
2. Reuse Your Existing Serial Port for File Transfer
Since you’re already using serial to handle command input, you can repurpose that same channel for file transfers—no extra USB setup needed:
- How to implement:
- Add a new command to your C program, like
SEND_FILE /path/to/video.mp4. When triggered, the program opens the video file, sends a header with the file size, then transmits the data in chunks (add simple checksums to catch errors). - Use a proven protocol like Xmodem/Ymodem (use libraries like
libxmodemto avoid reinventing the wheel) for reliable serial transfers—critical if you’re dealing with large files. - Write a small Python or C tool on your host PC to listen on the serial port, parse the incoming data, and save the file.
- Add a new command to your C program, like
- Pros: Zero additional hardware or kernel changes, fully integrates with your existing command system.
- Cons: Serial speeds are slow (even at 1Mbps, a 1GB video takes ~20 minutes), so this is best for small clips.
3. Network-Based Transfer (If You Have Ethernet/Wi-Fi)
If your Davinci board has an Ethernet port or you can add a Wi-Fi module, network transfers are hands-down the fastest option:
- How to implement:
- Configure your device’s network (static IP or DHCP) so it’s on the same LAN as your host PC.
- Run a lightweight file service:
nginxas a static file server—just drop videos in a web-accessible directory and download them via a browser orcurl.dropbearfor SFTP, or evennetcatfor quick one-off transfers (runcat video.mp4 | nc -l 1234on the device, thennc <device-ip> 1234 > video.mp4on the host).
- In your C program, add logic to automatically push files to the host when it gets a transfer command (use
scporcurlviasystem()calls).
- Pros: Blazing fast transfer speeds, ideal for large video files, and flexible for remote access if needed.
- Cons: Requires network hardware (Ethernet/Wi-Fi) and LAN setup.
4. USB Gadget Composite Device (Best of Both Worlds)
If you still want the convenience of a USB drive but need to avoid recording conflicts, set up a composite USB Gadget that supports both mass storage and a virtual serial port:
- How to implement:
- Configure your kernel to support USB Gadget composite devices, enabling both mass storage and CDC-ACM.
- In your C program, add logic to:
- Stop video recording and close the video file when a transfer is requested.
- Load the mass storage Gadget module (e.g.,
modprobe g_mass_storage file=/dev/mmcblk0p2 removable=1). - Wait for the host to copy the file, then unload the Gadget module (
rmmod g_mass_storage). - Restart video recording.
- Example code snippet for module control:
#include <stdlib.h> #include <stdio.h> int load_usb_mass_storage() { // Adjust the file path to match your video partition int ret = system("modprobe g_mass_storage file=/dev/mmcblk0p2 removable=1"); if (ret != 0) { printf("Failed to load USB mass storage gadget\n"); return -1; } return 0; } int unload_usb_mass_storage() { int ret = system("rmmod g_mass_storage"); if (ret != 0) { printf("Failed to unload USB mass storage gadget\n"); return -1; } return 0; }
- Pros: Keeps the "plug-and-play USB drive" experience, while eliminating conflicts with active recording.
- Cons: Requires careful handling of file system states to avoid data corruption.
Quick Recommendation
If you want minimal setup and don’t mind slower transfers, go with the serial reuse option. For speed and flexibility, the USB virtual Ethernet + SFTP combo is great. If you need the USB drive convenience, the composite gadget approach is your best bet.
内容的提问来源于stack exchange,提问作者Vinod kumar

