linux icon indicating copy to clipboard operation
linux copied to clipboard

/sys/class/udc/<device>/state hangs on "not attached"

Open flicfloc opened this issue 1 year ago • 1 comments

Describe the bug

Hi I put in place "USB mass storage gadget", working well, until you use mass storage content and the /sys/class/udc//state report, correctly, "configured".

Unfortunately, after 8-10 hours with no usage, it becomes "not attached" and hangs.

Power cycle the device on the other part of the usb cable does not change the situation, and it should be... so /sys/class/udc//state hangs on "not attached".

The only way to get back udc running correctly, is shutdown -r now, so it comes back working and it still goes on working correctly (you can connect or disconnect usb cable and state of udc change correctly and accordingly) until you stop accessing mass storage content for several hours.

Resetting usb ports or hubs does not affect the "state", even power down and up usb controllers or hubs or ports.

Thank you so much in advance.

Steps to reproduce the behaviour

Pi4B out-of-the-box

installed 2023-12-05-raspios-bookworm-arm64-full

enabled SSH

disabled wifi and BT sudo nano /boot/firmware/config.txt Add dtoverlay=disable-wifi under [all] to disable Wi-Fi. Add dtoverlay=disable-bt under [all] to disable Bluetooth. checked > OK

disabled Desktop GUI (I don't want it and don't need it) sudo raspi-config

System Options	
	Boot/Auto Login
		B1 Console Text console, requiring user to login
		OK
Exit and confirming to REBOOT

Enabled the USB driver

a)
sudo nano /boot/config.txt
Scroll to the bottom and append the line below:
dtoverlay=dwc2
Press CTRL+O followed by Enter to save, and then CTRL+X to quit.

b)
sudo nano /etc/modules
Append the line below, just after the i2c-dev line:
dwc2
Press CTRL+O followed by Enter to save, and then CTRL+X to quit.

created a large file to act as the storage medium on the SD card sudo dd bs=1M if=/dev/zero of=/piusb.bin count=87040

87040+0 records in
87040+0 records out
91268055040 bytes (91 GB, 85 GiB) copied, 2629.26 s, 34.7 MB/s

NOTE: of course, the Guide I followed reported an example; I needed and created a different, bigger container file
checked > OK

formatted the container file as a FAT32 file system

sudo mkdosfs /piusb.bin -F 32 -I mkfs.fat 4.2 (2021-01-31)

checked > OK

Mounted the container file

First, created a folder on which we can mount the file system:

sudo mkdir /mnt/usb_share

Added this to fstab, the configuration fi le that records our available disk partitions:

sudo nano /etc/fstab

Appended the line below to the end of the file:

/piusb.bin /mnt/usb_share vfat users,umask=000 0 2

NOTE: sudo mount -a >> differently from the guide, returned to me a message inviting to use instead
"sudo systemctl daemon-reload" >> DID IT >> good, checked >> OK

Downloaded a test file

cd /mnt/usb_share

sudo wget http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
--2024-01-21 19:24:38--  http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
Resolving commondatastorage.googleapis.com (commondatastorage.googleapis.com)... 142.250.180.155, 142.251.209.27, 142.251.209.59, ...
Connecting to commondatastorage.googleapis.com (commondatastorage.googleapis.com)|142.250.180.155|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 158008374 (151M) [video/mp4]
Saving to: ‘BigBuckBunny.mp4’

BigBuckBunny.mp4    100%[===================>] 150.69M  11.4MB/s    in 14s

2024-01-21 19:24:51 (11.1 MB/s) - ‘BigBuckBunny.mp4’ saved [158008374/158008374]

checked > OK

flushed any cached data to the disk

sync

done > OK

Tested mass storage device mode

sudo modprobe g_mass_storage file=/piusb.bin stall=0 removable=y

checked > OK

NOTE: in the guide you will see "sudo modprobe g_mass_storage file=/piusb.bin stall=0 ro=1", that will worked too,
but, given I need a read/write and not a read-only device, I changed it to "removable=y"

sudo modprobe -r g_mass_storage

checked > OK

