Subject: port-i386/36852: Port of the aps driver from OpenBSD
To: None <port-i386-maintainer@netbsd.org, gnats-admin@netbsd.org,>
From: Pierre Pronchery <khorben@defora.org>
List: netbsd-bugs
Date: 08/27/2007 22:40:02
>Number: 36852
>Category: port-i386
>Synopsis: Port of the aps driver from OpenBSD
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: port-i386-maintainer
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Mon Aug 27 22:40:02 +0000 2007
>Originator: Pierre Pronchery
>Release: NetBSD 4.99.26
>Organization:
>Environment:
Architecture: x86_64
Machine: amd64
>Description:
Please find a port of the aps driver from OpenBSD here. It supports the accel-o-meter device found on ThinkPads laptops for a while now.
This was tested only on amd64, within the jmcneill-pm branch. Modifications to make it compile with -current should be trivial if necessary.
I could only test the results with envstat so far, and observe that at least the keyboard activity detection was functional. wmhdaps may be an interesting application to port to test the results, but please tell if you know of any other.
Hope this helps.
>How-To-Repeat:
n/a
>Fix:
Add this to sys/dev/isa/files.isa:
device aps
attach aps at isa with aps_isa
file dev/isa/aps.c aps
Add this to your kernel configuration:
aps* at isa? port 0x1600
And this is sys/dev/isa/aps.c:
/* $NetBSD$ */
/* $OpenBSD: aps.c,v 1.15 2007/05/19 19:14:11 tedu Exp $ */
/*
* Copyright (c) 2005 Jonathan Gray <jsg@openbsd.org>
*
* 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.
*/
/*
* A driver for the ThinkPad Active Protection System based on notes from
* http://www.almaden.ibm.com/cs/people/marksmith/tpaps.html
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/envsys.h>
#include <sys/callout.h>
#include <machine/bus.h>
#include <dev/sysmon/sysmonvar.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#if defined(APSDEBUG)
#define DPRINTF(x) do { printf x; } while (0)
#else
#define DPRINTF(x)
#endif
#define APS_ACCEL_STATE 0x04
#define APS_INIT 0x10
#define APS_STATE 0x11
#define APS_XACCEL 0x12
#define APS_YACCEL 0x14
#define APS_TEMP 0x16
#define APS_XVAR 0x17
#define APS_YVAR 0x19
#define APS_TEMP2 0x1b
#define APS_UNKNOWN 0x1c
#define APS_INPUT 0x1d
#define APS_CMD 0x1f
#define APS_STATE_NEWDATA 0x50
#define APS_CMD_START 0x01
#define APS_INPUT_KB (1 << 5)
#define APS_INPUT_MS (1 << 6)
#define APS_INPUT_LIDOPEN (1 << 7)
#define APS_ADDR_SIZE 0x1f
struct sensor_rec {
u_int8_t state;
u_int16_t x_accel;
u_int16_t y_accel;
u_int8_t temp1;
u_int16_t x_var;
u_int16_t y_var;
u_int8_t temp2;
u_int8_t unk;
u_int8_t input;
};
#define APS_NUM_SENSORS 9
#define APS_SENSOR_XACCEL 0
#define APS_SENSOR_YACCEL 1
#define APS_SENSOR_XVAR 2
#define APS_SENSOR_YVAR 3
#define APS_SENSOR_TEMP1 4
#define APS_SENSOR_TEMP2 5
#define APS_SENSOR_KBACT 6
#define APS_SENSOR_MSACT 7
#define APS_SENSOR_LIDOPEN 8
struct aps_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct sysmon_envsys sc_sysmon;
envsys_data_t sc_data[APS_NUM_SENSORS];
struct callout sc_callout;
void (*refresh_sensor_data)(struct aps_softc *);
struct sensor_rec aps_data;
void * sc_powerhook;
};
int aps_match(struct device *, struct cfdata *, void *);
void aps_attach(struct device *, struct device *, void *);
int aps_init(bus_space_tag_t, bus_space_handle_t);
u_int8_t aps_mem_read_1(bus_space_tag_t, bus_space_handle_t, int, u_int8_t);
int aps_read_data(struct sysmon_envsys *, struct envsys_data *edata);
void aps_refresh_sensor_data(struct aps_softc *sc);
void aps_refresh(void *);
void aps_power(int, void *);
CFATTACH_DECL(aps_isa, sizeof(struct aps_softc),
aps_match, aps_attach, NULL, NULL);
int
aps_match(struct device *parent, struct cfdata *match, void *aux)
{
struct isa_attach_args *ia = aux;
bus_space_tag_t iot;
bus_space_handle_t ioh;
int iobase, i;
u_int8_t cr;
/* Must supply an address */
if (ia->ia_nio < 1)
return 0;
if (ISA_DIRECT_CONFIG(ia))
return 0;
if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
return 0;
iot = ia->ia_iot;
iobase = ia->ia_io[0].ir_addr;
if (bus_space_map(iot, iobase, APS_ADDR_SIZE, 0, &ioh)) {
printf("aps: can't map i/o space\n");
return 0;
}
/* See if this machine has APS */
bus_space_write_1(iot, ioh, APS_INIT, 0x13);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
/* ask again as the X40 is slightly deaf in one ear */
bus_space_read_1(iot, ioh, APS_CMD);
bus_space_write_1(iot, ioh, APS_INIT, 0x13);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (!aps_mem_read_1(iot, ioh, APS_CMD, 0x00)) {
bus_space_unmap(iot, ioh, APS_ADDR_SIZE);
return 0;
}
/*
* Observed values from Linux driver:
* 0x01: T42
* 0x02: chip already initialised
* 0x03: T41
*/
for (i = 0; i < 10; i++) {
cr = bus_space_read_1(iot, ioh, APS_STATE);
if (cr > 0 && cr < 6)
break;
delay(5 * 1000);
}
bus_space_unmap(iot, ioh, APS_ADDR_SIZE);
printf("aps: state register 0x%x\n", cr);
if (cr < 1 || cr > 5) {
printf("aps0: unsupported state %d\n", cr);
return 0;
}
ia->ia_nio = 1;
ia->ia_io[0].ir_size = APS_ADDR_SIZE;
ia->ia_niomem = 0;
ia->ia_nirq = 0;
ia->ia_ndrq = 0;
return 1;
}
void
aps_attach(struct device *parent, struct device *self, void *aux)
{
struct aps_softc *sc = (void *)self;
struct isa_attach_args *ia = aux;
int iobase, i;
bus_space_tag_t iot;
bus_space_handle_t ioh;
iot = sc->sc_iot = ia->ia_iot;
iobase = ia->ia_io[0].ir_addr;
if (bus_space_map(iot, iobase, APS_ADDR_SIZE, 0, &sc->sc_ioh)) {
printf(": can't map i/o space\n");
return;
}
ioh = sc->sc_ioh;
printf("\n");
if (!aps_init(iot, ioh))
goto out;
/* Initialize sensors */
for (i = 0; i < APS_NUM_SENSORS; ++i) {
sc->sc_data[i].sensor = i;
sc->sc_data[i].state = ENVSYS_SVALID;
}
sc->sc_data[APS_SENSOR_XACCEL].units = ENVSYS_INTEGER;
snprintf(sc->sc_data[APS_SENSOR_XACCEL].desc,
sizeof(sc->sc_data[APS_SENSOR_XACCEL].desc), "%s %s",
sc->sc_dev.dv_xname, "X_ACCEL");
sc->sc_data[APS_SENSOR_YACCEL].units = ENVSYS_INTEGER;
snprintf(sc->sc_data[APS_SENSOR_YACCEL].desc,
sizeof(sc->sc_data[APS_SENSOR_YACCEL].desc), "%s %s",
sc->sc_dev.dv_xname, "Y_ACCEL");
sc->sc_data[APS_SENSOR_TEMP1].units = ENVSYS_STEMP;
snprintf(sc->sc_data[APS_SENSOR_TEMP1].desc,
sizeof(sc->sc_data[APS_SENSOR_TEMP1].desc), "%s %s",
sc->sc_dev.dv_xname, "TEMP_1");
sc->sc_data[APS_SENSOR_TEMP2].units = ENVSYS_STEMP;
snprintf(sc->sc_data[APS_SENSOR_TEMP2].desc,
sizeof(sc->sc_data[APS_SENSOR_TEMP2].desc), "%s %s",
sc->sc_dev.dv_xname, "TEMP_2");
sc->sc_data[APS_SENSOR_XVAR].units = ENVSYS_INTEGER;
snprintf(sc->sc_data[APS_SENSOR_XVAR].desc,
sizeof(sc->sc_data[APS_SENSOR_XVAR].desc), "%s %s",
sc->sc_dev.dv_xname, "X_VAR");
sc->sc_data[APS_SENSOR_YVAR].units = ENVSYS_INTEGER;
snprintf(sc->sc_data[APS_SENSOR_YVAR].desc,
sizeof(sc->sc_data[APS_SENSOR_YVAR].desc), "%s %s",
sc->sc_dev.dv_xname, "Y_VAR");
sc->sc_data[APS_SENSOR_KBACT].units = ENVSYS_INDICATOR;
snprintf(sc->sc_data[APS_SENSOR_KBACT].desc,
sizeof(sc->sc_data[APS_SENSOR_KBACT].desc), "Keyboard Active");
sc->sc_data[APS_SENSOR_MSACT].units = ENVSYS_INDICATOR;
snprintf(sc->sc_data[APS_SENSOR_MSACT].desc,
sizeof(sc->sc_data[APS_SENSOR_MSACT].desc), "Mouse Active");
sc->sc_data[APS_SENSOR_LIDOPEN].units = ENVSYS_INDICATOR;
snprintf(sc->sc_data[APS_SENSOR_LIDOPEN].desc,
sizeof(sc->sc_data[APS_SENSOR_LIDOPEN].desc), "Lid Open");
/* stop hiding and report to the authorities */
sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
sc->sc_sysmon.sme_sensor_data = sc->sc_data;
sc->sc_sysmon.sme_cookie = sc;
sc->sc_sysmon.sme_gtredata = aps_read_data;
sc->sc_sysmon.sme_nsensors = APS_NUM_SENSORS;
if ((i = sysmon_envsys_register(&sc->sc_sysmon)))
printf("%s: unable to register with sysmon (%d)\n",
sc->sc_dev.dv_xname, i);
sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname, aps_power,
sc);
if(sc->sc_powerhook == NULL)
printf("%s: can't establish powerhook\n",
sc->sc_dev.dv_xname);
/* Refresh sensor data every 0.5 seconds */
callout_init(&sc->sc_callout, 0);
callout_setfunc(&sc->sc_callout, aps_refresh, sc);
callout_schedule(&sc->sc_callout, (hz) / 2);
return;
out:
printf("%s: failed to initialise\n", sc->sc_dev.dv_xname);
return;
}
int
aps_init(bus_space_tag_t iot, bus_space_handle_t ioh)
{
bus_space_write_1(iot, ioh, APS_INIT, 0x17);
bus_space_write_1(iot, ioh, APS_STATE, 0x81);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (!aps_mem_read_1(iot, ioh, APS_CMD, 0x00))
return (0);
if (!aps_mem_read_1(iot, ioh, APS_STATE, 0x00))
return (0);
if (!aps_mem_read_1(iot, ioh, APS_XACCEL, 0x60))
return (0);
if (!aps_mem_read_1(iot, ioh, APS_XACCEL + 1, 0x00))
return (0);
bus_space_write_1(iot, ioh, APS_INIT, 0x14);
bus_space_write_1(iot, ioh, APS_STATE, 0x01);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (!aps_mem_read_1(iot, ioh, APS_CMD, 0x00))
return (0);
bus_space_write_1(iot, ioh, APS_INIT, 0x10);
bus_space_write_1(iot, ioh, APS_STATE, 0xc8);
bus_space_write_1(iot, ioh, APS_XACCEL, 0x00);
bus_space_write_1(iot, ioh, APS_XACCEL + 1, 0x02);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (!aps_mem_read_1(iot, ioh, APS_CMD, 0x00))
return (0);
/* refresh data */
bus_space_write_1(iot, ioh, APS_INIT, 0x11);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (!aps_mem_read_1(iot, ioh, APS_ACCEL_STATE, 0x50))
return (0);
if (!aps_mem_read_1(iot, ioh, APS_STATE, 0x00))
return (0);
return (1);
}
u_int8_t
aps_mem_read_1(bus_space_tag_t iot, bus_space_handle_t ioh, int reg,
u_int8_t val)
{
int i;
u_int8_t cr;
/* should take no longer than 50 microseconds */
for (i = 0; i < 10; i++) {
cr = bus_space_read_1(iot, ioh, reg);
if (cr == val)
return (1);
delay(5 * 1000);
}
printf("aps: reg 0x%x not val 0x%x!\n", reg, val);
return (0);
}
int
aps_read_data(struct sysmon_envsys *sme, struct envsys_data *edata)
{
struct aps_softc *sc = sme->sme_cookie;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
sc->aps_data.state = bus_space_read_1(iot, ioh, APS_STATE);
sc->aps_data.x_accel = bus_space_read_2(iot, ioh, APS_XACCEL);
sc->aps_data.y_accel = bus_space_read_2(iot, ioh, APS_YACCEL);
sc->aps_data.temp1 = bus_space_read_1(iot, ioh, APS_TEMP);
sc->aps_data.x_var = bus_space_read_2(iot, ioh, APS_XVAR);
sc->aps_data.y_var = bus_space_read_2(iot, ioh, APS_YVAR);
sc->aps_data.temp2 = bus_space_read_1(iot, ioh, APS_TEMP2);
sc->aps_data.input = bus_space_read_1(iot, ioh, APS_INPUT);
return (0);
}
void
aps_refresh_sensor_data(struct aps_softc *sc)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
int64_t temp;
int i;
/* ask for new data */
bus_space_write_1(iot, ioh, APS_INIT, 0x11);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (!aps_mem_read_1(iot, ioh, APS_ACCEL_STATE, 0x50))
return;
aps_read_data(&sc->sc_sysmon, NULL);
bus_space_write_1(iot, ioh, APS_INIT, 0x11);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
/* tell accelerometer we're done reading from it */
bus_space_read_1(iot, ioh, APS_CMD);
bus_space_read_1(iot, ioh, APS_ACCEL_STATE);
for (i = 0; i < APS_NUM_SENSORS; i++) {
sc->sc_data[i].flags &= ~ENVSYS_FNOTVALID;
}
sc->sc_data[APS_SENSOR_XACCEL].value_cur = sc->aps_data.x_accel;
sc->sc_data[APS_SENSOR_YACCEL].value_cur = sc->aps_data.y_accel;
/* convert to micro (mu) degrees */
temp = sc->aps_data.temp1 * 1000000;
/* convert to kelvin */
temp += 273150000;
sc->sc_data[APS_SENSOR_TEMP1].value_cur = temp;
/* convert to micro (mu) degrees */
temp = sc->aps_data.temp2 * 1000000;
/* convert to kelvin */
temp += 273150000;
sc->sc_data[APS_SENSOR_TEMP2].value_cur = temp;
sc->sc_data[APS_SENSOR_XVAR].value_cur = sc->aps_data.x_var;
sc->sc_data[APS_SENSOR_YVAR].value_cur = sc->aps_data.y_var;
sc->sc_data[APS_SENSOR_KBACT].value_cur =
(sc->aps_data.input & APS_INPUT_KB) ? 1 : 0;
sc->sc_data[APS_SENSOR_MSACT].value_cur =
(sc->aps_data.input & APS_INPUT_MS) ? 1 : 0;
sc->sc_data[APS_SENSOR_LIDOPEN].value_cur =
(sc->aps_data.input & APS_INPUT_LIDOPEN) ? 1 : 0;
}
void
aps_refresh(void *arg)
{
struct aps_softc *sc = (struct aps_softc *)arg;
aps_refresh_sensor_data(sc);
callout_schedule(&sc->sc_callout, (hz) / 2);
}
void
aps_power(int why, void *arg)
{
struct aps_softc *sc = (struct aps_softc *)arg;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
if (why != PWR_RESUME) {
callout_stop(&sc->sc_callout);
} else {
/*
* Redo the init sequence on resume, because APS is
* as forgetful as it is deaf.
*/
bus_space_write_1(iot, ioh, APS_INIT, 0x13);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
bus_space_read_1(iot, ioh, APS_CMD);
bus_space_write_1(iot, ioh, APS_INIT, 0x13);
bus_space_write_1(iot, ioh, APS_CMD, 0x01);
if (aps_mem_read_1(iot, ioh, APS_CMD, 0x00) &&
aps_init(iot, ioh))
callout_schedule(&sc->sc_callout, (hz) / 2);
else
printf("aps: failed to wake up\n");
}
}