Home | History | Annotate | Line # | Download | only in dev
adb.c revision 1.57
      1 /*	$NetBSD: adb.c,v 1.57 2018/10/19 18:21:55 martin 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.57 2018/10/19 18:21:55 martin 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 	adb_softintr_cookie = softint_establish(SOFTINT_SERIAL,
     98 	    (void (*)(void *))adb_soft_intr, NULL);
     99 	printf("\n");
    100 
    101 	/*
    102 	 * Defer configuration until interrupts are enabled.
    103 	 */
    104 	config_interrupts(self, adb_config_interrupts);
    105 }
    106 
    107 void
    108 adb_config_interrupts(device_t self)
    109 {
    110 	ADBDataBlock adbdata;
    111 	struct adb_attach_args aa_args;
    112 	int totaladbs;
    113 	int adbindex, adbaddr;
    114 
    115 	printf("%s", device_xname(self));
    116 	adb_polling = 1;
    117 
    118 #ifdef MRG_ADB
    119 	if (!mrg_romready()) {
    120 		printf(": no ROM ADB driver in this kernel for this machine\n");
    121 		return;
    122 	}
    123 
    124 #ifdef ADB_DEBUG
    125 	if (adb_debug)
    126 		printf("adb: call mrg_initadbintr\n");
    127 #endif
    128 
    129 	mrg_initadbintr();	/* Mac ROM Glue okay to do ROM intr */
    130 #ifdef ADB_DEBUG
    131 	if (adb_debug)
    132 		printf("adb: returned from mrg_initadbintr\n");
    133 #endif
    134 
    135 	/* ADBReInit pre/post-processing */
    136 	JADBProc = adb_jadbproc;
    137 
    138 	/* Initialize ADB */
    139 #ifdef ADB_DEBUG
    140 	if (adb_debug)
    141 		printf("adb: calling ADBAlternateInit.\n");
    142 #endif
    143 
    144 	printf(" (mrg)");
    145 	ADBAlternateInit();
    146 #else
    147 	ADBReInit();
    148 	printf(" (direct, %s)", adbHardwareDescr[adbHardware]);
    149 #endif /* MRG_ADB */
    150 
    151 #ifdef ADB_DEBUG
    152 	if (adb_debug)
    153 		printf("adb: done with ADBReInit\n");
    154 #endif
    155 
    156 	totaladbs = CountADBs();
    157 
    158 	printf(": %d target%s\n", totaladbs, (totaladbs == 1) ? "" : "s");
    159 
    160 #if NAED > 0
    161 	/* ADB event device for compatibility */
    162 	aa_args.origaddr = 0;
    163 	aa_args.adbaddr = 0;
    164 	aa_args.handler_id = 0;
    165 	(void)config_found(self, &aa_args, adbprint);
    166 #endif
    167 
    168 	/* for each ADB device */
    169 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
    170 		/* Get the ADB information */
    171 		adbaddr = GetIndADB(&adbdata, adbindex);
    172 
    173 		aa_args.origaddr = (int)(adbdata.origADBAddr);
    174 		aa_args.adbaddr = adbaddr;
    175 		aa_args.handler_id = (int)(adbdata.devType);
    176 
    177 		(void)config_found(self, &aa_args, adbprint);
    178 	}
    179 	adb_polling = 0;
    180 }
    181 
    182 
    183 int
    184 adbprint(void *args, const char *name)
    185 {
    186 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
    187 	int rv = UNCONF;
    188 
    189 	if (name) {	/* no configured device matched */
    190 		rv = UNSUPP; /* most ADB device types are unsupported */
    191 
    192 		/* print out what kind of ADB device we have found */
    193 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
    194 		switch(aa_args->origaddr) {
    195 		case 0:
    196 #ifdef DIAGNOSTIC
    197 			aprint_normal("ADB event device");
    198 #endif
    199 			rv = UNCONF;
    200 			break;
    201 #ifdef DIAGNOSTIC
    202 		case ADBADDR_SECURE:
    203 			aprint_normal("security dongle (%d)",
    204 			    aa_args->handler_id);
    205 			break;
    206 #endif
    207 		case ADBADDR_MAP:
    208 			aprint_normal("mapped device (%d)",
    209 			    aa_args->handler_id);
    210 			rv = UNCONF;
    211 			break;
    212 		case ADBADDR_REL:
    213 			aprint_normal("relative positioning device (%d)",
    214 			    aa_args->handler_id);
    215 			rv = UNCONF;
    216 			break;
    217 #ifdef DIAGNOSTIC
    218 		case ADBADDR_ABS:
    219 			switch (aa_args->handler_id) {
    220 			case ADB_ARTPAD:
    221 				aprint_normal("WACOM ArtPad II");
    222 				break;
    223 			default:
    224 				aprint_normal("absolute positioning device (%d)",
    225 				    aa_args->handler_id);
    226 				break;
    227 			}
    228 			break;
    229 		case ADBADDR_DATATX:
    230 			aprint_normal("data transfer device (modem?) (%d)",
    231 			    aa_args->handler_id);
    232 			break;
    233 		case ADBADDR_MISC:
    234 			switch (aa_args->handler_id) {
    235 			case ADB_POWERKEY:
    236 				aprint_normal("Sophisticated Circuits PowerKey");
    237 				break;
    238 			default:
    239 				aprint_normal("misc. device (remote control?) (%d)",
    240 				    aa_args->handler_id);
    241 				break;
    242 			}
    243 			break;
    244 		default:
    245 			aprint_normal("unknown type device, (handler %d)",
    246 			    aa_args->handler_id);
    247 			break;
    248 #endif /* DIAGNOSTIC */
    249 		}
    250 	} else		/* a device matched and was configured */
    251 		aprint_normal(" addr %d: ", aa_args->adbaddr);
    252 
    253 	return (rv);
    254 }
    255 
    256 
    257 /*
    258  * adb_op_sync
    259  *
    260  * This routine does exactly what the adb_op routine does, except that after
    261  * the adb_op is called, it waits until the return value is present before
    262  * returning.
    263  *
    264  * NOTE: The user specified compRout is ignored, since this routine specifies
    265  * its own to adb_op, which is why you really called this in the first place
    266  * anyway.
    267  */
    268 int
    269 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
    270 {
    271 	int result;
    272 	volatile int flag = 0;
    273 
    274 	result = ADBOp(buffer, (void *)adb_op_comprout, __UNVOLATILE(&flag),
    275 	    command);	/* send command */
    276 	if (result == 0) {		/* send ok? */
    277 		adb_spin(&flag);
    278 		if (!flag)
    279 			result = -2;
    280 	}
    281 
    282 	return result;
    283 }
    284 
    285 /*
    286  * adb_spin
    287  *
    288  * Implements a spin-wait with timeout to be used for synchronous
    289  * operations on the ADB bus.
    290  *
    291  * Total time to wait is calculated as follows:
    292  *  - Tlt (stop to start time): 260 usec
    293  *  - start bit: 100 usec
    294  *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
    295  *  - stop bit (with SRQ): 140 usec
    296  * Total: 6900 usec
    297  *
    298  * This is the total time allowed by the specification.  Any device that
    299  * doesn't conform to this will fail to operate properly on some Apple
    300  * systems.  In spite of this we double the time to wait; Cuda-based
    301  * systems apparently queue commands and allow the main CPU to continue
    302  * processing (how radical!).  To be safe, allow time for two complete
    303  * ADB transactions to occur.
    304  */
    305 void
    306 adb_spin(volatile int *fp)
    307 {
    308 	int tmout;
    309 
    310 	for (tmout = 13800; *fp == 0 && tmout >= 10; tmout -= 10)
    311 		delay(10);
    312 	if (*fp == 0 && tmout > 0)
    313 		delay(tmout);
    314 }
    315 
    316 
    317 /*
    318  * adb_op_comprout
    319  *
    320  * This function is used by the adb_op_sync routine so it knows when the
    321  * function is done.
    322  */
    323 void
    324 adb_op_comprout(void)
    325 {
    326 	__asm("movw	#1,%a2@			| update flag value");
    327 }
    328