Home | History | Annotate | Line # | Download | only in dev
pfckbd.c revision 1.2.4.2
      1  1.2.4.2  bouyer /*	$NetBSD: pfckbd.c,v 1.2.4.2 2001/03/12 13:28:51 bouyer Exp $	*/
      2  1.2.4.2  bouyer 
      3  1.2.4.2  bouyer /*-
      4  1.2.4.2  bouyer  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.2.4.2  bouyer  * All rights reserved.
      6  1.2.4.2  bouyer  *
      7  1.2.4.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  bouyer  * by UCHIYAMA Yasushi.
      9  1.2.4.2  bouyer  *
     10  1.2.4.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.2.4.2  bouyer  * are met:
     13  1.2.4.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.2.4.2  bouyer  *    must display the following acknowledgement:
     20  1.2.4.2  bouyer  *        This product includes software developed by the NetBSD
     21  1.2.4.2  bouyer  *        Foundation, Inc. and its contributors.
     22  1.2.4.2  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2.4.2  bouyer  *    contributors may be used to endorse or promote products derived
     24  1.2.4.2  bouyer  *    from this software without specific prior written permission.
     25  1.2.4.2  bouyer  *
     26  1.2.4.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2.4.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2.4.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2.4.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2.4.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2.4.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2.4.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2.4.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2.4.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2.4.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2.4.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2.4.2  bouyer  */
     38  1.2.4.2  bouyer #define PFCKBD_DEBUG
     39  1.2.4.2  bouyer 
     40  1.2.4.2  bouyer #include <sys/param.h>
     41  1.2.4.2  bouyer #include <sys/systm.h>
     42  1.2.4.2  bouyer #include <sys/device.h>
     43  1.2.4.2  bouyer #include <sys/callout.h>
     44  1.2.4.2  bouyer 
     45  1.2.4.2  bouyer #include <machine/bus.h>
     46  1.2.4.2  bouyer #include <dev/hpc/hpckbdvar.h>
     47  1.2.4.2  bouyer 
     48  1.2.4.2  bouyer #include <sh3/pfcreg.h>
     49  1.2.4.2  bouyer 
     50  1.2.4.2  bouyer #include <hpcsh/dev/pfckbdvar.h>
     51  1.2.4.2  bouyer 
     52  1.2.4.2  bouyer #ifdef PFCKBD_DEBUG
     53  1.2.4.2  bouyer int	pfckbd_debug = 0;
     54  1.2.4.2  bouyer #define	DPRINTF(fmt, args...)						\
     55  1.2.4.2  bouyer 	if (pfckbd_debug)						\
     56  1.2.4.2  bouyer 		printf("%s: " fmt, __FUNCTION__ , ##args)
     57  1.2.4.2  bouyer #define	DPRINTFN(n, arg)						\
     58  1.2.4.2  bouyer 	if (pfckbd_debug > (n))						\
     59  1.2.4.2  bouyer 		printf("%s: " fmt, __FUNCTION__ , ##args)
     60  1.2.4.2  bouyer #else
     61  1.2.4.2  bouyer #define	DPRINTF(arg...)		((void)0)
     62  1.2.4.2  bouyer #define DPRINTFN(n, arg...)	((void)0)
     63  1.2.4.2  bouyer #endif
     64  1.2.4.2  bouyer 
     65  1.2.4.2  bouyer static int pfckbd_match(struct device *, struct cfdata *, void *);
     66  1.2.4.2  bouyer static void pfckbd_attach(struct device *, struct device *, void *);
     67  1.2.4.2  bouyer 
     68  1.2.4.2  bouyer static struct pfckbd_core {
     69  1.2.4.2  bouyer 	int pc_attached;
     70  1.2.4.2  bouyer 	int pc_enabled;
     71  1.2.4.2  bouyer 	struct callout pc_soft_ch;
     72  1.2.4.2  bouyer 	struct hpckbd_ic_if pc_if;
     73  1.2.4.2  bouyer 	struct hpckbd_if *pc_hpckbd;
     74  1.2.4.2  bouyer 	u_int16_t pc_column[8];
     75  1.2.4.2  bouyer } pfckbd_core;
     76  1.2.4.2  bouyer 
     77  1.2.4.2  bouyer struct pfckbd_softc {
     78  1.2.4.2  bouyer 	struct device sc_dev;
     79  1.2.4.2  bouyer };
     80  1.2.4.2  bouyer 
     81  1.2.4.2  bouyer struct cfattach pfckbd_ca = {
     82  1.2.4.2  bouyer 	sizeof(struct pfckbd_softc), pfckbd_match, pfckbd_attach
     83  1.2.4.2  bouyer };
     84  1.2.4.2  bouyer 
     85  1.2.4.2  bouyer static int pfckbd_poll(void *);
     86  1.2.4.2  bouyer static void pfckbd_soft(void *);
     87  1.2.4.2  bouyer static void pfckbd_ifsetup(struct pfckbd_core *);
     88  1.2.4.2  bouyer static int pfckbd_input_establish(void *, struct hpckbd_if *);
     89  1.2.4.2  bouyer 
     90  1.2.4.2  bouyer /*
     91  1.2.4.2  bouyer  * matrix scan keyboard connected to SH3 PFC module.
     92  1.2.4.2  bouyer  * currently, HP Jornada 690 only.
     93  1.2.4.2  bouyer  */
     94  1.2.4.2  bouyer void
     95  1.2.4.2  bouyer pfckbd_cnattach()
     96  1.2.4.2  bouyer {
     97  1.2.4.2  bouyer 	struct pfckbd_core *pc = &pfckbd_core;
     98  1.2.4.2  bouyer 
     99  1.2.4.2  bouyer 	/* initialize interface */
    100  1.2.4.2  bouyer 	pfckbd_ifsetup(pc);
    101  1.2.4.2  bouyer 	hpckbd_cnattach(&pc->pc_if);
    102  1.2.4.2  bouyer }
    103  1.2.4.2  bouyer 
    104  1.2.4.2  bouyer static int
    105  1.2.4.2  bouyer pfckbd_match(struct device *parent, struct cfdata *cf, void *aux)
    106  1.2.4.2  bouyer {
    107  1.2.4.2  bouyer 
    108  1.2.4.2  bouyer 	return !pfckbd_core.pc_attached;
    109  1.2.4.2  bouyer }
    110  1.2.4.2  bouyer 
    111  1.2.4.2  bouyer static void
    112  1.2.4.2  bouyer pfckbd_attach(struct device *parent, struct device *self, void *aux)
    113  1.2.4.2  bouyer {
    114  1.2.4.2  bouyer 	struct hpckbd_attach_args haa;
    115  1.2.4.2  bouyer 
    116  1.2.4.2  bouyer 	printf("\n");
    117  1.2.4.2  bouyer 
    118  1.2.4.2  bouyer 	/* pfckbd is singleton. no more attach */
    119  1.2.4.2  bouyer 	pfckbd_core.pc_attached = 1;
    120  1.2.4.2  bouyer 
    121  1.2.4.2  bouyer 	pfckbd_ifsetup(&pfckbd_core);
    122  1.2.4.2  bouyer 
    123  1.2.4.2  bouyer 	/* attach hpckbd */
    124  1.2.4.2  bouyer 	haa.haa_ic = &pfckbd_core.pc_if; /* tell the hpckbd to my interface */
    125  1.2.4.2  bouyer 	config_found(self, &haa, hpckbd_print);
    126  1.2.4.2  bouyer 
    127  1.2.4.2  bouyer 	/* install callout handler */
    128  1.2.4.2  bouyer 	callout_init(&pfckbd_core.pc_soft_ch);
    129  1.2.4.2  bouyer 	callout_reset(&pfckbd_core.pc_soft_ch, 1, pfckbd_soft, &pfckbd_core);
    130  1.2.4.2  bouyer }
    131  1.2.4.2  bouyer 
    132  1.2.4.2  bouyer static int
    133  1.2.4.2  bouyer pfckbd_input_establish(void *ic, struct hpckbd_if *kbdif)
    134  1.2.4.2  bouyer {
    135  1.2.4.2  bouyer 	struct pfckbd_core *pc = ic;
    136  1.2.4.2  bouyer 
    137  1.2.4.2  bouyer 	/* save hpckbd interface */
    138  1.2.4.2  bouyer 	pc->pc_hpckbd = kbdif;
    139  1.2.4.2  bouyer 	/* ok to transact hpckbd */
    140  1.2.4.2  bouyer 	pc->pc_enabled = 1;
    141  1.2.4.2  bouyer 
    142  1.2.4.2  bouyer 	return 0;
    143  1.2.4.2  bouyer }
    144  1.2.4.2  bouyer 
    145  1.2.4.2  bouyer static int
    146  1.2.4.2  bouyer pfckbd_poll(void *arg)
    147  1.2.4.2  bouyer {
    148  1.2.4.2  bouyer 	struct pfckbd_core *pc = arg;
    149  1.2.4.2  bouyer 
    150  1.2.4.2  bouyer 	if (pc->pc_enabled)
    151  1.2.4.2  bouyer 		pfckbd_soft(arg);
    152  1.2.4.2  bouyer 
    153  1.2.4.2  bouyer 	return 0;
    154  1.2.4.2  bouyer }
    155  1.2.4.2  bouyer 
    156  1.2.4.2  bouyer static void
    157  1.2.4.2  bouyer pfckbd_ifsetup(struct pfckbd_core *pc)
    158  1.2.4.2  bouyer {
    159  1.2.4.2  bouyer 	int i;
    160  1.2.4.2  bouyer 
    161  1.2.4.2  bouyer 	pc->pc_if.hii_ctx = pc;
    162  1.2.4.2  bouyer 	pc->pc_if.hii_establish	= pfckbd_input_establish;
    163  1.2.4.2  bouyer 	pc->pc_if.hii_poll = pfckbd_poll;
    164  1.2.4.2  bouyer 	for (i = 0; i < 8; i++)
    165  1.2.4.2  bouyer 		pc->pc_column[i] = 0xdfff;
    166  1.2.4.2  bouyer }
    167  1.2.4.2  bouyer 
    168  1.2.4.2  bouyer static void
    169  1.2.4.2  bouyer pfckbd_soft(void *arg)
    170  1.2.4.2  bouyer {
    171  1.2.4.2  bouyer 	static const struct {
    172  1.2.4.2  bouyer 		u_int8_t d, e;
    173  1.2.4.2  bouyer 	} scan[] = {
    174  1.2.4.2  bouyer 		{ 0xfd, 0xff },
    175  1.2.4.2  bouyer 		{ 0xdf, 0xff },
    176  1.2.4.2  bouyer 		{ 0xff, 0xfd },
    177  1.2.4.2  bouyer 		{ 0xff, 0xbf },
    178  1.2.4.2  bouyer 		{ 0xff, 0x7f },
    179  1.2.4.2  bouyer 		{ 0xff, 0xf7 },
    180  1.2.4.2  bouyer 		{ 0xff, 0xfe },
    181  1.2.4.2  bouyer 		{ 0x7f, 0xff },
    182  1.2.4.2  bouyer 	};
    183  1.2.4.2  bouyer 	struct pfckbd_core *pc = arg;
    184  1.2.4.2  bouyer 	struct hpckbd_if *hpckbd = pc->pc_hpckbd;
    185  1.2.4.2  bouyer 	u_int16_t *buf = pc->pc_column;
    186  1.2.4.2  bouyer 	int row, column, type, val;
    187  1.2.4.2  bouyer 	u_int16_t data, edge, mask;
    188  1.2.4.2  bouyer 
    189  1.2.4.2  bouyer 	if (!pc->pc_enabled)
    190  1.2.4.2  bouyer 		goto reinstall;
    191  1.2.4.2  bouyer 
    192  1.2.4.2  bouyer 	for (column = 0; column < 8; column++) {
    193  1.2.4.2  bouyer 		SHREG_PDDR = scan[column].d;
    194  1.2.4.2  bouyer 		SHREG_PEDR = scan[column].e;
    195  1.2.4.2  bouyer 		delay(50);
    196  1.2.4.2  bouyer 		data = SHREG_PFDR | (SHREG_PCDR << 8);
    197  1.2.4.2  bouyer 
    198  1.2.4.2  bouyer 		if ((edge = (data ^ buf[column]))) {
    199  1.2.4.2  bouyer 			buf[column] = data;
    200  1.2.4.2  bouyer 
    201  1.2.4.2  bouyer 			for (row = 0, mask = 1; row < 16; row++, mask <<= 1) {
    202  1.2.4.2  bouyer 				if (mask & edge) {
    203  1.2.4.2  bouyer 					type = mask & data ? 0 : 1;
    204  1.2.4.2  bouyer 					val = row * 8 + column;
    205  1.2.4.2  bouyer 					DPRINTF("(%2d, %2d) %d \n",
    206  1.2.4.2  bouyer 						row, column, type);
    207  1.2.4.2  bouyer 					hpckbd_input(hpckbd, type, val);
    208  1.2.4.2  bouyer 				}
    209  1.2.4.2  bouyer 			}
    210  1.2.4.2  bouyer 		}
    211  1.2.4.2  bouyer 	}
    212  1.2.4.2  bouyer 
    213  1.2.4.2  bouyer 	SHREG_PDDR = 0xff;
    214  1.2.4.2  bouyer 	SHREG_PEDR = 0xff;
    215  1.2.4.2  bouyer 	data = SHREG_PGDR | (SHREG_PHDR << 8);
    216  1.2.4.2  bouyer 
    217  1.2.4.2  bouyer  reinstall:
    218  1.2.4.2  bouyer 	callout_reset(&pc->pc_soft_ch, 1, pfckbd_soft, pc);
    219  1.2.4.2  bouyer }
    220