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