Просмотр исходного кода

Add Cherryview panel backlight and a brightness OSD

The HP Chromebook 11 G5 (google/setzer, Braswell) drives its internal panel
PWM from the display engine, not the Chrome EC.  backlight.c/h matches the
Intel 8086:22b0/22b1 graphics device, maps the graphics MMIO BAR and drives
the VLV/CHV BLC_PWM_CTL2/BLC_PWM_CTL registers (pipe A at 0x1E1250/0x1E1254,
pipe B +0x100), following the pipe the firmware enabled.  The 10% steps clamp
at 5..100% and the level is read back from the register at boot.

- backlight.c/h, gpu.c: probe the CHV PWM, expose present/percent/step/seq
  and a status snapshot for the command.
- io.c/h: consume F6/F7 (the ChromeOS EC action row, guarded by
  cros_ec_present()) and E0 14/E0 15 (standard brightness keys); add a
  64-entry raw scancode tap for the keytest helper.
- terminal.c: add the backlight command (probe/up/down/0-100) and keytest,
  which prints every raw byte with its E0/make/break state and decoded key.
- wm_abi.h, wm.c: carry the backlight level and change counter in wm_status.
- ucmd_wm.c: show a centered 3 s Brightness bubble with a sun icon, the
  percentage and a level bar, composited above every window (including the
  drag/resize passes).
- Makefile, AGENTS.md and docs (graphics, drivers, development, userland)
  updated.

make kernel.bin and ucmd_wm.elf pass; QEMU boot and keytest were checked.
The CHV PWM path was validated on the target and the OSD layout was checked
with a forced level in QEMU.
Johannes Zellner 1 неделя назад
Родитель
Сommit
cce9edb987
15 измененных файлов с 808 добавлено и 13 удалено
  1. 20 2
      AGENTS.md
  2. 7 4
      Makefile
  3. 241 0
      backlight.c
  4. 44 0
      backlight.h
  5. 2 0
      docs/development.html
  6. 2 0
      docs/drivers.html
  7. 22 1
      docs/graphics.html
  8. 1 1
      docs/userland.html
  9. 2 0
      gpu.c
  10. 90 2
      io.c
  11. 12 0
      io.h
  12. 204 0
      terminal.c
  13. 148 3
      ucmd_wm.c
  14. 8 0
      wm.c
  15. 5 0
      wm_abi.h

+ 20 - 2
AGENTS.md

@@ -462,6 +462,23 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   DMA channels never fetch until nouveau's full takeover (which re-programs the
   mode itself), the cursor staging registers are engine-only, and a channel
   that has run cannot be restarted — see the header of `nv50.c`.
+- **Panel backlight**: `backlight.c/h` — the Intel Cherryview/VLV display PWM
+  for the internal panel (the HP Chromebook 11 G5's `google/setzer` VBT
+  declares PWM controller 0, 200 Hz, active high).  It matches Intel
+  `8086:22b0`/`22b1`, maps the graphics MMIO BAR, reads `BLC_PWM_CTL2`
+  (`0x1E1250`, +`0x100` for pipe B) and `BLC_PWM_CTL` (`0x1E1254`), follows the
+  pipe the firmware enabled and falls back to controller 0; the period comes
+  from the firmware (`6000` = 19.2 MHz / (200 Hz × 16) when it reads zero).
+  The 10% steps clamp at 5..100% and the level is not persisted across a cold
+  boot.  `io.c` maps F6/F7 (the ChromeOS EC action row) and `E0 14`/`E0 15`
+  (standard brightness scancodes) to `backlight_step()` and consumes them.
+  The `backlight [0-100|up|down|probe]` kernel command is the other interface.
+  `struct wm_status` carries the level and a change counter
+  (`backlight_seq()`); the ring-3 WM shows a centered 3 s bubble with a
+  sun icon, the percentage and a bar after each change, and redraws it on
+  every pass while visible so a window cannot cover it.
+  The `keytest` kernel command drains the raw scancode tap in `io.c`
+  (E0/make/break/decoded) for key mapping.
 - **Text console**: `fbtext.c`, `font_8x8.c/h`, `io.c/h`, `screen.c/h` — 8×8 font,
   framebuffer glyph blitting (`fbtext_draw_cells`) and the boot-console cell
   buffer (`screen.c`, used by `io.c`'s fallback screen and `bootcon`).  The
@@ -1446,7 +1463,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
 `display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `meminfo`, `top`, `sched`, `format <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk), `install <target> [source|self]` (clone the live image, or write the running image; auto-detects the source, preferring `usb0`; `auto` targets the only non-USB NebOS disk),
