Alternate cpu policy on battery

2021-09-25 Thread Solene Rapenne
Last time I proposed a CPU frequency scheduling change to make it
more performance oriented, however this would hurt badly the battery
life for nomad users.

As we don't like tweaks and complicated settings, I create a new
CPU frequency policy that is oriented for nomad users, it reduces
performance a bit but from my (short) experiments it would give a
way better battery life while keeping the system responsive which
is not allowing apm -L

The current auto mode is kept as it is now and I added a new
powersaving mode that will be triggered automatically by apmd when
the system is on battery AND automatic mode is activated.

So, on A/C you have the usual automatic policy and on battery it
would switch on the new powersaving policy.

I hope this could be useful for nomad people and later we could
tune the automatic mode (to be used on A/C) to be giving more
performance, without affecting people on battery.

The code change is relatively simple, I'm pretty sure this can be
improved but I wanted to share it as this for now so people can
comment and/or try it out.

I created a new policy which is basically a copy/paste of the current
one with a different name and differents thresholds, declared it
in apmd. There is another change in apmd to check on event if we
are using the policy auto and if on battery or not, this takes care
of changing the policy automatically. I'm not sure it's at the right
place. As for the code in the kernel, I'm quite sure it could be
merged into one and use different thresholds depending on the policy
name currently in use.

Kernel + usr.sbin/apmd + usr.sbin/apm recompilation required

Index: sys/kern//sched_bsd.c
===
RCS file: /home/reposync/src/sys/kern/sched_bsd.c,v
retrieving revision 1.69
diff -u -p -r1.69 sched_bsd.c
--- sys/kern//sched_bsd.c   9 Sep 2021 18:41:39 -   1.69
+++ sys/kern//sched_bsd.c   25 Sep 2021 18:09:40 -
@@ -531,6 +531,7 @@ void (*cpu_setperf)(int);
 #define PERFPOL_MANUAL 0
 #define PERFPOL_AUTO 1
 #define PERFPOL_HIGH 2
+#define PERFPOL_POWERSAVING 4
 int perflevel = 100;
 int perfpolicy = PERFPOL_MANUAL;
 
@@ -541,7 +542,9 @@ int perfpolicy = PERFPOL_MANUAL;
 #include 
 
 void setperf_auto(void *);
+void setperf_powersaving(void *);
 struct timeout setperf_to = TIMEOUT_INITIALIZER(setperf_auto, NULL);
+struct timeout setperf_to_powersaving = 
TIMEOUT_INITIALIZER(setperf_powersaving, NULL);
 
 void
 setperf_auto(void *v)
@@ -606,6 +609,73 @@ setperf_auto(void *v)
timeout_add_msec(_to, 100);
 }
 
