pckbport.c revision 1.1 1 1.1 bjh21 /* $NetBSD: pckbport.c,v 1.1 2004/03/13 17:31:33 bjh21 Exp $ */
2 1.1 bjh21
3 1.1 bjh21 /*
4 1.1 bjh21 * Copyright (c) 2004 Ben Harris
5 1.1 bjh21 * Copyright (c) 1998
6 1.1 bjh21 * Matthias Drochner. All rights reserved.
7 1.1 bjh21 *
8 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
9 1.1 bjh21 * modification, are permitted provided that the following conditions
10 1.1 bjh21 * are met:
11 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
12 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
13 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
15 1.1 bjh21 * documentation and/or other materials provided with the distribution.
16 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software
17 1.1 bjh21 * must display the following acknowledgement:
18 1.1 bjh21 * This product includes software developed for the NetBSD Project
19 1.1 bjh21 * by Matthias Drochner.
20 1.1 bjh21 * 4. The name of the author may not be used to endorse or promote products
21 1.1 bjh21 * derived from this software without specific prior written permission.
22 1.1 bjh21 *
23 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 bjh21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 bjh21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 bjh21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 bjh21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 bjh21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 bjh21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 bjh21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 bjh21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 bjh21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 bjh21 */
34 1.1 bjh21
35 1.1 bjh21 #include <sys/cdefs.h>
36 1.1 bjh21 __KERNEL_RCSID(0, "$NetBSD: pckbport.c,v 1.1 2004/03/13 17:31:33 bjh21 Exp $");
37 1.1 bjh21
38 1.1 bjh21 #include <sys/param.h>
39 1.1 bjh21 #include <sys/systm.h>
40 1.1 bjh21 #include <sys/callout.h>
41 1.1 bjh21 #include <sys/kernel.h>
42 1.1 bjh21 #include <sys/proc.h>
43 1.1 bjh21 #include <sys/device.h>
44 1.1 bjh21 #include <sys/malloc.h>
45 1.1 bjh21 #include <sys/errno.h>
46 1.1 bjh21 #include <sys/queue.h>
47 1.1 bjh21 #include <sys/lock.h>
48 1.1 bjh21
49 1.1 bjh21 #include <dev/pckbport/pckbportvar.h>
50 1.1 bjh21
51 1.1 bjh21 #include "locators.h"
52 1.1 bjh21
53 1.1 bjh21 #ifdef __HAVE_NWSCONS /* XXX: this port uses sys/dev/pckbport */
54 1.1 bjh21 #include "pckbd.h"
55 1.1 bjh21 #else /* ie: only md drivers attach to pckbport */
56 1.1 bjh21 #define NPCKBD 0
57 1.1 bjh21 #endif
58 1.1 bjh21 #if (NPCKBD > 0)
59 1.1 bjh21 #include <dev/pckbport/pckbdvar.h>
60 1.1 bjh21 #endif
61 1.1 bjh21
62 1.1 bjh21 /* descriptor for one device command */
63 1.1 bjh21 struct pckbport_devcmd {
64 1.1 bjh21 TAILQ_ENTRY(pckbport_devcmd) next;
65 1.1 bjh21 int flags;
66 1.1 bjh21 #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
67 1.1 bjh21 #define KBC_CMDFLAG_SLOW 2
68 1.1 bjh21 u_char cmd[4];
69 1.1 bjh21 int cmdlen, cmdidx, retries;
70 1.1 bjh21 u_char response[4];
71 1.1 bjh21 int status, responselen, responseidx;
72 1.1 bjh21 };
73 1.1 bjh21
74 1.1 bjh21 /* data per slave device */
75 1.1 bjh21 struct pckbport_slotdata {
76 1.1 bjh21 int polling; /* don't process data in interrupt handler */
77 1.1 bjh21 TAILQ_HEAD(, pckbport_devcmd) cmdqueue; /* active commands */
78 1.1 bjh21 TAILQ_HEAD(, pckbport_devcmd) freequeue; /* free commands */
79 1.1 bjh21 #define NCMD 5
80 1.1 bjh21 struct pckbport_devcmd cmds[NCMD];
81 1.1 bjh21 };
82 1.1 bjh21
83 1.1 bjh21 #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
84 1.1 bjh21
85 1.1 bjh21 static void pckbport_init_slotdata __P((struct pckbport_slotdata *));
86 1.1 bjh21 static int pckbport_submatch __P((struct device *, struct cfdata *, void *));
87 1.1 bjh21 static int pckbportprint __P((void *, const char *));
88 1.1 bjh21
89 1.1 bjh21 static struct pckbport_slotdata pckbport_cons_slotdata;
90 1.1 bjh21
91 1.1 bjh21 static int pckbport_poll_data1 __P((pckbport_tag_t, pckbport_slot_t));
92 1.1 bjh21 static int pckbport_send_devcmd __P((struct pckbport_tag *, pckbport_slot_t,
93 1.1 bjh21 u_char));
94 1.1 bjh21 static void pckbport_poll_cmd1 __P((struct pckbport_tag *, pckbport_slot_t,
95 1.1 bjh21 struct pckbport_devcmd *));
96 1.1 bjh21
97 1.1 bjh21 static void pckbport_cleanqueue __P((struct pckbport_slotdata *));
98 1.1 bjh21 static void pckbport_cleanup __P((void *));
99 1.1 bjh21 static int pckbport_cmdresponse __P((struct pckbport_tag *, pckbport_slot_t,
100 1.1 bjh21 u_char));
101 1.1 bjh21 static void pckbport_start __P((struct pckbport_tag *, pckbport_slot_t));
102 1.1 bjh21
103 1.1 bjh21 static const char * const pckbport_slot_names[] = { "kbd", "aux" };
104 1.1 bjh21
105 1.1 bjh21 static struct pckbport_tag pckbport_cntag;
106 1.1 bjh21
107 1.1 bjh21 #define KBC_DEVCMD_ACK 0xfa
108 1.1 bjh21 #define KBC_DEVCMD_RESEND 0xfe
109 1.1 bjh21
110 1.1 bjh21 #define KBD_DELAY DELAY(8)
111 1.1 bjh21
112 1.1 bjh21 static int
113 1.1 bjh21 pckbport_poll_data1(t, slot)
114 1.1 bjh21 pckbport_tag_t t;
115 1.1 bjh21 pckbport_slot_t slot;
116 1.1 bjh21 {
117 1.1 bjh21
118 1.1 bjh21 return t->t_ops->t_poll_data1(t->t_cookie, slot);
119 1.1 bjh21 }
120 1.1 bjh21
121 1.1 bjh21 static int
122 1.1 bjh21 pckbport_send_devcmd(t, slot, val)
123 1.1 bjh21 struct pckbport_tag *t;
124 1.1 bjh21 pckbport_slot_t slot;
125 1.1 bjh21 u_char val;
126 1.1 bjh21 {
127 1.1 bjh21
128 1.1 bjh21 return t->t_ops->t_send_devcmd(t->t_cookie, slot, val);
129 1.1 bjh21 }
130 1.1 bjh21
131 1.1 bjh21 static int
132 1.1 bjh21 pckbport_submatch(parent, cf, aux)
133 1.1 bjh21 struct device *parent;
134 1.1 bjh21 struct cfdata *cf;
135 1.1 bjh21 void *aux;
136 1.1 bjh21 {
137 1.1 bjh21 struct pckbport_attach_args *pa = aux;
138 1.1 bjh21
139 1.1 bjh21 if (cf->cf_loc[PCKBPORTCF_SLOT] != PCKBPORTCF_SLOT_DEFAULT &&
140 1.1 bjh21 cf->cf_loc[PCKBPORTCF_SLOT] != pa->pa_slot)
141 1.1 bjh21 return (0);
142 1.1 bjh21 return (config_match(parent, cf, aux));
143 1.1 bjh21 }
144 1.1 bjh21
145 1.1 bjh21 pckbport_tag_t
146 1.1 bjh21 pckbport_attach(void *cookie, struct pckbport_accessops const *ops)
147 1.1 bjh21 {
148 1.1 bjh21 pckbport_tag_t t;
149 1.1 bjh21
150 1.1 bjh21 if (cookie == pckbport_cntag.t_cookie &&
151 1.1 bjh21 ops == pckbport_cntag.t_ops)
152 1.1 bjh21 return &pckbport_cntag;
153 1.1 bjh21 t = malloc(sizeof(struct pckbport_tag), M_DEVBUF, M_NOWAIT | M_ZERO);
154 1.1 bjh21 if (t == NULL) return NULL;
155 1.1 bjh21 t->t_cookie = cookie;
156 1.1 bjh21 t->t_ops = ops;
157 1.1 bjh21 return t;
158 1.1 bjh21 }
159 1.1 bjh21
160 1.1 bjh21 struct device *
161 1.1 bjh21 pckbport_attach_slot(dev, t, slot)
162 1.1 bjh21 struct device *dev;
163 1.1 bjh21 pckbport_tag_t t;
164 1.1 bjh21 pckbport_slot_t slot;
165 1.1 bjh21 {
166 1.1 bjh21 struct pckbport_attach_args pa;
167 1.1 bjh21 void *sdata;
168 1.1 bjh21 struct device *found;
169 1.1 bjh21 int alloced = 0;
170 1.1 bjh21
171 1.1 bjh21 pa.pa_tag = t;
172 1.1 bjh21 pa.pa_slot = slot;
173 1.1 bjh21
174 1.1 bjh21 if (t->t_slotdata[slot] == NULL) {
175 1.1 bjh21 sdata = malloc(sizeof(struct pckbport_slotdata),
176 1.1 bjh21 M_DEVBUF, M_NOWAIT);
177 1.1 bjh21 if (sdata == NULL) {
178 1.1 bjh21 printf("%s: no memory\n", dev->dv_xname);
179 1.1 bjh21 return (0);
180 1.1 bjh21 }
181 1.1 bjh21 t->t_slotdata[slot] = sdata;
182 1.1 bjh21 pckbport_init_slotdata(t->t_slotdata[slot]);
183 1.1 bjh21 alloced++;
184 1.1 bjh21 }
185 1.1 bjh21
186 1.1 bjh21 found = config_found_sm(dev, &pa, pckbportprint, pckbport_submatch);
187 1.1 bjh21
188 1.1 bjh21 if (found == NULL && alloced) {
189 1.1 bjh21 free(t->t_slotdata[slot], M_DEVBUF);
190 1.1 bjh21 t->t_slotdata[slot] = NULL;
191 1.1 bjh21 }
192 1.1 bjh21
193 1.1 bjh21 return (found);
194 1.1 bjh21 }
195 1.1 bjh21
196 1.1 bjh21 int
197 1.1 bjh21 pckbportprint(aux, pnp)
198 1.1 bjh21 void *aux;
199 1.1 bjh21 const char *pnp;
200 1.1 bjh21 {
201 1.1 bjh21 struct pckbport_attach_args *pa = aux;
202 1.1 bjh21
203 1.1 bjh21 if (!pnp)
204 1.1 bjh21 aprint_normal(" (%s slot)", pckbport_slot_names[pa->pa_slot]);
205 1.1 bjh21 return (QUIET);
206 1.1 bjh21 }
207 1.1 bjh21
208 1.1 bjh21 void
209 1.1 bjh21 pckbport_init_slotdata(q)
210 1.1 bjh21 struct pckbport_slotdata *q;
211 1.1 bjh21 {
212 1.1 bjh21 int i;
213 1.1 bjh21 TAILQ_INIT(&q->cmdqueue);
214 1.1 bjh21 TAILQ_INIT(&q->freequeue);
215 1.1 bjh21
216 1.1 bjh21 for (i = 0; i < NCMD; i++) {
217 1.1 bjh21 TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
218 1.1 bjh21 }
219 1.1 bjh21 q->polling = 0;
220 1.1 bjh21 }
221 1.1 bjh21
222 1.1 bjh21 void
223 1.1 bjh21 pckbport_flush(t, slot)
224 1.1 bjh21 pckbport_tag_t t;
225 1.1 bjh21 pckbport_slot_t slot;
226 1.1 bjh21 {
227 1.1 bjh21
228 1.1 bjh21 (void) pckbport_poll_data1(t, slot);
229 1.1 bjh21 }
230 1.1 bjh21
231 1.1 bjh21 int
232 1.1 bjh21 pckbport_poll_data(t, slot)
233 1.1 bjh21 pckbport_tag_t t;
234 1.1 bjh21 pckbport_slot_t slot;
235 1.1 bjh21 {
236 1.1 bjh21 struct pckbport_slotdata *q = t->t_slotdata[slot];
237 1.1 bjh21 int c;
238 1.1 bjh21
239 1.1 bjh21 c = pckbport_poll_data1(t, slot);
240 1.1 bjh21 if (c != -1 && q && CMD_IN_QUEUE(q)) {
241 1.1 bjh21 /* we jumped into a running command - try to
242 1.1 bjh21 deliver the response */
243 1.1 bjh21 if (pckbport_cmdresponse(t, slot, c))
244 1.1 bjh21 return (-1);
245 1.1 bjh21 }
246 1.1 bjh21 return (c);
247 1.1 bjh21 }
248 1.1 bjh21
249 1.1 bjh21 /*
250 1.1 bjh21 * switch scancode translation on / off
251 1.1 bjh21 * return nonzero on success
252 1.1 bjh21 */
253 1.1 bjh21 int
254 1.1 bjh21 pckbport_xt_translation(t, slot, on)
255 1.1 bjh21 pckbport_tag_t t;
256 1.1 bjh21 pckbport_slot_t slot;
257 1.1 bjh21 int on;
258 1.1 bjh21 {
259 1.1 bjh21
260 1.1 bjh21 return t->t_ops->t_xt_translation(t->t_cookie, slot, on);
261 1.1 bjh21 }
262 1.1 bjh21
263 1.1 bjh21 void
264 1.1 bjh21 pckbport_slot_enable(t, slot, on)
265 1.1 bjh21 pckbport_tag_t t;
266 1.1 bjh21 pckbport_slot_t slot;
267 1.1 bjh21 int on;
268 1.1 bjh21 {
269 1.1 bjh21
270 1.1 bjh21 t->t_ops->t_slot_enable(t->t_cookie, slot, on);
271 1.1 bjh21 }
272 1.1 bjh21
273 1.1 bjh21 void
274 1.1 bjh21 pckbport_set_poll(t, slot, on)
275 1.1 bjh21 pckbport_tag_t t;
276 1.1 bjh21 pckbport_slot_t slot;
277 1.1 bjh21 int on;
278 1.1 bjh21 {
279 1.1 bjh21
280 1.1 bjh21 t->t_slotdata[slot]->polling = on;
281 1.1 bjh21 t->t_ops->t_set_poll(t->t_cookie, slot, on);
282 1.1 bjh21 }
283 1.1 bjh21
284 1.1 bjh21 /*
285 1.1 bjh21 * Pass command to device, poll for ACK and data.
286 1.1 bjh21 * to be called at spltty()
287 1.1 bjh21 */
288 1.1 bjh21 static void
289 1.1 bjh21 pckbport_poll_cmd1(t, slot, cmd)
290 1.1 bjh21 struct pckbport_tag *t;
291 1.1 bjh21 pckbport_slot_t slot;
292 1.1 bjh21 struct pckbport_devcmd *cmd;
293 1.1 bjh21 {
294 1.1 bjh21 int i, c = 0;
295 1.1 bjh21
296 1.1 bjh21 while (cmd->cmdidx < cmd->cmdlen) {
297 1.1 bjh21 if (!pckbport_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
298 1.1 bjh21 printf("pckbport_cmd: send error\n");
299 1.1 bjh21 cmd->status = EIO;
300 1.1 bjh21 return;
301 1.1 bjh21 }
302 1.1 bjh21 for (i = 10; i; i--) { /* 1s ??? */
303 1.1 bjh21 c = pckbport_poll_data1(t, slot);
304 1.1 bjh21 if (c != -1)
305 1.1 bjh21 break;
306 1.1 bjh21 }
307 1.1 bjh21
308 1.1 bjh21 if (c == KBC_DEVCMD_ACK) {
309 1.1 bjh21 cmd->cmdidx++;
310 1.1 bjh21 continue;
311 1.1 bjh21 }
312 1.1 bjh21 if (c == KBC_DEVCMD_RESEND) {
313 1.1 bjh21 #ifdef PCKBPORTDEBUG
314 1.1 bjh21 printf("pckbport_cmd: RESEND\n");
315 1.1 bjh21 #endif
316 1.1 bjh21 if (cmd->retries++ < 5)
317 1.1 bjh21 continue;
318 1.1 bjh21 else {
319 1.1 bjh21 #ifdef PCKBPORTDEBUG
320 1.1 bjh21 printf("pckbport: cmd failed\n");
321 1.1 bjh21 #endif
322 1.1 bjh21 cmd->status = EIO;
323 1.1 bjh21 return;
324 1.1 bjh21 }
325 1.1 bjh21 }
326 1.1 bjh21 if (c == -1) {
327 1.1 bjh21 #ifdef PCKBPORTDEBUG
328 1.1 bjh21 printf("pckbport_cmd: timeout\n");
329 1.1 bjh21 #endif
330 1.1 bjh21 cmd->status = EIO;
331 1.1 bjh21 return;
332 1.1 bjh21 }
333 1.1 bjh21 #ifdef PCKBPORTDEBUG
334 1.1 bjh21 printf("pckbport_cmd: lost 0x%x\n", c);
335 1.1 bjh21 #endif
336 1.1 bjh21 }
337 1.1 bjh21
338 1.1 bjh21 while (cmd->responseidx < cmd->responselen) {
339 1.1 bjh21 if (cmd->flags & KBC_CMDFLAG_SLOW)
340 1.1 bjh21 i = 100; /* 10s ??? */
341 1.1 bjh21 else
342 1.1 bjh21 i = 10; /* 1s ??? */
343 1.1 bjh21 while (i--) {
344 1.1 bjh21 c = pckbport_poll_data1(t, slot);
345 1.1 bjh21 if (c != -1)
346 1.1 bjh21 break;
347 1.1 bjh21 }
348 1.1 bjh21 if (c == -1) {
349 1.1 bjh21 #ifdef PCKBPORTDEBUG
350 1.1 bjh21 printf("pckbport_cmd: no data\n");
351 1.1 bjh21 #endif
352 1.1 bjh21 cmd->status = ETIMEDOUT;
353 1.1 bjh21 return;
354 1.1 bjh21 } else
355 1.1 bjh21 cmd->response[cmd->responseidx++] = c;
356 1.1 bjh21 }
357 1.1 bjh21 }
358 1.1 bjh21
359 1.1 bjh21 /* for use in autoconfiguration */
360 1.1 bjh21 int
361 1.1 bjh21 pckbport_poll_cmd(t, slot, cmd, len, responselen, respbuf, slow)
362 1.1 bjh21 pckbport_tag_t t;
363 1.1 bjh21 pckbport_slot_t slot;
364 1.1 bjh21 u_char *cmd;
365 1.1 bjh21 int len, responselen;
366 1.1 bjh21 u_char *respbuf;
367 1.1 bjh21 int slow;
368 1.1 bjh21 {
369 1.1 bjh21 struct pckbport_devcmd nc;
370 1.1 bjh21
371 1.1 bjh21 if ((len > 4) || (responselen > 4))
372 1.1 bjh21 return (EINVAL);
373 1.1 bjh21
374 1.1 bjh21 memset(&nc, 0, sizeof(nc));
375 1.1 bjh21 memcpy(nc.cmd, cmd, len);
376 1.1 bjh21 nc.cmdlen = len;
377 1.1 bjh21 nc.responselen = responselen;
378 1.1 bjh21 nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
379 1.1 bjh21
380 1.1 bjh21 pckbport_poll_cmd1(t, slot, &nc);
381 1.1 bjh21
382 1.1 bjh21 if (nc.status == 0 && respbuf)
383 1.1 bjh21 memcpy(respbuf, nc.response, responselen);
384 1.1 bjh21
385 1.1 bjh21 return (nc.status);
386 1.1 bjh21 }
387 1.1 bjh21
388 1.1 bjh21 /*
389 1.1 bjh21 * Clean up a command queue, throw away everything.
390 1.1 bjh21 */
391 1.1 bjh21 void
392 1.1 bjh21 pckbport_cleanqueue(q)
393 1.1 bjh21 struct pckbport_slotdata *q;
394 1.1 bjh21 {
395 1.1 bjh21 struct pckbport_devcmd *cmd;
396 1.1 bjh21 #ifdef PCKBPORTDEBUG
397 1.1 bjh21 int i;
398 1.1 bjh21 #endif
399 1.1 bjh21
400 1.1 bjh21 while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
401 1.1 bjh21 TAILQ_REMOVE(&q->cmdqueue, cmd, next);
402 1.1 bjh21 #ifdef PCKBPORTDEBUG
403 1.1 bjh21 printf("pckbport_cleanqueue: removing");
404 1.1 bjh21 for (i = 0; i < cmd->cmdlen; i++)
405 1.1 bjh21 printf(" %02x", cmd->cmd[i]);
406 1.1 bjh21 printf("\n");
407 1.1 bjh21 #endif
408 1.1 bjh21 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
409 1.1 bjh21 }
410 1.1 bjh21 }
411 1.1 bjh21
412 1.1 bjh21 /*
413 1.1 bjh21 * Timeout error handler: clean queues and data port.
414 1.1 bjh21 * XXX could be less invasive.
415 1.1 bjh21 */
416 1.1 bjh21 void
417 1.1 bjh21 pckbport_cleanup(self)
418 1.1 bjh21 void *self;
419 1.1 bjh21 {
420 1.1 bjh21 struct pckbport_tag *t = self;
421 1.1 bjh21 int s;
422 1.1 bjh21
423 1.1 bjh21 printf("pckbport: command timeout\n");
424 1.1 bjh21
425 1.1 bjh21 s = spltty();
426 1.1 bjh21
427 1.1 bjh21 if (t->t_slotdata[PCKBPORT_KBD_SLOT])
428 1.1 bjh21 pckbport_cleanqueue(t->t_slotdata[PCKBPORT_KBD_SLOT]);
429 1.1 bjh21 if (t->t_slotdata[PCKBPORT_AUX_SLOT])
430 1.1 bjh21 pckbport_cleanqueue(t->t_slotdata[PCKBPORT_AUX_SLOT]);
431 1.1 bjh21
432 1.1 bjh21 #if 0 /* XXXBJH Move to controller driver? */
433 1.1 bjh21 while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
434 1.1 bjh21 KBD_DELAY;
435 1.1 bjh21 (void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
436 1.1 bjh21 }
437 1.1 bjh21 #endif
438 1.1 bjh21
439 1.1 bjh21 /* reset KBC? */
440 1.1 bjh21
441 1.1 bjh21 splx(s);
442 1.1 bjh21 }
443 1.1 bjh21
444 1.1 bjh21 /*
445 1.1 bjh21 * Pass command to device during normal operation.
446 1.1 bjh21 * to be called at spltty()
447 1.1 bjh21 */
448 1.1 bjh21 void
449 1.1 bjh21 pckbport_start(t, slot)
450 1.1 bjh21 struct pckbport_tag *t;
451 1.1 bjh21 pckbport_slot_t slot;
452 1.1 bjh21 {
453 1.1 bjh21 struct pckbport_slotdata *q = t->t_slotdata[slot];
454 1.1 bjh21 struct pckbport_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
455 1.1 bjh21
456 1.1 bjh21 if (q->polling) {
457 1.1 bjh21 do {
458 1.1 bjh21 pckbport_poll_cmd1(t, slot, cmd);
459 1.1 bjh21 if (cmd->status)
460 1.1 bjh21 printf("pckbport_start: command error\n");
461 1.1 bjh21
462 1.1 bjh21 TAILQ_REMOVE(&q->cmdqueue, cmd, next);
463 1.1 bjh21 if (cmd->flags & KBC_CMDFLAG_SYNC)
464 1.1 bjh21 wakeup(cmd);
465 1.1 bjh21 else {
466 1.1 bjh21 callout_stop(&t->t_cleanup);
467 1.1 bjh21 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
468 1.1 bjh21 }
469 1.1 bjh21 cmd = TAILQ_FIRST(&q->cmdqueue);
470 1.1 bjh21 } while (cmd);
471 1.1 bjh21 return;
472 1.1 bjh21 }
473 1.1 bjh21
474 1.1 bjh21 if (!pckbport_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
475 1.1 bjh21 printf("pckbport_start: send error\n");
476 1.1 bjh21 /* XXX what now? */
477 1.1 bjh21 return;
478 1.1 bjh21 }
479 1.1 bjh21 }
480 1.1 bjh21
481 1.1 bjh21 /*
482 1.1 bjh21 * Handle command responses coming in asynchronously,
483 1.1 bjh21 * return nonzero if valid response.
484 1.1 bjh21 * to be called at spltty()
485 1.1 bjh21 */
486 1.1 bjh21 int
487 1.1 bjh21 pckbport_cmdresponse(t, slot, data)
488 1.1 bjh21 struct pckbport_tag *t;
489 1.1 bjh21 pckbport_slot_t slot;
490 1.1 bjh21 u_char data;
491 1.1 bjh21 {
492 1.1 bjh21 struct pckbport_slotdata *q = t->t_slotdata[slot];
493 1.1 bjh21 struct pckbport_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
494 1.1 bjh21 #ifdef DIAGNOSTIC
495 1.1 bjh21 if (!cmd)
496 1.1 bjh21 panic("pckbport_cmdresponse: no active command");
497 1.1 bjh21 #endif
498 1.1 bjh21 if (cmd->cmdidx < cmd->cmdlen) {
499 1.1 bjh21 if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
500 1.1 bjh21 return (0);
501 1.1 bjh21
502 1.1 bjh21 if (data == KBC_DEVCMD_RESEND) {
503 1.1 bjh21 if (cmd->retries++ < 5) {
504 1.1 bjh21 /* try again last command */
505 1.1 bjh21 goto restart;
506 1.1 bjh21 } else {
507 1.1 bjh21 #ifdef PCKBPORTDEBUG
508 1.1 bjh21 printf("pckbport: cmd failed\n");
509 1.1 bjh21 #endif
510 1.1 bjh21 cmd->status = EIO;
511 1.1 bjh21 /* dequeue */
512 1.1 bjh21 }
513 1.1 bjh21 } else {
514 1.1 bjh21 if (++cmd->cmdidx < cmd->cmdlen)
515 1.1 bjh21 goto restart;
516 1.1 bjh21 if (cmd->responselen)
517 1.1 bjh21 return (1);
518 1.1 bjh21 /* else dequeue */
519 1.1 bjh21 }
520 1.1 bjh21 } else if (cmd->responseidx < cmd->responselen) {
521 1.1 bjh21 cmd->response[cmd->responseidx++] = data;
522 1.1 bjh21 if (cmd->responseidx < cmd->responselen)
523 1.1 bjh21 return (1);
524 1.1 bjh21 /* else dequeue */
525 1.1 bjh21 } else
526 1.1 bjh21 return (0);
527 1.1 bjh21
528 1.1 bjh21 /* dequeue: */
529 1.1 bjh21 TAILQ_REMOVE(&q->cmdqueue, cmd, next);
530 1.1 bjh21 if (cmd->flags & KBC_CMDFLAG_SYNC)
531 1.1 bjh21 wakeup(cmd);
532 1.1 bjh21 else {
533 1.1 bjh21 callout_stop(&t->t_cleanup);
534 1.1 bjh21 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
535 1.1 bjh21 }
536 1.1 bjh21 if (!CMD_IN_QUEUE(q))
537 1.1 bjh21 return (1);
538 1.1 bjh21 restart:
539 1.1 bjh21 pckbport_start(t, slot);
540 1.1 bjh21 return (1);
541 1.1 bjh21 }
542 1.1 bjh21
543 1.1 bjh21 /*
544 1.1 bjh21 * Put command into the device's command queue, return zero or errno.
545 1.1 bjh21 */
546 1.1 bjh21 int
547 1.1 bjh21 pckbport_enqueue_cmd(t, slot, cmd, len, responselen, sync, respbuf)
548 1.1 bjh21 pckbport_tag_t t;
549 1.1 bjh21 pckbport_slot_t slot;
550 1.1 bjh21 u_char *cmd;
551 1.1 bjh21 int len, responselen, sync;
552 1.1 bjh21 u_char *respbuf;
553 1.1 bjh21 {
554 1.1 bjh21 struct pckbport_slotdata *q = t->t_slotdata[slot];
555 1.1 bjh21 struct pckbport_devcmd *nc;
556 1.1 bjh21 int s, isactive, res = 0;
557 1.1 bjh21
558 1.1 bjh21 if ((len > 4) || (responselen > 4))
559 1.1 bjh21 return (EINVAL);
560 1.1 bjh21 s = spltty();
561 1.1 bjh21 nc = TAILQ_FIRST(&q->freequeue);
562 1.1 bjh21 if (nc) {
563 1.1 bjh21 TAILQ_REMOVE(&q->freequeue, nc, next);
564 1.1 bjh21 }
565 1.1 bjh21 splx(s);
566 1.1 bjh21 if (!nc)
567 1.1 bjh21 return (ENOMEM);
568 1.1 bjh21
569 1.1 bjh21 memset(nc, 0, sizeof(*nc));
570 1.1 bjh21 memcpy(nc->cmd, cmd, len);
571 1.1 bjh21 nc->cmdlen = len;
572 1.1 bjh21 nc->responselen = responselen;
573 1.1 bjh21 nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
574 1.1 bjh21
575 1.1 bjh21 s = spltty();
576 1.1 bjh21
577 1.1 bjh21 if (q->polling && sync) {
578 1.1 bjh21 /*
579 1.1 bjh21 * XXX We should poll until the queue is empty.
580 1.1 bjh21 * But we don't come here normally, so make
581 1.1 bjh21 * it simple and throw away everything.
582 1.1 bjh21 */
583 1.1 bjh21 pckbport_cleanqueue(q);
584 1.1 bjh21 }
585 1.1 bjh21
586 1.1 bjh21 isactive = CMD_IN_QUEUE(q);
587 1.1 bjh21 TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
588 1.1 bjh21 if (!isactive)
589 1.1 bjh21 pckbport_start(t, slot);
590 1.1 bjh21
591 1.1 bjh21 if (q->polling)
592 1.1 bjh21 res = (sync ? nc->status : 0);
593 1.1 bjh21 else if (sync) {
594 1.1 bjh21 if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
595 1.1 bjh21 TAILQ_REMOVE(&q->cmdqueue, nc, next);
596 1.1 bjh21 pckbport_cleanup(t);
597 1.1 bjh21 } else
598 1.1 bjh21 res = nc->status;
599 1.1 bjh21 } else
600 1.1 bjh21 callout_reset(&t->t_cleanup, hz, pckbport_cleanup, t);
601 1.1 bjh21
602 1.1 bjh21 if (sync) {
603 1.1 bjh21 if (respbuf)
604 1.1 bjh21 memcpy(respbuf, nc->response, responselen);
605 1.1 bjh21 TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
606 1.1 bjh21 }
607 1.1 bjh21
608 1.1 bjh21 splx(s);
609 1.1 bjh21
610 1.1 bjh21 return (res);
611 1.1 bjh21 }
612 1.1 bjh21
613 1.1 bjh21 void
614 1.1 bjh21 pckbport_set_inputhandler(t, slot, func, arg, name)
615 1.1 bjh21 pckbport_tag_t t;
616 1.1 bjh21 pckbport_slot_t slot;
617 1.1 bjh21 pckbport_inputfcn func;
618 1.1 bjh21 void *arg;
619 1.1 bjh21 char *name;
620 1.1 bjh21 {
621 1.1 bjh21
622 1.1 bjh21 if (slot >= PCKBPORT_NSLOTS)
623 1.1 bjh21 panic("pckbport_set_inputhandler: bad slot %d", slot);
624 1.1 bjh21
625 1.1 bjh21 t->t_ops->t_intr_establish(t->t_cookie, slot);
626 1.1 bjh21
627 1.1 bjh21 t->t_inputhandler[slot] = func;
628 1.1 bjh21 t->t_inputarg[slot] = arg;
629 1.1 bjh21 t->t_subname[slot] = name;
630 1.1 bjh21 }
631 1.1 bjh21
632 1.1 bjh21 void
633 1.1 bjh21 pckbportintr(pckbport_tag_t t, pckbport_slot_t slot, int data)
634 1.1 bjh21 {
635 1.1 bjh21 struct pckbport_slotdata *q;
636 1.1 bjh21
637 1.1 bjh21 q = t->t_slotdata[slot];
638 1.1 bjh21
639 1.1 bjh21 if (!q) {
640 1.1 bjh21 /* XXX do something for live insertion? */
641 1.1 bjh21 printf("pckbportintr: no dev for slot %d\n", slot);
642 1.1 bjh21 return;
643 1.1 bjh21 }
644 1.1 bjh21
645 1.1 bjh21 if (CMD_IN_QUEUE(q) && pckbport_cmdresponse(t, slot, data))
646 1.1 bjh21 return;
647 1.1 bjh21
648 1.1 bjh21 if (t->t_inputhandler[slot])
649 1.1 bjh21 (*t->t_inputhandler[slot])(t->t_inputarg[slot], data);
650 1.1 bjh21 #ifdef PCKBPORTDEBUG
651 1.1 bjh21 else
652 1.1 bjh21 printf("pckbportintr: slot %d lost %d\n", slot, data);
653 1.1 bjh21 #endif
654 1.1 bjh21 }
655 1.1 bjh21
656 1.1 bjh21 int
657 1.1 bjh21 pckbport_cnattach(void *cookie, struct pckbport_accessops const *ops,
658 1.1 bjh21 pckbport_slot_t slot)
659 1.1 bjh21 {
660 1.1 bjh21 int res = 0;
661 1.1 bjh21 pckbport_tag_t t = &pckbport_cntag;
662 1.1 bjh21
663 1.1 bjh21 t->t_cookie = cookie;
664 1.1 bjh21 t->t_ops = ops;
665 1.1 bjh21
666 1.1 bjh21 /* flush */
667 1.1 bjh21 pckbport_flush(t, slot);
668 1.1 bjh21
669 1.1 bjh21 #if (NPCKBD > 0)
670 1.1 bjh21 res = pckbd_cnattach(t, slot);
671 1.1 bjh21 #elif (NPCKBPORT_MACHDEP_CNATTACH > 0)
672 1.1 bjh21 res = pckbport_machdep_cnattach(t, slot);
673 1.1 bjh21 #else
674 1.1 bjh21 res = ENXIO;
675 1.1 bjh21 #endif /* NPCKBPORT_MACHDEP_CNATTACH > 0 */
676 1.1 bjh21
677 1.1 bjh21 if (res == 0) {
678 1.1 bjh21 t->t_slotdata[slot] = &pckbport_cons_slotdata;
679 1.1 bjh21 pckbport_init_slotdata(&pckbport_cons_slotdata);
680 1.1 bjh21 }
681 1.1 bjh21
682 1.1 bjh21 return res;
683 1.1 bjh21 }
684