btn_obio.c revision 1.5
11.5Suwe/*	$NetBSD: btn_obio.c,v 1.5 2008/03/27 03:01:05 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.5Suwe__KERNEL_RCSID(0, "$NetBSD: btn_obio.c,v 1.5 2008/03/27 03:01:05 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.4Suwe	device_t		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.4Suwestatic int btn_obio_probe(device_t, cfdata_t, void *);
761.4Suwestatic void btn_obio_attach(device_t, device_t, 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.4SuweCFATTACH_DECL_NEW(btn_obio, sizeof(struct btn_obio_softc),
961.1Suwe    btn_obio_probe, btn_obio_attach, NULL, NULL);
971.1Suwe
981.1Suwestatic int
991.4Suwebtn_obio_probe(device_t parent, cfdata_t 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.4Suwebtn_obio_attach(device_t parent, device_t self, void *aux)
1161.1Suwe{
1171.4Suwe	struct btn_obio_softc *sc;
1181.1Suwe	int i;
1191.1Suwe
1201.4Suwe	aprint_naive("\n");
1211.5Suwe	aprint_normal(": USL-5P buttons\n");
1221.4Suwe
1231.4Suwe	sc = device_private(self);
1241.4Suwe	sc->sc_dev = self;
1251.1Suwe
1261.1Suwe	btn_softc = sc;
1271.1Suwe
1281.2Sad	callout_init(&sc->sc_guard_ch, 0);
1291.1Suwe	callout_setfunc(&sc->sc_guard_ch, btn_guard_timeout, sc);
1301.1Suwe
1311.1Suwe	sc->sc_ih = extintr_establish(LANDISK_INTR_BTN, IPL_TTY, btn_intr, sc);
1321.1Suwe	if (sc->sc_ih == NULL) {
1331.4Suwe		aprint_error_dev(self, "unable to establish interrupt");
1341.1Suwe		panic("extintr_establish");
1351.1Suwe	}
1361.1Suwe
1371.4Suwe	sc->sc_smpsw.smpsw_name = device_xname(self);
1381.1Suwe	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_RESET;
1391.1Suwe	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1401.4Suwe		aprint_error_dev(self, "unable to register with sysmon\n");
1411.1Suwe		return;
1421.1Suwe	}
1431.1Suwe	sc->sc_mask |= BTN_RESET_BIT;
1441.1Suwe
1451.1Suwe	for (i = 0; i < NBUTTON; i++) {
1461.1Suwe		int idx = btnlist[i].idx;
1471.1Suwe		sc->sc_bev[idx].bev_name = btnlist[i].name;
1481.1Suwe		if (btn_event_register(&sc->sc_bev[idx]) != 0) {
1491.4Suwe			aprint_error_dev(self,
1501.4Suwe					 "unable to register '%s' button\n",
1511.4Suwe					 btnlist[i].name);
1521.1Suwe		} else {
1531.1Suwe			sc->sc_mask |= btnlist[i].mask;
1541.1Suwe		}
1551.1Suwe	}
1561.1Suwe}
1571.1Suwe
1581.1Suwestatic int
1591.1Suwebtn_intr(void *arg)
1601.1Suwe{
1611.1Suwe	struct btn_obio_softc *sc = (void *)arg;
1621.4Suwe	device_t self = sc->sc_dev;
1631.1Suwe	int status;
1641.1Suwe	int i;
1651.1Suwe
1661.1Suwe	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
1671.1Suwe	if (status == -1) {
1681.1Suwe		return (0);
1691.1Suwe	}
1701.1Suwe
1711.1Suwe	status = ~status;
1721.1Suwe	if (status & BTN_ALL_BIT) {
1731.1Suwe		if (status & BTN_RESET_BIT) {
1741.1Suwe			if (sc->sc_mask & BTN_RESET_BIT) {
1751.1Suwe				extintr_disable(sc->sc_ih);
1761.1Suwe#if NPWRSW_OBIO > 0
1771.1Suwe				extintr_disable_by_num(LANDISK_INTR_PWRSW);
1781.1Suwe#endif
1791.1Suwe				sysmon_task_queue_sched(0,
1801.1Suwe				    btn_sysmon_pressed_event, sc);
1811.1Suwe				return (1);
1821.1Suwe			} else {
1831.4Suwe				aprint_normal_dev(self,
1841.4Suwe					"reset button pressed\n");
1851.1Suwe			}
1861.1Suwe		}
1871.1Suwe
1881.1Suwe		for (i = 0; i < NBUTTON; i++) {
1891.1Suwe			uint8_t mask = btnlist[i].mask;
1901.1Suwe			int rv = 0;
1911.1Suwe			if (status & mask) {
1921.1Suwe				if (sc->sc_mask & mask) {
1931.1Suwe					sysmon_task_queue_sched(1,
1941.1Suwe					    btn_pressed_event,
1951.1Suwe					    &sc->sc_bev[btnlist[i].idx]);
1961.1Suwe				} else {
1971.4Suwe					aprint_normal_dev(self,
1981.4Suwe						"%s button pressed\n",
1991.4Suwe						btnlist[i].name);
2001.1Suwe				}
2011.1Suwe				rv = 1;
2021.1Suwe			}
2031.1Suwe			if (rv != 0) {
2041.1Suwe				extintr_disable(sc->sc_ih);
2051.1Suwe				callout_schedule(&sc->sc_guard_ch, BTN_TIMEOUT);
2061.1Suwe			}
2071.1Suwe		}
2081.1Suwe		return (1);
2091.1Suwe	}
2101.1Suwe	return (0);
2111.1Suwe}
2121.1Suwe
2131.1Suwestatic void
2141.1Suwebtn_sysmon_pressed_event(void *arg)
2151.1Suwe{
2161.1Suwe	struct btn_obio_softc *sc = (void *)arg;
2171.1Suwe
2181.1Suwe	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
2191.1Suwe}
2201.1Suwe
2211.1Suwestatic void
2221.1Suwebtn_pressed_event(void *arg)
2231.1Suwe{
2241.1Suwe	struct btn_event *bev = (void *)arg;
2251.1Suwe
2261.1Suwe	btn_event_send(bev, BUTTON_EVENT_PRESSED);
2271.1Suwe}
2281.1Suwe
2291.1Suwestatic void
2301.1Suwebtn_guard_timeout(void *arg)
2311.1Suwe{
2321.1Suwe	struct btn_obio_softc *sc = arg;
2331.1Suwe
2341.1Suwe	callout_stop(&sc->sc_guard_ch);
2351.1Suwe	extintr_enable(sc->sc_ih);
2361.1Suwe}
237