Home | History | Annotate | Line # | Download | only in adb
      1 /*	$NetBSD: adb_bt.c,v 1.10 2025/06/16 08:00:50 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: adb_bt.c,v 1.10 2025/06/16 08:00:50 macallan Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 
     35 #include <machine/autoconf.h>
     36 
     37 #include <dev/adb/adbvar.h>
     38 
     39 #include "ioconf.h"
     40 
     41 #include "opt_wsdisplay_compat.h"
     42 #include "adbdebug.h"
     43 
     44 #ifdef ADBBT_DEBUG
     45 #define DPRINTF printf
     46 #else
     47 #define DPRINTF while (0) printf
     48 #endif
     49 
     50 #define BT_VOL_UP	0x06
     51 #define BT_VOL_DOWN	0x07
     52 #define BT_VOL_MUTE	0x08
     53 #define BT_BRT_UP	0x09
     54 #define BT_BRT_DOWN	0x0a
     55 #define BT_EJECT	0x0b
     56 #define BT_F7		0x0c
     57 #define BT_NUMLOCK	0x7f
     58 
     59 static int adbbt_match(device_t, cfdata_t, void *);
     60 static void adbbt_attach(device_t, device_t, void *);
     61 
     62 struct adbbt_softc {
     63 	device_t sc_dev;
     64 	struct adb_device *sc_adbdev;
     65 	struct adb_bus_accessops *sc_ops;
     66 	uint8_t sc_us;
     67 };
     68 
     69 /* Driver definition. */
     70 CFATTACH_DECL_NEW(adbbt, sizeof(struct adbbt_softc),
     71     adbbt_match, adbbt_attach, NULL, NULL);
     72 
     73 static void adbbt_handler(void *, int, uint8_t *);
     74 
     75 static int
     76 adbbt_match(device_t parent, cfdata_t cf, void *aux)
     77 {
     78 	struct adb_attach_args *aaa = aux;
     79 
     80 	if ((aaa->dev->original_addr == ADBADDR_MISC) &&
     81 	    (aaa->dev->handler_id == 0x1f))
     82 		return 100;
     83 	else
     84 		return 0;
     85 }
     86 
     87 static void
     88 adbbt_attach(device_t parent, device_t self, void *aux)
     89 {
     90 	struct adbbt_softc *sc = device_private(self);
     91 	struct adb_attach_args *aaa = aux;
     92 
     93 	sc->sc_dev = self;
     94 	sc->sc_ops = aaa->ops;
     95 	sc->sc_adbdev = aaa->dev;
     96 	sc->sc_adbdev->cookie = sc;
     97 	sc->sc_adbdev->handler = adbbt_handler;
     98 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
     99 
    100 	printf(" addr %d: button device\n", sc->sc_adbdev->current_addr);
    101 }
    102 
    103 static void
    104 adbbt_handler(void *cookie, int len, uint8_t *data)
    105 {
    106 	/* struct adbbt_softc *sc = cookie; */
    107 	uint8_t k, scancode;
    108 
    109 #ifdef ADBBT_DEBUG
    110 	struct adbbt_softc *sc = cookie;
    111 	int i;
    112 	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
    113 	for (i = 0; i < len; i++) {
    114 		printf(" %02x", data[i]);
    115 	}
    116 	printf("\n");
    117 #endif
    118 	k = data[2];
    119 	scancode = ADBK_KEYVAL(k);
    120 	if ((scancode < 6) || (scancode > 0x0c))
    121 		return;
    122 
    123 	if (ADBK_PRESS(k)) {
    124 
    125 		switch (scancode) {
    126 			case BT_VOL_UP:
    127 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    128 				break;
    129 			case BT_VOL_DOWN:
    130 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    131 				break;
    132 			case BT_VOL_MUTE:
    133 				pmf_event_inject(NULL,
    134 				    PMFE_AUDIO_VOLUME_TOGGLE);
    135 				break;
    136 			case BT_BRT_UP:
    137 				pmf_event_inject(NULL,
    138 				    PMFE_DISPLAY_BRIGHTNESS_UP);
    139 				break;
    140 			case BT_BRT_DOWN:
    141 				pmf_event_inject(NULL,
    142 				    PMFE_DISPLAY_BRIGHTNESS_DOWN);
    143 				break;
    144 		}
    145 	}
    146 }
    147