fizzyade
Posts: 4
Joined: Wed Aug 14, 2019 10:34 am

USB Gadget Attached/Detached state

Wed Aug 14, 2019 10:45 am

Hi,

The file sys/class/udc/<device>/state should give the current state of the current OTG connection state, if you check this before connecting a USB host you will see that it says "not attached".

When you connect to a host the content of this file changes to "configured", all good.

However, when you disconnect the gadget from the host it remains at "configured", so there's no way of knowing if your currently connected and enumerated by the host.

I downloaded the kernel sources and enabled debugging on the dwc2 module, I could see that unplugging the device would result in a suspend interrupt, so I went to this interrupt (dwc2_handle_usb_suspend_intr in core_intr.c) and instead of calling:

Code: Select all

			/*
			 * Change to L2 (suspend) state before releasing
			 * spinlock
			 */
			hsotg->lx_state = DWC2_L2;

			/* Call gadget suspend callback */
			call_gadget(hsotg, suspend);

I changed it to:

Code: Select all

			hsotg->lx_state = DWC2_L3;

			usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);

And now the state file correctly matches that of the connection. I've tested it a bit and have not had any ill effects from this, but I've seen this question come up a few times with people having the same issue with gadget mode.

You obviously need to compile the kernel module for your installed kernel and replace the current dwc2.ko module file.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2480
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: USB Gadget Attached/Detached state

Wed Aug 14, 2019 1:43 pm

Can you open a Pull Request at our Linux GitHub repo? That way, the right people get to see it and comment, and it may eventually be merged.

fizzyade
Posts: 4
Joined: Wed Aug 14, 2019 10:34 am

Re: USB Gadget Attached/Detached state

Wed Aug 14, 2019 2:00 pm

PhilE wrote:
Wed Aug 14, 2019 1:43 pm
Can you open a Pull Request at our Linux GitHub repo? That way, the right people get to see it and comment, and it may eventually be merged.
It may well be a hack, I haven't got the time to read up on the controller and it's behaviour and whether it's something that affects all devices or just the pi, but it's working for me in my situation. Searching the net there appears to have been some chatter about this behaviour on other devices, but patches were applied but they're obviously not working in the case of the pi.

I'll pull down the kernel, change and then submit a pull request with my explanation.


PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2480
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: USB Gadget Attached/Detached state

Wed Aug 14, 2019 2:41 pm

Thanks.

fizzyade
Posts: 4
Joined: Wed Aug 14, 2019 10:34 am

Re: USB Gadget Attached/Detached state

Wed Aug 14, 2019 2:47 pm

I'm wishing I had another device that runs in OTG mode that uses the dwc2 driver that way I could see if it's something that is a problem generally or just with the pi.

I didn't actually test a proper USB suspend as I personally don't need that behaviour, but if anything hopefully this leads to a full resolution because as it stands you can't return the state of the gadget connection, which may or may not be a problem depending on what you're doing.

Return to “Linux Kernel”