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