Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usb/dwc2: Set correct state on gadget disconnect #3151

Open
wants to merge 1 commit into
base: rpi-4.19.y
Choose a base branch
from
Open

usb/dwc2: Set correct state on gadget disconnect #3151

wants to merge 1 commit into from

Conversation

sn00pster
Copy link

When operating as a USB device, when the device is disconnected the suspend interrupt is called rather than the disconnect interrupt, this results in the state remaining as "configured". This change changes the state to "not attached" instead when the suspend interrupt is called.

I don't know whether this patch interferes with anything else or whether it's the correct way of "fixing" this issue, there is chatter on the web about similar behaviour a long time ago but patches were submitted, so I don't know whether this is something which is just affecting the hardware implementation on the Pi.

Without the correct state being reflected, it's not possible to detect when a gadget is connected and disconnected from a device as without this patch after enumeration even pulling the the cable the Pi device remains in "configured" state. With this patch it reverts back to "not attached".

When operating as a USB device, when the device is disconnected the suspend interrupt is called rather than the disconnect interrupt, this results in the state remaining as "configured".  This change changes the state to "not attached" instead when the suspend interrupt is called.
@lategoodbye
Copy link
Contributor

Please provide some more information about the issue:

  • Which Raspberry Pi model did you use?
  • How do you power the Raspberry Pi?
  • Did you tried a recent mainline kernel?

@sn00pster
Copy link
Author

Pi Zero W, powered via a pi power supply. Only tried the most recent kernel available through raspbian. This issue has been mentioned on the Pi forums a few times in the past without any resolution, also searching the web throws up results.

@lategoodbye
Copy link
Contributor

lategoodbye commented Aug 14, 2019

Let me rephrase my second question, which interface powers the Raspberry Pi Zero W:
a) GPIO header
b) USB
c) PWR IN

dwc2 is under active development in the mainline kernel. So your patch should be submitted to linux-usb for a proper solution. But you're patch must be tested with a recent mainline kernel like Linux 5.2 before.

@sn00pster
Copy link
Author

PWR IN.

I’m unsure whether this issue affects anything other than raspberry pi’s (external hardware around the OTG section, my colleague made a slight “improvement” on the OTG section on one of our boards which had unintended consequences with host/device detection so I know its always a possibility), it would seem somewhat of an oversight if it affected everything and I’d expect more results when searching for the issue, there are some generic search hits for it from a couple of years back but patches were submitted.

I’ll see if I can try with a mainline kernel, I. don't suppose you have any links on setting up a system using such a beast? I suppose i could also try pulling the latest source for the dwc2 module and see if it compiles with my kernel.

I only posted this “fix” as i posted a solution to this problem on the forums and one of the staff suggested i create a pull request so people can look at it.

Regardless of my solution, its a problem on the Pi Zero W (and potentially other Pi’s) which means that you cant track the disconnected and enumerated state. I’ll admit it was a quick hack fix, but it works for me in my situation, at the very least hopefully it can cause some discussion about the problem.

@lategoodbye
Copy link
Contributor

Please try to use this gist in order to compile the mainline kernel. You can use bcm2835_defconfig as config and bcm2835-rpi-zero-w.dtb as devicetree binary. Do not use the devicetree from Raspbian in combination with a mainline kernel.

@sn00pster
Copy link
Author

ok, will take a look and have a go.

For what its worth, I’ve just looked at the history for the dwc2 module and can’t see any commits that look like they would have any effect on this, but I guess you never know until you try, ot holding my breath.

@sn00pster
Copy link
Author

@lategoodbye Hi, I've followed the instructions with the latest mainline, kernel modules installed, device tree overlay copied and selected.

Kernel starts to boot and then stops at "Waiting for root device"

Switch back to original kernel, system boots fine.

Any ideas?

@sn00pster
Copy link
Author

I also tried getting the .confid out of the booted rapbian system and used that as the config for the kernel when building, still have same issue.

@lategoodbye
Copy link
Contributor

lategoodbye commented Aug 15, 2019

I also tried getting the .confid out of the booted rapbian system and used that as the config for the kernel when building, still have same issue.

This won't work, you will need to replace kernel and DTB. You will need to use make bcm2835_defconfig as config. Overlays doesn't work yet in mainline.

Maybe you can provide your dmesg and config.txt.

@sn00pster
Copy link
Author

ok, will try again.

No boot log, I've just ordered a USB to TTL adaptor so I can hook up a terminal to capture everything.

I'll repull the repo and build from fresh and try again.

Thanks for your support, much appreciated!

@lategoodbye
Copy link
Contributor

Btw are you using the stable tree e.g. git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git ?

@sn00pster
Copy link
Author

No, I was using the version that was in the gist, I'm just pulling the stable now.

@lategoodbye
Copy link
Contributor

The torvalds tree is also okay, you will need to choose a final release like v5.2.0

@sn00pster
Copy link
Author

