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