xref: /netbsd/sys/arch/landisk/dev/btn_obio.c (revision c7146e4c)
1 /*	$NetBSD: btn_obio.c,v 1.7 2021/07/15 05:07:50 rin Exp $	*/
2 
3 /*-
4  * Copyright (C) 2005 NONAKA Kimihiro <nonaka@netbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "pwrsw_obio.h"
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: btn_obio.c,v 1.7 2021/07/15 05:07:50 rin Exp $");
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.h>
40 #include <sys/ioctl.h>
41 #include <sys/callout.h>
42 
43 #include <dev/sysmon/sysmonvar.h>
44 #include <dev/sysmon/sysmon_taskq.h>
45 
46 #include <sh3/devreg.h>
47 
48 #include <machine/button.h>
49 
50 #include <landisk/landisk/landiskreg.h>
51 #include <landisk/dev/obiovar.h>
52 #include <landisk/dev/buttonvar.h>
53 
54 #define	BTN_SELECT	0
55 #define	BTN_COPY	1
56 #define	BTN_REMOVE	2
57 #define	NBUTTON		3
58 
59 struct btn_obio_softc {
60 	device_t		sc_dev;
61 	void			*sc_ih;
62 
63 	struct callout		sc_guard_ch;
64 	struct sysmon_pswitch	sc_smpsw;	/* reset */
65 	struct btn_event	sc_bev[NBUTTON];
66 
67 	int			sc_mask;
68 };
69 
70 #ifndef	BTN_TIMEOUT
71 #define	BTN_TIMEOUT	(hz / 10)	/* 100ms */
72 #endif
73 
74 static int btn_obio_probe(device_t, cfdata_t, void *);
75 static void btn_obio_attach(device_t, device_t, void *);
76 
77 static int btn_intr(void *aux);
78 static void btn_sysmon_pressed_event(void *arg);
79 static void btn_pressed_event(void *arg);
80 static void btn_guard_timeout(void *arg);
81 
82 static struct btn_obio_softc *btn_softc;
83 
84 static const struct {
85 	int idx;
86 	int mask;
87 	const char *name;
88 } btnlist[NBUTTON] = {
89 	{ BTN_SELECT,	BTN_SELECT_BIT, "select" },
90 	{ BTN_COPY,	BTN_COPY_BIT,	"copy"   },
91 	{ BTN_REMOVE,	BTN_REMOVE_BIT,	"remove" },
92 };
93 
94 CFATTACH_DECL_NEW(btn_obio, sizeof(struct btn_obio_softc),
95     btn_obio_probe, btn_obio_attach, NULL, NULL);
96 
97 static int
btn_obio_probe(device_t parent,cfdata_t cfp,void * aux)98 btn_obio_probe(device_t parent, cfdata_t cfp, void *aux)
99 {
100 	struct obio_attach_args *oa = aux;
101 
102 	if (btn_softc)
103 		return (0);
104 
105 	oa->oa_nio = 0;
106 	oa->oa_niomem = 0;
107 	oa->oa_nirq = 1;
108 	oa->oa_irq[0].or_irq = LANDISK_INTR_BTN;
109 
110 	return (1);
111 }
112 
113 static void
btn_obio_attach(device_t parent,device_t self,void * aux)114 btn_obio_attach(device_t parent, device_t self, void *aux)
115 {
116 	struct btn_obio_softc *sc;
117 	int i;
118 
119 	aprint_naive("\n");
120 	aprint_normal(": USL-5P buttons\n");
121 
122 	sc = device_private(self);
123 	sc->sc_dev = self;
124 
125 	btn_softc = sc;
126 
127 	callout_init(&sc->sc_guard_ch, 0);
128 	callout_setfunc(&sc->sc_guard_ch, btn_guard_timeout, sc);
129 
130 	sc->sc_ih = extintr_establish(LANDISK_INTR_BTN, IPL_TTY, btn_intr, sc);
131 	if (sc->sc_ih == NULL) {
132 		aprint_error_dev(self, "unable to establish interrupt");
133 		panic("extintr_establish");
134 	}
135 
136 	sc->sc_smpsw.smpsw_name = device_xname(self);
137 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_RESET;
138 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
139 		aprint_error_dev(self, "unable to register with sysmon\n");
140 		return;
141 	}
142 	sc->sc_mask |= BTN_RESET_BIT;
143 
144 	btn_init();
145 	for (i = 0; i < NBUTTON; i++) {
146 		int idx = btnlist[i].idx;
147 		sc->sc_bev[idx].bev_name = btnlist[i].name;
148 		if (btn_event_register(&sc->sc_bev[idx]) != 0) {
149 			aprint_error_dev(self,
150 					 "unable to register '%s' button\n",
151 					 btnlist[i].name);
152 		} else {
153 			sc->sc_mask |= btnlist[i].mask;
154 		}
155 	}
156 }
157 
158 static int
btn_intr(void * arg)159 btn_intr(void *arg)
160 {
161 	struct btn_obio_softc *sc = (void *)arg;
162 	device_t self = sc->sc_dev;
163 	int status;
164 	int i;
165 
166 	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
167 	if (status == -1) {
168 		return (0);
169 	}
170 
171 	status = ~status;
172 	if (status & BTN_ALL_BIT) {
173 		if (status & BTN_RESET_BIT) {
174 			if (sc->sc_mask & BTN_RESET_BIT) {
175 				extintr_disable(sc->sc_ih);
176 #if NPWRSW_OBIO > 0
177 				extintr_disable_by_num(LANDISK_INTR_PWRSW);
178 #endif
179 				sysmon_task_queue_sched(0,
180 				    btn_sysmon_pressed_event, sc);
181 				return (1);
182 			} else {
183 				aprint_normal_dev(self,
184 					"reset button pressed\n");
185 			}
186 		}
187 
188 		for (i = 0; i < NBUTTON; i++) {
189 			uint8_t mask = btnlist[i].mask;
190 			int rv = 0;
191 			if (status & mask) {
192 				if (sc->sc_mask & mask) {
193 					sysmon_task_queue_sched(1,
194 					    btn_pressed_event,
195 					    &sc->sc_bev[btnlist[i].idx]);
196 				} else {
197 					aprint_normal_dev(self,
198 						"%s button pressed\n",
199 						btnlist[i].name);
200 				}
201 				rv = 1;
202 			}
203 			if (rv != 0) {
204 				extintr_disable(sc->sc_ih);
205 				callout_schedule(&sc->sc_guard_ch, BTN_TIMEOUT);
206 			}
207 		}
208 		return (1);
209 	}
210 	return (0);
211 }
212 
213 static void
btn_sysmon_pressed_event(void * arg)214 btn_sysmon_pressed_event(void *arg)
215 {
216 	struct btn_obio_softc *sc = (void *)arg;
217 
218 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
219 }
220 
221 static void
btn_pressed_event(void * arg)222 btn_pressed_event(void *arg)
223 {
224 	struct btn_event *bev = (void *)arg;
225 
226 	btn_event_send(bev, BUTTON_EVENT_PRESSED);
227 }
228 
229 static void
btn_guard_timeout(void * arg)230 btn_guard_timeout(void *arg)
231 {
232 	struct btn_obio_softc *sc = arg;
233 
234 	callout_stop(&sc->sc_guard_ch);
235 	extintr_enable(sc->sc_ih);
236 }
237