+void
+setperf_powersaving(void *v)
+{
+   static uint64_t *idleticks, *totalticks;
+   static int downbeats;
+
+   int i, j;
+   int speedup;
+   CPU_INFO_ITERATOR cii;
+   struct cpu_info *ci;
+   uint64_t idle, total, allidle, alltotal;
+
+   if (perfpolicy != PERFPOL_POWERSAVING)
+   return;
+
+   if (!idleticks)
+   if (!(idleticks = mallocarray(ncpusfound, sizeof(*idleticks),
+   M_DEVBUF, M_NOWAIT | M_ZERO)))
+   return;
+   if (!totalticks)
+   if (!(totalticks = mallocarray(ncpusfound, sizeof(*totalticks),
+   M_DEVBUF, M_NOWAIT | M_ZERO))) {
+   free(idleticks, M_DEVBUF,
+   sizeof(*idleticks) * ncpusfound);
+   return;
+   }
+
+   alltotal = allidle = 0;
+   j = 0;
+   speedup = 0;
+   CPU_INFO_FOREACH(cii, ci) {
+   if (!cpu_is_online(ci))
+   continue;
+   total = 0;
+   for (i = 0; i < CPUSTATES; i++) {
+   total += ci->ci_schedstate.spc_cp_time[i];
+   }
+   total -= totalticks[j];
+   idle = ci->ci_schedstate.spc_cp_time[CP_IDLE] - idleticks[j];
+   // speedup if at least one core idle time < 33%
+   if (idle < total / 3)
+   speedup = 1;
+   alltotal += total;
+   allidle += idle;
+   idleticks[j] += idle;
+   totalticks[j] += total;
+   j++;
+   }
+   if (allidle < alltotal / 3)
+   speedup = 1;
+   if (speedup)
+   /* twice as long here because we check every 200ms */
+   downbeats = 1;
+
+   if (speedup && perflevel != 100) {
+   perflevel = 100;
+   cpu_setperf(perflevel);
+   } else if (!speedup && perflevel != 0 && --downbeats <= 0) {
+   perflevel = 0;
+   cpu_setperf(perflevel);
+   }
+
+/* every 200ms to have a better resolution of the load */
+   timeout_add_msec(_to_powersaving, 200);
+}
+
+
 int
 sysctl_hwsetperf(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
 {
@@ -644,6 +714,9 @@ sysctl_hwperfpolicy(void *oldp, size_t *
case PERFPOL_AUTO:
strlcpy(policy, "auto", 

Re: new gpiocharger driver

2021-09-25 Thread Mark Kettenis
> Date: Sat, 25 Sep 2021 15:45:15 +
> From: Klemens Nanni 
> 
> On Sat, Sep 25, 2021 at 10:53:12AM +, Klemens Nanni wrote:
> > On Fri, Sep 24, 2021 at 10:35:56PM +, Klemens Nanni wrote:
> > > On Thu, Sep 16, 2021 at 11:07:20AM +0200, Mark Kettenis wrote:
> > > > > Date: Thu, 16 Sep 2021 06:14:39 +
> > > > > From: Klemens Nanni 
> > > > > 
> > > > > On 5 September 2021 01:22:53 GMT+05:00, Klemens Nanni 
> > > > >  wrote:
> > > > > >Read a single GPIO pin indicating whether AC is plugged in or not.
> > > > > >
> > > > > >This gives me a sensor on my Pinebook Pro.
> > > > > >cwfg(4) already provides battery information but not the charger 
> > > > > >bits.
> > > > > >
> > > > > >apm(4) integration can follow separately.
> > > > > >
> > > > > >Feedback? OK?
> > > > > 
> > > > > Ping.
> > > > > The diff applies after the "new gpioleds driver" one.
> > > 
> > > New diff.  OK for after unlock modulo the gpioleds bits after being
> > > committed separately (CVS diffs juggling is annoying)?
> > > 
> > > > > >+gpios_len = OF_getproplen(node,
> > > > > >+gpios_property = "charger-status-gpios");
> > > > 
> > > > No, please don't hide assignment statements as function arguments.
> > > 
> > > Fixed, thanks.
> > 
> > Clean diff after gpioleds(4) got in, this time with the same whitespace
> > fixes.
> > 
> > Sorry for the noise.
> 
> Sigh, an eagle-eyed reader noticed my copy-paste error:
> 
> > +   gpios_property = "charger-status-gpios");
> 
> Here's the correct diff that also compiles.

ok kettenis@

> Index: share/man/man4/gpiocharger.4
> ===
> RCS file: share/man/man4/gpiocharger.4
> diff -N share/man/man4/gpiocharger.4
> --- /dev/null 1 Jan 1970 00:00:00 -
> +++ share/man/man4/gpiocharger.4  24 Sep 2021 22:33:26 -
> @@ -0,0 +1,51 @@
> +.\"  $OpenBSD: $
> +.\"
> +.\" Copyright (c) 2021 Klemens Nanni 
> +.\"
> +.\" Permission to use, copy, modify, and distribute this software for any
> +.\" purpose with or without fee is hereby granted, provided that the above
> +.\" copyright notice and this permission notice appear in all copies.
> +.\"
> +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> +.\"
> +.Dd $Mdocdate: September 04 2021 $
> +.Dt GPIOCHARGER 4
> +.Os
> +.Sh NAME
> +.Nm gpiocharger
> +.Nd GPIO battery charger
> +.Sh SYNOPSIS
> +.Cd "gpiocharger* at fdt?"
> +.Sh DESCRIPTION
> +The
> +.Nm
> +driver provides support for battery chargers connected to GPIO pins.
> +Currently, only power supply status events are supported.
> +.Pp
> +The power supply status (connected or disconnected) is set up as a sensor
> +and can be monitored using
> +.Xr sysctl 8
> +or
> +.Xr sensorsd 8 .
> +.Sh SEE ALSO
> +.Xr gpio 4 ,
> +.Xr intro 4 ,
> +.Xr sensorsd 8 ,
> +.Xr sysctl 8
> +.Sh HISTORY
> +The
> +.Nm
> +driver first appeared in
> +.Ox 7.1 .
> +.Sh AUTHORS
> +.An -nosplit
> +The
> +.Nm
> +driver was written by
> +.An Klemens Nanni Aq Mt k...@openbsd.org .
> Index: share/man/man4/Makefile
> ===
> RCS file: /cvs/src/share/man/man4/Makefile,v
> retrieving revision 1.807
> diff -u -p -r1.807 Makefile
> --- share/man/man4/Makefile   25 Sep 2021 10:43:23 -  1.807
> +++ share/man/man4/Makefile   25 Sep 2021 10:45:42 -
> @@ -35,7 +35,8 @@ MAN=aac.4 abcrtc.4 abl.4 ac97.4 acphy.4
>   eso.4 ess.4 et.4 etherip.4 etphy.4 ex.4 exphy.4 exrtc.4 \
>   fanpwr.4 fd.4 fdc.4 fec.4 fido.4 fins.4 fintek.4 fms.4 fusbtc.4 \
>   fuse.4 fxp.4 \
> - gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 gpiodcf.4 \
> + gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 \
> + gpiocharger.4 gpiodcf.4 \
>   gpioiic.4 gpioleds.4 gpioow.4 graphaudio.4 gre.4 gscsio.4 \
>   hds.4 hiclock.4 hidwusb.4 hifn.4 hil.4 hilid.4 hilkbd.4 hilms.4 \
>   hireset.4 hitemp.4 hme.4 hotplug.4 hsq.4 \
> Index: sys/dev/fdt/gpiocharger.c
> ===
> RCS file: sys/dev/fdt/gpiocharger.c
> diff -N sys/dev/fdt/gpiocharger.c
> --- /dev/null 1 Jan 1970 00:00:00 -
> +++ sys/dev/fdt/gpiocharger.c 25 Sep 2021 14:42:16 -
> @@ -0,0 +1,117 @@
> +/*   $OpenBSD: $ */
> +/*
> + * Copyright (c) 2021 Klemens Nanni 
> + *
> + * Permission to use, copy, modify, and distribute this software for any
> + * purpose with or without fee is hereby granted, provided that the above
> + * copyright notice 

Re: umb(4) supports for SIMCom SIM7600

2021-09-25 Thread Shawn Chiou
Hi,

I tested it on my thinkpad X230 with OpenBSD -current amd64.

I can get the following messages from console after AT commands issued.

umb0 at uhub0 port 2 configuration 1 interface 5 "SIMCOM INCORPORATED
> SimTech, Incorporated" rev 2.00/3.18 addr 2
> ugen2 at uhub0 port 2 configuration 1 "SIMCOM INCORPORATED SimTech,
> Incorporated" rev 2.00/3.18 addr 2
>

The device connects to INTERNET when it is up.

umb0: flags=8851 mtu 1434
> index 7 priority 6 llprio 3
> roaming disabled registration home network
> state up cell-class LTE rssi -101dBm speed 47.7Mbps up 95.4Mbps
> down
> SIM initialized PIN valid (3 attempts left)
> subscriber-id 466891003015519 ICC-id 89886891000658005192
> device SIMCOM_SIM7600G-H IMEI 868822040711739 firmware
> LE20B03SIM7600M22
> provider  provider-id 46689
> dns 172.24.9.21 10.9.121.102
> groups: egress
> status: active
> inet 100.80.19.216 --> 100.80.19.217 netmask 0xfff0
>

And I can traceroute or ping via the device.

X230$ traceroute 168.95.1.1
> traceroute to 168.95.1.1 (168.95.1.1), 64 hops max, 40 byte packets
>  1  * * *
>  2  10.11.209.64 (10.11.209.64)  33.62 ms  15.621 ms  15.953 ms
>  3  10.11.30.34 (10.11.30.34)  15.849 ms  15.981 ms  16.889 ms
>  4  10.9.3.49 (10.9.3.49)  15.823 ms  15.87 ms  15.857 ms
>  5  10.9.166.105 (10.9.166.105)  15.909 ms  15.898 ms  16.858 ms
>  6  10.9.166.90 (10.9.166.90)  15.864 ms  15.902 ms  15.954 ms
>  7  202.144.222.130 (202.144.222.130)  15.939 ms  17.88 ms 202.144.222.222
> (202.144.222.222)  25.773 ms
>  8  r4209-s2.hinet.net (203.75.228.94)  26.89 ms  15.921 ms  15.973 ms
>  9  210-59-204-129.hinet-ip.hinet.net (210.59.204.129)  16.892 ms  18.772
> ms  16.885 ms
> 10  dns.hinet.net (168.95.1.1)  16.93 ms !C  15.869 ms !C  16.758 ms !C
> X230$ ping dns.hinet.net
> ping: Warning: dns.hinet.net has multiple addresses; using 168.95.192.1
> PING dns.hinet.net (168.95.192.1): 56 data bytes
> 64 bytes from 168.95.192.1: icmp_seq=0 ttl=54 time=18.378 ms
> 64 bytes from 168.95.192.1: icmp_seq=1 ttl=54 time=25.051 ms
> 64 bytes from 168.95.192.1: icmp_seq=2 ttl=54 time=24.953 ms
> ^C
> --- dns.hinet.net ping statistics ---
> 3 packets transmitted, 3 packets received, 0.0% packet loss
> round-trip min/avg/max/std-dev = 18.378/22.794/25.051/3.123 ms
>

It works properly!

On Fri, Sep 24, 2021 at 12:39 PM Kevin Lo  wrote:

> Hi,
>
> The diff below enables umb(4) support for SIMCom SIM7600.
>
> The SIM7600 is a mbim compatible chip, you'll need to switch it into mbim
> mode via specific AT-commands (AT+CUSBPIDSWITCH=9003,1,1 and AT+CLANMODE=1)
> over the serial port.
>
> umb0 at uhub1 port 2 "SIMCOM INCORPORATED SimTech, Incorporated" rev
> 2.00/3.18 addr 3
>
> ok?
>
> Index: share/man/man4/umb.4
> ===
> RCS file: /cvs/src/share/man/man4/umb.4,v
> retrieving revision 1.13
> diff -u -p -u -p -r1.13 umb.4
> --- share/man/man4/umb.418 May 2021 14:23:03 -  1.13
> +++ share/man/man4/umb.424 Sep 2021 01:12:22 -
> @@ -50,6 +50,7 @@ The following devices should work:
>  .\" .It Huawei ME906s -- attaches but needs more work
>  .It Medion Mobile S4222 (MediaTek OEM)
>  .It Quectel EC25
> +.It SIMCom SIM7600
>  .It Sierra Wireless EM7345
>  .It Sierra Wireless EM7455
>  .It Sierra Wireless EM8805
> Index: sys/dev/usb/if_umb.c
> ===
> RCS file: /cvs/src/sys/dev/usb/if_umb.c,v
> retrieving revision 1.46
> diff -u -p -u -p -r1.46 if_umb.c
> --- sys/dev/usb/if_umb.c4 Jul 2021 19:22:31 -   1.46
> +++ sys/dev/usb/if_umb.c24 Sep 2021 01:12:24 -
> @@ -256,6 +256,12 @@ const struct umb_quirk umb_quirks[] = {
>   0,
>   0
> },
> +
> +   { { USB_VENDOR_SIMCOM, USB_PRODUCT_SIMCOM_SIM7600 },
> + 0,
> + 1,
> + UMATCH_VENDOR_PRODUCT
> +   },
>  };
>
>  #define umb_lookup(vid, pid)   \
> Index: sys/dev/usb/usbdevs
> ===
> RCS file: /cvs/src/sys/dev/usb/usbdevs,v
> retrieving revision 1.741
> diff -u -p -u -p -r1.741 usbdevs
> --- sys/dev/usb/usbdevs 31 Aug 2021 22:55:56 -  1.741
> +++ sys/dev/usb/usbdevs 24 Sep 2021 01:12:24 -
> @@ -4123,6 +4123,7 @@ product SILICONPORTALS YAPPHONE   0x0201  Y
>
>  /* Simcom products */
>  product SIMCOM SIM7600E0x9001  SIM7600E modem
> +product SIMCOM SIM7600 0x9003  SIM7600 LTE
>
>  /* Sirius Technologies products */
>  product SIRIUS ROADSTER0x0001  NetComm Roadster II 56
>
>

-- 

Shawn Chiou
about.me/shawn.chiou



Re: new gpiocharger driver

2021-09-25 Thread Klemens Nanni
On Sat, Sep 25, 2021 at 10:53:12AM +, Klemens Nanni wrote:
> On Fri, Sep 24, 2021 at 10:35:56PM +, Klemens Nanni wrote:
> > On Thu, Sep 16, 2021 at 11:07:20AM +0200, Mark Kettenis wrote:
> > > > Date: Thu, 16 Sep 2021 06:14:39 +
> > > > From: Klemens Nanni 
> > > > 
> > > > On 5 September 2021 01:22:53 GMT+05:00, Klemens Nanni 
> > > >  wrote:
> > > > >Read a single GPIO pin indicating whether AC is plugged in or not.
> > > > >
> > > > >This gives me a sensor on my Pinebook Pro.
> > > > >cwfg(4) already provides battery information but not the charger bits.
> > > > >
> > > > >apm(4) integration can follow separately.
> > > > >
> > > > >Feedback? OK?
> > > > 
> > > > Ping.
> > > > The diff applies after the "new gpioleds driver" one.
> > 
> > New diff.  OK for after unlock modulo the gpioleds bits after being
> > committed separately (CVS diffs juggling is annoying)?
> > 
> > > > >+  gpios_len = OF_getproplen(node,
> > > > >+  gpios_property = "charger-status-gpios");
> > > 
> > > No, please don't hide assignment statements as function arguments.
> > 
> > Fixed, thanks.
> 
> Clean diff after gpioleds(4) got in, this time with the same whitespace
> fixes.
> 
> Sorry for the noise.

Sigh, an eagle-eyed reader noticed my copy-paste error:

> + gpios_property = "charger-status-gpios");

Here's the correct diff that also compiles.


Index: share/man/man4/gpiocharger.4
===
RCS file: share/man/man4/gpiocharger.4
diff -N share/man/man4/gpiocharger.4
--- /dev/null   1 Jan 1970 00:00:00 -
+++ share/man/man4/gpiocharger.424 Sep 2021 22:33:26 -
@@ -0,0 +1,51 @@
+.\"$OpenBSD: $
+.\"
+.\" Copyright (c) 2021 Klemens Nanni 
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: September 04 2021 $
+.Dt GPIOCHARGER 4
+.Os
+.Sh NAME
+.Nm gpiocharger
+.Nd GPIO battery charger
+.Sh SYNOPSIS
+.Cd "gpiocharger* at fdt?"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for battery chargers connected to GPIO pins.
+Currently, only power supply status events are supported.
+.Pp
+The power supply status (connected or disconnected) is set up as a sensor
+and can be monitored using
+.Xr sysctl 8
+or
+.Xr sensorsd 8 .
+.Sh SEE ALSO
+.Xr gpio 4 ,
+.Xr intro 4 ,
+.Xr sensorsd 8 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Ox 7.1 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Klemens Nanni Aq Mt k...@openbsd.org .
Index: share/man/man4/Makefile
===
RCS file: /cvs/src/share/man/man4/Makefile,v
retrieving revision 1.807
diff -u -p -r1.807 Makefile
--- share/man/man4/Makefile 25 Sep 2021 10:43:23 -  1.807
+++ share/man/man4/Makefile 25 Sep 2021 10:45:42 -
@@ -35,7 +35,8 @@ MAN=  aac.4 abcrtc.4 abl.4 ac97.4 acphy.4
eso.4 ess.4 et.4 etherip.4 etphy.4 ex.4 exphy.4 exrtc.4 \
fanpwr.4 fd.4 fdc.4 fec.4 fido.4 fins.4 fintek.4 fms.4 fusbtc.4 \
fuse.4 fxp.4 \
-   gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 gpiodcf.4 \
+   gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 \
+   gpiocharger.4 gpiodcf.4 \
gpioiic.4 gpioleds.4 gpioow.4 graphaudio.4 gre.4 gscsio.4 \
hds.4 hiclock.4 hidwusb.4 hifn.4 hil.4 hilid.4 hilkbd.4 hilms.4 \
hireset.4 hitemp.4 hme.4 hotplug.4 hsq.4 \
Index: sys/dev/fdt/gpiocharger.c
===
RCS file: sys/dev/fdt/gpiocharger.c
diff -N sys/dev/fdt/gpiocharger.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ sys/dev/fdt/gpiocharger.c   25 Sep 2021 14:42:16 -
@@ -0,0 +1,117 @@
+/* $OpenBSD: $ */
+/*
+ * Copyright (c) 2021 Klemens Nanni 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR 

Re: preferred #include guard idiom?

2021-09-25 Thread craekz7
On Fri, Sep 24, 2021 at 08:16:22PM +0100, ropers wrote:
> I'm looking at the #include guards in various .h files in /usr/include.
> There seem to be different (comment) idioms present at the bottom of
> those files/#include guards.
> 
> 
> For example:
> 
> ctype.h:
> #endif /* !_CTYPE_H_ */
> 
> float.h:
> #endif/* _FLOAT_H_ */
> 
> err.h:
> #endif /* !_ERR_H_ */
> 
> curses.h:
> #endif /* _CURSES_H_ */
> 
> 
> Is there agreement on what notation is correct?
> 
> 
> It seems to me that both are defensible or at least arguable.  The
> straightforward idiom without the ! works simply as a reminder of
> which file/#define the #endif belongs to.  The version with the !
> seems to me to be intended to also take note of the fact that it says
> #ifndef at the top, with emphasis on the n.
> 
> From running commands such as
> $ grep '#endif /\* _' *.h | wc -l
> and
> $ grep '#endif /\* !_' *.h | wc -l
> it would appear the former is about twice as common.
> 
> 
> Should a single style be adopted?
> 
> 
> style(9) doesn't cover this admittedly less-than-crucial point.
> Speaking of include guards, is #pragma once ever okay here, or is that
> a big no-no?
> 
> Thanks and regards,
> Ian
> 
> (Ian Ropers)
> 
Personally, I would prefer the style without the "!" because I think the
bang makes it look more important than it is.
#pragma once is non-standard and #ifndef works fine, so I see no
reason to use it.

craekz



[PATCH] add SO_PASSCRED to receive creds of Unix datagram sender

2021-09-25 Thread David MacKay
Good morning,

The patch attached below adds a new socket option, SO_PASSCRED, for
datagram sockets in the Unix domain. This socket option causes an
ancillary message of type SCM_CREDS to be attached by the kernel to
the datagram filled in with the credentials of the datagram sender.

SCM_CREDS messages may not be attached by the sender of the datagram
themselves (as is possible on Linux, where root may falsify that
message).

For now, this only works for datagram sockets. As stream and
sequenced-packet sockets may have peer credentials queried with
SO_PEERCRED, there is much less utility for those.

Kind regards,
David Mackay

diff --git sys/kern/uipc_socket.c sys/kern/uipc_socket.c
index 6f3f2ce4b..37a46032b 100644
--- sys/kern/uipc_socket.c
+++ sys/kern/uipc_socket.c
@@ -1866,6 +1866,19 @@ sosetopt(struct socket *so, int level, int optname, 
struct mbuf *m)
break;
 #endif /* SOCKET_SPLICE */
 
+   case SO_PASSCRED:
+   if (so->so_proto->pr_protocol == AF_UNIX) {
+   struct unpcb *unp = sotounpcb(so);
+   if (m == NULL || m->m_len < sizeof (int))
+   return (EINVAL);
+   if (*mtod(m, int *))
+   unp->unp_flags |= UNP_ADDCRED;
+   else
+   unp->unp_flags &= ~UNP_ADDCRED;
+   return (0);
+   }
+   return (EOPNOTSUPP);
+
default:
error = ENOPROTOOPT;
break;
@@ -2017,6 +2030,14 @@ sogetopt(struct socket *so, int level, int optname, 
struct mbuf *m)
}
return (EOPNOTSUPP);
 
+   case SO_PASSCRED:
+   if (so->so_proto->pr_protocol == AF_UNIX) {
+   struct unpcb *unp = sotounpcb(so);
+   *mtod(m, int *) = unp->unp_flags & UNP_ADDCRED;
+   return (0);
+   }
+   return (EOPNOTSUPP);
+
default:
return (ENOPROTOOPT);
}
diff --git sys/kern/uipc_usrreq.c sys/kern/uipc_usrreq.c
index 83e62bad6..b4449fbe2 100644
--- sys/kern/uipc_usrreq.c
+++ sys/kern/uipc_usrreq.c
@@ -124,6 +124,45 @@ uipc_setaddr(const struct unpcb *unp, struct mbuf *nam)
}
 }
 
