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