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