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