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