+struct mbuf *
+unp_addcred(struct mbuf *control)
+{
+   struct cmsghdr *cm;
+   struct cmsgcred *cmcred;
+   struct mbuf *m, *n;
+   void *p;
+   int i;
+
+   m = sbcreatecontrol(, sizeof(struct cmsgcred), SCM_CREDS, SOL_SOCKET);
+   if (m == NULL)
+   return control;
+
+   m->m_next = NULL;
+
+   if (control == NULL)
+   control = m;
+   else {
+   n = control;
+   while (n->m_next != NULL)
+   n = n->m_next;
+   /* append the SCM_CREDS mbuf to the end */
+   n->m_next = m;
+   }
+
+   cm = mtod(m, struct cmsghdr *);
+   cmcred = (struct cmsgcred *)CMSG_DATA(cm);
+
+   cmcred->cmcred_pid = curproc->p_p->ps_pid;
+   cmcred->cmcred_uid = curproc->p_ucred->cr_ruid;
+   cmcred->cmcred_euid = curproc->p_ucred->cr_uid;
+   cmcred->cmcred_gid = curproc->p_ucred->cr_rgid;
+   cmcred->cmcred_ngroups = MIN(curproc->p_ucred->cr_ngroups, CMGROUP_MAX);
+   for (i = 0; i < cmcred->cmcred_ngroups; i++)
+   cmcred->cmcred_groups[i] = curproc->p_ucred->cr_groups[i];
+
+   return (control);
+}
+
 int
 uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
 struct mbuf *control, struct proc *p)
