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