Home | History | Annotate | Line # | Download | only in dev
adb.c revision 1.44
      1  1.44  thorpej /*	$NetBSD: adb.c,v 1.44 2003/01/01 00:16:47 thorpej Exp $	*/
      2   1.1   briggs 
      3  1.25    ender /*
      4   1.1   briggs  * Copyright (C) 1994	Bradley A. Grantham
      5   1.1   briggs  * All rights reserved.
      6   1.1   briggs  *
      7   1.1   briggs  * Redistribution and use in source and binary forms, with or without
      8   1.1   briggs  * modification, are permitted provided that the following conditions
      9   1.1   briggs  * are met:
     10   1.1   briggs  * 1. Redistributions of source code must retain the above copyright
     11   1.1   briggs  *    notice, this list of conditions and the following disclaimer.
     12   1.1   briggs  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.25    ender  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   briggs  *    documentation and/or other materials provided with the distribution.
     15   1.1   briggs  * 3. All advertising materials mentioning features or use of this software
     16   1.1   briggs  *    must display the following acknowledgement:
     17   1.1   briggs  *	This product includes software developed by Bradley A. Grantham.
     18   1.1   briggs  * 4. The name of the author may not be used to endorse or promote products
     19   1.1   briggs  *    derived from this software without specific prior written permission.
     20   1.1   briggs  *
     21   1.1   briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1   briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1   briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1   briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1   briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1   briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1   briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1   briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1   briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30   1.1   briggs  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1   briggs  */
     32   1.1   briggs 
     33  1.20   scottr #include "opt_adb.h"
     34  1.20   scottr 
     35   1.1   briggs #include <sys/param.h>
     36   1.1   briggs #include <sys/device.h>
     37   1.1   briggs #include <sys/fcntl.h>
     38  1.10   scottr #include <sys/poll.h>
     39   1.1   briggs #include <sys/select.h>
     40   1.1   briggs #include <sys/proc.h>
     41   1.7   briggs #include <sys/signalvar.h>
     42   1.1   briggs #include <sys/systm.h>
     43   1.2   briggs 
     44   1.7   briggs #include <machine/autoconf.h>
     45  1.25    ender #include <machine/cpu.h>
     46   1.2   briggs 
     47  1.17   scottr #include <mac68k/mac68k/macrom.h>
     48  1.17   scottr #include <mac68k/dev/adbvar.h>
     49  1.34   scottr #include <mac68k/dev/akbdvar.h>
     50  1.25    ender 
     51  1.25    ender #include "aed.h"		/* ADB Event Device for compatibility */
     52   1.1   briggs 
     53   1.1   briggs /*
     54   1.2   briggs  * Function declarations.
     55   1.1   briggs  */
     56  1.13   scottr static int	adbmatch __P((struct device *, struct cfdata *, void *));
     57   1.7   briggs static void	adbattach __P((struct device *, struct device *, void *));
     58  1.25    ender static int	adbprint __P((void *, const char *));
     59  1.29   scottr void		adb_config_interrupts __P((struct device *));
     60  1.25    ender 
     61  1.25    ender extern void	adb_jadbproc __P((void));
     62   1.1   briggs 
     63   1.2   briggs /*
     64   1.2   briggs  * Global variables.
     65   1.2   briggs  */
     66  1.30   scottr int	adb_polling = 0;	/* Are we polling?  (Debugger mode) */
     67  1.20   scottr #ifdef ADB_DEBUG
     68  1.20   scottr int	adb_debug = 0;		/* Output debugging messages */
     69  1.20   scottr #endif /* ADB_DEBUG */
     70   1.1   briggs 
     71  1.25    ender extern struct	mac68k_machine_S mac68k_machine;
     72  1.25    ender extern int	adbHardware;
     73  1.25    ender extern char	*adbHardwareDescr[];
     74  1.25    ender 
     75   1.1   briggs /*
     76  1.25    ender  * Driver definition.
     77   1.1   briggs  */
     78  1.43  thorpej CFATTACH_DECL(adb, sizeof(struct device),
     79  1.43  thorpej     adbmatch, adbattach, NULL, NULL);
     80   1.7   briggs 
     81   1.7   briggs static int
     82  1.13   scottr adbmatch(parent, cf, aux)
     83  1.13   scottr 	struct device *parent;
     84  1.13   scottr 	struct cfdata *cf;
     85  1.13   scottr 	void *aux;
     86   1.7   briggs {
     87  1.18   scottr 	static int adb_matched = 0;
     88  1.18   scottr 
     89  1.18   scottr 	/* Allow only one instance. */
     90  1.18   scottr 	if (adb_matched)
     91  1.18   scottr 		return (0);
     92  1.18   scottr 
     93  1.18   scottr 	adb_matched = 1;
     94  1.18   scottr 	return (1);
     95   1.7   briggs }
     96   1.1   briggs 
     97   1.1   briggs static void
     98  1.29   scottr adbattach(parent, self, aux)
     99  1.29   scottr 	struct device *parent, *self;
    100  1.21   scottr 	void *aux;
    101   1.1   briggs {
    102  1.29   scottr 	printf("\n");
    103  1.29   scottr 
    104  1.29   scottr 	/*
    105  1.29   scottr 	 * Defer configuration until interrupts are enabled.
    106  1.29   scottr 	 */
    107  1.29   scottr 	config_interrupts(self, adb_config_interrupts);
    108  1.29   scottr }
    109  1.29   scottr 
    110  1.29   scottr void
    111  1.29   scottr adb_config_interrupts(self)
    112  1.29   scottr 	struct device *self;
    113  1.29   scottr {
    114  1.25    ender 	ADBDataBlock adbdata;
    115  1.25    ender 	struct adb_attach_args aa_args;
    116  1.25    ender 	int totaladbs;
    117  1.25    ender 	int adbindex, adbaddr;
    118  1.25    ender 
    119  1.29   scottr 	printf("%s", self->dv_xname);
    120  1.31   scottr 	adb_polling = 1;
    121  1.29   scottr 
    122  1.25    ender #ifdef MRG_ADB
    123  1.25    ender 	if (!mrg_romready()) {
    124  1.29   scottr 		printf(": no ROM ADB driver in this kernel for this machine\n");
    125  1.25    ender 		return;
    126  1.25    ender 	}
    127   1.1   briggs 
    128  1.25    ender #ifdef ADB_DEBUG
    129  1.25    ender 	if (adb_debug)
    130  1.25    ender 		printf("adb: call mrg_initadbintr\n");
    131  1.25    ender #endif
    132   1.1   briggs 
    133  1.25    ender 	mrg_initadbintr();	/* Mac ROM Glue okay to do ROM intr */
    134  1.25    ender #ifdef ADB_DEBUG
    135  1.25    ender 	if (adb_debug)
    136  1.25    ender 		printf("adb: returned from mrg_initadbintr\n");
    137  1.25    ender #endif
    138  1.10   scottr 
    139  1.25    ender 	/* ADBReInit pre/post-processing */
    140  1.25    ender 	JADBProc = adb_jadbproc;
    141   1.1   briggs 
    142  1.25    ender 	/* Initialize ADB */
    143  1.25    ender #ifdef ADB_DEBUG
    144  1.25    ender 	if (adb_debug)
    145  1.25    ender 		printf("adb: calling ADBAlternateInit.\n");
    146  1.10   scottr #endif
    147   1.1   briggs 
    148  1.28   scottr 	printf(" (mrg)");
    149  1.25    ender 	ADBAlternateInit();
    150  1.25    ender #else
    151  1.25    ender 	ADBReInit();
    152  1.25    ender 	printf(" (direct, %s)", adbHardwareDescr[adbHardware]);
    153  1.25    ender #endif /* MRG_ADB */
    154  1.25    ender 
    155  1.25    ender #ifdef ADB_DEBUG
    156  1.25    ender 	if (adb_debug)
    157  1.25    ender 		printf("adb: done with ADBReInit\n");
    158  1.25    ender #endif
    159   1.1   briggs 
    160  1.25    ender 	totaladbs = CountADBs();
    161   1.1   briggs 
    162  1.33   scottr 	printf(": %d target%s\n", totaladbs, (totaladbs == 1) ? "" : "s");
    163   1.1   briggs 
    164  1.25    ender #if NAED > 0
    165  1.25    ender 	/* ADB event device for compatibility */
    166  1.25    ender 	aa_args.origaddr = 0;
    167  1.25    ender 	aa_args.adbaddr = 0;
    168  1.25    ender 	aa_args.handler_id = 0;
    169  1.29   scottr 	(void)config_found(self, &aa_args, adbprint);
    170  1.25    ender #endif
    171   1.1   briggs 
    172  1.25    ender 	/* for each ADB device */
    173  1.25    ender 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
    174  1.25    ender 		/* Get the ADB information */
    175  1.25    ender 		adbaddr = GetIndADB(&adbdata, adbindex);
    176   1.1   briggs 
    177  1.27    ender 		aa_args.origaddr = (int)(adbdata.origADBAddr);
    178  1.25    ender 		aa_args.adbaddr = adbaddr;
    179  1.27    ender 		aa_args.handler_id = (int)(adbdata.devType);
    180   1.2   briggs 
    181  1.29   scottr 		(void)config_found(self, &aa_args, adbprint);
    182   1.1   briggs 	}
    183  1.30   scottr 	adb_polling = 0;
    184   1.1   briggs }
    185   1.1   briggs 
    186   1.1   briggs 
    187  1.25    ender int
    188  1.25    ender adbprint(args, name)
    189  1.34   scottr 	void *args;
    190  1.34   scottr 	const char *name;
    191   1.1   briggs {
    192  1.25    ender 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
    193  1.25    ender 	int rv = UNCONF;
    194   1.1   briggs 
    195  1.25    ender 	if (name) {	/* no configured device matched */
    196  1.25    ender 		rv = UNSUPP; /* most ADB device types are unsupported */
    197   1.2   briggs 
    198  1.25    ender 		/* print out what kind of ADB device we have found */
    199  1.44  thorpej 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
    200  1.25    ender 		switch(aa_args->origaddr) {
    201  1.25    ender #ifdef DIAGNOSTIC
    202  1.25    ender 		case 0:
    203  1.44  thorpej 			aprint_normal("ADB event device");
    204  1.25    ender 			rv = UNCONF;
    205  1.25    ender 			break;
    206  1.25    ender 		case ADBADDR_SECURE:
    207  1.44  thorpej 			aprint_normal("security dongle (%d)",
    208  1.44  thorpej 			    aa_args->handler_id);
    209  1.25    ender 			break;
    210  1.22   scottr #endif
    211  1.25    ender 		case ADBADDR_MAP:
    212  1.44  thorpej 			aprint_normal("mapped device (%d)",
    213  1.44  thorpej 			    aa_args->handler_id);
    214  1.25    ender 			rv = UNCONF;
    215  1.25    ender 			break;
    216  1.25    ender 		case ADBADDR_REL:
    217  1.44  thorpej 			aprint_normal("relative positioning device (%d)",
    218  1.25    ender 			    aa_args->handler_id);
    219  1.25    ender 			rv = UNCONF;
    220  1.25    ender 			break;
    221  1.25    ender #ifdef DIAGNOSTIC
    222  1.25    ender 		case ADBADDR_ABS:
    223  1.25    ender 			switch (aa_args->handler_id) {
    224  1.25    ender 			case ADB_ARTPAD:
    225  1.44  thorpej 				aprint_normal("WACOM ArtPad II");
    226  1.25    ender 				break;
    227  1.25    ender 			default:
    228  1.44  thorpej 				aprint_normal("absolute positioning device (%d)",
    229  1.25    ender 				    aa_args->handler_id);
    230  1.25    ender 				break;
    231  1.25    ender 			}
    232  1.15   scottr 			break;
    233  1.25    ender 		case ADBADDR_DATATX:
    234  1.44  thorpej 			aprint_normal("data transfer device (modem?) (%d)",
    235  1.25    ender 			    aa_args->handler_id);
    236  1.25    ender 			break;
    237  1.25    ender 		case ADBADDR_MISC:
    238  1.25    ender 			switch (aa_args->handler_id) {
    239  1.25    ender 			case ADB_POWERKEY:
    240  1.44  thorpej 				aprint_normal("Sophisticated Circuits PowerKey");
    241  1.25    ender 				break;
    242  1.25    ender 			default:
    243  1.44  thorpej 				aprint_normal("misc. device (remote control?) (%d)",
    244  1.25    ender 				    aa_args->handler_id);
    245  1.25    ender 				break;
    246  1.25    ender 			}
    247   1.8   briggs 			break;
    248   1.8   briggs 		default:
    249  1.44  thorpej 			aprint_normal("unknown type device, (handler %d)",
    250  1.25    ender 			    aa_args->handler_id);
    251   1.8   briggs 			break;
    252  1.25    ender #endif /* DIAGNOSTIC */
    253   1.4   briggs 		}
    254  1.25    ender 	} else		/* a device matched and was configured */
    255  1.44  thorpej 		aprint_normal(" addr %d: ", aa_args->adbaddr);
    256   1.2   briggs 
    257  1.25    ender 	return (rv);
    258  1.37   scottr }
    259  1.37   scottr 
    260  1.37   scottr 
    261  1.37   scottr /*
    262  1.37   scottr  * adb_op_sync
    263  1.37   scottr  *
    264  1.37   scottr  * This routine does exactly what the adb_op routine does, except that after
    265  1.37   scottr  * the adb_op is called, it waits until the return value is present before
    266  1.37   scottr  * returning.
    267  1.37   scottr  *
    268  1.37   scottr  * NOTE: The user specified compRout is ignored, since this routine specifies
    269  1.37   scottr  * it's own to adb_op, which is why you really called this in the first place
    270  1.37   scottr  * anyway.
    271  1.37   scottr  */
    272  1.37   scottr int
    273  1.37   scottr adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
    274  1.37   scottr {
    275  1.37   scottr 	int tmout;
    276  1.37   scottr 	int result;
    277  1.37   scottr 	volatile int flag = 0;
    278  1.37   scottr 
    279  1.37   scottr 	result = ADBOp(buffer, (void *)adb_op_comprout,
    280  1.37   scottr 	    (void *)&flag, command);	/* send command */
    281  1.37   scottr 	if (result == 0) {		/* send ok? */
    282  1.37   scottr 		/*
    283  1.37   scottr 		 * Total time to wait is calculated as follows:
    284  1.37   scottr 		 *  - Tlt (stop to start time): 260 usec
    285  1.37   scottr 		 *  - start bit: 100 usec
    286  1.37   scottr 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
    287  1.37   scottr 		 *  - stop bit (with SRQ): 140 usec
    288  1.37   scottr 		 * Total: 6900 usec
    289  1.40   scottr 		 *
    290  1.40   scottr 		 * This is the total time allowed by the specification.  Any
    291  1.40   scottr 		 * device that doesn't conform to this will fail to operate
    292  1.40   scottr 		 * properly on some Apple systems.  In spite of this we
    293  1.40   scottr 		 * double the time to wait; some Cuda-based apparently
    294  1.40   scottr 		 * queues some commands and allows the main CPU to continue
    295  1.40   scottr 		 * processing (radical concept, eh?).  To be safe, allow
    296  1.40   scottr 		 * time for two complete ADB transactions to occur.
    297  1.37   scottr 		 */
    298  1.40   scottr 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
    299  1.37   scottr 			delay(10);
    300  1.37   scottr 		if (!flag && tmout > 0)
    301  1.37   scottr 			delay(tmout);
    302  1.37   scottr 
    303  1.37   scottr 		if (!flag)
    304  1.37   scottr 			result = -2;
    305  1.37   scottr 	}
    306  1.37   scottr 
    307  1.37   scottr 	return result;
    308  1.37   scottr }
    309  1.37   scottr 
    310  1.37   scottr 
    311  1.37   scottr /*
    312  1.37   scottr  * adb_op_comprout
    313  1.37   scottr  *
    314  1.37   scottr  * This function is used by the adb_op_sync routine so it knows when the
    315  1.37   scottr  * function is done.
    316  1.37   scottr  */
    317  1.37   scottr void
    318  1.37   scottr adb_op_comprout(void)
    319  1.37   scottr {
    320  1.37   scottr #ifdef __NetBSD__
    321  1.41      chs 	asm("movw	#1,%a2@			| update flag value");
    322  1.37   scottr #else				/* for macos based testing */
    323  1.37   scottr 	asm {
    324  1.37   scottr 		move.w #1,(a2) }		/* update flag value */
    325  1.37   scottr #endif
    326   1.1   briggs }
    327