#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: adb_bt.c,v 1.10 2025/06/16 08:00:50 macallan Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <machine/autoconf.h>
#include <dev/adb/adbvar.h>
#include "ioconf.h"
#include "opt_wsdisplay_compat.h"
#include "adbdebug.h"
#ifdef ADBBT_DEBUG
#define DPRINTF printf
#else
#define DPRINTF while (0) printf
#endif
#define BT_VOL_UP 0x06
#define BT_VOL_DOWN 0x07
#define BT_VOL_MUTE 0x08
#define BT_BRT_UP 0x09
#define BT_BRT_DOWN 0x0a
#define BT_EJECT 0x0b
#define BT_F7 0x0c
#define BT_NUMLOCK 0x7f
static int adbbt_match(device_t, cfdata_t, void *);
static void adbbt_attach(device_t, device_t, void *);
struct adbbt_softc {
device_t sc_dev;
struct adb_device *sc_adbdev;
struct adb_bus_accessops *sc_ops;
uint8_t sc_us;
};
CFATTACH_DECL_NEW(adbbt, sizeof(struct adbbt_softc),
adbbt_match, adbbt_attach, NULL, NULL);
static void adbbt_handler(void *, int, uint8_t *);
static int
adbbt_match(device_t parent, cfdata_t cf, void *aux)
{
struct adb_attach_args *aaa = aux;
if ((aaa->dev->original_addr == ADBADDR_MISC) &&
(aaa->dev->handler_id == 0x1f))
return 100;
else
return 0;
}
static void
adbbt_attach(device_t parent, device_t self, void *aux)
{
struct adbbt_softc *sc = device_private(self);
struct adb_attach_args *aaa = aux;
sc->sc_dev = self;
sc->sc_ops = aaa->ops;
sc->sc_adbdev = aaa->dev;
sc->sc_adbdev->cookie = sc;
sc->sc_adbdev->handler = adbbt_handler;
sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
printf(" addr %d: button device\n", sc->sc_adbdev->current_addr);
}
static void
adbbt_handler(void *cookie, int len, uint8_t *data)
{
uint8_t k, scancode;
#ifdef ADBBT_DEBUG
struct adbbt_softc *sc = cookie;
int i;
printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
for (i = 0; i < len; i++) {
printf(" %02x", data[i]);
}
printf("\n");
#endif
k = data[2];
scancode = ADBK_KEYVAL(k);
if ((scancode < 6) || (scancode > 0x0c))
return;
if (ADBK_PRESS(k)) {
switch (scancode) {
case BT_VOL_UP:
pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
break;
case BT_VOL_DOWN:
pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
break;
case BT_VOL_MUTE:
pmf_event_inject(NULL,
PMFE_AUDIO_VOLUME_TOGGLE);
break;
case BT_BRT_UP:
pmf_event_inject(NULL,
PMFE_DISPLAY_BRIGHTNESS_UP);
break;
case BT_BRT_DOWN:
pmf_event_inject(NULL,
PMFE_DISPLAY_BRIGHTNESS_DOWN);
break;
}
}
}