Home | History | Annotate | Line # | Download | only in adb
      1 /*	$NetBSD: adb_bus.c,v 1.14 2025/06/16 07:51:16 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_bus.c,v 1.14 2025/06/16 07:51:16 macallan Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/condvar.h>
     35 
     36 #include <machine/autoconf.h>
     37 #include <dev/adb/adbvar.h>
     38 
     39 #include "adbdebug.h"
     40 
     41 #ifdef ADB_DEBUG
     42 #define DPRINTF printf
     43 #else
     44 #define DPRINTF while (0) printf
     45 #endif
     46 
     47 static int nadb_match(device_t, cfdata_t, void *);
     48 static void nadb_attach(device_t, device_t, void *);
     49 
     50 struct nadb_softc {
     51 	device_t sc_dev;
     52 	struct adb_bus_accessops *sc_ops;
     53 	uint32_t sc_msg;
     54 	kcondvar_t sc_event;
     55 	kmutex_t sc_interlock;
     56 	struct adb_device sc_devtable[16];
     57 	int sc_free;	/* highest free address */
     58 	int sc_len;	/* length of received message */
     59 	uint8_t sc_data[16];
     60 };
     61 
     62 CFATTACH_DECL_NEW(nadb, sizeof(struct nadb_softc),
     63     nadb_match, nadb_attach, NULL, NULL);
     64 
     65 static void nadb_init(device_t);
     66 static void nadb_handler(void *, int, uint8_t *);
     67 static void nadb_send_sync(void *, int, int, uint8_t *);
     68 static int nadb_register(struct nadb_softc *, int, int, int);
     69 static void nadb_remove(struct nadb_softc *, int);
     70 static int nadb_devprint(void *, const char *);
     71 
     72 static int
     73 nadb_match(device_t parent, cfdata_t cf, void *aux)
     74 {
     75 
     76 	return 1;
     77 }
     78 
     79 static void
     80 nadb_attach(device_t parent, device_t self, void *aux)
     81 {
     82 	struct nadb_softc *sc = device_private(self);
     83 	struct adb_bus_accessops *ops = aux;
     84 
     85 	sc->sc_dev = self;
     86 	sc->sc_ops = ops;
     87 	sc->sc_ops->set_handler(sc->sc_ops->cookie, nadb_handler, sc);
     88 
     89 	mutex_init(&sc->sc_interlock, MUTEX_DEFAULT, IPL_NONE);
     90 	cv_init(&sc->sc_event, "adbbus");
     91 
     92 	config_interrupts(self, nadb_init);
     93 }
     94 
     95 static void
     96 nadb_init(device_t dev)
     97 {
     98 	struct nadb_softc *sc = device_private(dev);
     99 	struct adb_attach_args aaa;
    100 	int i, last_moved_up, devmask = 0;
    101 	uint8_t cmd[2];
    102 
    103 	sc->sc_free = 15;
    104 	for (i = 0; i < 16; i++) {
    105 		sc->sc_devtable[i].original_addr = 0;
    106 		sc->sc_devtable[i].current_addr = 0;
    107 		sc->sc_devtable[i].handler_id = 0;
    108 		sc->sc_devtable[i].cookie = NULL;
    109 		sc->sc_devtable[i].handler = NULL;
    110 	}
    111 
    112 	/* bus reset (?) */
    113 	nadb_send_sync(sc, 0, 0, NULL);
    114 	delay(200000);
    115 
    116 	/*
    117 	 * scan only addresses 1 - 7
    118 	 * if something responds move it to >7 and see if something else is
    119 	 * there. If not move the previous one back.
    120 	 * XXX we don't check for collisions if we use up all addresses >7
    121 	 */
    122 	for (i = 1; i < 8; i++) {
    123 		DPRINTF("\n%d: ", i);
    124 		last_moved_up = 0;
    125 		nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
    126 		/* found something? */
    127 		while (sc->sc_len > 2) {
    128 			/* something answered, so move it up */
    129 
    130 			DPRINTF("Found a device on address %d\n", i);
    131 			cmd[0] = sc->sc_free | 0x60;
    132 			cmd[1] = 0xfe;
    133 			nadb_send_sync(sc, ADBLISTEN(i, 3), 2, cmd);
    134 
    135 			/* see if it really moved */
    136 			nadb_send_sync(sc, ADBTALK(sc->sc_free, 3), 0, NULL);
    137 			if (sc->sc_len > 2) {
    138 				/* ok */
    139 				DPRINTF("moved it to %d\n", sc->sc_free);
    140 				nadb_register(sc, sc->sc_free, i, sc->sc_data[3]);
    141 				last_moved_up = sc->sc_free;
    142 				sc->sc_free--;
    143 			}
    144 			/* see if something else is there */
    145 			nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
    146 		}
    147 		if (last_moved_up != 0) {
    148 			/* move last one back to original address */
    149 			cmd[0] = i | 0x60;
    150 			cmd[1] = 0xfe;
    151 			nadb_send_sync(sc, ADBLISTEN(last_moved_up, 3), 2, cmd);
    152 
    153 			nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
    154 			if (sc->sc_len > 2) {
    155 				DPRINTF("moved %d back to %d\n", last_moved_up, i);
    156 				nadb_remove(sc, last_moved_up);
    157 				nadb_register(sc, i, i, sc->sc_data[3]);
    158 				sc->sc_free = last_moved_up;
    159 			}
    160 		}
    161 	}
    162 
    163 	/* now attach the buggers we've found */
    164 	aaa.ops = sc->sc_ops;
    165 	for (i = 0; i < 16; i++) {
    166 		if (sc->sc_devtable[i].current_addr != 0) {
    167 			DPRINTF("dev: %d %d %02x\n",
    168 			    sc->sc_devtable[i].current_addr,
    169 			    sc->sc_devtable[i].original_addr,
    170 			    sc->sc_devtable[i].handler_id);
    171 			aaa.dev = &sc->sc_devtable[i];
    172 			if (config_found(sc->sc_dev, &aaa, nadb_devprint,
    173 					 CFARGS_NONE)) {
    174 				devmask |= (1 << i);
    175 			} else {
    176 				aprint_normal(" not configured\n");
    177 			}
    178 		}
    179 	}
    180 	/* now enable autopolling */
    181 	DPRINTF("devmask: %04x\n", devmask);
    182 	sc->sc_ops->autopoll(sc->sc_ops->cookie, devmask);
    183 }
    184 
    185 int
    186 nadb_print(void *aux, const char *what)
    187 {
    188 	aprint_normal(": Apple Desktop Bus\n");
    189 	return 0;
    190 }
    191 
    192 static int
    193 nadb_devprint(void *aux, const char *what)
    194 {
    195 	struct adb_attach_args *aaa = aux;
    196 
    197 	if (what == NULL)
    198 		return 0;
    199 
    200 	switch(aaa->dev->original_addr) {
    201 		case ADBADDR_KBD:
    202 			aprint_normal("%s: ADB Keyboard", what);
    203 			break;
    204 		case ADBADDR_MS:
    205 			aprint_normal("%s: ADB relative pointing device", what);
    206 			break;
    207 		default:
    208 			aprint_normal("%s: something from address %d:%02x",
    209 			    what,
    210 			    aaa->dev->original_addr,
    211 			    aaa->dev->handler_id);
    212 			break;
    213 	}
    214 	return 0;
    215 }
    216 
    217 static void
    218 nadb_handler(void *cookie, int len, uint8_t *data)
    219 {
    220 	struct nadb_softc *sc = cookie;
    221 	struct adb_device *dev;
    222 	int addr;
    223 
    224 #ifdef ADB_DEBUG
    225 	int i;
    226 	printf("adb:");
    227 	for (i = 0; i < len; i++) {
    228 		printf(" %02x", data[i]);
    229 	}
    230 	printf("\n");
    231 #endif
    232 
    233 	addr = data[1] >> 4;
    234 	dev  = &sc->sc_devtable[addr];
    235 	if ((dev->current_addr != 0) && (dev->handler != NULL)) {
    236 
    237 		dev->handler(dev->cookie, len, data);
    238 	} else {
    239 		sc->sc_msg = 1;
    240 		sc->sc_len = len;
    241 		memcpy(sc->sc_data, data, len);
    242 		cv_signal(&sc->sc_event);
    243 	}
    244 }
    245 
    246 static void
    247 nadb_send_sync(void *cookie, int command, int len, uint8_t *data)
    248 {
    249 	struct nadb_softc *sc = cookie;
    250 
    251 	sc->sc_msg = 0;
    252 	mutex_enter(&sc->sc_interlock);
    253 	sc->sc_ops->send(sc->sc_ops->cookie, 0, command, len, data);
    254 	while (sc->sc_msg == 0) {
    255 		cv_timedwait(&sc->sc_event, &sc->sc_interlock, hz);
    256 	}
    257 	mutex_exit(&sc->sc_interlock);
    258 }
    259 
    260 static int
    261 nadb_register(struct nadb_softc *sc, int current, int orig, int handler)
    262 {
    263 	struct adb_device *dev;
    264 
    265 	if ((current > 0) && (current < 16)) {
    266 		dev = &sc->sc_devtable[current];
    267 		if (dev->current_addr != 0)
    268 			/* in use! */
    269 			return -1;
    270 		dev->current_addr = current;
    271 		dev->original_addr = orig;
    272 		dev->handler_id = handler;
    273 		return 0;
    274 	}
    275 	return -1;
    276 }
    277 
    278 static void
    279 nadb_remove(struct nadb_softc *sc, int addr)
    280 {
    281 
    282 	if ((addr > 0) && (addr < 16)) {
    283 		sc->sc_devtable[addr].current_addr = 0;
    284 		sc->sc_devtable[addr].original_addr = 0;
    285 		sc->sc_devtable[addr].handler_id = 0;
    286 	}
    287 }
    288