btn_obio.c revision 1.1
11.1Suwe/*	$NetBSD: btn_obio.c,v 1.1 2006/09/01 21:26:18 uwe Exp $	*/
21.1Suwe
31.1Suwe/*-
41.1Suwe * Copyright (c) 2005 NONAKA Kimihiro
51.1Suwe * All rights reserved.
61.1Suwe *
71.1Suwe * Redistribution and use in source and binary forms, with or without
81.1Suwe * modification, are permitted provided that the following conditions
91.1Suwe * are met:
101.1Suwe * 1. Redistributions of source code must retain the above copyright
111.1Suwe *    notice, this list of conditions and the following disclaimer.
121.1Suwe * 2. Redistributions in binary form must reproduce the above copyright
131.1Suwe *    notice, this list of conditions and the following disclaimer in the
141.1Suwe *    documentation and/or other materials provided with the distribution.
151.1Suwe *
161.1Suwe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
171.1Suwe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181.1Suwe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Suwe * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
201.1Suwe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Suwe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Suwe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Suwe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Suwe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Suwe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Suwe * SUCH DAMAGE.
271.1Suwe */
281.1Suwe
291.1Suwe#include "pwrsw_obio.h"
301.1Suwe
311.1Suwe#include <sys/cdefs.h>
321.1Suwe__KERNEL_RCSID(0, "$NetBSD: btn_obio.c,v 1.1 2006/09/01 21:26:18 uwe Exp $");
331.1Suwe
341.1Suwe#include <sys/types.h>
351.1Suwe#include <sys/param.h>
361.1Suwe#include <sys/systm.h>
371.1Suwe#include <sys/device.h>
381.1Suwe#include <sys/malloc.h>
391.1Suwe#include <sys/kernel.h>
401.1Suwe#include <sys/conf.h>
411.1Suwe#include <sys/ioctl.h>
421.1Suwe#include <sys/callout.h>
431.1Suwe
441.1Suwe#include <dev/sysmon/sysmonvar.h>
451.1Suwe#include <dev/sysmon/sysmon_taskq.h>
461.1Suwe
471.1Suwe#include <sh3/devreg.h>
481.1Suwe
491.1Suwe#include <machine/button.h>
501.1Suwe
511.1Suwe#include <landisk/landisk/landiskreg.h>
521.1Suwe#include <landisk/dev/obiovar.h>
531.1Suwe#include <landisk/dev/buttonvar.h>
541.1Suwe
551.1Suwe#define	BTN_SELECT	0
561.1Suwe#define	BTN_COPY	1
571.1Suwe#define	BTN_REMOVE	2
581.1Suwe#define	NBUTTON		3
591.1Suwe
601.1Suwestruct btn_obio_softc {
611.1Suwe	struct device		sc_dev;
621.1Suwe	void			*sc_ih;
631.1Suwe
641.1Suwe	struct callout		sc_guard_ch;
651.1Suwe	struct sysmon_pswitch	sc_smpsw;	/* reset */
661.1Suwe	struct btn_event	sc_bev[NBUTTON];
671.1Suwe
681.1Suwe	int			sc_mask;
691.1Suwe};
701.1Suwe
711.1Suwe#ifndef	BTN_TIMEOUT
721.1Suwe#define	BTN_TIMEOUT	(hz / 10)	/* 100ms */
731.1Suwe#endif
741.1Suwe
751.1Suwestatic int btn_obio_probe(struct device *, struct cfdata *, void *);
761.1Suwestatic void btn_obio_attach(struct device *, struct device *, void *);
771.1Suwe
781.1Suwestatic int btn_intr(void *aux);
791.1Suwestatic void btn_sysmon_pressed_event(void *arg);
801.1Suwestatic void btn_pressed_event(void *arg);
811.1Suwestatic void btn_guard_timeout(void *arg);
821.1Suwe
831.1Suwestatic struct btn_obio_softc *btn_softc;
841.1Suwe
851.1Suwestatic const struct {
861.1Suwe	int idx;
871.1Suwe	int mask;
881.1Suwe	const char *name;
891.1Suwe} btnlist[NBUTTON] = {
901.1Suwe	{ BTN_SELECT,	BTN_SELECT_BIT, "select" },
911.1Suwe	{ BTN_COPY,	BTN_COPY_BIT,	"copy"   },
921.1Suwe	{ BTN_REMOVE,	BTN_REMOVE_BIT,	"remove" },
931.1Suwe};
941.1Suwe
951.1SuweCFATTACH_DECL(btn_obio, sizeof(struct btn_obio_softc),
961.1Suwe    btn_obio_probe, btn_obio_attach, NULL, NULL);
971.1Suwe
981.1Suwestatic int
991.1Suwebtn_obio_probe(struct device *parent, struct cfdata *cfp, void *aux)
1001.1Suwe{
1011.1Suwe	struct obio_attach_args *oa = aux;
1021.1Suwe
1031.1Suwe	if (btn_softc)
1041.1Suwe		return (0);
1051.1Suwe
1061.1Suwe	oa->oa_nio = 0;
1071.1Suwe	oa->oa_niomem = 0;
1081.1Suwe	oa->oa_nirq = 1;
1091.1Suwe	oa->oa_irq[0].or_irq = LANDISK_INTR_BTN;
1101.1Suwe
1111.1Suwe	return (1);
1121.1Suwe}
1131.1Suwe
1141.1Suwestatic void
1151.1Suwebtn_obio_attach(struct device *parent, struct device *self, void *aux)
1161.1Suwe{
1171.1Suwe	struct btn_obio_softc *sc = (void *)self;
1181.1Suwe	int i;
1191.1Suwe
1201.1Suwe	printf(": USL-5P Button\n");
1211.1Suwe
1221.1Suwe	btn_softc = sc;
1231.1Suwe
1241.1Suwe	callout_init(&sc->sc_guard_ch);
1251.1Suwe	callout_setfunc(&sc->sc_guard_ch, btn_guard_timeout, sc);
1261.1Suwe
1271.1Suwe	sc->sc_ih = extintr_establish(LANDISK_INTR_BTN, IPL_TTY, btn_intr, sc);
1281.1Suwe	if (sc->sc_ih == NULL) {
1291.1Suwe		printf("%s: couldn't establish intr handler",
1301.1Suwe		    sc->sc_dev.dv_xname);
1311.1Suwe		panic("extintr_establish");
1321.1Suwe	}
1331.1Suwe
1341.1Suwe	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
1351.1Suwe	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_RESET;
1361.1Suwe	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1371.1Suwe		printf("%s: unable to register with sysmon\n",
1381.1Suwe		    sc->sc_dev.dv_xname);
1391.1Suwe		return;
1401.1Suwe	}
1411.1Suwe	sc->sc_mask |= BTN_RESET_BIT;
1421.1Suwe
1431.1Suwe	for (i = 0; i < NBUTTON; i++) {
1441.1Suwe		int idx = btnlist[i].idx;
1451.1Suwe		sc->sc_bev[idx].bev_name = btnlist[i].name;
1461.1Suwe		if (btn_event_register(&sc->sc_bev[idx]) != 0) {
1471.1Suwe			printf("%s: unable to register '%s' button\n",
1481.1Suwe			    sc->sc_dev.dv_xname, btnlist[i].name);
1491.1Suwe		} else {
1501.1Suwe			sc->sc_mask |= btnlist[i].mask;
1511.1Suwe		}
1521.1Suwe	}
1531.1Suwe}
1541.1Suwe
1551.1Suwestatic int
1561.1Suwebtn_intr(void *arg)
1571.1Suwe{
1581.1Suwe	struct btn_obio_softc *sc = (void *)arg;
1591.1Suwe	int status;
1601.1Suwe	int i;
1611.1Suwe
1621.1Suwe	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
1631.1Suwe	if (status == -1) {
1641.1Suwe		return (0);
1651.1Suwe	}
1661.1Suwe
1671.1Suwe	status = ~status;
1681.1Suwe	if (status & BTN_ALL_BIT) {
1691.1Suwe		if (status & BTN_RESET_BIT) {
1701.1Suwe			if (sc->sc_mask & BTN_RESET_BIT) {
1711.1Suwe				extintr_disable(sc->sc_ih);
1721.1Suwe#if NPWRSW_OBIO > 0
1731.1Suwe				extintr_disable_by_num(LANDISK_INTR_PWRSW);
1741.1Suwe#endif
1751.1Suwe				sysmon_task_queue_sched(0,
1761.1Suwe				    btn_sysmon_pressed_event, sc);
1771.1Suwe				return (1);
1781.1Suwe			} else {
1791.1Suwe				printf("%s: reset button pressed\n",
1801.1Suwe				    sc->sc_dev.dv_xname);
1811.1Suwe			}
1821.1Suwe		}
1831.1Suwe
1841.1Suwe		for (i = 0; i < NBUTTON; i++) {
1851.1Suwe			uint8_t mask = btnlist[i].mask;
1861.1Suwe			int rv = 0;
1871.1Suwe			if (status & mask) {
1881.1Suwe				if (sc->sc_mask & mask) {
1891.1Suwe					sysmon_task_queue_sched(1,
1901.1Suwe					    btn_pressed_event,
1911.1Suwe					    &sc->sc_bev[btnlist[i].idx]);
1921.1Suwe				} else {
1931.1Suwe					printf("%s: %s button pressed\n",
1941.1Suwe					    sc->sc_dev.dv_xname,
1951.1Suwe					    btnlist[i].name);
1961.1Suwe				}
1971.1Suwe				rv = 1;
1981.1Suwe			}
1991.1Suwe			if (rv != 0) {
2001.1Suwe				extintr_disable(sc->sc_ih);
2011.1Suwe				callout_schedule(&sc->sc_guard_ch, BTN_TIMEOUT);
2021.1Suwe			}
2031.1Suwe		}
2041.1Suwe		return (1);
2051.1Suwe	}
2061.1Suwe	return (0);
2071.1Suwe}
2081.1Suwe
2091.1Suwestatic void
2101.1Suwebtn_sysmon_pressed_event(void *arg)
2111.1Suwe{
2121.1Suwe	struct btn_obio_softc *sc = (void *)arg;
2131.1Suwe
2141.1Suwe	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
2151.1Suwe}
2161.1Suwe
2171.1Suwestatic void
2181.1Suwebtn_pressed_event(void *arg)
2191.1Suwe{
2201.1Suwe	struct btn_event *bev = (void *)arg;
2211.1Suwe
2221.1Suwe	btn_event_send(bev, BUTTON_EVENT_PRESSED);
2231.1Suwe}
2241.1Suwe
2251.1Suwestatic void
2261.1Suwebtn_guard_timeout(void *arg)
2271.1Suwe{
2281.1Suwe	struct btn_obio_softc *sc = arg;
2291.1Suwe
2301.1Suwe	callout_stop(&sc->sc_guard_ch);
2311.1Suwe	extintr_enable(sc->sc_ih);
2321.1Suwe}
233