Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.13
      1  1.13  chopps /*	$NetBSD: kbd.c,v 1.13 1995/02/12 19:19:15 chopps 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.9  chopps void kbdattach __P((struct device *, struct device *, void *));
     65   1.9  chopps int kbdmatch __P((struct device *, struct cfdata *, void *));
     66   1.9  chopps 
     67   1.9  chopps struct cfdriver kbdcd = {
     68  1.12  chopps 	NULL, "kbd", (cfmatch_t)kbdmatch, kbdattach, DV_DULL,
     69   1.9  chopps 	sizeof(struct device), NULL, 0 };
     70   1.9  chopps 
     71   1.9  chopps /*ARGSUSED*/
     72   1.9  chopps int
     73   1.9  chopps kbdmatch(pdp, cfp, auxp)
     74   1.9  chopps 	struct device *pdp;
     75   1.9  chopps 	struct cfdata *cfp;
     76   1.9  chopps 	void *auxp;
     77   1.9  chopps {
     78   1.9  chopps 	if (matchname((char *)auxp, "kbd"))
     79   1.9  chopps 		return(1);
     80   1.9  chopps 	return(0);
     81   1.9  chopps }
     82   1.9  chopps 
     83   1.9  chopps /*ARGSUSED*/
     84   1.9  chopps void
     85   1.9  chopps kbdattach(pdp, dp, auxp)
     86   1.9  chopps 	struct device *pdp, *dp;
     87   1.9  chopps 	void *auxp;
     88   1.9  chopps {
     89   1.9  chopps 	printf("\n");
     90   1.9  chopps }
     91   1.5      mw 
     92   1.5      mw /* definitions for amiga keyboard encoding. */
     93   1.5      mw #define KEY_CODE(c)  ((c) & 0x7f)
     94   1.5      mw #define KEY_UP(c)    ((c) & 0x80)
     95   1.5      mw 
     96   1.1      mw void
     97   1.1      mw kbdenable ()
     98   1.1      mw {
     99   1.9  chopps 	int s;
    100   1.1      mw 
    101   1.9  chopps 	/*
    102   1.9  chopps 	 * collides with external ints from SCSI, watch out for this when
    103   1.9  chopps 	 * enabling/disabling interrupts there !!
    104   1.9  chopps 	 */
    105   1.9  chopps 	s = spltty();
    106   1.9  chopps 	custom.intena = INTF_SETCLR | INTF_PORTS;
    107   1.9  chopps 	ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;  /* SP interrupt enable */
    108   1.9  chopps 	ciaa.cra &= ~(1<<6);		/* serial line == input */
    109   1.9  chopps 	kbd_softc.k_event_mode = 0;
    110   1.9  chopps 	kbd_softc.k_events.ev_io = 0;
    111   1.9  chopps 	splx(s);
    112   1.1      mw }
    113   1.1      mw 
    114   1.1      mw 
    115   1.1      mw int
    116   1.5      mw kbdopen (dev_t dev, int flags, int mode, struct proc *p)
    117   1.5      mw {
    118   1.5      mw   int s, error;
    119   1.5      mw 
    120   1.5      mw   if (kbd_softc.k_events.ev_io)
    121   1.5      mw     return EBUSY;
    122   1.5      mw 
    123   1.5      mw   kbd_softc.k_events.ev_io = p;
    124   1.5      mw   ev_init(&kbd_softc.k_events);
    125   1.5      mw   return (0);
    126   1.5      mw }
    127   1.5      mw 
    128   1.5      mw int
    129   1.5      mw kbdclose (dev_t dev, int flags, int mode, struct proc *p)
    130   1.5      mw {
    131   1.5      mw   /* Turn off event mode, dump the queue */
    132   1.5      mw   kbd_softc.k_event_mode = 0;
    133   1.5      mw   ev_fini(&kbd_softc.k_events);
    134   1.5      mw   kbd_softc.k_events.ev_io = NULL;
    135   1.5      mw   return (0);
    136   1.5      mw }
    137   1.5      mw 
    138   1.5      mw int
    139   1.5      mw kbdread (dev_t dev, struct uio *uio, int flags)
    140   1.5      mw {
    141   1.5      mw   return ev_read (&kbd_softc.k_events, uio, flags);
    142   1.5      mw }
    143   1.5      mw 
    144   1.5      mw /* this routine should not exist, but is convenient to write here for now */
    145   1.5      mw int
    146   1.5      mw kbdwrite (dev_t dev, struct uio *uio, int flags)
    147   1.5      mw {
    148   1.5      mw   return EOPNOTSUPP;
    149   1.5      mw }
    150   1.5      mw 
    151   1.5      mw int
    152  1.12  chopps kbdioctl(dev_t dev, u_long cmd, register caddr_t data, int flag, struct proc *p)
    153   1.5      mw {
    154   1.5      mw   register struct kbd_softc *k = &kbd_softc;
    155   1.5      mw 
    156   1.5      mw   switch (cmd)
    157   1.5      mw     {
    158   1.5      mw     case KIOCTRANS:
    159   1.5      mw       if (*(int *)data == TR_UNTRANS_EVENT)
    160   1.5      mw 	return 0;
    161   1.5      mw       break;
    162   1.5      mw 
    163   1.5      mw     case KIOCGTRANS:
    164   1.5      mw       /*
    165   1.5      mw        * Get translation mode
    166   1.5      mw        */
    167   1.5      mw       *(int *)data = TR_UNTRANS_EVENT;
    168   1.5      mw       return 0;
    169   1.5      mw 
    170   1.5      mw     case KIOCSDIRECT:
    171   1.5      mw       k->k_event_mode = *(int *)data;
    172   1.5      mw       return 0;
    173   1.5      mw 
    174   1.5      mw     case FIONBIO:		/* we will remove this someday (soon???) */
    175   1.5      mw       return 0;
    176   1.5      mw 
    177   1.5      mw     case FIOASYNC:
    178   1.5      mw       k->k_events.ev_async = *(int *)data != 0;
    179   1.5      mw       return 0;
    180   1.5      mw 
    181   1.5      mw     case TIOCSPGRP:
    182   1.5      mw       if (*(int *)data != k->k_events.ev_io->p_pgid)
    183   1.5      mw 	return EPERM;
    184   1.5      mw       return 0;
    185   1.5      mw 
    186   1.5      mw     default:
    187   1.5      mw       return ENOTTY;
    188   1.5      mw     }
    189   1.5      mw 
    190   1.5      mw   /*
    191   1.5      mw    * We identified the ioctl, but we do not handle it.
    192   1.5      mw    */
    193   1.5      mw   return EOPNOTSUPP;		/* misuse, but what the heck */
    194   1.5      mw }
    195   1.5      mw 
    196   1.5      mw int
    197   1.5      mw kbdselect (dev_t dev, int rw, struct proc *p)
    198   1.5      mw {
    199   1.5      mw   return ev_select (&kbd_softc.k_events, rw, p);
    200   1.5      mw }
    201   1.5      mw 
    202   1.5      mw 
    203   1.5      mw int
    204   1.3      mw kbdintr (mask)
    205   1.3      mw      int mask;
    206   1.1      mw {
    207  1.13  chopps   u_char c;
    208   1.5      mw   struct kbd_softc *k = &kbd_softc;
    209   1.5      mw   struct firm_event *fe;
    210   1.5      mw   int put;
    211  1.13  chopps #ifdef KBDRESET
    212  1.13  chopps   static int reset_warn;
    213  1.13  chopps #endif
    214   1.1      mw 
    215   1.3      mw   /* now only invoked from generic CIA interrupt handler if there *is*
    216   1.3      mw      a keyboard interrupt pending */
    217   1.1      mw 
    218  1.13  chopps   c = ~ciaa.sdr;	/* keyboard data is inverted */
    219   1.1      mw   /* ack */
    220   1.1      mw   ciaa.cra |= (1 << 6);	/* serial line output */
    221  1.13  chopps #ifdef KBDRESET
    222  1.13  chopps   if (reset_warn && c == 0xf0) {
    223  1.13  chopps #ifdef DEBUG
    224  1.13  chopps     printf ("kbdintr: !!!! Reset Warning !!!!\n");
    225  1.13  chopps #endif
    226  1.13  chopps     bootsync();
    227  1.13  chopps     reset_warn = 0;
    228  1.13  chopps     DELAY(30000000);
    229  1.13  chopps   }
    230  1.13  chopps #endif
    231   1.5      mw   /* wait 200 microseconds (for bloody Cherry keyboards..) */
    232  1.13  chopps   DELAY(2000);			/* fudge delay a bit for some keyboards */
    233   1.1      mw   ciaa.cra &= ~(1 << 6);
    234   1.1      mw 
    235   1.5      mw   /* process the character */
    236   1.1      mw 
    237   1.1      mw   c = (c >> 1) | (c << 7);	/* rotate right once */
    238   1.1      mw 
    239   1.3      mw 
    240  1.13  chopps #ifdef KBDRESET
    241  1.13  chopps   if (c == 0x78) {
    242  1.13  chopps #ifdef DEBUG
    243  1.13  chopps     printf ("kbdintr: Reset Warning started\n");
    244  1.13  chopps #endif
    245  1.13  chopps     ++reset_warn;
    246  1.13  chopps     return;
    247  1.13  chopps   }
    248  1.13  chopps #endif
    249   1.5      mw   /* if not in event mode, deliver straight to ite to process key stroke */
    250   1.5      mw   if (! k->k_event_mode)
    251   1.5      mw     {
    252   1.8  chopps       ite_filter (c, ITEFILT_TTY);
    253   1.5      mw       return;
    254   1.5      mw     }
    255   1.5      mw 
    256   1.5      mw   /* Keyboard is generating events.  Turn this keystroke into an
    257   1.5      mw      event and put it in the queue.  If the queue is full, the
    258   1.5      mw      keystroke is lost (sorry!). */
    259   1.5      mw 
    260   1.5      mw   put = k->k_events.ev_put;
    261   1.5      mw   fe = &k->k_events.ev_q[put];
    262   1.5      mw   put = (put + 1) % EV_QSIZE;
    263   1.5      mw   if (put == k->k_events.ev_get)
    264   1.5      mw     {
    265   1.5      mw       log(LOG_WARNING, "keyboard event queue overflow\n"); /* ??? */
    266   1.5      mw       return;
    267   1.5      mw     }
    268   1.5      mw   fe->id = KEY_CODE(c);
    269   1.5      mw   fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    270   1.5      mw   fe->time = time;
    271   1.5      mw   k->k_events.ev_put = put;
    272   1.5      mw   EV_WAKEUP(&k->k_events);
    273   1.1      mw }
    274   1.1      mw 
    275   1.1      mw 
    276   1.1      mw int
    277   1.1      mw kbdbell()
    278   1.1      mw {
    279   1.3      mw   /* nice, mykes provided audio-support! */
    280   1.3      mw   cc_bell ();
    281   1.1      mw }
    282   1.1      mw 
    283   1.1      mw 
    284   1.1      mw int
    285   1.1      mw kbdgetcn ()
    286   1.1      mw {
    287   1.1      mw   int s = spltty ();
    288   1.3      mw   u_char ints, mask, c, in;
    289   1.1      mw 
    290   1.3      mw   for (ints = 0; ! ((mask = ciaa.icr) & CIA_ICR_SP); ints |= mask) ;
    291   1.1      mw 
    292   1.1      mw   in = ciaa.sdr;
    293   1.1      mw   c = ~in;
    294   1.1      mw 
    295   1.1      mw   /* ack */
    296   1.1      mw   ciaa.cra |= (1 << 6);	/* serial line output */
    297   1.1      mw   ciaa.sdr = 0xff;		/* ack */
    298   1.5      mw   /* wait 200 microseconds */
    299  1.10  chopps   DELAY(2000);    /* XXXX only works as long as DELAY doesn't use a timer and waits.. */
    300   1.1      mw   ciaa.cra &= ~(1 << 6);
    301   1.1      mw   ciaa.sdr = in;
    302   1.1      mw 
    303   1.1      mw   splx (s);
    304   1.1      mw   c = (c >> 1) | (c << 7);
    305   1.3      mw 
    306   1.3      mw   /* take care that no CIA-interrupts are lost */
    307   1.5      mw   if (ints)
    308   1.5      mw     dispatch_cia_ints (0, ints);
    309   1.3      mw 
    310   1.1      mw   return c;
    311   1.1      mw }
    312