Ok, so I have kernel booted, but a lot of stuff appears not to work, I don't seem to be able to load the dwc2 module and there's a whole load of errors about missing firmware.

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 5.3.0-rc4 (adrian@adrian-Standard-PC-Q35-ICH9-2009) (gcc version 7.3.1 20180425 [linaro-7.3-2018.05 revision d29120a424ecfbc167ef90065c0eeb7f91977701] (Linaro GCC 7.3-2018.05)) #1 Thu Aug 15 12:43:40 BST 2019
[    0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
[    0.000000] OF: fdt: Machine model: Raspberry Pi Zero W Rev 1.1
[    0.000000] Memory policy: Data cache writeback
[    0.000000] cma: Reserved 32 MiB at 0x19c00000
[    0.000000] On node 0 totalpages: 114688
[    0.000000]   Normal zone: 896 pages used for memmap
[    0.000000]   Normal zone: 0 pages reserved
[    0.000000]   Normal zone: 114688 pages, LIFO batch:31
[    0.000000] CPU: All CPU(s) started in SVC mode.
[    0.000000] pcpu-alloc: s0 r0 d32768 u32768 alloc=1*32768
[    0.000000] pcpu-alloc: [0] 0 
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 113792
[    0.000000] Kernel command line: video=HDMI-A-1:1920x1080@60 dma.dmachans=0x7f35 bcm2708.boardrev=0x9000c1 bcm2708.serial=0x7a1f2d6c bcm2708.uart_clock=48000000 bcm2708.disk_led_gpio=47 smsc95xx.macaddr=B8:27:EB:1F:2D:6C vc_mem.mem_base=0x1ec00000 vc_mem.mem_size=0x20000000  dwc_otg.lpm_enable=0 console=ttyS0,115200 console=tty1 root=PARTUUID=99c633c0-02 rootfstype=ext4 rootwait elevator=deadline fsck.repair=yes debug
[    0.000000] Dentry cache hash table entries: 65536 (order: 6, 262144 bytes, linear)
[    0.000000] Inode-cache hash table entries: 32768 (order: 5, 131072 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 408244K/458752K available (8192K kernel code, 611K rwdata, 2644K rodata, 1024K init, 679K bss, 17740K reserved, 32768K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] ftrace: allocating 28438 entries in 56 pages
[    0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
[    0.000000] random: get_random_bytes called from start_kernel+0x2dc/0x4f0 with crng_init=0
[    0.000024] sched_clock: 32 bits at 1000kHz, resolution 1000ns, wraps every 2147483647500ns
[    0.000059] clocksource: timer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
[    0.000152] bcm2835: system timer (irq = 27)
[    0.000833] Console: colour dummy device 80x30
[    0.001567] printk: console [tty1] enabled
[    0.001661] Calibrating delay loop... 697.95 BogoMIPS (lpj=3489792)
[    0.060331] pid_max: default: 32768 minimum: 301
[    0.060846] Mount-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.060913] Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.062254] CPU: Testing write buffer coherency: ok
[    0.063567] Setting up static identity map for 0x100000 - 0x100054
[    0.069014] devtmpfs: initialized
[    0.078267] VFP support v0.3: implementor 41 architecture 1 part 20 variant b rev 5
[    0.078752] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[    0.078833] futex hash table entries: 256 (order: -1, 3072 bytes, linear)
[    0.083552] pinctrl core: initialized pinctrl subsystem
[    0.084529] NET: Registered protocol family 16
[    0.087195] DMA: preallocated 256 KiB pool for atomic coherent allocations
[    0.092892] No ATAGs?
[    0.092923] hw-breakpoint: found 6 breakpoint and 1 watchpoint registers.
[    0.093011] hw-breakpoint: maximum watchpoint size is 4 bytes.
[    0.093365] Serial: AMBA PL011 UART driver
[    0.132174] SCSI subsystem initialized
[    0.132717] usbcore: registered new interface driver usbfs
[    0.132882] usbcore: registered new interface driver hub
[    0.133091] usbcore: registered new device driver usb
[    0.133751] videodev: Linux video capture interface: v2.00
[    0.134170] Advanced Linux Sound Architecture Driver Initialized.
[    0.135084] Bluetooth: Core ver 2.22
[    0.135229] NET: Registered protocol family 31
[    0.135267] Bluetooth: HCI device and connection manager initialized
[    0.135324] Bluetooth: HCI socket layer initialized
[    0.135367] Bluetooth: L2CAP socket layer initialized
[    0.135435] Bluetooth: SCO socket layer initialized
[    0.136794] clocksource: Switched to clocksource timer
[    0.366786] simple-framebuffer 1e887000.framebuffer: framebuffer at 0x1e887000, 0x373800 bytes, mapped to 0x(ptrval)
[    0.366996] simple-framebuffer 1e887000.framebuffer: format=r5g6b5, mode=1824x984x16, linelength=3648
[    0.398738] Console: switching to colour frame buffer device 228x61
[    0.429263] simple-framebuffer 1e887000.framebuffer: fb0: simplefb registered!
[    0.449029] thermal_sys: Registered thermal governor 'step_wise'
[    0.449767] NET: Registered protocol family 2
[    0.451264] tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 4096 bytes, linear)
[    0.451602] TCP established hash table entries: 4096 (order: 2, 16384 bytes, linear)
[    0.451934] TCP bind hash table entries: 4096 (order: 2, 16384 bytes, linear)
[    0.452236] TCP: Hash tables configured (established 4096 bind 4096)
[    0.452627] UDP hash table entries: 256 (order: 0, 4096 bytes, linear)
[    0.452879] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes, linear)
[    0.453411] NET: Registered protocol family 1
[    0.454677] RPC: Registered named UNIX socket transport module.
[    0.454911] RPC: Registered udp transport module.
[    0.455076] RPC: Registered tcp transport module.
[    0.455237] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    0.458195] hw perfevents: no irqs for PMU, sampling events not supported
[    0.458523] hw perfevents: enabled with armv6_1176 PMU driver, 3 counters available
[    0.461356] Initialise system trusted keyrings
[    0.461983] workingset: timestamp_bits=30 max_order=17 bucket_order=0
[    0.478550] Installing knfsd (copyright (C) 1996 [email protected]).
[    0.541206] jitterentropy: Initialization failed with host not compliant with requirements: 2
[    0.541629] Key type asymmetric registered
[    0.541794] Asymmetric key parser 'x509' registered
[    0.542086] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 252)
[    0.542337] io scheduler mq-deadline registered
[    0.542500] io scheduler kyber registered
[    0.552466] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
[    0.556369] 20215040.serial: ttyS1 at MMIO 0x0 (irq = 53, base_baud = 31250000) is a 16550
[    0.562707] bcm2835-power bcm2835-power: Broadcom BCM2835 power domains driver
[    0.564330] libphy: Fixed MDIO Bus: probed
[    0.565041] usbcore: registered new interface driver zd1211rw
[    0.565358] usbcore: registered new interface driver lan78xx
[    0.575057] usbcore: registered new interface driver asix
[    0.584568] usbcore: registered new interface driver ax88179_178a
[    0.593811] usbcore: registered new interface driver cdc_ether
[    0.603087] usbcore: registered new interface driver smsc95xx
[    0.612069] usbcore: registered new interface driver net1080
[    0.620950] usbcore: registered new interface driver cdc_subset
[    0.629688] usbcore: registered new interface driver zaurus
[    0.638462] usbcore: registered new interface driver cdc_ncm
[    0.647837] usbcore: registered new interface driver usb-storage
[    0.656577] i2c /dev entries driver
[    0.668135] i2c-bcm2835 20805000.i2c: Could not read clock-frequency property
[    0.679307] bcm2835-wdt bcm2835-wdt: Broadcom BCM2835 watchdog timer
[    0.688973] sdhci: Secure Digital Host Controller Interface driver
[    0.697554] sdhci: Copyright(c) Pierre Ossman
[    0.802653] sdhost-bcm2835 20202000.mmc: loaded - DMA enabled (>1)
[    0.811789] sdhci-pltfm: SDHCI platform and OF driver helper
[    0.821434] sdhci-iproc 20300000.sdhci: allocated mmc-pwrseq
[    0.862499] mmc1: SDHCI controller on 20300000.sdhci [20300000.sdhci] using PIO
[    0.873453] ledtrig-cpu: registered to indicate activity on CPUs
[    0.883463] usbcore: registered new interface driver usbhid
[    0.892348] usbhid: USB HID core driver
[    0.902675] bcm2835-mbox 2000b880.mailbox: mailbox enabled
[    0.915830] oprofile: using arm/armv6
[    0.931413] NET: Registered protocol family 10
[    0.942069] mmc1: queuing unknown CIS tuple 0x80 (2 bytes)
[    0.952598] Segment Routing with IPv6
[    0.962213] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
[    0.971997] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
[    0.982025] NET: Registered protocol family 17
[    0.993327] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
[    1.002442] random: fast init done
[    1.012517] Loading compiled-in X.509 certificates
[    1.023749] mmc1: queuing unknown CIS tuple 0x80 (7 bytes)
[    1.039305] mmc0: host does not support reading read-only switch, assuming write-enable
[    1.051904] 20201000.serial: ttyAMA0 at MMIO 0x20201000 (irq = 81, base_baud = 0) is a PL011 rev2
[    1.061148] mmc0: new high speed SDHC card at address aaaa
[    1.070453] serial serial0: tty port ttyAMA0 registered
[    1.081625] mmcblk0: mmc0:aaaa SC16G 14.8 GiB 
[    1.093850] raspberrypi-firmware soc:firmware: Attached to firmware from 2019-07-09 14:40
[    1.108752]  mmcblk0: p1 p2
[    1.119977] debugfs: Directory '20902000.hdmi' with parent 'vc4-hdmi' already present!
[    1.132270] vc4_hdmi 20902000.hdmi: ASoC: Failed to create component debugfs directory: -17
[    1.143143] vc4_hdmi 20902000.hdmi: vc4-hdmi-hifi <-> 20902000.hdmi mapping ok
[    1.154190] vc4-drm soc:gpu: bound 20902000.hdmi (ops vc4_hdmi_ops)
[    1.163622] vc4-drm soc:gpu: bound 20806000.vec (ops vc4_vec_ops)
[    1.172915] vc4-drm soc:gpu: bound 20004000.txp (ops vc4_txp_ops)
[    1.181970] vc4-drm soc:gpu: bound 20400000.hvs (ops vc4_hvs_ops)
[    1.191974] vc4-drm soc:gpu: bound 20206000.pixelvalve (ops vc4_crtc_ops)
[    1.202089] vc4-drm soc:gpu: bound 20207000.pixelvalve (ops vc4_crtc_ops)
[    1.211996] vc4-drm soc:gpu: bound 20807000.pixelvalve (ops vc4_crtc_ops)
[    1.220724] vc4-drm soc:gpu: bound 20c00000.v3d (ops vc4_v3d_ops)
[    1.229129] checking generic (1e887000 373800) vs hw (0 ffffffff)
[    1.237367] fb0: switching to vc4drmfb from simple
[    1.246124] Console: switching to colour dummy device 80x30
[    1.246354] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
[    1.246395] [drm] Driver supports precise vblank timestamp query.
[    1.248797] [drm] Initialized vc4 0.0.0 20140616 for soc:gpu on minor 0
[    1.338907] Console: switching to colour frame buffer device 240x67
[    1.389373] vc4-drm soc:gpu: fb0: vc4drmfb frame buffer device
[    1.414017] mmc1: new high speed SDIO card at address 0001
[    1.637039] dwc2 20980000.usb: EPs: 8, dedicated fifos, 4080 entries in SPRAM
[    1.638886] dwc2 20980000.usb: DWC OTG Controller
[    1.639173] dwc2 20980000.usb: new USB bus registered, assigned bus number 1
[    1.639508] dwc2 20980000.usb: irq 33, io mem 0x20980000
[    1.641555] hub 1-0:1.0: USB hub found
[    1.641883] hub 1-0:1.0: 1 port detected
[    1.646472] cfg80211: Loading compiled-in X.509 certificates for regulatory database
[    1.654180] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
[    1.654548] ALSA device list:
[    1.654951] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
[    1.655259] cfg80211: failed to load regulatory.db
[    1.655676]   #0: vc4-hdmi
[    1.673072] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[    1.673478] VFS: Mounted root (ext4 filesystem) readonly on device 179:2.
[    1.682967] devtmpfs: mounted
[    1.689573] Freeing unused kernel memory: 1024K
[    1.690207] Run /sbin/init as init process
[    2.479109] systemd[1]: System time before build time, advancing clock.
[    2.531218] systemd[1]: Failed to find module 'autofs4'
[    2.578948] systemd[1]: systemd 241 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid)
[    2.580022] systemd[1]: No virtualization found in DMI
[    2.580325] systemd[1]: No virtualization found in CPUID
[    2.580699] systemd[1]: Virtualization XEN not found, /proc/xen does not exist
[    2.581687] systemd[1]: No virtualization found in /proc/device-tree/*
[    2.582539] systemd[1]: UML virtualization not found in /proc/cpuinfo.
[    2.582947] systemd[1]: This platform does not support /proc/sysinfo
[    2.583271] systemd[1]: Found VM virtualization none
[    2.583559] systemd[1]: Detected architecture arm.
[    2.585159] systemd[1]: Mounting cgroup to /sys/fs/cgroup/perf_event of type cgroup with options perf_event.
[    4.106401] systemd-bless-boot-generator[81]: Skipping generator, not an EFI boot.
[    4.235710] systemd-fstab-generator[84]: Parsing /etc/fstab
[    4.256683] systemd-rc-local-generator[88]: Automatically adding rc-local.service.
[    4.301950] systemd-hibernate-resume-generator[87]: Not running in an initrd, quitting.
[    4.329244] systemd-gpt-auto-generator[86]: Failed to chase block device '/', ignoring: No such file or directory
[    4.393309] systemd-fstab-generator[84]: Found entry what=proc where=/proc type=proc makefs=no nofail=no noauto=no
[    4.412827] systemd-rc-local-generator[88]: /usr/sbin/halt.local does not exist, skipping.
[    4.429612] systemd-sysv-generator[91]: Native unit for nfs-common.service already exists, skipping.
[    4.440437] systemd-gpt-auto-generator[86]: mmcblk0p2: Root device /dev/mmcblk0.
[    4.451929] systemd-fstab-generator[84]: Found entry what=/dev/disk/by-partuuid/99c633c0-01 where=/boot type=vfat makefs=no nofail=no noauto=no
[    4.472301] systemd-sysv-generator[91]: Native unit for rpcbind.service already exists, skipping.
[    4.490248] systemd-sysv-generator[91]: Native unit for hwclock.service already exists, skipping.
[    4.524009] systemd-sysv-generator[91]: Native unit for fake-hwclock.service already exists, skipping.
[    4.541920] systemd-fstab-generator[84]: Found entry what=/dev/disk/by-partuuid/99c633c0-02 where=/ type=ext4 makefs=no nofail=no noauto=no
[    4.563265] systemd-sysv-generator[91]: Native unit for networking.service already exists, skipping.
[    4.591848] systemd-sysv-generator[91]: Native unit for bluetooth.service already exists, skipping.
[    4.613160] systemd-sysv-generator[91]: Native unit for paxctld.service already exists, skipping.
[    4.635653] systemd-sysv-generator[91]: Native unit for cron.service already exists, skipping.
[    4.654136] systemd-sysv-generator[91]: Native unit for dphys-swapfile.service already exists, skipping.
[    4.672632] systemd-sysv-generator[91]: Native unit for dbus.service already exists, skipping.
[    4.695208] systemd-gpt-auto-generator[86]: No suitable partition table found, ignoring.
[    4.799236] printk: systemd-sysv-ge: 69 output lines suppressed due to ratelimiting
[    5.837606] random: systemd: uninitialized urandom read (16 bytes read)
[    5.902079] random: systemd: uninitialized urandom read (16 bytes read)
[    5.931773] random: systemd: uninitialized urandom read (16 bytes read)
[    7.277886] systemd[114]: Operating on architecture: arm
[    7.283699] systemd[114]: Operating on architecture: arm
[    7.284765] systemd[114]: Operating on architecture: arm
[    7.285722] systemd[114]: Restricting namespace to: .
[    7.285961] systemd[114]: Operating on architecture: arm
[    7.286248] systemd[114]: Blocking cgroup.
[    7.286519] systemd[114]: Blocking ipc.
[    7.306936] systemd[114]: Blocking net.
[    7.307290] systemd[114]: Blocking mnt.
[    7.307588] systemd[114]: Blocking pid.
[    7.307909] systemd[114]: Blocking user.
[    7.308227] systemd[114]: Blocking uts.
[    7.309969] systemd[114]: Operating on architecture: arm
[    7.838758] systemd[1]: libmount event [rescan: yes]
[    7.875576] systemd[1]: run-rpc_pipefs.mount: Changed mounting -> mounting-done
[    7.927309] systemd[1]: sys-kernel-debug.mount: Changed mounting -> mounting-done
[    7.957678] systemd[1]: Received SIGCHLD from PID 96 (mount).
[    7.986625] systemd[1]: Child 96 (mount) died (code=exited, status=0/SUCCESS)
[    8.012369] systemd-journald[114]: Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller
[    8.022756] systemd[1]: run-rpc_pipefs.mount: Child 96 belongs to run-rpc_pipefs.mount.
[    8.037122] systemd[1]: run-rpc_pipefs.mount: Mount process exited, code=exited, status=0/SUCCESS
[    8.046687] systemd[1]: run-rpc_pipefs.mount: Changed mounting-done -> mounted
[    8.088060] systemd-journald[114]: Journal effective settings seal=no compress=yes compress_threshold_bytes=512B
[    8.097923] systemd[1]: run-rpc_pipefs.mount: Job 70 run-rpc_pipefs.mount/start finished, result=done
[    8.118088] systemd-journald[114]: Fixed min_use=1.0M max_use=21.5M max_size=2.6M min_size=512.0K keep_free=32.3M n_max_files=100
[    8.128013] systemd[1]: Mounted RPC Pipe File System.
[    8.151677] systemd-journald[114]: Reserving 4913 entries in hash table.
[    8.188261] systemd-journald[114]: Vacuuming...
[    8.217564] systemd-journald[114]: Vacuuming done, freed 0B of archived journals from /run/log/journal/88bb7e0266774e40a3986e7d17fc46ff.
[    8.217839] systemd-journald[114]: Flushing /dev/kmsg...
[    9.397082] systemd-journald[114]: systemd-journald running as pid 114
[    9.462267] systemd-journald[114]: Sent READY=1 notification.
[    9.462657] systemd-journald[114]: Sent WATCHDOG=1 notification.
[   11.083266] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
[   15.050776] vchiq: module is from the staging directory, the quality is unknown, you have been warned.
[   15.167155] vchiq: vchiq_init_state: slot_zero = (ptrval)
[   15.380466] bcm2835-rng 20104000.rng: hwrng registered
[   15.884864] Bluetooth: HCI UART driver ver 2.3
[   15.884887] Bluetooth: HCI UART protocol H4 registered
[   15.897253] uart-pl011 20201000.serial: no DMA platform data
[   16.048302] Bluetooth: hci0: BCM: chip id 94
[   16.048988] Bluetooth: hci0: BCM: features 0x2e
[   16.050897] Bluetooth: hci0: BCM43430A1
[   16.050936] Bluetooth: hci0: BCM43430A1 (001.002.009) build 0000
[   16.051885] Bluetooth: HCI UART protocol Broadcom registered
[   16.807651] Bluetooth: hci0: BCM43430A1 (001.002.009) build 0360
[   17.967345] brcmfmac: brcmf_fw_alloc_request: using brcm/brcmfmac43430-sdio for chip BCM43430/1
[   17.990124] brcmfmac mmc1:0001:1: Direct firmware load for brcm/brcmfmac43430-sdio.raspberrypi,model-zero-w.txt failed with error -2
[   18.270284] brcmfmac: brcmf_fw_alloc_request: using brcm/brcmfmac43430-sdio for chip BCM43430/1
[   18.270526] brcmfmac: brcmf_c_process_clm_blob: no clm_blob available (err=-2), device may have limited channels available
[   18.271942] brcmfmac: brcmf_c_preinit_dcmds: Firmware: BCM43430/1 wl0: Oct 23 2017 03:55:53 version 7.45.98.38 (r674442 CY) FWID 01-e58d219f
[   23.403367] snd_bcm2835: module is from the staging directory, the quality is unknown, you have been warned.
[   23.956250] bcm2835_audio bcm2835_audio: card created with 8 channels
[   24.856324] bcm2835_v4l2: module is from the staging directory, the quality is unknown, you have been warned.
[   25.287132] systemd[1]: Pulling in systemd-fsck-root.service/start from local-fs.target/start
[   25.337080] systemd[1]: Pulling in shutdown.target/stop from local-fs.target/start
[   25.357046] systemd[1]: Pulling in emergency.target/stop from sysinit.target/start
[   25.377094] systemd[1]: Pulling in dbus.socket/start from bluetooth.service/start
[   25.427102] systemd[1]: Pulling in -.mount/start from dbus.socket/start
[   25.467225] systemd[1]: Pulling in shutdown.target/stop from bluetooth.target/start
[   25.537106] systemd[1]: Found redundant job systemd-journal-flush.service/start, dropping from transaction.
[   25.557036] systemd[1]: Found redundant job systemd-udevd.service/start, dropping from transaction.
[   25.577107] systemd[1]: Found redundant job boot.mount/start, dropping from transaction.
[   25.667326] systemd[1]: Found redundant job -.slice/start, dropping from transaction.
[   29.567005] random: crng init done
[   29.567031] random: 7 urandom warning(s) missed due to ratelimiting
[   33.858720] systemd[1]: Accepted new private connection.
[   33.882561] systemd[1]: systemd-journald.service: Got notification message from PID 114 (FDSTORE=1)
[   33.897079] systemd[1]: systemd-journald.service: Added fd 58 (n/a) to fd store.
[   33.927146] systemd[1]: systemd-journald.service: Received EPOLLHUP on stored fd 46 (stored), closing.
[   33.957050] systemd[1]: systemd-journald.service: Received EPOLLHUP on stored fd 50 (stored), closing.
[   33.977056] systemd[1]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=AddMatch cookie=2 reply_cookie=0 signature=s error-name=n/a error-message=n/a
[   33.997089] systemd[1]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=AddMatch cookie=3 reply_cookie=0 signature=s error-name=n/a error-message=n/a
[   34.017102] systemd[1]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=AddMatch cookie=4 reply_cookie=0 signature=s error-name=n/a error-message=n/a
[   34.037106] systemd[1]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=AddMatch cookie=6 reply_cookie=0 signature=s error-name=n/a error-message=n/a
[   34.057041] systemd[1]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=RequestName cookie=7 reply_cookie=0 signature=su error-name=n/a error-message=n/a
[   34.608059] Adding 102396k swap on /var/swap.  Priority:-2 extents:1 across:102396k SS
[   36.483529] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
[   80.357032] systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=202 reply_cookie=0 signature=so error-name=n/a error-message=n/a

@lategoodbye
Copy link
Contributor

lategoodbye commented Aug 15, 2019

Congratulation, you successful compiled a mainline kernel. dwc2 is working fine (no module, it's compiled in). Don't care about Wifi firmware, it's not relevant for the issue.

Are you able to reproduce the issue or are the sysfs entries missing?

@sn00pster
Copy link
Author

ok, f_hid is disabled in this kernel, time to build another kernel image.

Everything else looks good, UDC is appearing.

@sn00pster
Copy link
Author

sn00pster commented Aug 15, 2019

@lategoodbye Same issue, disconnect the cable and the Pi Zero W still thinks it's in a configured state.

pi@raspberrypi:/sys/class/udc/20980000.usb $ cat state
configured

<cable pulled>
<1 minute later>

pi@raspberrypi:/sys/class/udc/20980000.usb $ cat state
configured

It's definitely broken.

@lategoodbye
Copy link
Contributor

"Great". Please apply your patch to your mainline repo and retest.

After that you need to report this issue (including reference to this issue and your inlined patch) via email to the following addresses:
Minas Harutyunyan [email protected]
Felipe Balbi [email protected]
Greg Kroah-Hartman [email protected]
Stefan Wahren [email protected]
[email protected]

Thanks

@sn00pster
Copy link
Author

Ok, let me do a little more testing and reading and if I need to amend I will, then I'll submit a patch.

What happens if this issue only occurs on the Pi? I guess the people looking at the patch will try without it first to see they can reproduce it on their hardware?

@lategoodbye
Copy link
Contributor

It's a bug and you were able to reproduce with a mainline kernel reliable. Don't be afraid this might be specific to the Pi (which i don't believe). It's job of the maintainers to decide, how to fix this. So don't waste too much time with preparing the "perfect" patch. This can be done afterwards.

@sn00pster
Copy link
Author

It's a bug and you were able to reproduce with a mainline kernel reliable. Don't be afraid this might be specific to the Pi (which i don't believe). It's job of the maintainers to decide, how to fix this. So don't waste too much time with preparing the "perfect" patch. This can be done afterwards.

Ok, thanks for all your time, help and advice, it's been much appreciated, I've never really delved into the kernel on linux (I did on the now dead ucLinux where I wrote some drivers for stuff).

This particular bug is particularly annoying in that I found it straight away while conducting tests for using a Pi Zero W as a USB gadget (It's actually a bridge to make a Bluetooth Keyboard into a USB HID keyboard so I an use it with ESXi) and while I stormed through all the initial tests, enumeration, getting the actual "bridge" working I then hit a roadblock when it came to detecting whether the USB device was connected, bluetooth I get udev events, but USB requires monitoring that state file and that's when the trouble started.

@sn00pster
Copy link
Author

It's a bug and you were able to reproduce with a mainline kernel reliable. Don't be afraid this might be specific to the Pi (which i don't believe). It's job of the maintainers to decide, how to fix this. So don't waste too much time with preparing the "perfect" patch. This can be done afterwards.

Quick question, I hooked up a serial console and while it works with the raspbian kernel I get no output when booting my 5.3 kernel, is this a limitation of the mainline kernel?

I looked about the config and everything that should be enabled (AFAIK) is enabled.

@sn00pster
Copy link
Author

Ahh, it moves the serial port. Changed by console parameter to console=ttyS1,115200 and now I get console output. (Although not the complete log, it starts some time after boot)

@lategoodbye
Copy link
Contributor

The downstream kernel has some patches to simplify the UART naming. Please use ttyAMA0 or ttyS1. But it should provide the complete log.

@sn00pster
Copy link
Author

The downstream kernel has some patches to simplify the UART naming. Please use ttyAMA0 or ttyS1. But it should provide the complete log.

its weird, it definitely misses off the start, when the terminal appears i’m getting junk being rx’d, i will try a different USB to serial just in case.

on an aside, ive just been looking around everywhere as ive been involved in another conversation about a usb library, it was suggested to use extcon to monitor gadget connection and disconnection events but the DWC2 driver has no references to the extcon API.

I’ve also ben studying the devicetree for the RPI device, and given that theres no schematics I’m assuming the phy is part of the DWC2 IP.

so is there any way of reading the VBUS state (level)? i eventually ended up reading a post in the kernel dev about the DWC2 driver by yourself!

@lategoodbye
Copy link
Contributor

lategoodbye commented Aug 16, 2019

It's possibly an issue with the baud rate.

Yes, i also assume the PHY is part of the DWC2 IP. Unfortunately we don't have a PHY driver yet, the devicetree fake this via non-op driver. Maybe this is the root cause of this issue. The BCM2835 datasheet provides a register description for MDIO access incl. VBUS interrupt.

I already started a template for a PHY driver:
https://github.com/lategoodbye/rpi-zero/commits/bcm2835-dwc2-improvements
but this actually needs more flesh

In case you need a datasheet for the DWC2 IP, try to google for Silicon Labs EZR32WG. It seems to use the same core.

@sn00pster
Copy link
Author

It's possibly an issue with the baud rate.

Yes, i also assume the PHY is part of the DWC2 IP. Unfortunately we don't have a PHY driver yet, the devicetree fake this via non-op driver. Maybe this is the root cause of this issue. The BCM2835 provides a register description for MDIO access incl. VBUS interrupt.

I already started a template for a PHY driver:
https://github.com/lategoodbye/rpi-zero/commits/bcm2835-dwc2-improvements
but this actually needs more flesh

In case you need a datasheet for the DWC2 IP, try to google for Silicon Labs EZR32WG. It seems to use a the same core.

Awesome, I really appreciate your knowledge and help here. i’ll take a look at that document and have a read up on the VBUS interrupt to see if theres anything useful there, I’m trying to figure out a sane way of managing kernel rebuilds and transfer to the SD without network (in the case where i break the kernel) or taking apart my Pi Zero and removing the SD.

@sn00pster
Copy link
Author

@lategoodbye i’ve had a quick skim of that datasheet and i concur its the same core. Having a read about as well, they (synopsis) also do phy ip, so we really have to guess at what the phy is.

The vbus sensing on that gecko part is done through a usb register, however, its located not in the same area of memory which means its not part of the core ip, its some other ip.

so without knowing how to read vbus, gadget mode is never going to work “as it should”. my hack is a fix for my particularl circumstance, but its not the correct fix. the correct fix requires the raspberry pi foundation to release some information on how to read the vbus information.

@lategoodbye
Copy link
Contributor

lategoodbye commented Aug 16, 2019

Is the MDIO register definition (p. 203 ff) in the BCM2835 datasheet not sufficient?
What do you think, we need exactly?
Sorry, i don't have in-depth USB knowledge.

@fizzyade
Copy link

Ok, I have sent the email, hopefully I did everything correctly.

Thanks for take care of this.

The patch base looks strange to me:

diff -uprN -X usb-vanilla/Documentation/dontdiff
usb-vanilla/drivers/usb/dwc2/core_intr.c
usb/drivers/usb/dwc2/core_intr.c
--- usb-vanilla/drivers/usb/dwc2/core_intr.c    2020-01-13

Please always use git add / git commit / git format against https://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git on branch next.

I followed the instructions on the “how to format a patch” on the kernel page, it said to take the base from the one listed in the maintainers file.

It’s more than possible I’ve messed up or done it wrong, I tried to take my time and looked at how other patches had been submitted.

This is a bit of learning experience and probably a trial by fire!

@Ir3nicuz
Copy link

Thanks for take care of this Issue from me, too.

@mdevaev
Copy link
Contributor

mdevaev commented Sep 28, 2020

@fizzyade @sn00pster I have encountered this problem on my Pi-KVM devices. Sorry to ask, was there any progress with the kernel devs? As far as I understood, there was only one answer to this patch. So is this problem specific to the Raspberry Pi or what?

2all: if so, maybe it's worth accepting this patch here?

@Testato
Copy link

Testato commented Sep 28, 2020

I agree, if this problem is present only on Rpi (because it do not use a PHI) this patch should be accepted here.

@lategoodbye
Copy link
Contributor

@mdevaev Could you please tell which kernel version is affected? I don't see the reason why this shouldn't be fixed upstream.

@mdevaev
Copy link
Contributor

mdevaev commented Sep 28, 2020

@lategoodbye affected:

  • Raspbian: Linux raspberrypi 5.4.51-v7l+ #1333 SMP Mon Aug 10 16:51:40 BST 2020 armv7l GNU/Linux
  • Arch Linux ARM: Linux pikvm 5.4.65-1-ARCH #1 SMP PREEMPT Tue Sep 15 16:21:17 UTC 2020 armv7l GNU/Linux
  • Old 4.19.118 is also affected.

But in fact all kernels I used were affected by this problem at least in recent year.

Also it might be related: #3870

@mdevaev
Copy link
Contributor

mdevaev commented Sep 28, 2020

(Edited) ^^^

I have a large user base that uses OTG with different machines, and this is evident on absolutely all devices in our cases. So it's not just my local problem :(

@pelwell
Copy link
Contributor

pelwell commented Sep 28, 2020

And there's also #3862. Clearly libcomposite issues are like buses - you wait years for one and then two or three arrive together.

@mdevaev
Copy link
Contributor

mdevaev commented Sep 28, 2020

@pelwell you also think my problem is #3870 and that #3862 and this PR are of the same nature? Any chance of fixing this?

@pelwell
Copy link
Contributor

pelwell commented Sep 28, 2020

They might not be related, but Occam's razor would suggest that they might. Either way, I'll be looking at libcomposite.

@pelwell
Copy link
Contributor

pelwell commented Sep 30, 2020

This patch has been merged offline into rpi-5.4.y.

@mdevaev
Copy link
Contributor

mdevaev commented Sep 30, 2020

Thanks!

mdevaev added a commit to pikvm/kvmd that referenced this pull request Oct 3, 2020
@mdevaev
Copy link
Contributor

mdevaev commented Oct 7, 2020

Okay, after #3885 I tried this (I repeat my comment to make it easier to understand the meaning):

I've skimmed the code and maybe (I haven't tested it yet, I'll do it in the next few hours) that the correct way is to use dwc2_hsotg_disconnect():

void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)

As far as I can see, this function sets DWC2_L3, switches the state to USB_STATE_NOTATTACHED, but also additionally calls call_gadget() and kills requests to the device.

So, I've tested it:

diff --git a/drivers/usb/dwc2/core_intr.c b/drivers/usb/dwc2/core_intr.c
index 6272b4ae4740f..11c8893fbc1ab 100644
--- a/drivers/usb/dwc2/core_intr.c
+++ b/drivers/usb/dwc2/core_intr.c
@@ -539,13 +539,10 @@ static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg)
 			}
 skip_power_saving:
 			/*
-			 * Change to L2 (suspend) state before releasing
-			 * spinlock
+			 * Raspberry Pi seems to call the suspend interrupt on gadget disconnect,
+			 * so instead of setting state to suspend set to not attach
 			 */
