Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 22 Sep 2011 16:52:39 -0400
From:      Justin Hibbits <jrh29@alumni.cwru.edu>
To:        Peter Grehan <grehan@freebsd.org>
Cc:        FreeBSD PowerPC ML <freebsd-ppc@freebsd.org>
Subject:   Re: ofw syscons brightness patch
Message-ID:  <F6ABF05F-795A-49A6-A13D-8D2C496F978F@alumni.cwru.edu>
In-Reply-To: <4E7AAA9D.9010503@freebsd.org>
References:  <91CC14C1-14F2-41B3-81F1-D90B42F038CA@alumni.cwru.edu> <4E7AAA9D.9010503@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail-11--884856648
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed;
	delsp=yes
Content-Transfer-Encoding: 7bit

On Sep 21, 2011, at 11:25 PM, Peter Grehan wrote:

> Hi Justin,
>
>> Attached is a patch that adds brightness control to ofw syscons
>> (dev.sc.0.brightness sysctl). Pretty simple. Comments and tests
>> requested. Hopefully the listserv doesn't eat the patch.
>
> Is it possible to have the value in the sysctl be 0-100, or 0-10 ?  
> It might be a bit confusing for the user to have to know that the  
> range is 0x34-0xff, and have the sysctl code clamp to these.
>
> Are the MIN_BRIGHTNESS/MAX_BRIGHTNESS values the same for all models ?
>
> later,
>
> Peter.


Patch take 2 - Range is 0-12.  Not sure how to present this to the  
user.  Anything above 12 is capped at 12.

- Justin

--Apple-Mail-11--884856648
Content-Disposition: attachment;
	filename=ofw_sc_brightness.diff
Content-Type: application/octet-stream; x-unix-mode=0644;
	name="ofw_sc_brightness.diff"
Content-Transfer-Encoding: 7bit

Index: sys/powerpc/ofw/ofw_syscons.c
===================================================================
--- sys/powerpc/ofw/ofw_syscons.c	(revision 225715)
+++ sys/powerpc/ofw/ofw_syscons.c	(working copy)
@@ -98,7 +98,11 @@
 static vi_putc_t ofwfb_putc;
 static vi_puts_t ofwfb_puts;
 static vi_putm_t ofwfb_putm;
+static int brightness;
 
+static void ofwfb_set_brightness(int);
+static int ofwfb_set_brightness_sc(SYSCTL_HANDLER_ARGS);
+
 static video_switch_t ofwfbvidsw = {
 	.probe			= ofwfb_probe,
 	.init			= ofwfb_init,
@@ -185,6 +189,7 @@
 static u_int16_t ofwfb_static_window[ROW*COL];
 
 static struct ofwfb_softc ofwfb_softc;
+static ihandle_t stdout_ih;
 
 static __inline int
 ofwfb_background(uint8_t attr)
@@ -399,7 +404,6 @@
 {
 	struct ofwfb_softc *sc;
 	char name[64];
-	ihandle_t ih;
 	int i, retval;
 
 	sc = (struct ofwfb_softc *)adp;
@@ -410,7 +414,7 @@
 
 	memset(name, 0, sizeof(name));
 	OF_package_to_path(sc->sc_node, name, sizeof(name));
-	ih = OF_open(name);
+	stdout_ih = OF_open(name);
 
 	if (sc->sc_depth == 8) {
 		/*
@@ -418,7 +422,7 @@
 		 * don't do this by default
 		 */
 		for (i = 0; i < 16; i++) {
-			OF_call_method("color!", ih, 4, 1,
+			OF_call_method("color!", stdout_ih, 4, 1,
 				       ofwfb_cmap[i].red,
 				       ofwfb_cmap[i].green,
 				       ofwfb_cmap[i].blue,
@@ -965,10 +969,66 @@
 static int
 ofwfb_scattach(device_t dev)
 {
+	char model[32];
+	struct sysctl_ctx_list *ctx;
+	struct sysctl_oid *tree;
+	phandle_t rootnode;
+
+	ctx = device_get_sysctl_ctx(dev);
+	tree = device_get_sysctl_tree(dev);
+
+	rootnode = OF_finddevice("/");
+
+	/* Brightness can only be adjusted on PowerBook/iBook
+	 * (maybe iMac, but can't confirm)
+	 */
+	if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
+		if (strncmp(model, "PowerBook",9) == 0) {
+			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
+					"brightness", CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
+					ofwfb_set_brightness_sc, "I", "Adjust the brightness of the screen");
+
+			ofwfb_set_brightness(DEFAULT_BRIGHTNESS);
+		}
+	}
+
 	return (sc_attach_unit(device_get_unit(dev),
 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
 }
 
+static void ofwfb_set_brightness(int newbright)
+{
+	if (newbright > MAX_BRIGHTNESS)
+		newbright = MAX_BRIGHTNESS;
+
+	/* Cache the new brightness level before scaling. */
+	brightness = newbright;
+
+	if (newbright > 0)
+	{
+		/* Scale by 17, so it's a range of 0-12.  Also, from OpenBSD's source,
+		 * 0x34 is the minimum brightness allowed before the backlight shuts off.
+		 */
+		newbright = newbright * 17 + 0x34;
+		if (newbright > 255)
+			newbright = 255;
+	}
+	OF_call_method("set-contrast", stdout_ih, 1, 0, newbright);
+}
+
+static int ofwfb_set_brightness_sc(SYSCTL_HANDLER_ARGS)
+{
+	int newbright = brightness;
+	int error;
+
+	error = sysctl_handle_int(oidp, &newbright, 0, req);
+
+	if (error || !req->newptr)
+		return error;
+	ofwfb_set_brightness(newbright);
+	return (0);
+}
+
 static device_method_t ofwfb_sc_methods[] = {
   	DEVMETHOD(device_identify,	ofwfb_scidentify),
 	DEVMETHOD(device_probe,		ofwfb_scprobe),
Index: sys/powerpc/ofw/ofw_syscons.h
===================================================================
--- sys/powerpc/ofw/ofw_syscons.h	(revision 225715)
+++ sys/powerpc/ofw/ofw_syscons.h	(working copy)
@@ -29,6 +29,10 @@
 #ifndef _OFW_SYSCONS_H_
 #define _OFW_SYSCONS_H_
 
+#define MIN_BRIGHTNESS	0
+#define MAX_BRIGHTNESS	12
+#define DEFAULT_BRIGHTNESS	6
+
 struct ofwfb_softc {
 	video_adapter_t	sc_va;
 	struct cdev *sc_si;

--Apple-Mail-11--884856648
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed
Content-Transfer-Encoding: 7bit



--Apple-Mail-11--884856648--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?F6ABF05F-795A-49A6-A13D-8D2C496F978F>