USB gadget on sama5d27-som1-ek

This forum is for users of Microchip MPUs and who are interested in using Linux OS.

Moderator: nferre

schrott
Posts: 1
Joined: Thu Sep 26, 2019 10:41 am

USB gadget on sama5d27-som1-ek

Thu Oct 17, 2019 1:14 pm

I went to Microchip's EUMasters19 last week and now I'm just trying to modify my linux image so it does what I want it to do. The idea is to make it behave like a usb mass storage, so a thumb drive. I stumbled upon this thread: https://unix.stackexchange.com/question ... h-configfs and from there I took the script and modified it a little bit.

Code: Select all

$ cat board/company/sama5d27-som1-ek1/fsoverlay/root/usb_gadget_mass_storage.sh 
#!/bin/sh

set -e

CONFIGFS="/sys/kernel/config"
GADGET="$CONFIGFS/usb_gadget"
VID="0x0525"
PID="0xa4a1"
SERIAL="5180"
MANUF="Myself"
PRODUCT="MyStorageProduct"

case "$1" in
	start)
		echo "Creating the USB gadget"
		echo "Loading composite module"
		modprobe atmel_usba_udc
		modprobe libcomposite

		echo "Mounting configFS"
		mount -t configfs none $CONFIGFS

		echo "Creating gadget directory g1"
		mkdir -p $GADGET/g1

		cd $GADGET/g1
		if [ $? -ne 0 ]; then
			echo "Error creating usb gadget in configfs"
			exit 1;
		else
			echo "OK"
		fi

		echo "Creating Mass Storage interface"
		echo "  Creating backing file"
		dd if=/dev/zero of=/root/storage.img bs=1M count=2 > /dev/null 2>&1
		mkdosfs /root/storage.img > /dev/null 2>&1
		echo "  OK"

		echo "  Creating gadget functionality"
		mkdir $GADGET/g1/functions/mass_storage.0
		echo 1 > $GADGET/g1/functions/mass_storage.0/stall
		echo "/root/storage.img" > $GADGET/g1/functions/mass_storage.0/lun.0/file
		echo 1 > $GADGET/g1/functions/mass_storage.0/lun.0/removable
		echo 0 > $GADGET/g1/functions/mass_storage.0/lun.0/cdrom
		mkdir $GADGET/g1/configs/c.1
		mkdir $GADGET/g1/configs/c.1/strings/0x409
		ln -s $GADGET/g1/functions/mass_storage.0 $GADGET/g1/configs/c.1
		echo "  OK"
		echo "OK"

		echo "Setting Vendor and Product ID's"
		echo $VID > $GADGET/g1/idVendor
		echo $PID > $GADGET/g1/idProduct
		echo "OK"

		echo "Setting English strings"
		mkdir -p $GADGET/g1/strings/0x409
		echo $SERIAL > $GADGET/g1/strings/0x409/serialnumber
		echo $MANUF > $GADGET/g1/strings/0x409/manufacturer
		echo $PRODUCT > $GADGET/g1/strings/0x409/product
		echo "OK"

		echo "Binding USB Device Controller"
		echo `ls /sys/class/udc` > $GADGET/g1/UDC
		echo "OK"
		;;
	stop)
		echo "Stopping the USB gadget"

		if [ $? -ne 0 ]; then
			echo "Error: no configfs gadget found" 
			exit 1;
		fi

		echo "Unbinding USB Device Controller"
		echo "" > $GADGET/g1/UDC
		echo "OK"

		echo "Removing Mass Storage interface"
		rm -f $GADGET/g1/configs/c.1/mass_storage.0
		rm -f /root/storage.img
		rmdir $GADGET/g1/functions/mass_storage.0
		echo "OK"

		echo "Clearing English strings"
		rmdir $GADGET/g1/strings/0x409
		echo "OK"

		echo "Cleaning up configuration"
		rmdir $GADGET/g1/configs/c.1/strings/0x409
		rmdir $GADGET/g1/configs/c.1
		echo "OK"

		echo "Removing gadget directory"
		rmdir $GADGET/g1
		cd /
		echo "OK"

		echo "Unmounting configFS"
		umount $CONFIGFS

		echo "Disable composite USB gadgets"
		modprobe -r usb_f_mass_storage
		modprobe -r libcomposite
		modprobe -r atmel_usba_udc
		echo "OK"
		;;
	*)
		echo "Usage : $0 {start|stop}"
esac
The script seems to work and I get these messages:

Code: Select all

atmel_usba_udc 300000.gadget: MMIO registers at [mem 0xfc02c000-0xfc02c3ff] mapped at d985ad36
atmel_usba_udc 300000.gadget: FIFO at [mem 0x00300000-0x003fffff] mapped at 31626f52
Mass Storage Function, version: 2009/09/11
LUN: removable file: (no medium)
But when I connect the running board to another PC through USBA nothing happens.
This should be the correct interface, right? I also tried USBB same result.

I looked into https://www.at91.com/linux4sam/bin/view ... dgetConfig but I have to admit, that I don't get the fifo_mode thing.