-			hsotg->lx_state = DWC2_L2;
-
-			/* Call gadget suspend callback */
-			call_gadget(hsotg, suspend);
+			dwc2_hsotg_disconnect(hsotg);
 		}
 	} else {
 		if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) {

No success. The device does not connect back:

[  115.284804] dwc2 fe980000.usb: new device is full-speed
[  115.294582] dwc2 fe980000.usb: dwc2_hsotg_enqueue_setup: failed queue (-11)

Does anyone have any ideas how this should be implemented in the right way?

@mdevaev
Copy link
Contributor

mdevaev commented Oct 7, 2020

I guess the reason is that dwc2_hsotg_disconnect() calls call_gadget(hsotg, disconnect). It seems to me that the problem is not in the suspend processing code, but in the fact that disabling the device is interpreted as a suspend. Perhaps should be fixed somewhere on a lower level.

@lategoodbye
Copy link
Contributor

@mdevaev Did you read this comment?

@mdevaev
Copy link
Contributor

mdevaev commented Oct 9, 2020

@lategoodbye for some reason, I completely forgot about it. I didn't associate one problem with another. Well, that explains a lot. It seems that this requires the help of the community, but since I'm not a kernel/hw developer, it's unlikely that I personally could be of any use.

Although, I could assign a bounty for solving this problem if it helps :)

