Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.17
      1  1.17   mhitch /*	$NetBSD: kbd.c,v 1.17 1996/03/17 05:58:50 mhitch Exp $	*/
      2  1.11      cgd 
      3   1.1       mw /*
      4   1.1       mw  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      5   1.1       mw  * All rights reserved.
      6   1.1       mw  *
      7   1.1       mw  * Redistribution and use in source and binary forms, with or without
      8   1.1       mw  * modification, are permitted provided that the following conditions
      9   1.1       mw  * are met:
     10   1.1       mw  * 1. Redistributions of source code must retain the above copyright
     11   1.1       mw  *    notice, this list of conditions and the following disclaimer.
     12   1.1       mw  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       mw  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       mw  *    documentation and/or other materials provided with the distribution.
     15   1.1       mw  * 3. All advertising materials mentioning features or use of this software
     16   1.1       mw  *    must display the following acknowledgement:
     17   1.1       mw  *	This product includes software developed by the University of
     18   1.1       mw  *	California, Berkeley and its contributors.
     19   1.1       mw  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       mw  *    may be used to endorse or promote products derived from this software
     21   1.1       mw  *    without specific prior written permission.
     22   1.1       mw  *
     23   1.1       mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       mw  * SUCH DAMAGE.
     34   1.1       mw  *
     35   1.4       mw  *	kbd.c
     36   1.1       mw  */
     37   1.7   chopps #include <sys/param.h>
     38   1.7   chopps #include <sys/systm.h>
     39   1.9   chopps #include <sys/device.h>
     40   1.7   chopps #include <sys/ioctl.h>
     41   1.7   chopps #include <sys/tty.h>
     42   1.7   chopps #include <sys/proc.h>
     43   1.7   chopps #include <sys/conf.h>
     44   1.7   chopps #include <sys/file.h>
     45   1.7   chopps #include <sys/kernel.h>
     46   1.7   chopps #include <sys/syslog.h>
     47   1.8   chopps #include <dev/cons.h>
     48   1.7   chopps #include <machine/cpu.h>
     49   1.9   chopps #include <amiga/amiga/device.h>
     50   1.7   chopps #include <amiga/amiga/custom.h>
     51   1.7   chopps #include <amiga/amiga/cia.h>
     52   1.9   chopps #include <amiga/dev/itevar.h>
     53   1.9   chopps #include <amiga/dev/kbdreg.h>
     54   1.7   chopps #include <amiga/dev/event_var.h>
     55   1.7   chopps #include <amiga/dev/vuid_event.h>
     56   1.9   chopps #include "kbd.h"
     57   1.5       mw 
     58   1.5       mw struct kbd_softc {
     59   1.9   chopps 	int k_event_mode;	/* if true, collect events, else pass to ite */
     60   1.9   chopps 	struct evvar k_events;	/* event queue state */
     61   1.9   chopps };
     62   1.9   chopps struct kbd_softc kbd_softc;
     63   1.9   chopps 
     64  1.17   mhitch int kbdmatch __P((struct device *, void *, void *));
     65  1.17   mhitch void kbdattach __P((struct device *, struct device *, void *));
     66   1.9   chopps 
     67  1.16  thorpej struct cfattach kbd_ca = {
     68  1.16  thorpej 	sizeof(struct device), kbdmatch, kbdattach
     69  1.16  thorpej };
     70  1.16  thorpej 
     71  1.16  thorpej struct cfdriver kbd_cd = {
     72  1.16  thorpej 	NULL, "kbd", DV_DULL, NULL, 0
     73  1.16  thorpej };
     74   1.9   chopps 
     75   1.9   chopps /*ARGSUSED*/
     76   1.9   chopps int
     77  1.16  thorpej kbdmatch(pdp, match, auxp)
     78   1.9   chopps 	struct device *pdp;
     79  1.16  thorpej 	void *match, *auxp;
     80   1.9   chopps {
     81  1.16  thorpej 	struct cfdata *cfp = match;
     82  1.16  thorpej 
     83   1.9   chopps 	if (matchname((char *)auxp, "kbd"))
     84   1.9   chopps 		return(1);
     85   1.9   chopps 	return(0);
     86   1.9   chopps }
     87   1.9   chopps 
     88   1.9   chopps /*ARGSUSED*/
     89   1.9   chopps void
     90   1.9   chopps kbdattach(pdp, dp, auxp)
     91   1.9   chopps 	struct device *pdp, *dp;
     92   1.9   chopps 	void *auxp;
     93   1.9   chopps {
     94   1.9   chopps 	printf("\n");
     95   1.9   chopps }
     96   1.5       mw 
     97   1.5       mw /* definitions for amiga keyboard encoding. */
     98   1.5       mw #define KEY_CODE(c)  ((c) & 0x7f)
     99   1.5       mw #define KEY_UP(c)    ((c) & 0x80)
    100   1.5       mw 
    101   1.1       mw void
    102   1.1       mw kbdenable ()
    103   1.1       mw {
    104   1.9   chopps 	int s;
    105   1.1       mw 
    106   1.9   chopps 	/*
    107   1.9   chopps 	 * collides with external ints from SCSI, watch out for this when
    108   1.9   chopps 	 * enabling/disabling interrupts there !!
    109   1.9   chopps 	 */
    110   1.9   chopps 	s = spltty();
    111   1.9   chopps 	custom.intena = INTF_SETCLR | INTF_PORTS;
    112   1.9   chopps 	ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;  /* SP interrupt enable */
    113   1.9   chopps 	ciaa.cra &= ~(1<<6);		/* serial line == input */
    114   1.9   chopps 	kbd_softc.k_event_mode = 0;
    115   1.9   chopps 	kbd_softc.k_events.ev_io = 0;
    116   1.9   chopps 	splx(s);
    117   1.1       mw }
    118   1.1       mw 
    119   1.1       mw 
    120   1.1       mw int
    121   1.5       mw kbdopen (dev_t dev, int flags, int mode, struct proc *p)
    122   1.5       mw {
    123   1.5       mw   int s, error;
    124   1.5       mw 
    125   1.5       mw   if (kbd_softc.k_events.ev_io)
    126   1.5       mw     return EBUSY;
    127   1.5       mw 
    128   1.5       mw   kbd_softc.k_events.ev_io = p;
    129   1.5       mw   ev_init(&kbd_softc.k_events);
    130   1.5       mw   return (0);
    131   1.5       mw }
    132   1.5       mw 
    133   1.5       mw int
    134   1.5       mw kbdclose (dev_t dev, int flags, int mode, struct proc *p)
    135   1.5       mw {
    136   1.5       mw   /* Turn off event mode, dump the queue */
    137   1.5       mw   kbd_softc.k_event_mode = 0;
    138   1.5       mw   ev_fini(&kbd_softc.k_events);
    139   1.5       mw   kbd_softc.k_events.ev_io = NULL;
    140   1.5       mw   return (0);
    141   1.5       mw }
    142   1.5       mw 
    143   1.5       mw int
    144   1.5       mw kbdread (dev_t dev, struct uio *uio, int flags)
    145   1.5       mw {
    146   1.5       mw   return ev_read (&kbd_softc.k_events, uio, flags);
    147   1.5       mw }
    148   1.5       mw 
    149   1.5       mw int
    150  1.12   chopps kbdioctl(dev_t dev, u_long cmd, register caddr_t data, int flag, struct proc *p)
    151   1.5       mw {
    152   1.5       mw   register struct kbd_softc *k = &kbd_softc;
    153   1.5       mw 
    154   1.5       mw   switch (cmd)
    155   1.5       mw     {
    156   1.5       mw     case KIOCTRANS:
    157   1.5       mw       if (*(int *)data == TR_UNTRANS_EVENT)
    158   1.5       mw 	return 0;
    159   1.5       mw       break;
    160   1.5       mw 
    161   1.5       mw     case KIOCGTRANS:
    162   1.5       mw       /*
    163   1.5       mw        * Get translation mode
    164   1.5       mw        */
    165   1.5       mw       *(int *)data = TR_UNTRANS_EVENT;
    166   1.5       mw       return 0;
    167   1.5       mw 
    168   1.5       mw     case KIOCSDIRECT:
    169   1.5       mw       k->k_event_mode = *(int *)data;
    170   1.5       mw       return 0;
    171   1.5       mw 
    172   1.5       mw     case FIONBIO:		/* we will remove this someday (soon???) */
    173   1.5       mw       return 0;
    174   1.5       mw 
    175   1.5       mw     case FIOASYNC:
    176   1.5       mw       k->k_events.ev_async = *(int *)data != 0;
    177   1.5       mw       return 0;
    178   1.5       mw 
    179   1.5       mw     case TIOCSPGRP:
    180   1.5       mw       if (*(int *)data != k->k_events.ev_io->p_pgid)
    181   1.5       mw 	return EPERM;
    182   1.5       mw       return 0;
    183   1.5       mw 
    184   1.5       mw     default:
    185   1.5       mw       return ENOTTY;
    186   1.5       mw     }
    187   1.5       mw 
    188   1.5       mw   /*
    189   1.5       mw    * We identified the ioctl, but we do not handle it.
    190   1.5       mw    */
    191   1.5       mw   return EOPNOTSUPP;		/* misuse, but what the heck */
    192   1.5       mw }
    193   1.5       mw 
    194   1.5       mw int
    195   1.5       mw kbdselect (dev_t dev, int rw, struct proc *p)
    196   1.5       mw {
    197   1.5       mw   return ev_select (&kbd_softc.k_events, rw, p);
    198   1.5       mw }
    199   1.5       mw 
    200   1.5       mw 
    201   1.5       mw int
    202   1.3       mw kbdintr (mask)
    203   1.3       mw      int mask;
    204   1.1       mw {
    205  1.13   chopps   u_char c;
    206   1.5       mw   struct kbd_softc *k = &kbd_softc;
    207   1.5       mw   struct firm_event *fe;
    208   1.5       mw   int put;
    209  1.13   chopps #ifdef KBDRESET
    210  1.13   chopps   static int reset_warn;
    211  1.13   chopps #endif
    212   1.1       mw 
    213   1.3       mw   /* now only invoked from generic CIA interrupt handler if there *is*
    214   1.3       mw      a keyboard interrupt pending */
    215   1.1       mw 
    216  1.13   chopps   c = ~ciaa.sdr;	/* keyboard data is inverted */
    217   1.1       mw   /* ack */
    218   1.1       mw   ciaa.cra |= (1 << 6);	/* serial line output */
    219  1.13   chopps #ifdef KBDRESET
    220  1.13   chopps   if (reset_warn && c == 0xf0) {
    221  1.13   chopps #ifdef DEBUG
    222  1.13   chopps     printf ("kbdintr: !!!! Reset Warning !!!!\n");
    223  1.13   chopps #endif
    224  1.13   chopps     bootsync();
    225  1.13   chopps     reset_warn = 0;
    226  1.13   chopps     DELAY(30000000);
    227  1.13   chopps   }
    228  1.13   chopps #endif
    229   1.5       mw   /* wait 200 microseconds (for bloody Cherry keyboards..) */
    230  1.13   chopps   DELAY(2000);			/* fudge delay a bit for some keyboards */
    231   1.1       mw   ciaa.cra &= ~(1 << 6);
    232   1.1       mw 
    233   1.5       mw   /* process the character */
    234   1.1       mw 
    235   1.1       mw   c = (c >> 1) | (c << 7);	/* rotate right once */
    236   1.1       mw 
    237   1.3       mw 
    238  1.13   chopps #ifdef KBDRESET
    239  1.13   chopps   if (c == 0x78) {
    240  1.13   chopps #ifdef DEBUG
    241  1.13   chopps     printf ("kbdintr: Reset Warning started\n");
    242  1.13   chopps #endif
    243  1.13   chopps     ++reset_warn;
    244  1.13   chopps     return;
    245  1.13   chopps   }
    246  1.13   chopps #endif
    247   1.5       mw   /* if not in event mode, deliver straight to ite to process key stroke */
    248   1.5       mw   if (! k->k_event_mode)
    249   1.5       mw     {
    250   1.8   chopps       ite_filter (c, ITEFILT_TTY);
    251   1.5       mw       return;
    252   1.5       mw     }
    253   1.5       mw 
    254   1.5       mw   /* Keyboard is generating events.  Turn this keystroke into an
    255   1.5       mw      event and put it in the queue.  If the queue is full, the
    256   1.5       mw      keystroke is lost (sorry!). */
    257   1.5       mw 
    258   1.5       mw   put = k->k_events.ev_put;
    259   1.5       mw   fe = &k->k_events.ev_q[put];
    260   1.5       mw   put = (put + 1) % EV_QSIZE;
    261   1.5       mw   if (put == k->k_events.ev_get)
    262   1.5       mw     {
    263   1.5       mw       log(LOG_WARNING, "keyboard event queue overflow\n"); /* ??? */
    264   1.5       mw       return;
    265   1.5       mw     }
    266   1.5       mw   fe->id = KEY_CODE(c);
    267   1.5       mw   fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    268   1.5       mw   fe->time = time;
    269   1.5       mw   k->k_events.ev_put = put;
    270   1.5       mw   EV_WAKEUP(&k->k_events);
    271   1.1       mw }
    272   1.1       mw 
    273   1.1       mw 
    274   1.1       mw int
    275   1.1       mw kbdgetcn ()
    276   1.1       mw {
    277   1.1       mw   int s = spltty ();
    278   1.3       mw   u_char ints, mask, c, in;
    279   1.1       mw 
    280   1.3       mw   for (ints = 0; ! ((mask = ciaa.icr) & CIA_ICR_SP); ints |= mask) ;
    281   1.1       mw 
    282   1.1       mw   in = ciaa.sdr;
    283   1.1       mw   c = ~in;
    284   1.1       mw 
    285   1.1       mw   /* ack */
    286   1.1       mw   ciaa.cra |= (1 << 6);	/* serial line output */
    287   1.1       mw   ciaa.sdr = 0xff;		/* ack */
    288   1.5       mw   /* wait 200 microseconds */
    289  1.10   chopps   DELAY(2000);    /* XXXX only works as long as DELAY doesn't use a timer and waits.. */
    290   1.1       mw   ciaa.cra &= ~(1 << 6);
    291   1.1       mw   ciaa.sdr = in;
    292   1.1       mw 
    293   1.1       mw   splx (s);
    294   1.1       mw   c = (c >> 1) | (c << 7);
    295   1.3       mw 
    296   1.3       mw   /* take care that no CIA-interrupts are lost */
    297   1.5       mw   if (ints)
    298   1.5       mw     dispatch_cia_ints (0, ints);
    299   1.3       mw 
    300   1.1       mw   return c;
    301   1.1       mw }
    302