LOG:
[Jan22 17:58] Mass Storage Function, version: 2009/09/11
[  +0.000018] LUN: removable file: (no medium)
[  +0.000058] LUN: removable file: /piusb.bin
[  +0.000005] Number of LUNs=1
[  +0.000153] g_mass_storage gadget.0: Mass Storage Gadget, version: 2009/09/11
[  +0.000008] g_mass_storage gadget.0: userspace failed to provide iSerialNumber
[  +0.000004] g_mass_storage gadget.0: g_mass_storage ready
[  +0.000008] dwc2 fe980000.usb: bound driver g_mass_storage

enabled Samba

sudo apt-get update
sudo apt-get install samba winbind -y

sudo nano /etc/samba/smb.conf

Scrolled down to the end of the file and append the lines below:

[usb]
browseable = yes
path = /mnt/usb_share
guest ok = yes
read only = no
create mask = 777

saved and exit

then:
sudo systemctl restart smbd.service

then:
cat /etc/hostname

inserted my private hostname

checked access by Windows10 >> perfect OK

I see two shared folders:

nobody
	(never touched, I don't know what it is)

usb
	(the good one: it contains all my files and work like a charm)
	

Automated USB device reconnect

sudo pip3 install watchdog >> NOTE1: unfortunately, as for Raspberry original documentation
(I discovered after my setup) this command doesn't work, but I did not know and I solved,
googling around with this one:

sudo pip install watchdog --break-system-packages

	checked pip package installation and working > OK
		
As for the guide: 
"We then need some code to start a timer whenever something changes in the shared folder. The timer is reset to zero every time a new change occurs, and the USB reconnect is only triggered if we see 30 seconds of inactivity after a change."

We’ve written a program to do this. To download it, type:

cd /usr/local/share
sudo wget http://rpf.io/usbzw -O usb_share.py
sudo chmod +x usb_share.py

	done > OK
	
NOTE2:

for the reason I written before, I had to edit this usb_share.py script, this way:

sudo nano usb_share.py

changed from "modprobe g_mass_storage file=/piusb.bin stall=0 ro=1" 
to
"modprobe g_mass_storage file=/piusb.bin stall=0 removable=y"

checked reboot > checked loading script, changing files, view update of the folder >> all working with no issue

NEXT STEP, mine specific step (the reason I bought Pi4B is to have a "customizable via script" usb mass storage):

created a test_rename_script in python in order to
	test renaming of 3 file inside the root my usb mass storage container file, one time per day, in the middle of the night

	done, checked > OK
	

created a rename_all_script in python in order to
	renaming file every file inside the root my usb mass storage container file, one time per day, in the middle of the night

	done, checked > OK

	mkdir myscripts
	
	put these scripts here

setting up Cron and its log

installed rsyslog

sudo apt install rsyslog

sudo nano /etc/rsyslog.conf
cron.* /var/log/cron.log

checked Cron execution and log > OK

coming back to the Guide I followed: Background service We need to make this program into a background service, so that it starts automatically at boot time. We can do that by making it into a systemd unit. Enter the commands below:

cd /etc/systemd/system
sudo nano usbshare.service

This will start a new blank file. In the new file, enter:

[Unit]
Description=USB Share Watchdog

[Service]
Type=simple
ExecStart=/usr/local/share/usb_share.py
Restart=always

[Install]
WantedBy=multi-user.target

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now we need to register the service, enable it, and set it running:

sudo systemctl daemon-reload
sudo systemctl enable usbshare.service
sudo systemctl start usbshare.service

Whenever you copy files over to the network share, the USB device should automatically reconnect after 30 seconds of inactivity.

done, checked > OK

Device (s)

Raspberry Pi 4 Mod. B

System

Raspberry Pi reference 2023-12-05 Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 70cd6f2a1e34d07f5cba7047aea5b92457372e05, stage5

Oct 17 2023 15:39:16 Copyright (c) 2012 Broadcom version 30f0c5e4d076da3ab4f341d88e7d505760b93ad7 (clean) (release) (start)

Linux casettapi 6.1.0-rpi7-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.63-1+rpt1 (2023-11-24) aarch64 GNU/Linux

Logs

No response

Additional context

No response

flicfloc avatar Feb 10 '24 14:02 flicfloc

I am not a linux guru, so maybe something could be not so well described so, if you need, please ask to me everything you need..

I miss to mention : dmesg sees correctly power on of the device that should access to mass storage.

flicfloc avatar Feb 10 '24 14:02 flicfloc