@fizzyade
Copy link

fizzyade commented Oct 9, 2020

To be honest, I lost the will after a couple of weeks on the kernel mailing list, I pointed out the problem and that I had patched it which worked in my situation, and that it to me it looked like a problem that existed on everything that uses that driver. I was patient, but ultimately nobody seemed interested in solving the problem, so a year later it still exists.

My patch (my previous account was @sn00pster) was a workaround, I don't profess that it is the correct fix, but it worked in my limited testing for the use I intended.

It's a shame that the kernel list didn't seem bothered, but I'm also unsurprised.

@lategoodbye
Copy link
Contributor

Just for the reference, i think this was the thread about the patch.

I thought we come to the point that the issue should be fixed by implementing the missing PHY driver. Btw i can't see any prove that other dwc2 "users" are affected.

@mdevaev
Copy link
Contributor

mdevaev commented Oct 9, 2020

@fizzyade I understand your frustration. I once reported an API bug to the core mailing list that no one fixed. Postponed to API 2.0, lol. Not even documented. Anyway, right now I'm interested in solving this problem, but I can't help with code. Maybe a beer will do? A couple of dozen liters of beer.

@lategoodbye Oh, I assure you, the problem is really exists. The fact that it was not noticed earlier is due to the fact that this functionality was almost never used the way I use it. For about the same reason, #3870 remained in the kernel for years: very few people used dwc2 for the boot keyboard emulator and could not notice that OOPS happened due to frequent reinitializations of the host. Now my KVM software (which uses dwc2 very actively in a variety of configurations) has raised all these problems. It is used by thousands of people and they all bring me messages about USB problems.

