Home | History | Annotate | Line # | Download | only in dev
adb.c revision 1.61
      1 /*	$NetBSD: adb.c,v 1.61 2024/09/18 01:29:02 nat Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1994	Bradley A. Grantham
      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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.61 2024/09/18 01:29:02 nat Exp $");
     30 
     31 #include "opt_adb.h"
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/fcntl.h>
     36 #include <sys/poll.h>
     37 #include <sys/select.h>
     38 #include <sys/proc.h>
     39 #include <sys/signalvar.h>
     40 #include <sys/systm.h>
     41 #include <sys/cpu.h>
     42 #include <sys/intr.h>
     43 
     44 #include <machine/autoconf.h>
     45 
     46 #include <mac68k/mac68k/macrom.h>
     47 #include <mac68k/dev/adbvar.h>
     48 #include <mac68k/dev/akbdvar.h>
     49 
     50 #include "aed.h"		/* ADB Event Device for compatibility */
     51 
     52 /*
     53  * Function declarations.
     54  */
     55 static int	adbmatch(device_t, cfdata_t, void *);
     56 static void	adbattach(device_t, device_t, void *);
     57 static int	adbprint(void *, const char *);
     58 void		adb_config_interrupts(device_t);
     59 
     60 extern void	adb_jadbproc(void);
     61 
     62 /*
     63  * Global variables.
     64  */
     65 int	adb_polling = 0;	/* Are we polling?  (Debugger mode) */
     66 #ifdef ADB_DEBUG
     67 int	adb_debug = 0;		/* Output debugging messages */
     68 #endif /* ADB_DEBUG */
     69 
     70 extern struct	mac68k_machine_S mac68k_machine;
     71 extern int	adbHardware;
     72 extern char	*adbHardwareDescr[];
     73 
     74 /*
     75  * Driver definition.
     76  */
     77 CFATTACH_DECL_NEW(adb, 0,
     78     adbmatch, adbattach, NULL, NULL);
     79 
     80 static int
     81 adbmatch(device_t parent, cfdata_t cf, void *aux)
     82 {
     83 	static bool adb_matched;
     84 
     85 	/* Allow only one instance. */
     86 	if (adb_matched)
     87 		return (0);
     88 
     89 	adb_matched = true;
     90 	return (1);
     91 }
     92 
     93 static void
     94 adbattach(device_t parent, device_t self, void *aux)
     95 {
     96 
     97 	switch (mac68k_machine.machineid) {
     98 		case MACH_MACPB140:
     99 		case MACH_MACPB145:
    100 		case MACH_MACPB160:
    101 		case MACH_MACPB165:
    102 		case MACH_MACPB165C:
    103 		case MACH_MACPB170:
    104 		case MACH_MACPB180:
    105 		case MACH_MACPB180C:
    106 		case MACH_MACPB150:
    107 		case MACH_MACPB210:
    108 		case MACH_MACPB230:
    109 		case MACH_MACPB250:
    110 		case MACH_MACPB270:
    111 		case MACH_MACPB280:
    112 		case MACH_MACPB280C:
    113 			/* Reqired on machines with in-built trackballs. */
    114 			printf("...  Waiting 5 seconds for adb devices to "
    115 			     "settle.");
    116 			kpause("adb-slp", false, mstohz(5000), NULL);
    117 			break;
    118 		default:
    119 			break;
    120 	}
    121 
    122 	adb_softintr_cookie = softint_establish(SOFTINT_SERIAL,
    123 	    (void (*)(void *))adb_soft_intr, NULL);
    124 	printf("\n");
    125 
    126 	/*
    127 	 * Defer configuration until interrupts are enabled.
    128 	 */
    129 	config_interrupts(self, adb_config_interrupts);
    130 }
    131 
    132 void
    133 adb_config_interrupts(device_t self)
    134 {
    135 	ADBDataBlock adbdata;
    136 	struct adb_attach_args aa_args;
    137 	int totaladbs;
    138 	int adbindex, adbaddr;
    139 
    140 	printf("%s", device_xname(self));
    141 	adb_polling = 1;
    142 
    143 #ifdef MRG_ADB
    144 	if (!mrg_romready()) {
    145 		printf(": no ROM ADB driver in this kernel for this machine\n");
    146 		return;
    147 	}
    148 
    149 #ifdef ADB_DEBUG
    150 	if (adb_debug)
    151 		printf("adb: call mrg_initadbintr\n");
    152 #endif
    153 
    154 	mrg_initadbintr();	/* Mac ROM Glue okay to do ROM intr */
    155 #ifdef ADB_DEBUG
    156 	if (adb_debug)
    157 		printf("adb: returned from mrg_initadbintr\n");
    158 #endif
    159 
    160 	/* ADBReInit pre/post-processing */
    161 	JADBProc = adb_jadbproc;
    162 
    163 	/* Initialize ADB */
    164 #ifdef ADB_DEBUG
    165 	if (adb_debug)
    166 		printf("adb: calling ADBAlternateInit.\n");
    167 #endif
    168 
    169 	printf(" (mrg)");
    170 	ADBAlternateInit();
    171 #else
    172 	ADBReInit();
    173 	printf(" (direct, %s)", adbHardwareDescr[adbHardware]);
    174 #endif /* MRG_ADB */
    175 
    176 #ifdef ADB_DEBUG
    177 	if (adb_debug)
    178 		printf("adb: done with ADBReInit\n");
    179 #endif
    180 
    181 	totaladbs = CountADBs();
    182 
    183 	printf(": %d target%s\n", totaladbs, (totaladbs == 1) ? "" : "s");
    184 
    185 #if NAED > 0
    186 	/* ADB event device for compatibility */
    187 	aa_args.origaddr = 0;
    188 	aa_args.adbaddr = 0;
    189 	aa_args.handler_id = 0;
    190 	(void)config_found(self, &aa_args, adbprint, CFARGS_NONE);
    191 #endif
    192 
    193 	/* for each ADB device */
    194 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
    195 		/* Get the ADB information */
    196 		adbaddr = GetIndADB(&adbdata, adbindex);
    197 
    198 		aa_args.origaddr = (int)(adbdata.origADBAddr);
    199 		aa_args.adbaddr = adbaddr;
    200 		aa_args.handler_id = (int)(adbdata.devType);
    201 
    202 		(void)config_found(self, &aa_args, adbprint, CFARGS_NONE);
    203 	}
    204 	adb_polling = 0;
    205 }
    206 
    207 
    208 int
    209 adbprint(void *args, const char *name)
    210 {
    211 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
    212 	int rv = UNCONF;
    213 
    214 	if (name) {	/* no configured device matched */
    215 		rv = UNSUPP; /* most ADB device types are unsupported */
    216 
    217 		/* print out what kind of ADB device we have found */
    218 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
    219 		switch(aa_args->origaddr) {
    220 		case 0:
    221 #ifdef DIAGNOSTIC
    222 			aprint_normal("ADB event device");
    223 #endif
    224 			rv = UNCONF;
    225 			break;
    226 #ifdef DIAGNOSTIC
    227 		case ADBADDR_SECURE:
    228 			aprint_normal("security dongle (%d)",
    229 			    aa_args->handler_id);
    230 			break;
    231 #endif
    232 		case ADBADDR_MAP:
    233 			aprint_normal("mapped device (%d)",
    234 			    aa_args->handler_id);
    235 			rv = UNCONF;
    236 			break;
    237 		case ADBADDR_REL:
    238 			aprint_normal("relative positioning device (%d)",
    239 			    aa_args->handler_id);
    240 			rv = UNCONF;
    241 			break;
    242 #ifdef DIAGNOSTIC
    243 		case ADBADDR_ABS:
    244 			switch (aa_args->handler_id) {
    245 			case ADB_ARTPAD:
    246 				aprint_normal("WACOM ArtPad II");
    247 				break;
    248 			default:
    249 				aprint_normal("absolute positioning device (%d)",
    250 				    aa_args->handler_id);
    251 				break;
    252 			}
    253 			break;
    254 		case ADBADDR_DATATX:
    255 			aprint_normal("data transfer device (modem?) (%d)",
    256 			    aa_args->handler_id);
    257 			break;
    258 		case ADBADDR_MISC:
    259 			switch (aa_args->handler_id) {
    260 			case ADB_POWERKEY:
    261 				aprint_normal("Sophisticated Circuits PowerKey");
    262 				break;
    263 			default:
    264 				aprint_normal("misc. device (remote control?) (%d)",
    265 				    aa_args->handler_id);
    266 				break;
    267 			}
    268 			break;
    269 		default:
    270 			aprint_normal("unknown type device, (handler %d)",
    271 			    aa_args->handler_id);
    272 			break;
    273 #endif /* DIAGNOSTIC */
    274 		}
    275 	} else		/* a device matched and was configured */
    276 		aprint_normal(" addr %d: ", aa_args->adbaddr);
    277 
    278 	return (rv);
    279 }
    280 
    281 
    282 /*
    283  * adb_op_sync
    284  *
    285  * This routine does exactly what the adb_op routine does, except that after
    286  * the adb_op is called, it waits until the return value is present before
    287  * returning.
    288  *
    289  * NOTE: The user specified compRout is ignored, since this routine specifies
    290  * its own to adb_op, which is why you really called this in the first place
    291  * anyway.
    292  */
    293 int
    294 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
    295 {
    296 	int result;
    297 	volatile int flag = 0;
    298 
    299 	result = ADBOp(buffer, (void *)adb_op_comprout, __UNVOLATILE(&flag),
    300 	    command);	/* send command */
    301 	if (result == 0) {		/* send ok? */
    302 		adb_spin(&flag);
    303 		if (!flag)
    304 			result = -2;
    305 	}
    306 
    307 	return result;
    308 }
    309 
    310 /*
    311  * adb_spin
    312  *
    313  * Implements a spin-wait with timeout to be used for synchronous
    314  * operations on the ADB bus.
    315  *
    316  * Total time to wait is calculated as follows:
    317  *  - Tlt (stop to start time): 260 usec
    318  *  - start bit: 100 usec
    319  *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
    320  *  - stop bit (with SRQ): 140 usec
    321  * Total: 6900 usec
    322  *
    323  * This is the total time allowed by the specification.  Any device that
    324  * doesn't conform to this will fail to operate properly on some Apple
    325  * systems.  In spite of this we double the time to wait; Cuda-based
    326  * systems apparently queue commands and allow the main CPU to continue
    327  * processing (how radical!).  To be safe, allow time for two complete
    328  * ADB transactions to occur.
    329  */
    330 void
    331 adb_spin(volatile int *fp)
    332 {
    333 	int tmout;
    334 
    335 	for (tmout = 13800; *fp == 0 && tmout >= 10; tmout -= 10)
    336 		delay(10);
    337 	if (*fp == 0 && tmout > 0)
    338 		delay(tmout);
    339 }
    340 
    341 
    342 /*
    343  * adb_op_comprout
    344  *
    345  * This function is used by the adb_op_sync routine so it knows when the
    346  * function is done.
    347  */
    348 void
    349 adb_op_comprout(void)
    350 {
    351 	__asm("movw	#1,%a2@			| update flag value");
    352 }
    353