简体   繁体   中英

USB : gadget : device mode : configfs: uvc and mass storage on single configuration?

I'm trying to add uvc and mass storage in device mode for single usb device controller port. I'm using configfs to do job.

Can anybody tell how to do it.

Regards, GBiradar

You're making a device that functions as both a camera and a thumb drive? Maybe a dongle where you can upload videos then play them back in Skype? Either way, this sounds like a good exercise for configfs.

At a high level the steps are:

  1. Get mass storage working by itself.
  2. Get UVC working (streaming images to the host) by itself.
  3. Combine the two into one script.

Jumping to step 3 too early would make it harder to diagnose issues. For instance, if the userspace uvc-gadget program has issues that might prevent both gadgets from enumerating.

As root, you can run the following script verified to enumerate and stream on a Raspberry Pi Zero W with Raspbian 9. For step 1 you can remove the portions specific to UVC, then likewise with mass storage lines in step 2.

#!/bin/bash -xe

modprobe libcomposite
cd /sys/kernel/config/usb_gadget/
mkdir -p stackoverflow
cd stackoverflow
echo 0x1d6b > idVendor   # Linux Foundation
echo 0x0104 > idProduct  # Multifunction Composite Gadget
echo 239  > bDeviceClass # USB_CLASS_MISC
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
mkdir -p configs/c.1/strings/0x409
echo "My Composite Gadget" > strings/0x409/product

###################### mass storage ########################
mkdir -p functions/mass_storage.usb0
echo /dev/mmcblk0p1 > functions/mass_storage.usb0/lun.0/file
ln -s functions/mass_storage.usb0 configs/c.1/
############################################################

########################## UVC #############################
mkdir -p functions/uvc.usb1/control/header/h
cd functions/uvc.usb1/control/
ln -s header/h class/fs
cd ../../../
mkdir -p functions/uvc.usb1/streaming/uncompressed/u/360p
cat <<EOF > functions/uvc.usb1/streaming/uncompressed/u/360p/dwFrameInterval
666666
1000000
5000000
EOF
mkdir functions/uvc.usb1/streaming/header/h
cd functions/uvc.usb1/streaming/header/h
ln -s ../../uncompressed/u
cd ../../class/fs
ln -s ../../header/h
cd ../../class/hs
ln -s ../../header/h
cd ../../../../..
ln -s functions/uvc.usb1 configs/c.1/
############################################################

ls /sys/class/udc > UDC

######################## UVC part 2 ########################
sleep 1 # workaround: if gadget activated too soon, may hit a dmesg error with usb_function_activate [libcomposite]
./uvc-gadget -d
############################################################

(For reference, here's my uvc-gadget repo with patches.)

Pointing simply to the boot partition at /dev/mmcblk0p1 is one quick-and-dirty way to test mass storage off a Pi's SD card. Run sudo fdisk -l to find what you could use on your particular system.

As the Pi Zero is a USB 2.0 device I didn't need to include any class/ss (superspeed). I also intentionally omitted fields such as MaxPower, serialnumber, and manufacturer which you might find in other examples. This script is intended to show what's minimally required to get up and running.

You've posted on other channels asking about this on platforms ranging from NXP to TI, and I can't predict what problems may arise there. However, I saw it was recommended to try UVC-related fixes introduced in kernel version 4.11-rc3. Seeing as Raspbian is currently on version 4.9, perhaps those changes aren't necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM