xref: /netbsd-src/sys/arch/mac68k/dev/adb.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: adb.c,v 1.50 2007/03/08 02:24:39 tsutsui 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.50 2007/03/08 02:24:39 tsutsui 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 
101 	adb_softintr_cookie = softintr_establish(IPL_SOFT,
102 	    (void (*)(void *))adb_soft_intr, NULL);
103 	printf("\n");
104 
105 	/*
106 	 * Defer configuration until interrupts are enabled.
107 	 */
108 	config_interrupts(self, adb_config_interrupts);
109 }
110 
111 void
112 adb_config_interrupts(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(void *args, const char *name)
189 {
190 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
191 	int rv = UNCONF;
192 
193 	if (name) {	/* no configured device matched */
194 		rv = UNSUPP; /* most ADB device types are unsupported */
195 
196 		/* print out what kind of ADB device we have found */
197 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
198 		switch(aa_args->origaddr) {
199 #ifdef DIAGNOSTIC
200 		case 0:
201 			aprint_normal("ADB event device");
202 			rv = UNCONF;
203 			break;
204 		case ADBADDR_SECURE:
205 			aprint_normal("security dongle (%d)",
206 			    aa_args->handler_id);
207 			break;
208 #endif
209 		case ADBADDR_MAP:
210 			aprint_normal("mapped device (%d)",
211 			    aa_args->handler_id);
212 			rv = UNCONF;
213 			break;
214 		case ADBADDR_REL:
215 			aprint_normal("relative positioning device (%d)",
216 			    aa_args->handler_id);
217 			rv = UNCONF;
218 			break;
219 #ifdef DIAGNOSTIC
220 		case ADBADDR_ABS:
221 			switch (aa_args->handler_id) {
222 			case ADB_ARTPAD:
223 				aprint_normal("WACOM ArtPad II");
224 				break;
225 			default:
226 				aprint_normal("absolute positioning device (%d)",
227 				    aa_args->handler_id);
228 				break;
229 			}
230 			break;
231 		case ADBADDR_DATATX:
232 			aprint_normal("data transfer device (modem?) (%d)",
233 			    aa_args->handler_id);
234 			break;
235 		case ADBADDR_MISC:
236 			switch (aa_args->handler_id) {
237 			case ADB_POWERKEY:
238 				aprint_normal("Sophisticated Circuits PowerKey");
239 				break;
240 			default:
241 				aprint_normal("misc. device (remote control?) (%d)",
242 				    aa_args->handler_id);
243 				break;
244 			}
245 			break;
246 		default:
247 			aprint_normal("unknown type device, (handler %d)",
248 			    aa_args->handler_id);
249 			break;
250 #endif /* DIAGNOSTIC */
251 		}
252 	} else		/* a device matched and was configured */
253 		aprint_normal(" addr %d: ", aa_args->adbaddr);
254 
255 	return (rv);
256 }
257 
258 
259 /*
260  * adb_op_sync
261  *
262  * This routine does exactly what the adb_op routine does, except that after
263  * the adb_op is called, it waits until the return value is present before
264  * returning.
265  *
266  * NOTE: The user specified compRout is ignored, since this routine specifies
267  * it's own to adb_op, which is why you really called this in the first place
268  * anyway.
269  */
270 int
271 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
272 {
273 	int tmout;
274 	int result;
275 	volatile int flag = 0;
276 
277 	result = ADBOp(buffer, (void *)adb_op_comprout, __UNVOLATILE(&flag),
278 	    command);	/* send command */
279 	if (result == 0) {		/* send ok? */
280 		/*
281 		 * Total time to wait is calculated as follows:
282 		 *  - Tlt (stop to start time): 260 usec
283 		 *  - start bit: 100 usec
284 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
285 		 *  - stop bit (with SRQ): 140 usec
286 		 * Total: 6900 usec
287 		 *
288 		 * This is the total time allowed by the specification.  Any
289 		 * device that doesn't conform to this will fail to operate
290 		 * properly on some Apple systems.  In spite of this we
291 		 * double the time to wait; some Cuda-based apparently
292 		 * queues some commands and allows the main CPU to continue
293 		 * processing (radical concept, eh?).  To be safe, allow
294 		 * time for two complete ADB transactions to occur.
295 		 */
296 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
297 			delay(10);
298 		if (!flag && tmout > 0)
299 			delay(tmout);
300 
301 		if (!flag)
302 			result = -2;
303 	}
304 
305 	return result;
306 }
307 
308 
309 /*
310  * adb_op_comprout
311  *
312  * This function is used by the adb_op_sync routine so it knows when the
313  * function is done.
314  */
315 void
316 adb_op_comprout(void)
317 {
318 	__asm("movw	#1,%a2@			| update flag value");
319 }
320