Detection of the keyboard connection state is usually performed by indirect signs, such as EAGAIN during writing and other. Now when I tried to use the port state and gave it to my users, we found that state was lying :)

@Ir3nicuz
Copy link

I don't want to make any pressure or stress to you guys but is there a chance to get a fix for this state-stuck-issue sometime?
Its really annoying to have the intended way to determine the usb connection state and struggle at it because the driver not change it. :(

@fizzyade
Copy link

fizzyade commented Feb 28, 2021 via email

@Ir3nicuz
Copy link

Should I initiate a signature list? :P Or can I help somehow else? Thanks for take care of that issue!

@mdevaev
Copy link
Contributor

mdevaev commented Mar 1, 2021

I'm also interested in the solution, but all I can do right now is offer a small reward for it.

@alassaneg
Copy link

I'm also having issues with that ad i'm using an atsama5d27 from microchip, that would really help, i don't see how i can do without a fix for this.

@mdevaev
Copy link
Contributor

mdevaev commented Jun 4, 2022

In short, it can't be fixed since it's a hardware problem. I found out it while studying how the USB core on Rockchip works, it uses the same DWC2 as in Raspberry Pi. The USB state machine in DWC2 distinguishes the disconnection of the device from using the lack or availability of the power on the +5V-USB-IN line. Apparently, in Raspberry Pi, voltage is always applied to this line, so the USB core thinks that when disconnected, suspend occurs.

When I talk about +5V, I don't mean the power supply of the entire Raspberry, I'm talking specifically about a special pin that should be on the BCM chip, which serves to determine whether there is power on USB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants