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