Has anyone a working example for me? Or a manual?
blue_z
Location: USA
Posts: 1986
Joined: Thu Apr 19, 2007 10:15 pm

Re: USB gadget on sama5d27-som1-ek

Fri Oct 18, 2019 11:50 pm

schrott wrote: ... I'm just trying to modify my linux image so it does what I want it to do.
What is this mystery kernel version/build, and does it have all the necessary drivers?

schrott wrote: The script seems to work and I get these messages:

Code: Select all

atmel_usba_udc 300000.gadget: MMIO registers at [mem 0xfc02c000-0xfc02c3ff] mapped at d985ad36
atmel_usba_udc 300000.gadget: FIFO at [mem 0x00300000-0x003fffff] mapped at 31626f52
Mass Storage Function, version: 2009/09/11
LUN: removable file: (no medium)
Since I get a different response with a Linux4SAM 6.0 demo, I'm inclined to disagree that you're seeing the script "working".
My results with your script:

Code: Select all

# ls -l /sys/kernel/config                                                      
total 0                                                                         
# cd /tmp
# source ./schrott.sh start                                                           
Creating the USB gadget                                                         
Loading composite module                                                        
Mounting configFS                                                               
Creating gadget directory g1                                                    
OK                                                                              
Creating Mass Storage interface                                                 
  Creating backing file                                                         
  OK                                                                            
  Creating gadget functionality                                                 
Mass Storage Function, version: 2009/09/11                                      
LUN: removable file: (no medium)                                                
  OK                                                                            
OK                                                                              
Setting Vendor and Product ID's                                                 
OK                                                                              
Setting English strings                                                         
OK                                                                              
Binding USB Device Controller                                                   
OK                                                                              
# pwd                                                                           
/sys/kernel/config/usb_gadget/g1  
# ls -la                                                                        
total 0                                                                         
drwxr-xr-x    6 root     root             0 Jul 17 00:22 .                      
drwxr-xr-x    3 root     root             0 Jul 17 00:22 ..                     
-rw-r--r--    1 root     root          4096 Jul 17 00:22 UDC                    
-rw-r--r--    1 root     root          4096 Jul 17 00:42 bDeviceClass           
-rw-r--r--    1 root     root          4096 Jul 17 00:42 bDeviceProtocol        
-rw-r--r--    1 root     root          4096 Jul 17 00:42 bDeviceSubClass        
-rw-r--r--    1 root     root          4096 Jul 17 00:42 bMaxPacketSize0        
-rw-r--r--    1 root     root          4096 Jul 17 00:42 bcdDevice              
-rw-r--r--    1 root     root          4096 Jul 17 00:42 bcdUSB                 
drwxr-xr-x    3 root     root             0 Jul 17 00:22 configs                
drwxr-xr-x    3 root     root             0 Jul 17 00:22 functions              
-rw-r--r--    1 root     root          4096 Jul 17 00:22 idProduct              
-rw-r--r--    1 root     root          4096 Jul 17 00:22 idVendor               
drwxr-xr-x    2 root     root             0 Jul 17 00:22 os_desc                
drwxr-xr-x    3 root     root             0 Jul 17 00:22 strings                
#   


schrott wrote: But when I connect the running board to another PC through USBA nothing happens.
I see the target board responding with "configfs-gadget gadget: high-speed config #1: c",
and the host Linux PC responds with a "removeable medium is inserted" message while its syslog has:

Code: Select all

[11728.848168] usb 2-1: new high-speed USB device number 4 using ehci-pci
[11729.005327] usb 2-1: New USB device found, idVendor=0525, idProduct=a4a1
[11729.005334] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[11729.005353] usb 2-1: Product: MyStorageProduct
[11729.005357] usb 2-1: Manufacturer: Myself
[11729.005361] usb 2-1: SerialNumber: 5180
[11729.115247] usb-storage 2-1:1.0: USB Mass Storage device detected
[11729.115478] scsi host4: usb-storage 2-1:1.0
[11729.115632] usbcore: registered new interface driver usb-storage
[11729.131160] usbcore: registered new interface driver uas
[11730.145083] scsi 4:0:0:0: Direct-Access     Linux    File-Stor Gadget 0414 PQ: 0 ANSI: 2
[11730.148626] sd 4:0:0:0: Attached scsi generic sg2 type 0
[11730.149675] sd 4:0:0:0: Power-on or device reset occurred
[11730.150837] sd 4:0:0:0: [sdb] 4096 512-byte logical blocks: (2.10 MB/2.00 MiB)
[11730.264407] sd 4:0:0:0: [sdb] Write Protect is off
[11730.264412] sd 4:0:0:0: [sdb] Mode Sense: 0f 00 00 00
[11730.384429] sd 4:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[11730.505001]  sdb:
[11730.744418] sd 4:0:0:0: [sdb] Attached SCSI removable disk
I did not do any further testing.

Seems like your script might work (in a suitable kernel + rootfs environment).

Regards

Return to “LINUX”

Who is online

Users browsing this forum: No registered users and 6 guests