-`ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
+`ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `backlight [0-100|up|down]` (also `probe`), `keytest` (raw scancode tap), `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
 DHCP client), `netdev [name]` (kernel command: show the active NIC driver or
@@ -1958,7 +1975,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 7. `display` (needs a GPU snapshot) and `lsusb` (a 45-byte wrapper around
    the USB tree printer) are the last non-privileged commands still living in
    the kernel, plus the privileged ones (`peek`, `poke`, `dbg`, `serial`,
-   `kexec`, `reboot`, `poweroff`, `format`, `install`, `ps`/`kill`).
+   `kexec`, `reboot`, `poweroff`, `format`, `install`, `ps`/`kill`,
+   `backlight`/`keytest`).
 8. The rootfs pack is inflated into one contiguous page-allocated run at
    boot, capped by `RAMFS_MAX_DECOMPRESSED` (8 MB); a larger pack fails to
    load (the USB rootfs partition is 8 MB, so this only bites if the cap is

Разница между файлами не показана из-за своего большого размера
+ 7 - 4
Makefile


+ 241 - 0
backlight.c

@@ -0,0 +1,241 @@
+/* Intel Cherryview/VLV display PWM panel backlight.
+ *
+ * Braswell/Cherryview (and Bay Trail/Valleyview) drive the internal panel
+ * backlight from the display engine, not from the Chrome EC.  The VBT
+ * (block 43, LFP backlight) on google/setzer selects PWM controller 0 at
+ * 200 Hz, active high.  Linux drives the same registers from i915
+ * (vlv_setup_backlight()/vlv_set_backlight()):
+ *
+ *   BLC_PWM_CTL2  pipe A  VLV_DISPLAY_BASE + 0x61250  (0x1E1250)
+ *   BLC_PWM_CTL   pipe A  VLV_DISPLAY_BASE + 0x61254  (0x1E1254)
+ *   pipe B adds 0x100.
+ *
+ * BLC_PWM_CTL2 bit 31 enables the PWM, bit 28 is the polarity.
+ * BLC_PWM_CTL bits 31:16 are the period and bits 15:0 the duty cycle.
+ *
+ * The PWM clock is 19.2 MHz with a fixed /16 increment on Cherryview, so
+ * 200 Hz is a period of 6000.  At probe time the period the firmware left
+ * in the register wins; 6000 is only the fallback when it reads as zero.
+ */
+#include "backlight.h"
+#include "paging.h"
+#include "pci.h"
+#include "serial.h"
+
+#define INTEL_CHV_GFX_B0	0x22b0
+#define INTEL_CHV_GFX_C0	0x22b1
+
+#define VLV_DISPLAY_BASE	0x180000u
+#define VLV_BLC_PWM_CTL2(pipe)	(VLV_DISPLAY_BASE + 0x61250u + \
+				 ((pipe) ? 0x100u : 0u))
+#define VLV_BLC_PWM_CTL(pipe)	(VLV_DISPLAY_BASE + 0x61254u + \
+				 ((pipe) ? 0x100u : 0u))
+
+#define BLM_PWM_ENABLE		(1u << 31)
+#define BLC_DUTY_MASK		0xFFFFu
+#define BLC_MIN_PERCENT		5
+#define BLC_STEP_PERCENT	10
+#define BLC_DEFAULT_PERIOD	6000u	/* 19.2 MHz / (200 Hz * 16) */
+
+static unsigned int blc_bar;
+static unsigned int blc_pipe;
+static unsigned int blc_period;
+static unsigned int blc_duty;
+static int blc_level;
+static int blc_present;
+static int blc_enabled;
+static unsigned int blc_seq;	/* bumped on every user-visible change */
+
+static unsigned int blc_read(unsigned int reg)
+{
+	return *(volatile unsigned int *)(blc_bar + reg);
+}
+
+static void blc_write(unsigned int reg, unsigned int val)
+{
+	*(volatile unsigned int *)(blc_bar + reg) = val;
+}
+
+void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id)
+{
+	unsigned int bar0, ctl2[2], ctl[2], period, duty, i;
+	int pipe, a_ok, b_ok;
+
+	blc_bar = 0;
+	blc_pipe = 0;
+	blc_period = 0;
+	blc_duty = 0;
+	blc_level = 0;
+	blc_present = 0;
+	blc_enabled = 0;
+
+	if (device_id != INTEL_CHV_GFX_B0 && device_id != INTEL_CHV_GFX_C0)
+		return;
+
+	bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
+	if (bar0 & 1)
+		return;			/* I/O BAR, not MMIO */
+	if (!(bar0 & 0xFFFFFFF0u))
+		return;
+	if ((bar0 & 0x6) == 0x4 &&
+	    pci_cfg_read32(bus, dev, 0, 0x14) != 0)
+		return;			/* 64-bit BAR above 4 GB */
+
+	/* The backlight registers live below 2 MB in the display MMIO BAR. */
+	blc_bar = bar0 & 0xFFFFFFF0u;
+	paging_map_mmio(blc_bar, 0x200000);
+
+	for (i = 0; i < 2; i++) {
+		ctl2[i] = blc_read(VLV_BLC_PWM_CTL2(i));
+		ctl[i] = blc_read(VLV_BLC_PWM_CTL(i));
+	}
+
+	a_ok = !(ctl2[0] == 0xFFFFFFFFu && ctl[0] == 0xFFFFFFFFu);
+	b_ok = !(ctl2[1] == 0xFFFFFFFFu && ctl[1] == 0xFFFFFFFFu);
+	if (!a_ok && !b_ok) {
+		serial_puts(SERIAL_COM1, "backlight: no display MMIO\n");
+		return;
+	}
+
+	/* Follow the pipe the firmware enabled; when both or neither are
+	 * enabled, use the active display plane; otherwise fall back to the
+	 * VBT's controller 0 (pipe A).  The internal panel is normally on A. */
+	pipe = 0;
+	if (!a_ok) {
+		pipe = 1;
+	} else if (b_ok &&
+		   (ctl2[0] & BLM_PWM_ENABLE) == (ctl2[1] & BLM_PWM_ENABLE)) {
+		unsigned int dspa = blc_read(VLV_DISPLAY_BASE + 0x70180);
+		unsigned int dspb = blc_read(VLV_DISPLAY_BASE + 0x71180);
+
+		if (!(dspa & (1u << 31)) && (dspb & (1u << 31)))
+			pipe = 1;
+	} else if (b_ok && (ctl2[1] & BLM_PWM_ENABLE)) {
+		pipe = 1;
+	}
+
+	period = ctl[pipe] >> 16;
+	if (!period)
+		period = BLC_DEFAULT_PERIOD;
+	duty = ctl[pipe] & BLC_DUTY_MASK;
+	if (duty > period)
+		duty = period;
+
+	blc_pipe = (unsigned int)pipe;
+	blc_period = period;
+	blc_duty = duty;
+	blc_level = (int)((duty * 100u + period / 2u) / period);
+	if (blc_level > 100)
+		blc_level = 100;
+	if (blc_level < BLC_MIN_PERCENT)
+		blc_level = BLC_MIN_PERCENT;
+	blc_enabled = (ctl2[pipe] & BLM_PWM_ENABLE) != 0;
+	blc_present = 1;
+
+	serial_puts(SERIAL_COM1, "backlight: CHV PWM pipe ");
+	serial_puts(SERIAL_COM1, pipe ? "B" : "A");
+	serial_puts(SERIAL_COM1, ", period ");
+	serial_putdec(SERIAL_COM1, period);
+	serial_puts(SERIAL_COM1, ", duty ");
+	serial_putdec(SERIAL_COM1, duty);
+	serial_puts(SERIAL_COM1, " (");
+	serial_putdec(SERIAL_COM1, (unsigned int)blc_level);
+	serial_puts(SERIAL_COM1, "%), ");
+	serial_puts(SERIAL_COM1, blc_enabled ? "enabled" : "disabled");
+	serial_puts(SERIAL_COM1, ", ctl2 ");
+	serial_puthex(SERIAL_COM1, ctl2[0]);
+	serial_puthex(SERIAL_COM1, ctl2[1]);
+	serial_puts(SERIAL_COM1, ", ctl ");
+	serial_puthex(SERIAL_COM1, ctl[0]);
+	serial_puthex(SERIAL_COM1, ctl[1]);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+int backlight_present(void)
+{
+	return blc_present;
+}
+
+int backlight_percent(void)
+{
+	return blc_present ? blc_level : -1;
+}
+
+unsigned int backlight_seq(void)
+{
+	return blc_seq;
+}
+
+void backlight_set_percent(int percent)
+{
+	unsigned int ctl2, duty;
+
+	if (!blc_present)
+		return;
+	if (percent < BLC_MIN_PERCENT)
+		percent = BLC_MIN_PERCENT;
+	if (percent > 100)
+		percent = 100;
+	blc_seq++;
+
+	duty = (blc_period * (unsigned int)percent + 50u) / 100u;
+	if (duty > blc_period)
+		duty = blc_period;
+	if (percent && !duty)
+		duty = 1;
+
+	if (!blc_enabled) {
+		ctl2 = blc_read(VLV_BLC_PWM_CTL2(blc_pipe));
+		blc_write(VLV_BLC_PWM_CTL2(blc_pipe), ctl2 & ~BLM_PWM_ENABLE);
+		blc_write(VLV_BLC_PWM_CTL(blc_pipe),
+			  (blc_period << 16) | duty);
+		(void)blc_read(VLV_BLC_PWM_CTL(blc_pipe));
+		blc_write(VLV_BLC_PWM_CTL2(blc_pipe),
+			  (ctl2 & ~BLM_PWM_ENABLE) | BLM_PWM_ENABLE);
+		(void)blc_read(VLV_BLC_PWM_CTL2(blc_pipe));
+		blc_enabled = 1;
+	} else {
+		blc_write(VLV_BLC_PWM_CTL(blc_pipe),
+			  (blc_period << 16) | duty);
+		(void)blc_read(VLV_BLC_PWM_CTL(blc_pipe));
+	}
+
+	blc_duty = duty;
+	blc_level = percent;
+}
+
+void backlight_step(int dir)
+{
+	int level;
+
+	if (!blc_present)
+		return;
+	level = blc_level + (dir > 0 ? BLC_STEP_PERCENT : -BLC_STEP_PERCENT);
+	backlight_set_percent(level);
+}
+
+int backlight_status(struct backlight_status *out)
+{
+	unsigned int i;
+
+	if (!out)
+		return -1;
+	for (i = 0; i < sizeof(*out); i++)
+		((unsigned char *)out)[i] = 0;
+	out->present = blc_present;
+	if (!blc_present)
+		return -1;
+
+	out->level = blc_level;
+	out->enabled = blc_enabled;
+	out->pipe = blc_pipe;
+	out->period = blc_period;
+	out->duty = blc_duty;
+	out->bar = blc_bar;
+	/* Keep the two pipe snapshots live for the `backlight probe` dump. */
+	out->ctl2[0] = blc_read(VLV_BLC_PWM_CTL2(0));
+	out->ctl2[1] = blc_read(VLV_BLC_PWM_CTL2(1));
+	out->ctl[0] = blc_read(VLV_BLC_PWM_CTL(0));
+	out->ctl[1] = blc_read(VLV_BLC_PWM_CTL(1));
+	return 0;
+}

+ 44 - 0
backlight.h

@@ -0,0 +1,44 @@
+/* Intel Cherryview/VLV display-engine panel backlight.
+ *
+ * The internal panel's PWM lives in the graphics MMIO BAR, not on the
+ * Chrome EC: BLC_PWM_CTL2 enables it and BLC_PWM_CTL holds the period and
+ * duty cycle.  The HP Chromebook 11 G5 (google/setzer, Braswell) VBT
+ * declares PWM controller 0, 200 Hz, active high.
+ */
+#ifndef BACKLIGHT_H
+#define BACKLIGHT_H
+
+struct backlight_status {
+	int present;
+	int level;              /* percent, 5..100 */
+	int enabled;
+	unsigned int pipe;      /* 0 = A, 1 = B */
+	unsigned int period;    /* raw PWM period from BLC_PWM_CTL */
+	unsigned int duty;      /* raw duty from BLC_PWM_CTL */
+	unsigned int bar;       /* graphics MMIO BAR0 base */
+	unsigned int ctl2[2];   /* raw BLC_PWM_CTL2 for pipes A and B */
+	unsigned int ctl[2];    /* raw BLC_PWM_CTL for pipes A and B */
+};
+
+/* Called from gpu_init() for the active Intel adapter; no-op elsewhere. */
+void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id);
+
+int backlight_present(void);
+
+/* Current brightness in percent, or -1 when no panel PWM was found. */
+int backlight_percent(void);
+
+/* Monotonic change counter, bumped by every successful set/step.  The WM
+ * uses it to pop the brightness OSD without polling the registers. */
+unsigned int backlight_seq(void);
+
+/* Clamped to 5..100. */
+void backlight_set_percent(int percent);
+
+/* Step by 10 percent; dir > 0 is brighter. */
+void backlight_step(int dir);
+
+/* Fill the status snapshot used by the `backlight` command. */
+int backlight_status(struct backlight_status *out);
+
+#endif

+ 2 - 0
docs/development.html

@@ -306,6 +306,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime).  On QEMU the USB image reaches it about 0.4 s after the kernel starts; USB, the NIC and the disks are probed after that (<code>[boot] usb/net/storage</code> lines).</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
+			<li><strong>Keys.</strong> <code>keytest</code> drains the raw scancode tap in <span class="file">io.c</span> and prints every byte with its E0-prefix state, make/break kind and decoded <code>IO_KEY_*</code>/ASCII value; Ctrl+C stops it.  It sees releases and keys the decoder ignores, so an unknown action-row key is easy to identify.</li>
 			<li><strong>Panic.</strong> A ring-0 fault dumps vector, error code, registers, CRs, EIP bytes and stack candidates to the LFB and COM1. Resolve addresses with <code>nm kernel.elf</code> / <code>addr2line -e kernel.elf</code>.</li>
 			<li><strong>Profiling.</strong> <code>gfxbench</code> times fill/copy/cell/canvas/flush; <code>top</code>, <code>sched</code>, <code>sensors</code>, <code>powerinfo</code>, <code>power</code> and <code>lspci</code>/<code>lsusb</code> expose live state.</li>
 			<li><strong>Memory.</strong> <code>peek addr len</code> hex-dumps identity-mapped memory (MMIO too); <code>poke addr val</code> writes one dword and prints the read-back.</li>
@@ -370,6 +371,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 			<li><code>netkexec.c</code>, <code>netkexec_send.c</code> — network reload</li>
 			<li><code>netlog.c</code>, <code>netmonitor.c</code> — UDP console</li>
 			<li><code>dbg.c</code> — serial debug monitor</li>
+			<li><code>io.c</code>, <code>terminal.c</code> — scancode decoder/tap and the kernel command registry</li>
 			<li><code>panic.c</code> — fatal exception dump</li>
 			<li><code>kernel.ld</code>, <code>ucmd.ld</code> — link limits</li>
 			<li><code>tools/</code> — QMP USB and installer tests</li>

+ 2 - 0
docs/drivers.html

@@ -87,6 +87,7 @@
 					<tr><td>Network</td><td><span class="file">e1000e.c</span> <span class="file">r8168.c</span> <span class="file">ne2000.c</span> <span class="file">asix.c</span> <span class="file">rndis.c</span></td><td>PCI or USB</td><td>Polled, one active receiver</td></tr>
 					<tr><td>WiFi</td><td><span class="file">iwl_trans.c</span> <span class="file">iwl_mvm.c</span> <span class="file">iwl_dvm.c</span> <span class="file">wifi.c</span></td><td>PCIe, Intel 7265D/3165 (<code>8086:095a/095b/3165/3166</code>) and 6200/6300 (<code>8086:4239/422c</code>, <code>4238/422b</code>)</td><td>Polled RX ring; firmware from <code>/ramfs/firmware</code>, loaded on first use</td></tr>
 					<tr><td>USB WiFi</td><td><span class="file">rtl8821au.c</span> <span class="file">wifi.c</span></td><td>USB 2.0 high speed, Realtek RTL8821AU/8811AU (TP-Link Archer T2U Nano <code>2357:011f</code> and the other Linux <code>rtw88_8821au</code> IDs)</td><td>Vendor control requests for registers, one bulk-IN/one bulk-OUT endpoint; firmware + register tables from <code>/ramfs/firmware</code></td></tr>
+					<tr><td>Panel backlight</td><td><span class="file">backlight.c</span></td><td>Intel Braswell/Cherryview graphics MMIO, PCI <code>00:02.0</code> (<code>8086:22b0/22b1</code>)</td><td>Polled register writes; F6/F7 and <code>E0 14</code>/<code>E0 15</code> step 10%</td></tr>
 					<tr><td>PS/2</td><td><span class="file">mouse.c</span>, <span class="file">io.c</span></td><td>i8042</td><td>IRQ 1 keyboard, IRQ 12 mouse; polling fallback</td></tr>
 					<tr><td>Touchpad</td><td><span class="file">i2c.c</span>, <span class="file">touchpad.c</span>, <span class="file">touchpad_gesture.c</span></td><td>LPSS DesignWare I2C, slave <code>0x15</code></td><td>Polled input register every 25 ms; software tap and two-finger scroll gestures; no GPIO interrupt driver</td></tr>
 					<tr><td>Serial</td><td><span class="file">serial.c</span></td><td>16550 UART, COM1</td><td>IRQ 4 ring buffer + polled transmitter</td></tr>
@@ -692,6 +693,7 @@
 			<li><code>wifi.c</code>, <code>wifi.h</code> — 802.11 station, <code>wifi</code> netdev, <code>SYS_WIFI</code></li>
 			<li><code>ccmp.c</code>, <code>ccmp.h</code> — software CCMP (AES-CCM): RX fallback, RTL8821AU TX</li>
 			<li><code>install.c</code> — live-image clone installer</li>
+			<li><code>backlight.c</code>, <code>backlight.h</code> — Cherryview/Braswell panel PWM</li>
 			<li><code>usbhid.c</code>, <code>mouse.c</code>, <code>io.c</code> — input</li>
 			<li><code>i2c.c</code>, <code>i2c.h</code> — DesignWare/LPSS I2C master</li>
 			<li><code>touchpad.c</code>, <code>touchpad.h</code> — Elan HID-over-I2C touchpad</li>

+ 22 - 1
docs/graphics.html

@@ -61,6 +61,7 @@
 			<li><a href="#apps">Applications</a></li>
 			<li><a href="#images">Image library</a></li>
 			<li><a href="#gpu">GPU quirks</a></li>
+			<li><a href="#backlight">Panel backlight</a></li>
 			<li><a href="#sources">Source map</a></li>
 		</ul>
 	</nav>
@@ -385,11 +386,30 @@
 		<ul>
 			<li><strong>ATI Mach64.</strong> A CRTC fixup is applied after mode set. The legacy CRTC is skipped under UEFI.</li>
 			<li><strong>NVIDIA.</strong> VBE-only. The legacy VGA CRTC is never touched. On the ION, the framebuffer is stolen DRAM behind a BAR3 aperture; <span class="file">nvidia.c</span> finds the stolen range and writes through a cached alias once a WC MTRR covers it.</li>
-			<li><strong>Intel.</strong> The Ironlake-era path supports a hardware cursor where available; the software cursor is the common fallback.</li>
+			<li><strong>Intel.</strong> The Ironlake-era path supports a hardware cursor where available; the software cursor is the common fallback. Cherryview/Braswell also exposes the panel backlight (below).</li>
 			<li><strong>UEFI.</strong> <code>vga.c</code> honours the RGBX flag and skips the VBE-only ATI CRTC reset and NVIDIA stolen-VRAM alias. <span class="file">mouse.c</span> forces the software cursor.</li>
 		</ul>
 	</section>
 
+	<section id="backlight">
+		<h2>Panel backlight</h2>
+		<p>
+			The HP Chromebook 11 G5 (<code>google/setzer</code>, Braswell)
+			drives its internal panel PWM from the display engine, not from
+			the Chrome EC. The board VBT (<code>data.vbt</code>, block 43)
+			declares PWM controller 0 at 200 Hz, active high. Linux uses the
+			same Valleyview/Cherryview registers through
+			<code>intel_backlight</code>.
+		</p>
+		<ul>
+			<li><strong>Registers.</strong> <code>BLC_PWM_CTL2</code> at GPU BAR0 + <code>0x1E1250</code> (pipe A; pipe B is <code>+0x100</code>): bit 31 enables, bit 28 is polarity. <code>BLC_PWM_CTL</code> at <code>0x1E1254</code>: period in bits 31:16, duty in bits 15:0. The Cherryview PWM clock is 19.2 MHz with a fixed /16 increment, so 200 Hz is a period of 6000.</li>
+			<li><strong>Probe.</strong> <span class="file">backlight.c</span> matches Intel <code>8086:22b0</code>/<code>22b1</code>, maps the graphics MMIO BAR, reads both pipes, follows the one the firmware enabled and falls back to controller 0. It logs the chosen pipe, period, duty and raw registers to COM1.</li>
+			<li><strong>Use.</strong> The <code>backlight</code> command prints the percentage and PWM state; <code>backlight up|down</code> steps by 10%, <code>backlight 0-100</code> sets a level (clamped to 5..100), and <code>backlight probe</code> dumps both pipes' registers. F6/F7 (the ChromeOS EC action-row brightness keys) and the standard <code>E0 14</code>/<code>E0 15</code> brightness scancodes adjust it; the keys are consumed in <span class="file">io.c</span> and never reach a window.</li>
+			<li><strong>Overlay.</strong> <code>SYS_WM_POLL</code> also reports the backlight percentage and a change counter. After each change the ring-3 WM (<span class="file">ucmd_wm.c</span>) draws a centered <em>Brightness</em> bubble with a sun icon, the percentage and a level bar, keeps it up for 3 s, and composits it above the windows on every pass while it is visible.</li>
+			<li><strong>Limits.</strong> Only the Cherryview/Braswell native PWM (<code>8086:22b0</code>/<code>22b1</code>) is driven. The level is not persisted; a cold boot returns to the firmware's duty.</li>
+		</ul>
+	</section>
+
 	<section id="sources">
 		<h2>Source map</h2>
 		<ul class="filelist">
@@ -410,6 +430,7 @@
 			<li><code>ucmd_files.c</code>, <code>gallery.c</code>, <code>ucmd_solitaire.c</code>, <code>ucmd_chat.c</code></li>
 			<li><code>img*.c</code> — image codecs</li>
 			<li><code>ati.c</code>, <code>nvidia.c</code>, <code>intel.c</code>, <code>nv50.c</code></li>
+			<li><code>backlight.c</code>, <code>backlight.h</code> — Cherryview/VLV panel PWM</li>
 		</ul>
 	</section>
 </main>

+ 1 - 1
docs/userland.html

@@ -275,7 +275,7 @@
 				<tbody>
 					<tr><td>Shell</td><td><code>cd</code> <code>path</code> <code>help</code> <code>clear</code> <code>bootlog</code></td></tr>
 					<tr><td>Process / scheduler</td><td><code>ps</code> <code>sched</code> <code>renice</code> <code>kill</code> <code>idle</code> <code>cpufreq</code> <code>power</code></td></tr>
-					<tr><td>Hardware / debug</td><td><code>display</code> <code>gfxbench</code> <code>lsusb</code> <code>lspci</code> <code>peek</code> <code>poke</code> <code>format</code> <code>install</code> <code>dbg</code> <code>serial</code></td></tr>
+					<tr><td>Hardware / debug</td><td><code>display</code> <code>gfxbench</code> <code>lsusb</code> <code>lspci</code> <code>peek</code> <code>poke</code> <code>backlight</code> <code>keytest</code> <code>format</code> <code>install</code> <code>dbg</code> <code>serial</code></td></tr>
 					<tr><td>Network control</td><td><code>netdev</code></td></tr>
 					<tr><td>Boot / power</td><td><code>reboot</code> <code>poweroff</code> <code>kexec</code></td></tr>
 				</tbody>

+ 2 - 0
gpu.c

@@ -2,6 +2,7 @@
 
 #include "gpu.h"
 #include "pci.h"
+#include "backlight.h"
 #include "ati.h"
 #include "cirrus.h"
 #include "intel.h"
@@ -100,6 +101,7 @@ void gpu_init(unsigned int lfb_base)
 	} else if (active_vendor == PCI_VENDOR_INTEL) {
 		gpu_vendor_id = GPU_VENDOR_INTEL;
 		intel_init(active_bus, active_dev, (active_id >> 16) & 0xFFFF);
+		backlight_init(active_bus, active_dev, (active_id >> 16) & 0xFFFF);
 	} else if (active_vendor == PCI_VENDOR_CIRRUS) {
 		gpu_vendor_id = GPU_VENDOR_CIRRUS;
 		cirrus_init();

+ 90 - 2
io.c

@@ -3,10 +3,13 @@
    slot 0 / boot fall back to a static screen the WM does not render. */
 
 #include "io.h"
+#include "backlight.h"
+#include "cros_ec.h"
 #include "fbtext.h"
 #include "mouse.h"
 #include "process.h"
 #include "screen.h"
+#include "spinlock.h"
 #include "timer.h"
 #include "tty.h"
 
@@ -67,6 +70,57 @@ static unsigned int repeat_at;
 static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
 static volatile unsigned int kbd_head, kbd_tail;
 
+/* Raw decoder tap for `keytest`; independent of the decoded key queue so it
+ * also records releases, E0 prefixes and keys the decoder ignores. */
+#define KBD_TAP_SIZE 64
+static struct io_keytap kbd_tap[KBD_TAP_SIZE];
+static unsigned int kbd_tap_head, kbd_tap_tail;
+static spinlock_t kbd_tap_lock;   /* producer/consumer can run on different CPUs */
+
+static void kbd_tap_push(unsigned char raw, unsigned char ext,
+			 unsigned char kind, int key)
+{
+	unsigned int flags;
+	unsigned int next;
+
+	spin_lock_irqsave(&kbd_tap_lock, &flags);
+	next = (kbd_tap_head + 1) % KBD_TAP_SIZE;
+	kbd_tap[kbd_tap_head].raw = raw;
+	kbd_tap[kbd_tap_head].ext = ext;
+	kbd_tap[kbd_tap_head].kind = kind;
+	kbd_tap[kbd_tap_head].key = key;
+	kbd_tap_head = next;
+	if (next == kbd_tap_tail)
+		kbd_tap_tail = (kbd_tap_tail + 1) % KBD_TAP_SIZE;
+	spin_unlock_irqrestore(&kbd_tap_lock, flags);
+}
+
+void io_keytap_reset(void)
+{
+	unsigned int flags;
+
+	spin_lock_irqsave(&kbd_tap_lock, &flags);
+	kbd_tap_head = kbd_tap_tail = 0;
+	spin_unlock_irqrestore(&kbd_tap_lock, flags);
+}
+
+int io_keytap_read(struct io_keytap *out)
+{
+	unsigned int flags;
+	int ok = 0;
+
+	if (!out)
+		return 0;
+	spin_lock_irqsave(&kbd_tap_lock, &flags);
+	if (kbd_tap_tail != kbd_tap_head) {
+		*out = kbd_tap[kbd_tap_tail];
+		kbd_tap_tail = (kbd_tap_tail + 1) % KBD_TAP_SIZE;
+		ok = 1;
+	}
+	spin_unlock_irqrestore(&kbd_tap_lock, flags);
+	return ok;
+}
+
 /* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
 static struct text_screen io_fallback;
 
@@ -274,8 +328,8 @@ static int kbd_repeat_poll(void)
 	return repeat_key;
 }
 
-/* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
-static int kbd_process_byte(unsigned char sc)
+/* Decode one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
+static int kbd_process_byte_inner(unsigned char sc)
 {
 	int key;
 
@@ -295,6 +349,13 @@ static int kbd_process_byte(unsigned char sc)
 			ctrl_pressed = 1;
 			return 0;
 		}
+		if (sc == 0x14 || sc == 0x15) {
+			/* Standard AT brightness down/up (E0 14 / E0 15);
+			 * ChromeOS EC action-key boards send these. */
+			if (backlight_present())
+				backlight_step(sc == 0x14 ? -1 : 1);
+			return 0;
+		}
 		if (sc == 0x4B)
 			key = ctrl_pressed ? IO_KEY_CTRL_LEFT : IO_KEY_LEFT;
 		else if (sc == 0x4D)
@@ -324,6 +385,14 @@ static int kbd_process_byte(unsigned char sc)
 		kbd_repeat_stop(0, sc & 0x7F);
 		return 0;
 	}
+	/* A ChromeOS EC keyboard sends the action-row brightness keys as plain
+	 * F6/F7 (the older standard table).  Only claim them when a Chrome EC
+	 * is present, so a regular Braswell laptop keeps its function keys. */
+	if ((sc == 0x40 || sc == 0x41) && backlight_present() &&
+	    cros_ec_present()) {
+		backlight_step(sc == 0x40 ? -1 : 1);
+		return 0;
+	}
 	if (sc == 0x2A || sc == 0x36) {
 		shift_pressed = 1;
 		return 0;
@@ -357,6 +426,25 @@ static int kbd_process_byte(unsigned char sc)
 	return 0;
 }
 
+/* Tap every byte after decoding, so `keytest` sees the raw wire sequence
+ * (including E0 prefixes, releases and keys the decoder ignores). */
+static int kbd_process_byte(unsigned char sc)
+{
+	unsigned char ext = (unsigned char)kbd_extended;
+	unsigned char kind;
+	int key;
+
+	key = kbd_process_byte_inner(sc);
+	if (sc == 0xE0)
+		kind = 2;
+	else if (sc & 0x80)
+		kind = 1;
+	else
+		kind = 0;
+	kbd_tap_push(sc, ext, kind, key);
+	return key;
+}
+
 int io_poll_key(void)
 {
 	unsigned char sc;

+ 12 - 0
io.h

@@ -46,6 +46,18 @@ int io_keyboard_irq_masked(void);
    it is then decoded by the normal io_poll_key path. */
 void io_inject_scancode(unsigned char sc);
 
+/* Raw scancode tap for diagnostics (the `keytest` kernel command).  Every
+   byte fed to the set-1 decoder is recorded with the E0-prefix state, the
+   make/break/prefix kind and the decoded key (0 when nothing is produced). */
+struct io_keytap {
+	unsigned char raw;
+	unsigned char ext;
+	unsigned char kind;      /* 0 make, 1 break, 2 0xE0 prefix */
+	int key;
+};
+void io_keytap_reset(void);
+int io_keytap_read(struct io_keytap *out);
+
 /* While set, the WM stops draining the keyboard ring: an inline command
    (e.g. `format`'s confirmation) is polling for keys itself.  The WM still
    runs and the USB backend still queues scancodes. */

+ 204 - 0
terminal.c

@@ -2,6 +2,7 @@
    Commands are spawned as separate processes for preemptive multitasking. */
 
 #include "io.h"
+#include "backlight.h"
 #include "power.h"
 #include "bootcon.h"
 #include "dbg.h"
@@ -67,7 +68,9 @@ static int cmd_idle(struct term *t, const char *args);
 static int cmd_cpufreq(struct term *t, const char *args);
 static int cmd_power(struct term *t, const char *args);
 static int cmd_kexec(struct term *t, const char *args);
+static int cmd_keytest(struct term *t, const char *args);
 static int cmd_kill(struct term *t, const char *args);
+static int cmd_backlight(struct term *t, const char *args);
 static int cmd_poweroff(struct term *t, const char *args);
 static int cmd_ps(struct term *t, const char *args);
 static int cmd_renice(struct term *t, const char *args);
@@ -84,6 +87,8 @@ static const struct kcmd cmds[] = {
 	{ "poweroff",   "poweroff",                      CK_CUSTOM, 0, 0, 0, 0, cmd_poweroff },
 	{ "display",    "display",                       CK_SPAWN, 0, program_display_main, 0, 0, 0 },
 	{ "gfxbench",   "gfxbench",                      CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
+	{ "backlight",  "backlight [0-100|up|down]",     CK_CUSTOM, 0, 0, 0, 0, cmd_backlight },
+	{ "keytest",    "keytest",                       CK_CUSTOM, 0, 0, 0, 0, cmd_keytest },
 	{ "lsusb",      "lsusb",                         CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
 	{ "touchpad",   "touchpad [rightclick|tapclick|scroll]", CK_SPAWN_ARG, 0, 0, program_touchpad_main, 0, 0 },
 	{ "netdev",     "netdev [name]",                 CK_SPAWN_ARG, 0, 0, program_netdev_main, 0, 0 },
@@ -404,6 +409,205 @@ static int cmd_power(struct term *t, const char *args)
 	return 0;
 }
 
+static void cmd_put_dec(unsigned int v)
+{
+	char b[12];
+	int i = 0;
+
+	if (!v) {
+		io_putchar('0');
+		return;
+	}
+	while (v) {
+		b[i++] = (char)('0' + v % 10);
+		v /= 10;
+	}
+	while (i)
+		io_putchar(b[--i]);
+}
+
+static void cmd_put_hex(unsigned int v, unsigned int digits)
+{
+	static const char hex[] = "0123456789ABCDEF";
+
+	while (digits) {
+		digits--;
+		io_putchar(hex[(v >> (digits * 4)) & 0xF]);
+	}
+}
+
+static const char *cmd_raw_name(unsigned char raw)
+{
+	switch (raw) {
+	case 0x3B: return "F1";
+	case 0x3C: return "F2";
+	case 0x3D: return "F3";
+	case 0x3E: return "F4";
+	case 0x3F: return "F5";
+	case 0x40: return "F6";
+	case 0x41: return "F7";
+	case 0x42: return "F8";
+	case 0x43: return "F9";
+	case 0x44: return "F10";
+	case 0x57: return "F11";
+	case 0x58: return "F12";
+	}
+	return 0;
+}
+
+static const char *cmd_key_name(int k)
+{
+	switch (k) {
+	case '\n':                 return "Enter";
+	case '\t':                 return "Tab";
+	case '\b':                 return "Backspace";
+	case 0x1B:                 return "Esc";
+	case IO_KEY_LEFT:          return "Left";
+	case IO_KEY_RIGHT:         return "Right";
+	case IO_KEY_UP:            return "Up";
+	case IO_KEY_DOWN:          return "Down";
+	case IO_KEY_PAGE_UP:       return "PageUp";
+	case IO_KEY_PAGE_DOWN:     return "PageDown";
+	case IO_KEY_HOME:          return "Home";
+	case IO_KEY_END:           return "End";
+	case IO_KEY_DELETE:        return "Delete";
+	case IO_KEY_CTRL_LEFT:     return "Ctrl+Left";
+	case IO_KEY_CTRL_RIGHT:    return "Ctrl+Right";
+	case IO_KEY_CTRL_DELETE:   return "Ctrl+Delete";
+	case IO_KEY_CTRL_BACKSPACE: return "Ctrl+Backspace";
+	}
+	return 0;
+}
+
+/* Print every raw scancode the decoder sees, including releases, E0
+ * prefixes and keys that decode to 0 (the brightness/action keys). */
+static int cmd_keytest(struct term *t, const char *args)
+{
+	struct io_keytap e;
+
+	(void)t;
+	(void)args;
+	io_keytap_reset();
+	io_putstr("keytest: press keys (Ctrl+C to stop; E0 = prefix)\n");
+	for (;;) {
+		while (io_keytap_read(&e)) {
+			const char *name;
+
+			io_putstr("raw ");
+			cmd_put_hex(e.raw, 2);
+			io_putstr(e.ext ? " ext" : "    ");
+			if (e.kind == 2)
+				io_putstr(" prefix");
+			else if (e.kind == 1)
+				io_putstr(" break ");
+			else
+				io_putstr(" make  ");
+			io_putstr(" -> 0x");
+			cmd_put_hex((unsigned int)e.key, 4);
+			name = e.kind == 0 ? cmd_key_name(e.key) : 0;
+			if (name) {
+				io_putstr(" (");
+				io_putstr(name);
+				io_putchar(')');
+			} else if (e.kind == 0 && e.key >= 0x20 && e.key <= 0x7E) {
+				io_putstr(" '");
+				io_putchar((char)e.key);
+				io_putstr("'");
+			} else if (e.kind == 0 && !e.ext) {
+				name = cmd_raw_name(e.raw);
+				if (name) {
+					io_putstr(" (");
+					io_putstr(name);
+					io_putchar(')');
+				}
+			}
+			io_putchar('\n');
+		}
+		process_sleep_ms(20);
+	}
+	return 0;
+}
+
+static void backlight_print(int probe)
+{
+	struct backlight_status st;
+
+	if (backlight_status(&st) != 0) {
+		io_putstr("backlight: no Intel CHV/VLV panel PWM\n");
+		return;
+	}
+	io_putstr("backlight: ");
+	cmd_put_dec((unsigned int)st.level);
+	io_putstr("% (pipe ");
+	io_putstr(st.pipe ? "B" : "A");
+	io_putstr(", period ");
+	cmd_put_dec(st.period);
+	io_putstr(", duty ");
+	cmd_put_dec(st.duty);
+	io_putstr(", ");
+	io_putstr(st.enabled ? "enabled" : "disabled");
+	io_putstr(")\n");
+	if (!probe)
+		return;
+	io_putstr("  bar 0x");
+	cmd_put_hex(st.bar, 8);
+	io_putstr("\n  ctl2 A=0x");
+	cmd_put_hex(st.ctl2[0], 8);
+	io_putstr(" B=0x");
+	cmd_put_hex(st.ctl2[1], 8);
+	io_putstr("\n  ctl  A=0x");
+	cmd_put_hex(st.ctl[0], 8);
+	io_putstr(" B=0x");
+	cmd_put_hex(st.ctl[1], 8);
+	io_putchar('\n');
+}
+
+static int cmd_backlight(struct term *t, const char *args)
+{
+	const char *p = args;
+
+	(void)t;
+	while (*p == ' ')
+		p++;
+	if (!*p) {
+		backlight_print(0);
+		return 0;
+	}
+	if (arg_is(p, "probe")) {
+		backlight_print(1);
+		return 0;
+	}
+	if (arg_is(p, "up")) {
+		backlight_step(1);
+		backlight_print(0);
+		return 0;
+	}
+	if (arg_is(p, "down")) {
+		backlight_step(-1);
+		backlight_print(0);
+		return 0;
+	}
+	{
+		unsigned int pct = 0;
+		int digits = 0;
+
+		while (*p >= '0' && *p <= '9') {
+			pct = pct * 10 + (unsigned int)(*p - '0');
+			p++;
+			digits = 1;
+		}
+		while (*p == ' ')
+			p++;
+		if (digits && !*p) {
+			backlight_set_percent((int)pct);
+			backlight_print(0);
+			return 0;
+		}
+	}
+	io_putstr("backlight: usage backlight [0-100|up|down|probe]\n");
+	return 0;
+}
+
 static int cmd_cpufreq(struct term *t, const char *args)
 {
 	(void)t;

+ 148 - 3
ucmd_wm.c

@@ -65,6 +65,19 @@ static int bar_pwr_tip_want;       /* tooltip computed for this frame */
 static int bar_pwr_tip_on;         /* tooltip pixels currently on screen */
 static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
 static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
+/* Brightness OSD: a centered bubble shown after the kernel's backlight
+ * seq changes, then hidden after OSD_MS. */
+#define OSD_MS    3000
+#define OSD_W     210
+#define OSD_H     60
+static int osd_show;               /* should be visible this frame */
+static int osd_visible;            /* pixels are on screen */
+static int osd_level;
+static unsigned int osd_until;
+static unsigned int osd_seen_seq;
+static int osd_seen_valid;
+static int osd_x, osd_y;
+static unsigned int osd_w, osd_h;
 static char bar_msg[24];
 static unsigned int bar_msg_until;
 static int bar_msg_dirty;
@@ -865,6 +878,98 @@ static void bar_tooltip_overlay(struct wm_present_info *p)
 		 bar_pwr_tip_on_w, bar_pwr_tip_on_h);
 }
 
+/* Center the brightness bubble in the work area, clamped on small screens. */
+static void osd_geom(void)
+{
+	int x, y;
+
+	osd_w = OSD_W;
+	osd_h = OSD_H;
+	x = ((int)fbw - (int)osd_w) / 2;
+	y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
+	if (x < 0)
+		x = 0;
+	if (x + (int)osd_w > (int)fbw)
+		x = (int)fbw - (int)osd_w;
+	if (y < WM_BAR_H)
+		y = WM_BAR_H;
+	if (y + (int)osd_h > (int)fbh)
+		y = (int)fbh - (int)osd_h;
+	osd_x = x;
+	osd_y = y;
+}
+
+/* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
+static void osd_sun(int cx, int cy)
+{
+	static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
+	int i;
+
+	for (i = -5; i <= 5; i++)
+		usurf_fill(&screen, cx - half[i + 5], cy + i,
+			   half[i + 5] * 2 + 1, 1, 0xFFD250u);
+	usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
+	usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
+	usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
+	usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
+}
+
+static void osd_draw(void)
+{
+	char pct[8];
+	unsigned int n = 0;
+	unsigned int bar_x, bar_y, bar_w;
+	int x, y, tw;
+
+	osd_geom();
+	x = osd_x;
+	y = osd_y;
+	usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
+	usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
+	usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
+	usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
+	usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
+		   0x70709Au);
+	osd_sun(x + 22, y + (int)osd_h / 2);
+
+	if (osd_level >= 100) {
+		pct[n++] = '1';
+		pct[n++] = '0';
+		pct[n++] = '0';
+	} else {
+		if (osd_level >= 10)
+			pct[n++] = (char)('0' + osd_level / 10);
+		pct[n++] = (char)('0' + osd_level % 10);
+	}
+	pct[n++] = '%';
+	pct[n] = '\0';
+
+	usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
+		   x, y, osd_w, osd_h);
+	tw = (int)usurf_text_width(&screen, pct);
+	usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
+		   0xFFD250u, x, y, osd_w, osd_h);
+
+	bar_x = (unsigned int)(x + 42);
+	bar_y = (unsigned int)(y + 35);
+	bar_w = osd_w - 42 - 14;
+	usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
+	usurf_fill(&screen, (int)bar_x, (int)bar_y,
+		   (bar_w * (unsigned int)osd_level + 50) / 100, 10,
+		   0xFFD250u);
+}
+
+/* Put the power tooltip and the brightness OSD back on top after a render
+ * pass that may have overwritten them. */
+static void wm_overlays(struct wm_present_info *p)
+{
+	bar_tooltip_overlay(p);
+	if (osd_show) {
+		osd_draw();
+		add_rect(p, osd_x, osd_y, osd_w, osd_h);
+	}
+}
+
 /* A focused window is topmost, so the shadow contains exactly its pixels in
  * the old rectangle.  Move that block and repaint only the old part the new
  * rectangle does not cover.  When either rectangle is partly off-screen the
@@ -904,7 +1009,7 @@ static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 	for (k = 0; k < np; k++)
 		add_rect(&present, pieces[k].x, pieces[k].y,
 			 pieces[k].w, pieces[k].h);
-	bar_tooltip_overlay(&present);
+	wm_overlays(&present);
 	u_wm_present(&present);
 }
 
@@ -928,7 +1033,7 @@ static void redraw_geom(struct uwin *u, int ox, int oy,
 		for (i = 0; i < present.count; i++)
 			draw_rect(present.rect[i].x, present.rect[i].y,
 				  present.rect[i].w, present.rect[i].h);
-		bar_tooltip_overlay(&present);
+		wm_overlays(&present);
 		u_wm_present(&present);
 	}
 }
@@ -1026,6 +1131,32 @@ static void do_update(struct wm_status *st)
 	int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
 		(st->kexec_busy && st->kexec_progress != bar_kexec_progress);
 	unsigned int msg_now = u_ticks_ms();
+	int osd_hide = 0, osd_repaint = 0;
+
+	/* The kernel bumps backlight_seq on every brightness key/command.
+	 * Pop the OSD on a change and keep it up for OSD_MS after the last
+	 * one.  The first poll only records the current sequence, so a WM
+	 * restart does not flash a stale bubble. */
+	if (st->backlight_present) {
+		if (!osd_seen_valid) {
+			osd_seen_valid = 1;
+			osd_seen_seq = st->backlight_seq;
+		} else if (st->backlight_seq != osd_seen_seq) {
+			osd_seen_seq = st->backlight_seq;
+			osd_level = (int)st->backlight_level;
+			osd_until = msg_now + OSD_MS;
+			osd_show = 1;
+		}
+	} else if (osd_show) {
+		osd_show = 0;
+		osd_hide = 1;
+	}
+	if (osd_show && (int)(msg_now - osd_until) >= 0) {
+		osd_show = 0;
+		osd_hide = 1;
+	}
+	if (osd_hide && osd_visible)
+		osd_repaint = 1;
 
 	/* Spawn the first terminal once the kernel says the storage scan is
 	 * done, so a shell command can never race the mount table. */
@@ -1226,6 +1357,9 @@ static void do_update(struct wm_status *st)
 			draw_rect(0, 0, fbw, fbh);
 			draw_bar();
 			bar_tooltip_commit();
+			if (osd_show)
+				osd_draw();
+			osd_visible = osd_show;
 			present.count = 0;
 			add_rect(&present, 0, 0, fbw, fbh);
 			present.immediate = 0;
@@ -1243,6 +1377,12 @@ static void do_update(struct wm_status *st)
 	/* Partial cell/canvas updates and the bar clock. */
 	present.count = 0;
 	present.immediate = 0;
+	if (osd_repaint) {
+		add_rect(&present, osd_x, osd_y, osd_w, osd_h);
+		need = 1;
+	}
+	if (osd_show)
+		need = 1;
 	if (bar_dirty) {
 		need = 1;
 		add_rect(&present, 0, 0, fbw, WM_BAR_H);
@@ -1295,6 +1435,11 @@ static void do_update(struct wm_status *st)
 	} else {
 		bar_pwr_tip_on = 0;
 	}
+	if (osd_show) {
+		osd_draw();
+		add_rect(&present, osd_x, osd_y, osd_w, osd_h);
+	}
+	osd_visible = osd_show;
 
 	for (i = 0; i < WM_MAX_WINDOWS; i++) {
 		struct uwin *u = &wins[i];
@@ -1546,7 +1691,7 @@ static void handle_mouse(struct wm_status *st)
 								  present.rect[i].y,
 								  present.rect[i].w,
 								  present.rect[i].h);
-							bar_tooltip_overlay(&present);
+							wm_overlays(&present);
 							u_wm_present(&present);
 						}
 					}

+ 8 - 0
wm.c

@@ -2,6 +2,7 @@
  * mapping/presentation and input routing.  There is no in-kernel WM. */
 
 #include "wm.h"
+#include "backlight.h"
 #include "bootcon.h"
 #include "io.h"
 #include "kexec.h"
@@ -930,6 +931,13 @@ int wm_server_poll(struct wm_status *out)
 	out->kexec_busy = kexec_busy ? 1 : 0;
 	out->kexec_progress = kexec_progress;
 	out->kexec_total = kexec_total;
+	{
+		int level = backlight_percent();
+
+		out->backlight_present = level >= 0 ? 1 : 0;
+		out->backlight_level = level > 0 ? (unsigned int)level : 0;
+		out->backlight_seq = backlight_seq();
+	}
 	wm_full_redraw = 0;
 	return 0;
 }

+ 5 - 0
wm_abi.h

@@ -89,6 +89,11 @@ struct wm_status {
 	unsigned int kexec_busy;
 	unsigned int kexec_progress;
 	unsigned int kexec_total;
+	/* Panel backlight, for the WM's brightness OSD.  seq changes on every
+	 * key/command adjustment; level is 0 when no PWM was found. */
+	unsigned int backlight_present;
+	unsigned int backlight_level;
+	unsigned int backlight_seq;
 };
 
 struct wm_window_info {

Некоторые файлы не были показаны из-за большого количества измененных файлов