@@ -245,6 +284,8 @@ uipc_usrreq(struct socket *so, int req, struct mbuf *m, 
struct mbuf *nam,
from = mtod(unp->unp_addr, struct sockaddr *);
else
from = _noname;
+   if (unp->unp_conn->unp_flags & UNP_ADDCRED)
+   control = unp_addcred(control);
if (sbappendaddr(so2, >so_rcv, from, m, control)) {
sorwakeup(so2);
m = NULL;
@@ -819,19 +860,23 @@ unp_externalize(struct mbuf *rights, socklen_t 
controllen, int flags)
 
rw_assert_wrlock(_lock);
 
+   if (controllen < CMSG_ALIGN(sizeof(struct cmsghdr)))
+   controllen = 0;
+   else
+   controllen -= CMSG_ALIGN(sizeof(struct cmsghdr));
+
/*
-* This code only works because SCM_RIGHTS is the only supported
-* control message type on unix sockets. Enforce this here.
+* These checks are applicable only to SCM_RIGHTS control messages. If
+* this is not an SCM_RIGHTS message, there are no file 

Re: new gpiocharger driver

2021-09-25 Thread Klemens Nanni
On Fri, Sep 24, 2021 at 10:35:56PM +, Klemens Nanni wrote:
> On Thu, Sep 16, 2021 at 11:07:20AM +0200, Mark Kettenis wrote:
> > > Date: Thu, 16 Sep 2021 06:14:39 +
> > > From: Klemens Nanni 
> > > 
> > > On 5 September 2021 01:22:53 GMT+05:00, Klemens Nanni  
> > > wrote:
> > > >Read a single GPIO pin indicating whether AC is plugged in or not.
> > > >
> > > >This gives me a sensor on my Pinebook Pro.
> > > >cwfg(4) already provides battery information but not the charger bits.
> > > >
> > > >apm(4) integration can follow separately.
> > > >
> > > >Feedback? OK?
> > > 
> > > Ping.
> > > The diff applies after the "new gpioleds driver" one.
> 
> New diff.  OK for after unlock modulo the gpioleds bits after being
> committed separately (CVS diffs juggling is annoying)?
> 
> > > >+gpios_len = OF_getproplen(node,
> > > >+gpios_property = "charger-status-gpios");
> > 
> > No, please don't hide assignment statements as function arguments.
> 
> Fixed, thanks.

Clean diff after gpioleds(4) got in, this time with the same whitespace
fixes.

Sorry for the noise.


Index: share/man/man4/gpiocharger.4
===
RCS file: share/man/man4/gpiocharger.4
diff -N share/man/man4/gpiocharger.4
--- /dev/null   1 Jan 1970 00:00:00 -
+++ share/man/man4/gpiocharger.425 Sep 2021 10:51:18 -
@@ -0,0 +1,51 @@
+.\"$OpenBSD: $
+.\"
+.\" Copyright (c) 2021 Klemens Nanni 
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: September 04 2021 $
+.Dt GPIOCHARGER 4
+.Os
+.Sh NAME
+.Nm gpiocharger
+.Nd GPIO battery charger
+.Sh SYNOPSIS
+.Cd "gpiocharger* at fdt?"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for battery chargers connected to GPIO pins.
+Currently, only power supply status events are supported.
+.Pp
+The power supply status (connected or disconnected) is set up as a sensor
+and can be monitored using
+.Xr sysctl 8
+or
+.Xr sensorsd 8 .
+.Sh SEE ALSO
+.Xr gpio 4 ,
+.Xr intro 4 ,
+.Xr sensorsd 8 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Ox 7.1 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Klemens Nanni Aq Mt k...@openbsd.org .
Index: share/man/man4/Makefile
===
RCS file: /cvs/src/share/man/man4/Makefile,v
retrieving revision 1.807
diff -u -p -r1.807 Makefile
--- share/man/man4/Makefile 25 Sep 2021 10:43:23 -  1.807
+++ share/man/man4/Makefile 25 Sep 2021 10:51:18 -
@@ -35,7 +35,8 @@ MAN=  aac.4 abcrtc.4 abl.4 ac97.4 acphy.4
eso.4 ess.4 et.4 etherip.4 etphy.4 ex.4 exphy.4 exrtc.4 \
fanpwr.4 fd.4 fdc.4 fec.4 fido.4 fins.4 fintek.4 fms.4 fusbtc.4 \
fuse.4 fxp.4 \
-   gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 gpiodcf.4 \
+   gdt.4 gentbi.4 gem.4 gfrtc.4 gif.4 glenv.4 glkgpio.4 gpio.4 \
+   gpiocharger.4 gpiodcf.4 \
gpioiic.4 gpioleds.4 gpioow.4 graphaudio.4 gre.4 gscsio.4 \
hds.4 hiclock.4 hidwusb.4 hifn.4 hil.4 hilid.4 hilkbd.4 hilms.4 \
hireset.4 hitemp.4 hme.4 hotplug.4 hsq.4 \
Index: sys/dev/fdt/gpiocharger.c
===
RCS file: sys/dev/fdt/gpiocharger.c
diff -N sys/dev/fdt/gpiocharger.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ sys/dev/fdt/gpiocharger.c   25 Sep 2021 10:51:18 -
@@ -0,0 +1,117 @@
+/* $OpenBSD: $ */
+/*
+ * Copyright (c) 2021 Klemens Nanni 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+#include