xref: /netbsd-src/sys/arch/mac68k/dev/adb.c (revision df0caa2637da0538ecdf6b878c4d08e684b43d8f)
1 /*	$NetBSD: adb.c,v 1.47 2005/06/16 22:43:36 jmc 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 <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.47 2005/06/16 22:43:36 jmc Exp $");
35 
36 #include "opt_adb.h"
37 
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/fcntl.h>
41 #include <sys/poll.h>
42 #include <sys/select.h>
43 #include <sys/proc.h>
44 #include <sys/signalvar.h>
45 #include <sys/systm.h>
46 
47 #include <machine/autoconf.h>
48 #include <machine/cpu.h>
49 
50 #include <mac68k/mac68k/macrom.h>
51 #include <mac68k/dev/adbvar.h>
52 #include <mac68k/dev/akbdvar.h>
53 
54 #include "aed.h"		/* ADB Event Device for compatibility */
55 
56 /*
57  * Function declarations.
58  */
59 static int	adbmatch(struct device *, struct cfdata *, void *);
60 static void	adbattach(struct device *, struct device *, void *);
61 static int	adbprint(void *, const char *);
62 void		adb_config_interrupts(struct device *);
63 
64 extern void	adb_jadbproc(void);
65 
66 /*
67  * Global variables.
68  */
69 int	adb_polling = 0;	/* Are we polling?  (Debugger mode) */
70 #ifdef ADB_DEBUG
71 int	adb_debug = 0;		/* Output debugging messages */
72 #endif /* ADB_DEBUG */
73 
74 extern struct	mac68k_machine_S mac68k_machine;
75 extern int	adbHardware;
76 extern char	*adbHardwareDescr[];
77 
78 /*
79  * Driver definition.
80  */
81 CFATTACH_DECL(adb, sizeof(struct device),
82     adbmatch, adbattach, NULL, NULL);
83 
84 static int
85 adbmatch(struct device *parent, struct cfdata *cf, 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(struct device *parent, struct device *self, void *aux)
99 {
100 	printf("\n");
101 
102 	/*
103 	 * Defer configuration until interrupts are enabled.
104 	 */
105 	config_interrupts(self, adb_config_interrupts);
106 }
107 
108 void
109 adb_config_interrupts(struct device *self)
110 {
111 	ADBDataBlock adbdata;
112 	struct adb_attach_args aa_args;
113 	int totaladbs;
114 	int adbindex, adbaddr;
115 
116 	printf("%s", self->dv_xname);
117 	adb_polling = 1;
118 
119 #ifdef MRG_ADB
120 	if (!mrg_romready()) {
121 		printf(": no ROM ADB driver in this kernel for this machine\n");
122 		return;
123 	}
124 
125 #ifdef ADB_DEBUG
126 	if (adb_debug)
127 		printf("adb: call mrg_initadbintr\n");
128 #endif
129 
130 	mrg_initadbintr();	/* Mac ROM Glue okay to do ROM intr */
131 #ifdef ADB_DEBUG
132 	if (adb_debug)
133 		printf("adb: returned from mrg_initadbintr\n");
134 #endif
135 
136 	/* ADBReInit pre/post-processing */
137 	JADBProc = adb_jadbproc;
138 
139 	/* Initialize ADB */
140 #ifdef ADB_DEBUG
141 	if (adb_debug)
142 		printf("adb: calling ADBAlternateInit.\n");
143 #endif
144 
145 	printf(" (mrg)");
146 	ADBAlternateInit();
147 #else
148 	ADBReInit();
149 	printf(" (direct, %s)", adbHardwareDescr[adbHardware]);
150 #endif /* MRG_ADB */
151 
152 #ifdef ADB_DEBUG
153 	if (adb_debug)
154 		printf("adb: done with ADBReInit\n");
155 #endif
156 
157 	totaladbs = CountADBs();
158 
159 	printf(": %d target%s\n", totaladbs, (totaladbs == 1) ? "" : "s");
160 
161 #if NAED > 0
162 	/* ADB event device for compatibility */
163 	aa_args.origaddr = 0;
164 	aa_args.adbaddr = 0;
165 	aa_args.handler_id = 0;
166 	(void)config_found(self, &aa_args, adbprint);
167 #endif
168 
169 	/* for each ADB device */
170 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
171 		/* Get the ADB information */
172 		adbaddr = GetIndADB(&adbdata, adbindex);
173 
174 		aa_args.origaddr = (int)(adbdata.origADBAddr);
175 		aa_args.adbaddr = adbaddr;
176 		aa_args.handler_id = (int)(adbdata.devType);
177 
178 		(void)config_found(self, &aa_args, adbprint);
179 	}
180 	adb_polling = 0;
181 }
182 
183 
184 int
185 adbprint(void *args, const char *name)
186 {
187 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
188 	int rv = UNCONF;
189 
190 	if (name) {	/* no configured device matched */
191 		rv = UNSUPP; /* most ADB device types are unsupported */
192 
193 		/* print out what kind of ADB device we have found */
194 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
195 		switch(aa_args->origaddr) {
196 #ifdef DIAGNOSTIC
197 		case 0:
198 			aprint_normal("ADB event device");
199 			rv = UNCONF;
200 			break;
201 		case ADBADDR_SECURE:
202 			aprint_normal("security dongle (%d)",
203 			    aa_args->handler_id);
204 			break;
205 #endif
206 		case ADBADDR_MAP:
207 			aprint_normal("mapped device (%d)",
208 			    aa_args->handler_id);
209 			rv = UNCONF;
210 			break;
211 		case ADBADDR_REL:
212 			aprint_normal("relative positioning device (%d)",
213 			    aa_args->handler_id);
214 			rv = UNCONF;
215 			break;
216 #ifdef DIAGNOSTIC
217 		case ADBADDR_ABS:
218 			switch (aa_args->handler_id) {
219 			case ADB_ARTPAD:
220 				aprint_normal("WACOM ArtPad II");
221 				break;
222 			default:
223 				aprint_normal("absolute positioning device (%d)",
224 				    aa_args->handler_id);
225 				break;
226 			}
227 			break;
228 		case ADBADDR_DATATX:
229 			aprint_normal("data transfer device (modem?) (%d)",
230 			    aa_args->handler_id);
231 			break;
232 		case ADBADDR_MISC:
233 			switch (aa_args->handler_id) {
234 			case ADB_POWERKEY:
235 				aprint_normal("Sophisticated Circuits PowerKey");
236 				break;
237 			default:
238 				aprint_normal("misc. device (remote control?) (%d)",
239 				    aa_args->handler_id);
240 				break;
241 			}
242 			break;
243 		default:
244 			aprint_normal("unknown type device, (handler %d)",
245 			    aa_args->handler_id);
246 			break;
247 #endif /* DIAGNOSTIC */
248 		}
249 	} else		/* a device matched and was configured */
250 		aprint_normal(" addr %d: ", aa_args->adbaddr);
251 
252 	return (rv);
253 }
254 
255 
256 /*
257  * adb_op_sync
258  *
259  * This routine does exactly what the adb_op routine does, except that after
260  * the adb_op is called, it waits until the return value is present before
261  * returning.
262  *
263  * NOTE: The user specified compRout is ignored, since this routine specifies
264  * it's own to adb_op, which is why you really called this in the first place
265  * anyway.
266  */
267 int
268 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
269 {
270 	int tmout;
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 		/*
278 		 * Total time to wait is calculated as follows:
279 		 *  - Tlt (stop to start time): 260 usec
280 		 *  - start bit: 100 usec
281 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
282 		 *  - stop bit (with SRQ): 140 usec
283 		 * Total: 6900 usec
284 		 *
285 		 * This is the total time allowed by the specification.  Any
286 		 * device that doesn't conform to this will fail to operate
287 		 * properly on some Apple systems.  In spite of this we
288 		 * double the time to wait; some Cuda-based apparently
289 		 * queues some commands and allows the main CPU to continue
290 		 * processing (radical concept, eh?).  To be safe, allow
291 		 * time for two complete ADB transactions to occur.
292 		 */
293 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
294 			delay(10);
295 		if (!flag && tmout > 0)
296 			delay(tmout);
297 
298 		if (!flag)
299 			result = -2;
300 	}
301 
302 	return result;
303 }
304 
305 
306 /*
307  * adb_op_comprout
308  *
309  * This function is used by the adb_op_sync routine so it knows when the
310  * function is done.
311  */
312 void
313 adb_op_comprout(void)
314 {
315 	asm("movw	#1,%a2@			| update flag value");
316 }
317