Home | History | Annotate | Line # | Download | only in adb
      1  1.10  macallan /*	$NetBSD: adb_bt.c,v 1.10 2025/06/16 08:00:50 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.10  macallan __KERNEL_RCSID(0, "$NetBSD: adb_bt.c,v 1.10 2025/06/16 08:00:50 macallan Exp $");
     31   1.1  macallan 
     32   1.1  macallan #include <sys/param.h>
     33   1.1  macallan #include <sys/device.h>
     34   1.1  macallan 
     35   1.1  macallan #include <machine/autoconf.h>
     36   1.1  macallan 
     37   1.1  macallan #include <dev/adb/adbvar.h>
     38   1.1  macallan 
     39   1.7  riastrad #include "ioconf.h"
     40   1.7  riastrad 
     41   1.1  macallan #include "opt_wsdisplay_compat.h"
     42   1.1  macallan #include "adbdebug.h"
     43   1.1  macallan 
     44   1.1  macallan #ifdef ADBBT_DEBUG
     45   1.1  macallan #define DPRINTF printf
     46   1.1  macallan #else
     47   1.1  macallan #define DPRINTF while (0) printf
     48   1.1  macallan #endif
     49   1.1  macallan 
     50   1.1  macallan #define BT_VOL_UP	0x06
     51   1.1  macallan #define BT_VOL_DOWN	0x07
     52   1.1  macallan #define BT_VOL_MUTE	0x08
     53   1.1  macallan #define BT_BRT_UP	0x09
     54   1.1  macallan #define BT_BRT_DOWN	0x0a
     55   1.1  macallan #define BT_EJECT	0x0b
     56   1.1  macallan #define BT_F7		0x0c
     57   1.1  macallan #define BT_NUMLOCK	0x7f
     58   1.1  macallan 
     59   1.2      matt static int adbbt_match(device_t, cfdata_t, void *);
     60   1.2      matt static void adbbt_attach(device_t, device_t, void *);
     61   1.1  macallan 
     62   1.1  macallan struct adbbt_softc {
     63   1.2      matt 	device_t sc_dev;
     64   1.1  macallan 	struct adb_device *sc_adbdev;
     65   1.1  macallan 	struct adb_bus_accessops *sc_ops;
     66   1.1  macallan 	uint8_t sc_us;
     67   1.1  macallan };
     68   1.1  macallan 
     69   1.1  macallan /* Driver definition. */
     70   1.2      matt CFATTACH_DECL_NEW(adbbt, sizeof(struct adbbt_softc),
     71   1.1  macallan     adbbt_match, adbbt_attach, NULL, NULL);
     72   1.1  macallan 
     73   1.1  macallan static void adbbt_handler(void *, int, uint8_t *);
     74   1.1  macallan 
     75   1.1  macallan static int
     76   1.2      matt adbbt_match(device_t parent, cfdata_t cf, void *aux)
     77   1.1  macallan {
     78   1.1  macallan 	struct adb_attach_args *aaa = aux;
     79   1.1  macallan 
     80   1.1  macallan 	if ((aaa->dev->original_addr == ADBADDR_MISC) &&
     81   1.1  macallan 	    (aaa->dev->handler_id == 0x1f))
     82   1.1  macallan 		return 100;
     83   1.1  macallan 	else
     84   1.1  macallan 		return 0;
     85   1.1  macallan }
     86   1.1  macallan 
     87   1.1  macallan static void
     88   1.2      matt adbbt_attach(device_t parent, device_t self, void *aux)
     89   1.1  macallan {
     90   1.2      matt 	struct adbbt_softc *sc = device_private(self);
     91   1.1  macallan 	struct adb_attach_args *aaa = aux;
     92   1.1  macallan 
     93   1.2      matt 	sc->sc_dev = self;
     94   1.1  macallan 	sc->sc_ops = aaa->ops;
     95   1.1  macallan 	sc->sc_adbdev = aaa->dev;
     96   1.1  macallan 	sc->sc_adbdev->cookie = sc;
     97   1.1  macallan 	sc->sc_adbdev->handler = adbbt_handler;
     98   1.1  macallan 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
     99   1.1  macallan 
    100   1.1  macallan 	printf(" addr %d: button device\n", sc->sc_adbdev->current_addr);
    101   1.1  macallan }
    102   1.1  macallan 
    103   1.1  macallan static void
    104   1.1  macallan adbbt_handler(void *cookie, int len, uint8_t *data)
    105   1.1  macallan {
    106   1.5  macallan 	/* struct adbbt_softc *sc = cookie; */
    107   1.5  macallan 	uint8_t k, scancode;
    108   1.1  macallan 
    109   1.1  macallan #ifdef ADBBT_DEBUG
    110   1.6  macallan 	struct adbbt_softc *sc = cookie;
    111   1.1  macallan 	int i;
    112   1.6  macallan 	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
    113   1.1  macallan 	for (i = 0; i < len; i++) {
    114   1.1  macallan 		printf(" %02x", data[i]);
    115   1.1  macallan 	}
    116   1.1  macallan 	printf("\n");
    117   1.1  macallan #endif
    118   1.1  macallan 	k = data[2];
    119   1.5  macallan 	scancode = ADBK_KEYVAL(k);
    120   1.5  macallan 	if ((scancode < 6) || (scancode > 0x0c))
    121   1.1  macallan 		return;
    122   1.1  macallan 
    123   1.5  macallan 	if (ADBK_PRESS(k)) {
    124   1.1  macallan 
    125   1.5  macallan 		switch (scancode) {
    126   1.5  macallan 			case BT_VOL_UP:
    127   1.5  macallan 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    128   1.5  macallan 				break;
    129   1.5  macallan 			case BT_VOL_DOWN:
    130   1.5  macallan 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    131   1.5  macallan 				break;
    132   1.5  macallan 			case BT_VOL_MUTE:
    133   1.5  macallan 				pmf_event_inject(NULL,
    134   1.5  macallan 				    PMFE_AUDIO_VOLUME_TOGGLE);
    135   1.5  macallan 				break;
    136   1.5  macallan 			case BT_BRT_UP:
    137   1.5  macallan 				pmf_event_inject(NULL,
    138   1.5  macallan 				    PMFE_DISPLAY_BRIGHTNESS_UP);
    139   1.5  macallan 				break;
    140   1.5  macallan 			case BT_BRT_DOWN:
    141   1.5  macallan 				pmf_event_inject(NULL,
    142   1.5  macallan 				    PMFE_DISPLAY_BRIGHTNESS_DOWN);
    143   1.5  macallan 				break;
    144   1.5  macallan 		}
    145   1.1  macallan 	}
    146   1.1  macallan }
    147