1 /* $NetBSD: lk201_ws.c,v 1.5 2001/11/13 12:49:45 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1998 5 * Matthias Drochner. 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 for the NetBSD Project 18 * by Matthias Drochner. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: lk201_ws.c,v 1.5 2001/11/13 12:49:45 lukem Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 41 #include <dev/wscons/wsconsio.h> 42 43 #include <dev/dec/lk201reg.h> 44 #include <dev/dec/lk201var.h> 45 #include <dev/dec/wskbdmap_lk201.h> /* for {MIN,MAX}_LK201_KEY */ 46 47 #define send(lks, c) ((*((lks)->attmt.sendchar))((lks)->attmt.cookie, c)) 48 49 int 50 lk201_init(lks) 51 struct lk201_state *lks; 52 { 53 int i; 54 55 send(lks, LK_LED_ENABLE); 56 send(lks, LK_LED_ALL); 57 58 /* 59 * set all keys to updown mode; autorepeat is 60 * done by wskbd software 61 */ 62 for (i = 1; i <= 14; i++) 63 send(lks, LK_CMD_MODE(LK_UPDOWN, i)); 64 65 send(lks, LK_CL_ENABLE); 66 send(lks, LK_PARAM_VOLUME(3)); 67 lks->kcvol = (8 - 3) * 100 / 8; 68 69 lks->bellvol = -1; /* not yet set */ 70 71 for (i = 0; i < LK_KLL; i++) 72 lks->down_keys_list[i] = -1; 73 send(lks, LK_KBD_ENABLE); 74 75 send(lks, LK_LED_DISABLE); 76 send(lks, LK_LED_ALL); 77 lks->leds_state = 0; 78 79 return (0); 80 } 81 82 int 83 lk201_decode(lks, datain, type, dataout) 84 struct lk201_state *lks; 85 int datain; 86 u_int *type; 87 int *dataout; 88 { 89 int i, freeslot; 90 91 switch (datain) { 92 case LK_KEY_UP: 93 for (i = 0; i < LK_KLL; i++) 94 lks->down_keys_list[i] = -1; 95 *type = WSCONS_EVENT_ALL_KEYS_UP; 96 return (1); 97 case LK_POWER_UP: 98 printf("lk201_decode: powerup detected\n"); 99 lk201_init(lks); 100 return (0); 101 case LK_KDOWN_ERROR: 102 case LK_POWER_ERROR: 103 case LK_OUTPUT_ERROR: 104 case LK_INPUT_ERROR: 105 printf("lk201_decode: error %x\n", datain); 106 /* FALLTHRU */ 107 case LK_KEY_REPEAT: /* autorepeat handled by wskbd */ 108 case LK_MODE_CHANGE: /* ignore silently */ 109 return (0); 110 } 111 112 if (datain < MIN_LK201_KEY || datain > MAX_LK201_KEY) { 113 printf("lk201_decode: %x\n", datain); 114 return (0); 115 } 116 117 *dataout = datain - MIN_LK201_KEY; 118 119 freeslot = -1; 120 for (i = 0; i < LK_KLL; i++) { 121 if (lks->down_keys_list[i] == datain) { 122 *type = WSCONS_EVENT_KEY_UP; 123 lks->down_keys_list[i] = -1; 124 return (1); 125 } 126 if (lks->down_keys_list[i] == -1 && freeslot == -1) 127 freeslot = i; 128 } 129 130 if (freeslot == -1) { 131 printf("lk201_decode: down(%d) no free slot\n", datain); 132 return (0); 133 } 134 135 *type = WSCONS_EVENT_KEY_DOWN; 136 lks->down_keys_list[freeslot] = datain; 137 return (1); 138 } 139 140 void 141 lk201_bell(lks, bell) 142 struct lk201_state *lks; 143 struct wskbd_bell_data *bell; 144 { 145 unsigned int vol; 146 147 if (bell->which & WSKBD_BELL_DOVOLUME) { 148 vol = 8 - bell->volume * 8 / 100; 149 if (vol > 7) 150 vol = 7; 151 } else 152 vol = 3; 153 154 if (vol != lks->bellvol) { 155 send(lks, LK_BELL_ENABLE); 156 send(lks, LK_PARAM_VOLUME(vol)); 157 lks->bellvol = vol; 158 } 159 send(lks, LK_RING_BELL); 160 } 161 162 void 163 lk201_set_leds(lks, leds) 164 struct lk201_state *lks; 165 int leds; 166 { 167 int newleds; 168 169 newleds = 0; 170 if (leds & WSKBD_LED_SCROLL) 171 newleds |= LK_LED_WAIT; 172 if (leds & WSKBD_LED_CAPS) 173 newleds |= LK_LED_LOCK; 174 175 send(lks, LK_LED_DISABLE); 176 send(lks, (0x80 | (~newleds & 0x0f))); 177 178 send(lks, LK_LED_ENABLE); 179 send(lks, (0x80 | (newleds & 0x0f))); 180 181 lks->leds_state = leds; 182 } 183 184 void 185 lk201_set_keyclick(lks, vol) 186 struct lk201_state *lks; 187 int vol; 188 { 189 unsigned int newvol; 190 191 if (vol == 0) 192 send(lks, LK_CL_DISABLE); 193 else { 194 newvol = 8 - vol * 8 / 100; 195 if (newvol > 7) 196 newvol = 7; 197 198 send(lks, LK_CL_ENABLE); 199 send(lks, LK_PARAM_VOLUME(newvol)); 200 } 201 202 lks->kcvol = vol; 203 } 204