So the basic issue is this: once the Pi Zero with g_mass_storage is booted up, as long as there is a power source for it (like a battery, so it won't shut off) I can plug it into any windows machine (or another Pi) and the volume pops right up. But on one computer, just one so far, it won't see the device unless it had been connected to and recognized by one other device previously. It seems as if some part of connecting the Pi to another computer changes... something, and lets the finicky machine see it.
Background:
My goal here is to create a gadget that connects to a network drive on my NAS, syncs it to the backing store for g_mass_storage, and then presents that file to a device as a USB drive. This works more or less fine for any windows computer I try it with (I am having some weirdness with copying files, but that's a matter for another question) but not for the important one.
I want this to make it easier to transfer files to a CNC Lathe (HAAS ST20SS), but the control unit is acting differently than any other computer I have tried, including another HAAS machine with a newer control system (DT2 CNC Mill). The "next gen" control on the Mill has no issue seeing the Pi as a storage device. None of the windows computers in the shop have any problems, and neither does another Pi.
I'm fairly sure the lathe controller is running some variant of Linux, but I'm not sure.
If I plug the Pi Zero into the lathe, it boots up fine, connects to Wi-Fi, and I can SSH into it. If I connect a power supply or battery to the power USB port, I can disconnect it from the lathe and it remain powered. I can boot up the lathe with it plugged in, I can unplug it and plug it back in, whatever. The lathe will not see it.
However, if I take it (without powering it off) and plug it into a PC or another Pi, or even the mill, it works perfectly. Then, if I bring it back to the lathe... no problem. It sees it, browses the file system, no problem. As long as the Pi is connected to any other computer (even tried it with a micro to USB-C cable to my Phone) once, that computer will see the drive, and it will work fine with the lathe until the Pi is rebooted.
Weirder still, my script that handles syncing files modprobe's g_mass_storage and removes it before mounting the file system internally to do the copy, then unmounts and modprobe's it back again to avoid corrupting anything by mounting the FS in two places at once. Resyncing will not break the lathes ability to see the drive, it will simply disappear momentarily and pop back up without issue. I would totally blame the lathe controller if it just couldn't figure out how to connect to a drive that was unmounted without being physically unplugged, but that isn't a problem, and mere physical unplugging doesn't solve the issue anyway.
Basically, once something recognizes the mass storage device, I can do anything I want with it with the lathe or any other computer and no problems. Until the Pi is restarted that is, at which point it loses something and won't work with the lathe again.
So what happens when I plug the Pi into a computer? What changes? It's as if the Pi isn't presenting itself as a mass storage device until after it connects to a regular computer, and whatever those computers do, the lathe doesn't.
[Edit: 1] More info:
I have been searching around for information, and found a few things that seem useful. First, if I run:
cat /sys/class/udc/20980000.usb/state
It returns not attached
until I have modprobe'ed g_mass_storage and connected it to one of the working devices. After that, it reads configured
until I remove the g_mass_storage mod, which returns it to not attached
- connecting it to the Lathe before any other device doesn't change its state (still not attached
).
The other thing I have found, is some odd log entries in /var/log/messages.
If I run:
sudo modprobe g_mass_storage file=/piusb.bin stall=0
The log gains the entries:
Jun 12 11:43:58 PiZERO kernel: [ 2224.624959] Mass Storage Function, version: 2009/09/11
Jun 12 11:43:58 PiZERO kernel: [ 2224.625003] LUN: removable file: (no medium)
Jun 12 11:43:58 PiZERO kernel: [ 2224.635460] LUN: file: /piusb.bin
Jun 12 11:43:58 PiZERO kernel: [ 2224.635500] Number of LUNs=1
Jun 12 11:43:58 PiZERO kernel: [ 2224.635778] g_mass_storage gadget: Mass Storage Gadget, version: 2009/09/11
Jun 12 11:43:58 PiZERO kernel: [ 2224.635808] g_mass_storage gadget: userspace failed to provide iSerialNumber
Jun 12 11:43:58 PiZERO kernel: [ 2224.635822] g_mass_storage gadget: g_mass_storage ready
Jun 12 11:43:58 PiZERO kernel: [ 2224.638901] dwc2 20980000.usb: bound driver g_mass_storage
If I then connect a computer, it gains:
Jun 12 11:46:24 PiZERO kernel: [ 2370.203320] dwc2 20980000.usb: new device is high-speed
Jun 12 11:46:24 PiZERO kernel: [ 2370.283379] dwc2 20980000.usb: new device is high-speed
Jun 12 11:46:24 PiZERO kernel: [ 2370.348503] dwc2 20980000.usb: new address 5
Jun 12 11:46:24 PiZERO kernel: [ 2370.369774] g_mass_storage gadget: high-speed config #1: Linux File-Backed Storage
But after that, if I connected it again to another computer I get:
Jun 12 11:48:11 PiZERO kernel: [ 2477.974985] ------------[ cut here ]------------
Jun 12 11:48:11 PiZERO kernel: [ 2477.975182] WARNING: CPU: 0 PID: 0 at drivers/usb/dwc2/gadget.c:176 dwc2_hsotg_init_fifo+0x188/0x1a8 [dwc2]()
Jun 12 11:48:11 PiZERO kernel: [ 2477.975198] Modules linked in: g_mass_storage usb_f_mass_storage libcomposite arc4 ecb md4 md5 hmac nls_utf8 cifs bnep hci_uart btbcm bluetooth brcmfmac brcmutil cfg80211 snd_bcm2835 rfkill snd_pcm snd_timer snd dwc2 udc_core bcm2835_gpiomem bcm2835_wdt uio_pdrv_genirq uio fuse ipv6 [last unloaded: libcomposite]
Jun 12 11:48:11 PiZERO kernel: [ 2477.975334] CPU: 0 PID: 0 Comm: swapper Tainted: G W 4.4.50+ #970
Jun 12 11:48:11 PiZERO kernel: [ 2477.975346] Hardware name: BCM2708
Jun 12 11:48:11 PiZERO kernel: [ 2477.975413] [<c0016d18>] (unwind_backtrace) from [<c0013c3c>] (show_stack+0x20/0x24)
Jun 12 11:48:11 PiZERO kernel: [ 2477.975452] [<c0013c3c>] (show_stack) from [<c02e49dc>] (dump_stack+0x20/0x28)
Jun 12 11:48:11 PiZERO kernel: [ 2477.975488] [<c02e49dc>] (dump_stack) from [<c0021f2c>] (warn_slowpath_common+0x8c/0xc4)
Jun 12 11:48:11 PiZERO kernel: [ 2477.975517] [<c0021f2c>] (warn_slowpath_common) from [<c0022020>] (warn_slowpath_null+0x2c/0x34)
Jun 12 11:48:11 PiZERO kernel: [ 2477.975630] [<c0022020>] (warn_slowpath_null) from [<bf0c7938>] (dwc2_hsotg_init_fifo+0x188/0x1a8 [dwc2])
Jun 12 11:48:11 PiZERO kernel: [ 2477.975796] [<bf0c7938>] (dwc2_hsotg_init_fifo [dwc2]) from [<bf0c94cc>] (dwc2_hsotg_core_init_disconnected+0x60/0x308 [dwc2])
Jun 12 11:48:11 PiZERO kernel: [ 2477.975952] [<bf0c94cc>] (dwc2_hsotg_core_init_disconnected [dwc2]) from [<bf0c9c74>] (dwc2_hsotg_irq+0x500/0x6dc [dwc2])
Jun 12 11:48:11 PiZERO kernel: [ 2477.976059] [<bf0c9c74>] (dwc2_hsotg_irq [dwc2]) from [<c005acf4>] (handle_irq_event_percpu+0x94/0x1f0)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976092] [<c005acf4>] (handle_irq_event_percpu) from [<c005ae88>] (handle_irq_event+0x38/0x4c)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976126] [<c005ae88>] (handle_irq_event) from [<c005dc14>] (handle_level_irq+0x9c/0x134)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976154] [<c005dc14>] (handle_level_irq) from [<c005a368>] (generic_handle_irq+0x30/0x44)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976182] [<c005a368>] (generic_handle_irq) from [<c005a600>] (__handle_domain_irq+0x58/0xb8)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976216] [<c005a600>] (__handle_domain_irq) from [<c0010748>] (handle_IRQ+0x2c/0x30)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976246] [<c0010748>] (handle_IRQ) from [<c000940c>] (bcm2835_handle_irq+0x3c/0x58)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976281] [<c000940c>] (bcm2835_handle_irq) from [<c05768e4>] (__irq_svc+0x44/0x5c)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976295] Exception stack(0xc0821f18 to 0xc0821f60)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976311] 1f00: 00000000 00000000
Jun 12 11:48:11 PiZERO kernel: [ 2477.976333] 1f20: 00000000 c0822f74 c088d5f0 c0820000 c08220ac 00000000 c0887d04 c0887268
Jun 12 11:48:11 PiZERO kernel: [ 2477.976355] 1f40: c0887268 c0821f74 c0821f68 c0821f68 c0010804 c0010808 60000013 ffffffff
Jun 12 11:48:11 PiZERO kernel: [ 2477.976385] [<c05768e4>] (__irq_svc) from [<c0010808>] (arch_cpu_idle+0x30/0x40)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976425] [<c0010808>] (arch_cpu_idle) from [<c004d7ec>] (default_idle_call+0x34/0x48)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976455] [<c004d7ec>] (default_idle_call) from [<c004d930>] (cpu_startup_entry+0x130/0x17c)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976483] [<c004d930>] (cpu_startup_entry) from [<c05722e0>] (rest_init+0x6c/0x84)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976521] [<c05722e0>] (rest_init) from [<c07c2ca0>] (start_kernel+0x358/0x3cc)
Jun 12 11:48:11 PiZERO kernel: [ 2477.976537] ---[ end trace f3f094dda548745c ]---
Jun 12 11:48:11 PiZERO kernel: [ 2477.989720] dwc2 20980000.usb: new device is high-speed
Jun 12 11:48:12 PiZERO kernel: [ 2478.069784] dwc2 20980000.usb: new device is high-speed
Jun 12 11:48:12 PiZERO kernel: [ 2478.134876] dwc2 20980000.usb: new address 6
Jun 12 11:48:12 PiZERO kernel: [ 2478.156147] g_mass_storage gadget: high-speed config #1: Linux File-Backed Storage
I also see a line like:
Jun 12 11:48:11 PiZERO rsyslogd-2007: action 'action 17' suspended, next retry is Mon Jun 12 11:48:41 2017 [try http://www.rsyslog.com/e/2007 ]
Every so often.
The other interesting thing of note, is that all the devices that work are being called "high speed" but if I connect the Lathe controller, it's "low speed" instead. I'm guessing that the port on the controller is USB 1.0, and this may be causing an issue. But it will work, after the Pi has gotten a chance to connect to another device.