Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 by Jan-Benedict Glaw <jbglaw@lug-owl.de> |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * LK keyboard driver for Linux, based on sunkbd.c (C) by Vojtech Pavlik |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * DEC LK201 and LK401 keyboard driver for Linux (primary for DECstations |
| 11 | * and VAXstations, but can also be used on any standard RS232 with an |
| 12 | * adaptor). |
| 13 | * |
| 14 | * DISCLAIMER: This works for _me_. If you break anything by using the |
| 15 | * information given below, I will _not_ be liable! |
| 16 | * |
| 17 | * RJ10 pinout: To DE9: Or DB25: |
Dmitry Torokhov | d083e90 | 2005-05-29 02:28:42 -0500 | [diff] [blame] | 18 | * 1 - RxD <----> Pin 3 (TxD) <-> Pin 2 (TxD) |
| 19 | * 2 - GND <----> Pin 5 (GND) <-> Pin 7 (GND) |
| 20 | * 4 - TxD <----> Pin 2 (RxD) <-> Pin 3 (RxD) |
| 21 | * 3 - +12V (from HDD drive connector), DON'T connect to DE9 or DB25!!! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * |
| 23 | * Pin numbers for DE9 and DB25 are noted on the plug (quite small:). For |
| 24 | * RJ10, it's like this: |
| 25 | * |
| 26 | * __=__ Hold the plug in front of you, cable downwards, |
| 27 | * /___/| nose is hidden behind the plug. Now, pin 1 is at |
| 28 | * |1234|| the left side, pin 4 at the right and 2 and 3 are |
| 29 | * |IIII|| in between, of course:) |
| 30 | * | || |
| 31 | * |____|/ |
| 32 | * || So the adaptor consists of three connected cables |
| 33 | * || for data transmission (RxD and TxD) and signal ground. |
| 34 | * Additionally, you have to get +12V from somewhere. |
| 35 | * Most easily, you'll get that from a floppy or HDD power connector. |
| 36 | * It's the yellow cable there (black is ground and red is +5V). |
| 37 | * |
| 38 | * The keyboard and all the commands it understands are documented in |
| 39 | * "VCB02 Video Subsystem - Technical Manual", EK-104AA-TM-001. This |
| 40 | * document is LK201 specific, but LK401 is mostly compatible. It comes |
| 41 | * up in LK201 mode and doesn't report any of the additional keys it |
| 42 | * has. These need to be switched on with the LK_CMD_ENABLE_LK401 |
| 43 | * command. You'll find this document (scanned .pdf file) on MANX, |
| 44 | * a search engine specific to DEC documentation. Try |
| 45 | * http://www.vt100.net/manx/details?pn=EK-104AA-TM-001;id=21;cp=1 |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * This program is free software; you can redistribute it and/or modify |
| 50 | * it under the terms of the GNU General Public License as published by |
| 51 | * the Free Software Foundation; either version 2 of the License, or |
| 52 | * (at your option) any later version. |
| 53 | * |
| 54 | * This program is distributed in the hope that it will be useful, |
| 55 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 56 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 57 | * GNU General Public License for more details. |
| 58 | * |
| 59 | * You should have received a copy of the GNU General Public License |
| 60 | * along with this program; if not, write to the Free Software |
| 61 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 62 | * |
| 63 | * Should you need to contact me, the author, you can do so either by |
| 64 | * email or by paper mail: |
| 65 | * Jan-Benedict Glaw, Lilienstraße 16, 33790 Hörste (near Halle/Westf.), |
| 66 | * Germany. |
| 67 | */ |
| 68 | |
| 69 | #include <linux/delay.h> |
| 70 | #include <linux/slab.h> |
| 71 | #include <linux/module.h> |
| 72 | #include <linux/moduleparam.h> |
| 73 | #include <linux/interrupt.h> |
| 74 | #include <linux/init.h> |
| 75 | #include <linux/input.h> |
| 76 | #include <linux/serio.h> |
| 77 | #include <linux/workqueue.h> |
| 78 | |
| 79 | #define DRIVER_DESC "LK keyboard driver" |
| 80 | |
| 81 | MODULE_AUTHOR ("Jan-Benedict Glaw <jbglaw@lug-owl.de>"); |
| 82 | MODULE_DESCRIPTION (DRIVER_DESC); |
| 83 | MODULE_LICENSE ("GPL"); |
| 84 | |
| 85 | /* |
| 86 | * Known parameters: |
| 87 | * bell_volume |
| 88 | * keyclick_volume |
| 89 | * ctrlclick_volume |
| 90 | * |
| 91 | * Please notice that there's not yet an API to set these at runtime. |
| 92 | */ |
| 93 | static int bell_volume = 100; /* % */ |
| 94 | module_param (bell_volume, int, 0); |
| 95 | MODULE_PARM_DESC (bell_volume, "Bell volume (in %). default is 100%"); |
| 96 | |
| 97 | static int keyclick_volume = 100; /* % */ |
| 98 | module_param (keyclick_volume, int, 0); |
| 99 | MODULE_PARM_DESC (keyclick_volume, "Keyclick volume (in %), default is 100%"); |
| 100 | |
| 101 | static int ctrlclick_volume = 100; /* % */ |
| 102 | module_param (ctrlclick_volume, int, 0); |
| 103 | MODULE_PARM_DESC (ctrlclick_volume, "Ctrlclick volume (in %), default is 100%"); |
| 104 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 105 | static int lk201_compose_is_alt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | module_param (lk201_compose_is_alt, int, 0); |
| 107 | MODULE_PARM_DESC (lk201_compose_is_alt, "If set non-zero, LK201' Compose key " |
| 108 | "will act as an Alt key"); |
| 109 | |
| 110 | |
| 111 | |
| 112 | #undef LKKBD_DEBUG |
| 113 | #ifdef LKKBD_DEBUG |
| 114 | #define DBG(x...) printk (x) |
| 115 | #else |
| 116 | #define DBG(x...) do {} while (0) |
| 117 | #endif |
| 118 | |
| 119 | /* LED control */ |
| 120 | #define LK_LED_WAIT 0x81 |
| 121 | #define LK_LED_COMPOSE 0x82 |
| 122 | #define LK_LED_SHIFTLOCK 0x84 |
| 123 | #define LK_LED_SCROLLLOCK 0x88 |
| 124 | #define LK_CMD_LED_ON 0x13 |
| 125 | #define LK_CMD_LED_OFF 0x11 |
| 126 | |
| 127 | /* Mode control */ |
| 128 | #define LK_MODE_DOWN 0x80 |
| 129 | #define LK_MODE_AUTODOWN 0x82 |
| 130 | #define LK_MODE_UPDOWN 0x86 |
| 131 | #define LK_CMD_SET_MODE(mode,div) ((mode) | ((div) << 3)) |
| 132 | |
| 133 | /* Misc commands */ |
| 134 | #define LK_CMD_ENABLE_KEYCLICK 0x1b |
| 135 | #define LK_CMD_DISABLE_KEYCLICK 0x99 |
| 136 | #define LK_CMD_DISABLE_BELL 0xa1 |
| 137 | #define LK_CMD_SOUND_BELL 0xa7 |
| 138 | #define LK_CMD_ENABLE_BELL 0x23 |
| 139 | #define LK_CMD_DISABLE_CTRCLICK 0xb9 |
| 140 | #define LK_CMD_ENABLE_CTRCLICK 0xbb |
| 141 | #define LK_CMD_SET_DEFAULTS 0xd3 |
| 142 | #define LK_CMD_POWERCYCLE_RESET 0xfd |
| 143 | #define LK_CMD_ENABLE_LK401 0xe9 |
| 144 | #define LK_CMD_REQUEST_ID 0xab |
| 145 | |
| 146 | /* Misc responses from keyboard */ |
| 147 | #define LK_STUCK_KEY 0x3d |
| 148 | #define LK_SELFTEST_FAILED 0x3e |
| 149 | #define LK_ALL_KEYS_UP 0xb3 |
| 150 | #define LK_METRONOME 0xb4 |
| 151 | #define LK_OUTPUT_ERROR 0xb5 |
| 152 | #define LK_INPUT_ERROR 0xb6 |
| 153 | #define LK_KBD_LOCKED 0xb7 |
| 154 | #define LK_KBD_TEST_MODE_ACK 0xb8 |
| 155 | #define LK_PREFIX_KEY_DOWN 0xb9 |
| 156 | #define LK_MODE_CHANGE_ACK 0xba |
| 157 | #define LK_RESPONSE_RESERVED 0xbb |
| 158 | |
| 159 | #define LK_NUM_KEYCODES 256 |
| 160 | #define LK_NUM_IGNORE_BYTES 6 |
| 161 | typedef u_int16_t lk_keycode_t; |
| 162 | |
| 163 | |
| 164 | |
| 165 | static lk_keycode_t lkkbd_keycode[LK_NUM_KEYCODES] = { |
| 166 | [0x56] = KEY_F1, |
| 167 | [0x57] = KEY_F2, |
| 168 | [0x58] = KEY_F3, |
| 169 | [0x59] = KEY_F4, |
| 170 | [0x5a] = KEY_F5, |
| 171 | [0x64] = KEY_F6, |
| 172 | [0x65] = KEY_F7, |
| 173 | [0x66] = KEY_F8, |
| 174 | [0x67] = KEY_F9, |
| 175 | [0x68] = KEY_F10, |
| 176 | [0x71] = KEY_F11, |
| 177 | [0x72] = KEY_F12, |
| 178 | [0x73] = KEY_F13, |
| 179 | [0x74] = KEY_F14, |
| 180 | [0x7c] = KEY_F15, |
| 181 | [0x7d] = KEY_F16, |
| 182 | [0x80] = KEY_F17, |
| 183 | [0x81] = KEY_F18, |
| 184 | [0x82] = KEY_F19, |
| 185 | [0x83] = KEY_F20, |
| 186 | [0x8a] = KEY_FIND, |
| 187 | [0x8b] = KEY_INSERT, |
| 188 | [0x8c] = KEY_DELETE, |
| 189 | [0x8d] = KEY_SELECT, |
| 190 | [0x8e] = KEY_PAGEUP, |
| 191 | [0x8f] = KEY_PAGEDOWN, |
| 192 | [0x92] = KEY_KP0, |
| 193 | [0x94] = KEY_KPDOT, |
| 194 | [0x95] = KEY_KPENTER, |
| 195 | [0x96] = KEY_KP1, |
| 196 | [0x97] = KEY_KP2, |
| 197 | [0x98] = KEY_KP3, |
| 198 | [0x99] = KEY_KP4, |
| 199 | [0x9a] = KEY_KP5, |
| 200 | [0x9b] = KEY_KP6, |
| 201 | [0x9c] = KEY_KPCOMMA, |
| 202 | [0x9d] = KEY_KP7, |
| 203 | [0x9e] = KEY_KP8, |
| 204 | [0x9f] = KEY_KP9, |
| 205 | [0xa0] = KEY_KPMINUS, |
| 206 | [0xa1] = KEY_PROG1, |
| 207 | [0xa2] = KEY_PROG2, |
| 208 | [0xa3] = KEY_PROG3, |
| 209 | [0xa4] = KEY_PROG4, |
| 210 | [0xa7] = KEY_LEFT, |
| 211 | [0xa8] = KEY_RIGHT, |
| 212 | [0xa9] = KEY_DOWN, |
| 213 | [0xaa] = KEY_UP, |
| 214 | [0xab] = KEY_RIGHTSHIFT, |
| 215 | [0xac] = KEY_LEFTALT, |
| 216 | [0xad] = KEY_COMPOSE, /* Right Compose, that is. */ |
| 217 | [0xae] = KEY_LEFTSHIFT, /* Same as KEY_RIGHTSHIFT on LK201 */ |
| 218 | [0xaf] = KEY_LEFTCTRL, |
| 219 | [0xb0] = KEY_CAPSLOCK, |
| 220 | [0xb1] = KEY_COMPOSE, /* Left Compose, that is. */ |
| 221 | [0xb2] = KEY_RIGHTALT, |
| 222 | [0xbc] = KEY_BACKSPACE, |
| 223 | [0xbd] = KEY_ENTER, |
| 224 | [0xbe] = KEY_TAB, |
| 225 | [0xbf] = KEY_ESC, |
| 226 | [0xc0] = KEY_1, |
| 227 | [0xc1] = KEY_Q, |
| 228 | [0xc2] = KEY_A, |
| 229 | [0xc3] = KEY_Z, |
| 230 | [0xc5] = KEY_2, |
| 231 | [0xc6] = KEY_W, |
| 232 | [0xc7] = KEY_S, |
| 233 | [0xc8] = KEY_X, |
| 234 | [0xc9] = KEY_102ND, |
| 235 | [0xcb] = KEY_3, |
| 236 | [0xcc] = KEY_E, |
| 237 | [0xcd] = KEY_D, |
| 238 | [0xce] = KEY_C, |
| 239 | [0xd0] = KEY_4, |
| 240 | [0xd1] = KEY_R, |
| 241 | [0xd2] = KEY_F, |
| 242 | [0xd3] = KEY_V, |
| 243 | [0xd4] = KEY_SPACE, |
| 244 | [0xd6] = KEY_5, |
| 245 | [0xd7] = KEY_T, |
| 246 | [0xd8] = KEY_G, |
| 247 | [0xd9] = KEY_B, |
| 248 | [0xdb] = KEY_6, |
| 249 | [0xdc] = KEY_Y, |
| 250 | [0xdd] = KEY_H, |
| 251 | [0xde] = KEY_N, |
| 252 | [0xe0] = KEY_7, |
| 253 | [0xe1] = KEY_U, |
| 254 | [0xe2] = KEY_J, |
| 255 | [0xe3] = KEY_M, |
| 256 | [0xe5] = KEY_8, |
| 257 | [0xe6] = KEY_I, |
| 258 | [0xe7] = KEY_K, |
| 259 | [0xe8] = KEY_COMMA, |
| 260 | [0xea] = KEY_9, |
| 261 | [0xeb] = KEY_O, |
| 262 | [0xec] = KEY_L, |
| 263 | [0xed] = KEY_DOT, |
| 264 | [0xef] = KEY_0, |
| 265 | [0xf0] = KEY_P, |
| 266 | [0xf2] = KEY_SEMICOLON, |
| 267 | [0xf3] = KEY_SLASH, |
| 268 | [0xf5] = KEY_EQUAL, |
| 269 | [0xf6] = KEY_RIGHTBRACE, |
| 270 | [0xf7] = KEY_BACKSLASH, |
| 271 | [0xf9] = KEY_MINUS, |
| 272 | [0xfa] = KEY_LEFTBRACE, |
| 273 | [0xfb] = KEY_APOSTROPHE, |
| 274 | }; |
| 275 | |
Jan-Benedict Glaw | 0aeafa7 | 2005-11-02 22:53:11 -0500 | [diff] [blame] | 276 | #define CHECK_LED(LK, VAR_ON, VAR_OFF, LED, BITS) do { \ |
| 277 | if (test_bit (LED, (LK)->dev->led)) \ |
| 278 | VAR_ON |= BITS; \ |
| 279 | else \ |
| 280 | VAR_OFF |= BITS; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } while (0) |
| 282 | |
| 283 | /* |
| 284 | * Per-keyboard data |
| 285 | */ |
| 286 | struct lkkbd { |
| 287 | lk_keycode_t keycode[LK_NUM_KEYCODES]; |
| 288 | int ignore_bytes; |
| 289 | unsigned char id[LK_NUM_IGNORE_BYTES]; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 290 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | struct serio *serio; |
| 292 | struct work_struct tq; |
| 293 | char name[64]; |
| 294 | char phys[32]; |
| 295 | char type; |
| 296 | int bell_volume; |
| 297 | int keyclick_volume; |
| 298 | int ctrlclick_volume; |
| 299 | }; |
| 300 | |
Jan-Benedict Glaw | 0aeafa7 | 2005-11-02 22:53:11 -0500 | [diff] [blame] | 301 | #ifdef LKKBD_DEBUG |
| 302 | /* |
| 303 | * Responses from the keyboard and mapping back to their names. |
| 304 | */ |
| 305 | static struct { |
| 306 | unsigned char value; |
| 307 | unsigned char *name; |
| 308 | } lk_response[] = { |
| 309 | #define RESPONSE(x) { .value = (x), .name = #x, } |
| 310 | RESPONSE (LK_STUCK_KEY), |
| 311 | RESPONSE (LK_SELFTEST_FAILED), |
| 312 | RESPONSE (LK_ALL_KEYS_UP), |
| 313 | RESPONSE (LK_METRONOME), |
| 314 | RESPONSE (LK_OUTPUT_ERROR), |
| 315 | RESPONSE (LK_INPUT_ERROR), |
| 316 | RESPONSE (LK_KBD_LOCKED), |
| 317 | RESPONSE (LK_KBD_TEST_MODE_ACK), |
| 318 | RESPONSE (LK_PREFIX_KEY_DOWN), |
| 319 | RESPONSE (LK_MODE_CHANGE_ACK), |
| 320 | RESPONSE (LK_RESPONSE_RESERVED), |
| 321 | #undef RESPONSE |
| 322 | }; |
| 323 | |
| 324 | static unsigned char * |
| 325 | response_name (unsigned char value) |
| 326 | { |
| 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < ARRAY_SIZE (lk_response); i++) |
| 330 | if (lk_response[i].value == value) |
| 331 | return lk_response[i].name; |
| 332 | |
| 333 | return "<unknown>"; |
| 334 | } |
| 335 | #endif /* LKKBD_DEBUG */ |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | /* |
| 338 | * Calculate volume parameter byte for a given volume. |
| 339 | */ |
| 340 | static unsigned char |
| 341 | volume_to_hw (int volume_percent) |
| 342 | { |
| 343 | unsigned char ret = 0; |
| 344 | |
| 345 | if (volume_percent < 0) |
| 346 | volume_percent = 0; |
| 347 | if (volume_percent > 100) |
| 348 | volume_percent = 100; |
| 349 | |
| 350 | if (volume_percent >= 0) |
| 351 | ret = 7; |
| 352 | if (volume_percent >= 13) /* 12.5 */ |
| 353 | ret = 6; |
| 354 | if (volume_percent >= 25) |
| 355 | ret = 5; |
| 356 | if (volume_percent >= 38) /* 37.5 */ |
| 357 | ret = 4; |
| 358 | if (volume_percent >= 50) |
| 359 | ret = 3; |
| 360 | if (volume_percent >= 63) /* 62.5 */ |
| 361 | ret = 2; /* This is the default volume */ |
| 362 | if (volume_percent >= 75) |
| 363 | ret = 1; |
| 364 | if (volume_percent >= 88) /* 87.5 */ |
| 365 | ret = 0; |
| 366 | |
| 367 | ret |= 0x80; |
| 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | lkkbd_detection_done (struct lkkbd *lk) |
| 374 | { |
| 375 | int i; |
| 376 | |
| 377 | /* |
| 378 | * Reset setting for Compose key. Let Compose be KEY_COMPOSE. |
| 379 | */ |
| 380 | lk->keycode[0xb1] = KEY_COMPOSE; |
| 381 | |
| 382 | /* |
| 383 | * Print keyboard name and modify Compose=Alt on user's request. |
| 384 | */ |
| 385 | switch (lk->id[4]) { |
| 386 | case 1: |
Dmitry Torokhov | ea08c6f | 2006-06-26 01:46:17 -0400 | [diff] [blame] | 387 | strlcpy (lk->name, "DEC LK201 keyboard", |
| 388 | sizeof (lk->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | if (lk201_compose_is_alt) |
| 391 | lk->keycode[0xb1] = KEY_LEFTALT; |
| 392 | break; |
| 393 | |
| 394 | case 2: |
Dmitry Torokhov | ea08c6f | 2006-06-26 01:46:17 -0400 | [diff] [blame] | 395 | strlcpy (lk->name, "DEC LK401 keyboard", |
| 396 | sizeof (lk->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | |
| 399 | default: |
Dmitry Torokhov | ea08c6f | 2006-06-26 01:46:17 -0400 | [diff] [blame] | 400 | strlcpy (lk->name, "Unknown DEC keyboard", |
| 401 | sizeof (lk->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | printk (KERN_ERR "lkkbd: keyboard on %s is unknown, " |
| 403 | "please report to Jan-Benedict Glaw " |
| 404 | "<jbglaw@lug-owl.de>\n", lk->phys); |
| 405 | printk (KERN_ERR "lkkbd: keyboard ID'ed as:"); |
| 406 | for (i = 0; i < LK_NUM_IGNORE_BYTES; i++) |
| 407 | printk (" 0x%02x", lk->id[i]); |
| 408 | printk ("\n"); |
| 409 | break; |
| 410 | } |
| 411 | printk (KERN_INFO "lkkbd: keyboard on %s identified as: %s\n", |
| 412 | lk->phys, lk->name); |
| 413 | |
| 414 | /* |
| 415 | * Report errors during keyboard boot-up. |
| 416 | */ |
| 417 | switch (lk->id[2]) { |
| 418 | case 0x00: |
| 419 | /* All okay */ |
| 420 | break; |
| 421 | |
| 422 | case LK_STUCK_KEY: |
| 423 | printk (KERN_ERR "lkkbd: Stuck key on keyboard at " |
| 424 | "%s\n", lk->phys); |
| 425 | break; |
| 426 | |
| 427 | case LK_SELFTEST_FAILED: |
| 428 | printk (KERN_ERR "lkkbd: Selftest failed on keyboard " |
| 429 | "at %s, keyboard may not work " |
| 430 | "properly\n", lk->phys); |
| 431 | break; |
| 432 | |
| 433 | default: |
| 434 | printk (KERN_ERR "lkkbd: Unknown error %02x on " |
| 435 | "keyboard at %s\n", lk->id[2], |
| 436 | lk->phys); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Try to hint user if there's a stuck key. |
| 442 | */ |
| 443 | if (lk->id[2] == LK_STUCK_KEY && lk->id[3] != 0) |
| 444 | printk (KERN_ERR "Scancode of stuck key is 0x%02x, keycode " |
| 445 | "is 0x%04x\n", lk->id[3], |
| 446 | lk->keycode[lk->id[3]]); |
| 447 | |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * lkkbd_interrupt() is called by the low level driver when a character |
| 453 | * is received. |
| 454 | */ |
| 455 | static irqreturn_t |
| 456 | lkkbd_interrupt (struct serio *serio, unsigned char data, unsigned int flags, |
| 457 | struct pt_regs *regs) |
| 458 | { |
| 459 | struct lkkbd *lk = serio_get_drvdata (serio); |
| 460 | int i; |
| 461 | |
| 462 | DBG (KERN_INFO "Got byte 0x%02x\n", data); |
| 463 | |
| 464 | if (lk->ignore_bytes > 0) { |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 465 | DBG (KERN_INFO "Ignoring a byte on %s\n", lk->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data; |
| 467 | |
| 468 | if (lk->ignore_bytes == 0) |
| 469 | lkkbd_detection_done (lk); |
| 470 | |
| 471 | return IRQ_HANDLED; |
| 472 | } |
| 473 | |
| 474 | switch (data) { |
| 475 | case LK_ALL_KEYS_UP: |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 476 | input_regs (lk->dev, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | for (i = 0; i < ARRAY_SIZE (lkkbd_keycode); i++) |
| 478 | if (lk->keycode[i] != KEY_RESERVED) |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 479 | input_report_key (lk->dev, lk->keycode[i], 0); |
| 480 | input_sync (lk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | break; |
Jan-Benedict Glaw | 0aeafa7 | 2005-11-02 22:53:11 -0500 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | case 0x01: |
| 484 | DBG (KERN_INFO "Got 0x01, scheduling re-initialization\n"); |
| 485 | lk->ignore_bytes = LK_NUM_IGNORE_BYTES; |
| 486 | lk->id[LK_NUM_IGNORE_BYTES - lk->ignore_bytes--] = data; |
| 487 | schedule_work (&lk->tq); |
| 488 | break; |
| 489 | |
Jan-Benedict Glaw | 0aeafa7 | 2005-11-02 22:53:11 -0500 | [diff] [blame] | 490 | case LK_METRONOME: |
| 491 | case LK_OUTPUT_ERROR: |
| 492 | case LK_INPUT_ERROR: |
| 493 | case LK_KBD_LOCKED: |
| 494 | case LK_KBD_TEST_MODE_ACK: |
| 495 | case LK_PREFIX_KEY_DOWN: |
| 496 | case LK_MODE_CHANGE_ACK: |
| 497 | case LK_RESPONSE_RESERVED: |
| 498 | DBG (KERN_INFO "Got %s and don't know how to handle...\n", |
| 499 | response_name (data)); |
| 500 | break; |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | default: |
| 503 | if (lk->keycode[data] != KEY_RESERVED) { |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 504 | input_regs (lk->dev, regs); |
| 505 | if (!test_bit (lk->keycode[data], lk->dev->key)) |
| 506 | input_report_key (lk->dev, lk->keycode[data], 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | else |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 508 | input_report_key (lk->dev, lk->keycode[data], 0); |
| 509 | input_sync (lk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } else |
| 511 | printk (KERN_WARNING "%s: Unknown key with " |
| 512 | "scancode 0x%02x on %s.\n", |
| 513 | __FILE__, data, lk->name); |
| 514 | } |
| 515 | |
| 516 | return IRQ_HANDLED; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * lkkbd_event() handles events from the input module. |
| 521 | */ |
| 522 | static int |
| 523 | lkkbd_event (struct input_dev *dev, unsigned int type, unsigned int code, |
| 524 | int value) |
| 525 | { |
| 526 | struct lkkbd *lk = dev->private; |
| 527 | unsigned char leds_on = 0; |
| 528 | unsigned char leds_off = 0; |
| 529 | |
| 530 | switch (type) { |
| 531 | case EV_LED: |
Jan-Benedict Glaw | 0aeafa7 | 2005-11-02 22:53:11 -0500 | [diff] [blame] | 532 | CHECK_LED (lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK); |
| 533 | CHECK_LED (lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE); |
| 534 | CHECK_LED (lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK); |
| 535 | CHECK_LED (lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | if (leds_on != 0) { |
| 537 | lk->serio->write (lk->serio, LK_CMD_LED_ON); |
| 538 | lk->serio->write (lk->serio, leds_on); |
| 539 | } |
| 540 | if (leds_off != 0) { |
| 541 | lk->serio->write (lk->serio, LK_CMD_LED_OFF); |
| 542 | lk->serio->write (lk->serio, leds_off); |
| 543 | } |
| 544 | return 0; |
| 545 | |
| 546 | case EV_SND: |
| 547 | switch (code) { |
| 548 | case SND_CLICK: |
| 549 | if (value == 0) { |
| 550 | DBG ("%s: Deactivating key clicks\n", __FUNCTION__); |
| 551 | lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK); |
| 552 | lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK); |
| 553 | } else { |
| 554 | DBG ("%s: Activating key clicks\n", __FUNCTION__); |
| 555 | lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK); |
| 556 | lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume)); |
| 557 | lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK); |
| 558 | lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume)); |
| 559 | } |
| 560 | return 0; |
| 561 | |
| 562 | case SND_BELL: |
| 563 | if (value != 0) |
| 564 | lk->serio->write (lk->serio, LK_CMD_SOUND_BELL); |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | break; |
| 569 | |
| 570 | default: |
| 571 | printk (KERN_ERR "%s (): Got unknown type %d, code %d, value %d\n", |
| 572 | __FUNCTION__, type, code, value); |
| 573 | } |
| 574 | |
| 575 | return -1; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * lkkbd_reinit() sets leds and beeps to a state the computer remembers they |
| 580 | * were in. |
| 581 | */ |
| 582 | static void |
| 583 | lkkbd_reinit (void *data) |
| 584 | { |
| 585 | struct lkkbd *lk = data; |
| 586 | int division; |
| 587 | unsigned char leds_on = 0; |
| 588 | unsigned char leds_off = 0; |
| 589 | |
| 590 | /* Ask for ID */ |
| 591 | lk->serio->write (lk->serio, LK_CMD_REQUEST_ID); |
| 592 | |
| 593 | /* Reset parameters */ |
| 594 | lk->serio->write (lk->serio, LK_CMD_SET_DEFAULTS); |
| 595 | |
| 596 | /* Set LEDs */ |
Jan-Benedict Glaw | 0aeafa7 | 2005-11-02 22:53:11 -0500 | [diff] [blame] | 597 | CHECK_LED (lk, leds_on, leds_off, LED_CAPSL, LK_LED_SHIFTLOCK); |
| 598 | CHECK_LED (lk, leds_on, leds_off, LED_COMPOSE, LK_LED_COMPOSE); |
| 599 | CHECK_LED (lk, leds_on, leds_off, LED_SCROLLL, LK_LED_SCROLLLOCK); |
| 600 | CHECK_LED (lk, leds_on, leds_off, LED_SLEEP, LK_LED_WAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (leds_on != 0) { |
| 602 | lk->serio->write (lk->serio, LK_CMD_LED_ON); |
| 603 | lk->serio->write (lk->serio, leds_on); |
| 604 | } |
| 605 | if (leds_off != 0) { |
| 606 | lk->serio->write (lk->serio, LK_CMD_LED_OFF); |
| 607 | lk->serio->write (lk->serio, leds_off); |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Try to activate extended LK401 mode. This command will |
| 612 | * only work with a LK401 keyboard and grants access to |
| 613 | * LAlt, RAlt, RCompose and RShift. |
| 614 | */ |
| 615 | lk->serio->write (lk->serio, LK_CMD_ENABLE_LK401); |
| 616 | |
| 617 | /* Set all keys to UPDOWN mode */ |
| 618 | for (division = 1; division <= 14; division++) |
| 619 | lk->serio->write (lk->serio, LK_CMD_SET_MODE (LK_MODE_UPDOWN, |
| 620 | division)); |
| 621 | |
| 622 | /* Enable bell and set volume */ |
| 623 | lk->serio->write (lk->serio, LK_CMD_ENABLE_BELL); |
| 624 | lk->serio->write (lk->serio, volume_to_hw (lk->bell_volume)); |
| 625 | |
| 626 | /* Enable/disable keyclick (and possibly set volume) */ |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 627 | if (test_bit (SND_CLICK, lk->dev->snd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | lk->serio->write (lk->serio, LK_CMD_ENABLE_KEYCLICK); |
| 629 | lk->serio->write (lk->serio, volume_to_hw (lk->keyclick_volume)); |
| 630 | lk->serio->write (lk->serio, LK_CMD_ENABLE_CTRCLICK); |
| 631 | lk->serio->write (lk->serio, volume_to_hw (lk->ctrlclick_volume)); |
| 632 | } else { |
| 633 | lk->serio->write (lk->serio, LK_CMD_DISABLE_KEYCLICK); |
| 634 | lk->serio->write (lk->serio, LK_CMD_DISABLE_CTRCLICK); |
| 635 | } |
| 636 | |
| 637 | /* Sound the bell if needed */ |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 638 | if (test_bit (SND_BELL, lk->dev->snd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | lk->serio->write (lk->serio, LK_CMD_SOUND_BELL); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * lkkbd_connect() probes for a LK keyboard and fills the necessary structures. |
| 644 | */ |
| 645 | static int |
| 646 | lkkbd_connect (struct serio *serio, struct serio_driver *drv) |
| 647 | { |
| 648 | struct lkkbd *lk; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 649 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | int i; |
| 651 | int err; |
| 652 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 653 | lk = kzalloc (sizeof (struct lkkbd), GFP_KERNEL); |
| 654 | input_dev = input_allocate_device (); |
| 655 | if (!lk || !input_dev) { |
| 656 | err = -ENOMEM; |
| 657 | goto fail; |
| 658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | lk->serio = serio; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 661 | lk->dev = input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | INIT_WORK (&lk->tq, lkkbd_reinit, lk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | lk->bell_volume = bell_volume; |
| 664 | lk->keyclick_volume = keyclick_volume; |
| 665 | lk->ctrlclick_volume = ctrlclick_volume; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 666 | memcpy (lk->keycode, lkkbd_keycode, sizeof (lk_keycode_t) * LK_NUM_KEYCODES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 668 | strlcpy (lk->name, "DEC LK keyboard", sizeof(lk->name)); |
| 669 | snprintf (lk->phys, sizeof(lk->phys), "%s/input0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 671 | input_dev->name = lk->name; |
| 672 | input_dev->phys = lk->phys; |
| 673 | input_dev->id.bustype = BUS_RS232; |
| 674 | input_dev->id.vendor = SERIO_LKKBD; |
| 675 | input_dev->id.product = 0; |
| 676 | input_dev->id.version = 0x0100; |
| 677 | input_dev->cdev.dev = &serio->dev; |
| 678 | input_dev->event = lkkbd_event; |
| 679 | input_dev->private = lk; |
| 680 | |
| 681 | set_bit (EV_KEY, input_dev->evbit); |
| 682 | set_bit (EV_LED, input_dev->evbit); |
| 683 | set_bit (EV_SND, input_dev->evbit); |
| 684 | set_bit (EV_REP, input_dev->evbit); |
| 685 | set_bit (LED_CAPSL, input_dev->ledbit); |
| 686 | set_bit (LED_SLEEP, input_dev->ledbit); |
| 687 | set_bit (LED_COMPOSE, input_dev->ledbit); |
| 688 | set_bit (LED_SCROLLL, input_dev->ledbit); |
| 689 | set_bit (SND_BELL, input_dev->sndbit); |
| 690 | set_bit (SND_CLICK, input_dev->sndbit); |
| 691 | |
| 692 | input_dev->keycode = lk->keycode; |
| 693 | input_dev->keycodesize = sizeof (lk_keycode_t); |
| 694 | input_dev->keycodemax = LK_NUM_KEYCODES; |
| 695 | for (i = 0; i < LK_NUM_KEYCODES; i++) |
| 696 | set_bit (lk->keycode[i], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | serio_set_drvdata (serio, lk); |
| 699 | |
| 700 | err = serio_open (serio, drv); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 701 | if (err) |
| 702 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 704 | input_register_device (lk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | lk->serio->write (lk->serio, LK_CMD_POWERCYCLE_RESET); |
| 706 | |
| 707 | return 0; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 708 | |
| 709 | fail: serio_set_drvdata (serio, NULL); |
| 710 | input_free_device (input_dev); |
| 711 | kfree (lk); |
| 712 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
| 716 | * lkkbd_disconnect() unregisters and closes behind us. |
| 717 | */ |
| 718 | static void |
| 719 | lkkbd_disconnect (struct serio *serio) |
| 720 | { |
| 721 | struct lkkbd *lk = serio_get_drvdata (serio); |
| 722 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 723 | input_get_device (lk->dev); |
| 724 | input_unregister_device (lk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | serio_close (serio); |
| 726 | serio_set_drvdata (serio, NULL); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 727 | input_put_device (lk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | kfree (lk); |
| 729 | } |
| 730 | |
| 731 | static struct serio_device_id lkkbd_serio_ids[] = { |
| 732 | { |
| 733 | .type = SERIO_RS232, |
| 734 | .proto = SERIO_LKKBD, |
| 735 | .id = SERIO_ANY, |
| 736 | .extra = SERIO_ANY, |
| 737 | }, |
| 738 | { 0 } |
| 739 | }; |
| 740 | |
| 741 | MODULE_DEVICE_TABLE(serio, lkkbd_serio_ids); |
| 742 | |
| 743 | static struct serio_driver lkkbd_drv = { |
| 744 | .driver = { |
| 745 | .name = "lkkbd", |
| 746 | }, |
| 747 | .description = DRIVER_DESC, |
| 748 | .id_table = lkkbd_serio_ids, |
| 749 | .connect = lkkbd_connect, |
| 750 | .disconnect = lkkbd_disconnect, |
| 751 | .interrupt = lkkbd_interrupt, |
| 752 | }; |
| 753 | |
| 754 | /* |
| 755 | * The functions for insering/removing us as a module. |
| 756 | */ |
| 757 | static int __init |
| 758 | lkkbd_init (void) |
| 759 | { |
| 760 | serio_register_driver(&lkkbd_drv); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static void __exit |
| 765 | lkkbd_exit (void) |
| 766 | { |
| 767 | serio_unregister_driver(&lkkbd_drv); |
| 768 | } |
| 769 | |
| 770 | module_init (lkkbd_init); |
| 771 | module_exit (lkkbd_exit); |
| 772 | |