Patched gpsd and /dev/pps0 results in "sleeping thread" kernel panic

From: Craig Leres <leres_at_freebsd.org>
Date: Wed, 01 Sep 2021 01:26:37 UTC
I have many FreeBSD based ntp time servers using half a dozen different 
kinds of receivers. Now I have a Javad Alpha2 satellite receiver I'd 
like to use. Since gpsd knows how to talk GREIS to it I'm pretty deep 
into trying to figure out how to configure that.

gpsd appears to know a lot about the RFC 2783 pps api but incorrectly 
assumes only tty-like devices can support it. It tries to use time_pps_* 
functions with /dev/gps0 (a symlink to cuau0) which fail and doesn't 
even open /dev/pps0. My gpsd rc.conf config is:

     gpsd_enable="YES"
     gpsd_flags="-D1 -n -s 115200"
     gpsd_devices="/dev/gps0 /dev/pps0"

The relevant part of my ntp.conf is (driver 46 is the GPSD-NG driver):

     server 127.127.46.0
     fudge 127.127.46.0 flag4 1

Running with these two configs, the gpsd tool cgps shows the radio 
happily is tracking ~20 satellites. ntpd shows "GPSD" running about 10 
us fast but because this so far off it eventually discards it (ntpq -p 
show 'x' before it). I know /dev/pps0 works because if I add pps clock 
config (driver 22) for it to ntp.conf:

     server 127.127.22.0 prefer
     fudge 127.127.22.0 flag3 1

ntpd is then able to poll for data and shows a reasonable offset that is 
comparable to ntpd running on nearby servers.

So I'm pretty close, I just need to get gpsd working with /dev/pps0. To 
that end, I made a copy of the source and made some changes to 
ppsthread.c (see attached patch.txt) which basically forces an open() on 
/dev/pps0. This gets further:

     gpsd:INFO: KPPS:/dev/pps0 pps_caps 0x1151
     gpsd:INFO: KPPS:/dev/pps0 have PPS_CANWAIT
     gpsd:WARN: KPPS:/dev/pps0 missing PPS_CAPTURECLEAR, pulse may be offset
     gpsd:INFO: KPPS:/dev/pps0 kernel PPS will be used
     [New LWP 101408 of process 2046]
     gpsd:PROG: PPS:/dev/pps0 thread launched
     gpsd:INFO: PPS:/dev/pps0 ntpshm_link_activate: 0
     gpsd:INFO: device /dev/pps0 activated

and gpsd starts to talk GREIS protocol to the radio and then the kernel 
panics with "sleeping thread".

Here are more details about my setup. I have 12.2-RELEASE-p10 kernel 
with these options:

     device          pps
     options         PPS_SYNC
     options         P1003_1B_MQUEUE

I'm building the ports version of net/ntp with these options:

     ATOM DEBUG IPV6 JUPITER NMEA ONCORE PERL_UTILS SHM SSL THREADS

I'm building the ports version of astro/gpsd with these options:

     GPSCLOCK IPV6 NCURSES NMEA0183 NTP NTPSHM ONCORE PPS RECONFIG 
SHMEXPORT SOCKEXPORT

My gpsd source tree is just a copy of patched version of the astro/gpsd 
port. (Which caused me to have to learn how to use scons...) When 
testing I start gpsd with:

     ./gpsd -N -D5 -n -s 115200 -P /var/run/gpsd.pid /dev/gps0 /dev/pps0

The Alpha2 is connected to /dev/cuau0 at 115200 baud, its pps is 
connected to a parallel printer port.

Can anyone help me with gpsd+pps and/or the kernel panic?

		Craig