pwrsw_obio.c revision 1.1
1/* $NetBSD: pwrsw_obio.c,v 1.1 2006/09/01 21:26:18 uwe 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 "btn_obio.h" 30 31#include <sys/cdefs.h> 32__KERNEL_RCSID(0, "$NetBSD: pwrsw_obio.c,v 1.1 2006/09/01 21:26:18 uwe 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/conf.h> 40#include <sys/ioctl.h> 41 42#include <dev/sysmon/sysmonvar.h> 43#include <dev/sysmon/sysmon_taskq.h> 44 45#include <sh3/devreg.h> 46 47#include <landisk/landisk/landiskreg.h> 48#include <landisk/dev/obiovar.h> 49 50struct pwrsw_obio_softc { 51 struct device sc_dev; 52 void *sc_ih; 53 54 struct sysmon_pswitch sc_smpsw; /* our sysmon glue */ 55 56 int sc_flags; 57#define SYSMON_ATTACHED 1 58}; 59 60static int pwrsw_obio_probe(struct device *, struct cfdata *, void *); 61static void pwrsw_obio_attach(struct device *, struct device *, void *); 62 63static int pwrsw_intr(void *aux); 64static void pwrsw_pressed_event(void *arg); 65 66CFATTACH_DECL(pwrsw_obio, sizeof(struct pwrsw_obio_softc), 67 pwrsw_obio_probe, pwrsw_obio_attach, NULL, NULL); 68 69static struct pwrsw_obio_softc *pwrsw_softc; 70 71static int 72pwrsw_obio_probe(struct device *parent, struct cfdata *cfp, void *aux) 73{ 74 struct obio_attach_args *oa = aux; 75 76 if (pwrsw_softc) 77 return (0); 78 79 oa->oa_nio = 0; 80 oa->oa_niomem = 0; 81 oa->oa_nirq = 1; 82 oa->oa_irq[0].or_irq = LANDISK_INTR_PWRSW; 83 84 return (1); 85} 86 87static void 88pwrsw_obio_attach(struct device *parent, struct device *self, void *aux) 89{ 90 struct pwrsw_obio_softc *sc = (void *)self; 91 92 printf(": Power Switch\n"); 93 94 pwrsw_softc = sc; 95 96 sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname; 97 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 98 99 sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY, 100 pwrsw_intr, sc); 101 if (sc->sc_ih == NULL) { 102 printf("%s: couldn't establish intr handler", 103 sc->sc_dev.dv_xname); 104 panic("extintr_establish"); 105 } 106 107 if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) { 108 printf("%s: unable to register with sysmon\n", 109 sc->sc_dev.dv_xname); 110 return; 111 } 112 sc->sc_flags |= SYSMON_ATTACHED; 113} 114 115static int 116pwrsw_intr(void *arg) 117{ 118 struct pwrsw_obio_softc *sc = (void *)arg; 119 int status; 120 121 status = (int8_t)_reg_read_1(LANDISK_BTNSTAT); 122 if (status == -1) { 123 return (0); 124 } 125 126 status = ~status; 127 if (status & BTN_POWER_BIT) { 128 if (sc->sc_flags & SYSMON_ATTACHED) { 129 sysmon_task_queue_sched(0, pwrsw_pressed_event, sc); 130 extintr_disable(sc->sc_ih); 131#if NBTN_OBIO > 0 132 extintr_disable_by_num(LANDISK_INTR_BTN); 133#endif 134 } else { 135 printf("%s: pressed\n", sc->sc_dev.dv_xname); 136 } 137 _reg_write_1(LANDISK_PWRSW_INTCLR, 1); 138 return (1); 139 } 140 return (0); 141} 142 143static void 144pwrsw_pressed_event(void *arg) 145{ 146 struct pwrsw_obio_softc *sc = (void *)arg; 147 148 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 149} 150