NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

AW: kern/61012: spi(4): add ca_rescan so children attach on runtime modload / drvctl -r



> [snip]
> 
> [I am neither encouraging for discouraging this PRs desire mostly just
> pointing out a couple of things]
> 
> Ya, modules with the SPI drivers have never really been a thing.  In
> particular, the main reason, I think, is that indirect config / probe of
> the device really doesn't work very well for them.  You mostly need to
> pin down the slave that the device is attached to in a kernel config.
> 
> You should provide the ioconf fragment you used for your module.
> Usually that specifies all possible addresses that a device could be at.
> For SPI you would have no way to know how many you are dealing with.
> (Yes, for the RPI it would be up to 2, but any platform with USB can use
> umcpmio(4) to get SPI and then you have up to 8 possible SPI slaves.
> Think of a AMD64 Virtualbox VM that has a ucmcpmio(4) device attached to
> it.  The virtual VM gets a real SPI port.).
> 
> For platforms with FDT this probably all works or seems to because the
> FDT would lock down the slave address along with the kernel config, but
> non-FDT platforms with SPI would have trouble with ghost devices on
> rescan.
> 
> You can also get ghost devices with I2C and 1-Wire (onewire(4)) on
> indirect probes, unless a particular device driver has taken steps in
> its match function to see if there really is something present at a
> particular address.  For I2C that is quite possible (probably), for
> 1-Wire, not so much.
>  
> --
> Brad Spencer - brad%anduin.eldar.org@localhost


Thanks for the review!
The ioconf I used pins the slaves explicitly (matching the number of plugs, but not necessarily of controllers plugged into them):

    pseudo-root spi*
    snespad0    at spi? slave 0
    snespad1    at spi? slave 1

(This specific driver works with a single snes controller, and nothing else, directly wired to an SPI bus and more than one controller on one bus using a small adaptor board with a tristate output buffer IC, like this one: https://github.com/thgrass/SNES-Gamepads-to-SPI-Adaptor )


If just for the sake of it, I'll try and give arguments FOR the proposed patch: 

You're right about ghost devices, but it already exists at boot: spi_attach() runs the same config_search, and some current spi children (tmp121temp, mcp3kadc, bmx280) just return 1 from match(). This patch doesn't make it worse, it just makes runtime rescan behave like boot. New spi drivers should probe in match() like they already must/should at boot.

Also, no existing driver seems to need changes: CFATTACH_DECL_NEW is literally CFATTACH_DECL2_NEW with NULL,NULL, so the patch only changes ca_rescan NULL -> spi_rescan. Boot is unchanged anyhow; and runtime rescan previously just failed ENODEV, so nothing working today can break.

Best regards,
Thorsten

 


Von: Brad Spencer via gnats <gnats-admin%NetBSD.org@localhost>
Gesendet: Sonntag, 15. November 2026 20:50:02
An: kern-bug-people%netbsd.org@localhost; gnats-admin%netbsd.org@localhost; netbsd-bugs%netbsd.org@localhost; Grassmann, Thorsten
Betreff: Re: kern/61012: spi(4): add ca_rescan so children attach on runtime modload / drvctl -r
 
The following reply was made to PR kern/61012; it has been noted by GNATS.

From: Brad Spencer <brad%anduin.eldar.org@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc: kern-bug-people%netbsd.org@localhost, gnats-admin%netbsd.org@localhost, netbsd-bugs%netbsd.org@localhost
Subject: Re: kern/61012: spi(4): add ca_rescan so children attach on runtime
 modload / drvctl -r
Date: Sun, 15 Nov 2026 14:24:19 -0500

 "thorsten.grassmann%zae-bayern.de@localhost via gnats" <gnats-admin%NetBSD.org@localhost>
 writes:
 
 >>Number:         61012
 >>Category:       kern
 >>Synopsis:       spi(4): add ca_rescan so children attach on runtime modload / drvctl -r
 >>Confidential:   no
 >>Severity:       non-critical
 >>Priority:       low
 >>Responsible:    kern-bug-people
 >>State:          open
 >>Class:          change-request
 >>Submitter-Id:   net
 >>Arrival-Date:   Sun Nov 15 17:40:00 +0000 2026
 >>Originator:     Thorsten Grassmann
 >>Release:        11.1, NetBSD-current
 >>Organization:
 > ZAE Bayern
 >>Environment:
 > NetBSD netbsdrpi 11.1 NetBSD 11.1 (GENERIC64) #4: Sun Nov 15 02:19:15 CET 2026  grass@mirrorbox:/tmp/nbsd/obj/sys/arch/evbarm/compile/GENERIC64 evbarm
 >>Description:
 > I wrote a small SNES-gamepad driver as an LKM that attaches with "snespad* at spi? slave N" (for a Raspberry Pi 3 via the SPI0 header; the driver is pure spi(4) and machine-independent; https://github.com/thgrass/NetBSD_snespad_spi ).
 > On NetBSD 11.1 evbarm GENERIC64 with bcmspi(4)/spi(4):
 >
 >     # modload snespad
 >     modload: rc=0            // module loads fine
 >     // dmesg: nothing. no snespad0 at spi0, no /dev/snespad*.
 >     # drvctl -r -a spi spi0   -> also nothing.
 >
 > Tracing config_cfdata_attach() -> rescan_with_cfdata() showed the rescan is dispatched to ca_rescan of each attached bus. But spi's cfattach is ca_rescan = NULL (CFATTACH_DECL_NEW).
 > The same driver works when built into the kernel directly.
 >
 > Is this by design? As SPI devices usually are "built-in" and there is rarely the need for loadable modules in production.
 > Adding proposed patch (see below) would at least make SPI driver development more convenient...
 >>How-To-Repeat:
 > On any system with an spi(4) bus attached:
 >
 >     # drvctl -l                  // shows spi0
 >     # drvctl -r -a spi spi0
 >     drvctl: DRVRESCANBUS: No such device or address
 >>Fix:
 > Patch spi-rescan-trunk.patch:
 > adds spi_rescan() to spi(4) and registers it as ca_rescan via CFATTACH_DECL2_NEW. (Boot-time attach is unchanged).
 
 [snip]
 
 [I am neither encouraging for discouraging this PRs desire mostly just
 pointing out a couple of things]
 
 Ya, modules with the SPI drivers have never really been a thing.  In
 particular, the main reason, I think, is that indirect config / probe of
 the device really doesn't work very well for them.  You mostly need to
 pin down the slave that the device is attached to in a kernel config.
 
 You should provide the ioconf fragment you used for your module.
 Usually that specifies all possible addresses that a device could be at.
 For SPI you would have no way to know how many you are dealing with.
 (Yes, for the RPI it would be up to 2, but any platform with USB can use
 umcpmio(4) to get SPI and then you have up to 8 possible SPI slaves.
 Think of a AMD64 Virtualbox VM that has a ucmcpmio(4) device attached to
 it.  The virtual VM gets a real SPI port.).
 
 For platforms with FDT this probably all works or seems to because the
 FDT would lock down the slave address along with the kernel config, but
 non-FDT platforms with SPI would have trouble with ghost devices on
 rescan.
 
 You can also get ghost devices with I2C and 1-Wire (onewire(4)) on
 indirect probes, unless a particular device driver has taken steps in
 its match function to see if there really is something present at a
 particular address.  For I2C that is quite possible (probably), for
 1-Wire, not so much.
 
 
 
 
 
 
 --
 Brad Spencer - brad%anduin.eldar.org@localhost
 


Home | Main Index | Thread Index | Old Index