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