Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Parallel port to Keyboard port adapter driver for Linux |
| 3 | * |
| 4 | * Copyright (c) 1999-2004 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published by |
| 10 | * the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * To connect an AT or XT keyboard to the parallel port, a fairly simple adapter |
| 15 | * can be made: |
| 16 | * |
| 17 | * Parallel port Keyboard port |
| 18 | * |
| 19 | * +5V --------------------- +5V (4) |
| 20 | * |
| 21 | * ______ |
| 22 | * +5V -------|______|--. |
| 23 | * | |
| 24 | * ACK (10) ------------| |
| 25 | * |--- KBD CLOCK (5) |
| 26 | * STROBE (1) ---|<|----' |
| 27 | * |
| 28 | * ______ |
| 29 | * +5V -------|______|--. |
| 30 | * | |
| 31 | * BUSY (11) -----------| |
| 32 | * |--- KBD DATA (1) |
| 33 | * AUTOFD (14) --|<|----' |
| 34 | * |
| 35 | * GND (18-25) ------------- GND (3) |
| 36 | * |
| 37 | * The diodes can be fairly any type, and the resistors should be somewhere |
| 38 | * around 5 kOhm, but the adapter will likely work without the resistors, |
| 39 | * too. |
| 40 | * |
| 41 | * The +5V source can be taken either from USB, from mouse or keyboard ports, |
| 42 | * or from a joystick port. Unfortunately, the parallel port of a PC doesn't |
| 43 | * have a +5V pin, and feeding the keyboard from signal pins is out of question |
| 44 | * with 300 mA power reqirement of a typical AT keyboard. |
| 45 | */ |
| 46 | |
| 47 | #include <linux/module.h> |
| 48 | #include <linux/parport.h> |
| 49 | #include <linux/init.h> |
| 50 | #include <linux/serio.h> |
| 51 | |
| 52 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 53 | MODULE_DESCRIPTION("Parallel port to Keyboard port adapter driver"); |
| 54 | MODULE_LICENSE("GPL"); |
| 55 | |
| 56 | static unsigned int parkbd_pp_no; |
| 57 | module_param_named(port, parkbd_pp_no, int, 0); |
| 58 | MODULE_PARM_DESC(port, "Parallel port the adapter is connected to (default is 0)"); |
| 59 | |
| 60 | static unsigned int parkbd_mode = SERIO_8042; |
| 61 | module_param_named(mode, parkbd_mode, uint, 0); |
| 62 | MODULE_PARM_DESC(mode, "Mode of operation: XT = 0/AT = 1 (default)"); |
| 63 | |
| 64 | #define PARKBD_CLOCK 0x01 /* Strobe & Ack */ |
| 65 | #define PARKBD_DATA 0x02 /* AutoFd & Busy */ |
| 66 | |
| 67 | static int parkbd_buffer; |
| 68 | static int parkbd_counter; |
| 69 | static unsigned long parkbd_last; |
| 70 | static int parkbd_writing; |
| 71 | static unsigned long parkbd_start; |
| 72 | |
| 73 | static struct pardevice *parkbd_dev; |
| 74 | static struct serio *parkbd_port; |
| 75 | |
| 76 | static int parkbd_readlines(void) |
| 77 | { |
| 78 | return (parport_read_status(parkbd_dev->port) >> 6) ^ 2; |
| 79 | } |
| 80 | |
| 81 | static void parkbd_writelines(int data) |
| 82 | { |
| 83 | parport_write_control(parkbd_dev->port, (~data & 3) | 0x10); |
| 84 | } |
| 85 | |
| 86 | static int parkbd_write(struct serio *port, unsigned char c) |
| 87 | { |
| 88 | unsigned char p; |
| 89 | |
| 90 | if (!parkbd_mode) return -1; |
| 91 | |
| 92 | p = c ^ (c >> 4); |
| 93 | p = p ^ (p >> 2); |
| 94 | p = p ^ (p >> 1); |
| 95 | |
| 96 | parkbd_counter = 0; |
| 97 | parkbd_writing = 1; |
| 98 | parkbd_buffer = c | (((int) (~p & 1)) << 8) | 0x600; |
| 99 | |
| 100 | parkbd_writelines(2); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame^] | 105 | static void parkbd_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | |
| 108 | if (parkbd_writing) { |
| 109 | |
| 110 | if (parkbd_counter && ((parkbd_counter == 11) || time_after(jiffies, parkbd_last + HZ/100))) { |
| 111 | parkbd_counter = 0; |
| 112 | parkbd_buffer = 0; |
| 113 | parkbd_writing = 0; |
| 114 | parkbd_writelines(3); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | parkbd_writelines(((parkbd_buffer >> parkbd_counter++) & 1) | 2); |
| 119 | |
| 120 | if (parkbd_counter == 11) { |
| 121 | parkbd_counter = 0; |
| 122 | parkbd_buffer = 0; |
| 123 | parkbd_writing = 0; |
| 124 | parkbd_writelines(3); |
| 125 | } |
| 126 | |
| 127 | } else { |
| 128 | |
| 129 | if ((parkbd_counter == parkbd_mode + 10) || time_after(jiffies, parkbd_last + HZ/100)) { |
| 130 | parkbd_counter = 0; |
| 131 | parkbd_buffer = 0; |
| 132 | } |
| 133 | |
| 134 | parkbd_buffer |= (parkbd_readlines() >> 1) << parkbd_counter++; |
| 135 | |
| 136 | if (parkbd_counter == parkbd_mode + 10) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame^] | 137 | serio_interrupt(parkbd_port, (parkbd_buffer >> (2 - parkbd_mode)) & 0xff, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | parkbd_last = jiffies; |
| 141 | } |
| 142 | |
| 143 | static int parkbd_getport(void) |
| 144 | { |
| 145 | struct parport *pp; |
| 146 | |
| 147 | pp = parport_find_number(parkbd_pp_no); |
| 148 | |
| 149 | if (pp == NULL) { |
| 150 | printk(KERN_ERR "parkbd: no such parport\n"); |
| 151 | return -ENODEV; |
| 152 | } |
| 153 | |
| 154 | parkbd_dev = parport_register_device(pp, "parkbd", NULL, NULL, parkbd_interrupt, PARPORT_DEV_EXCL, NULL); |
| 155 | parport_put_port(pp); |
| 156 | |
| 157 | if (!parkbd_dev) |
| 158 | return -ENODEV; |
| 159 | |
| 160 | if (parport_claim(parkbd_dev)) { |
| 161 | parport_unregister_device(parkbd_dev); |
| 162 | return -EBUSY; |
| 163 | } |
| 164 | |
| 165 | parkbd_start = jiffies; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static struct serio * __init parkbd_allocate_serio(void) |
| 171 | { |
| 172 | struct serio *serio; |
| 173 | |
Eric Sesterhenn | b39787a | 2006-03-14 00:09:16 -0500 | [diff] [blame] | 174 | serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (serio) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | serio->id.type = parkbd_mode; |
| 177 | serio->write = parkbd_write, |
| 178 | strlcpy(serio->name, "PARKBD AT/XT keyboard adapter", sizeof(serio->name)); |
| 179 | snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", parkbd_dev->port->name); |
| 180 | } |
| 181 | |
| 182 | return serio; |
| 183 | } |
| 184 | |
| 185 | static int __init parkbd_init(void) |
| 186 | { |
| 187 | int err; |
| 188 | |
| 189 | err = parkbd_getport(); |
| 190 | if (err) |
| 191 | return err; |
| 192 | |
| 193 | parkbd_port = parkbd_allocate_serio(); |
| 194 | if (!parkbd_port) { |
| 195 | parport_release(parkbd_dev); |
| 196 | return -ENOMEM; |
| 197 | } |
| 198 | |
| 199 | parkbd_writelines(3); |
| 200 | |
| 201 | serio_register_port(parkbd_port); |
| 202 | |
| 203 | printk(KERN_INFO "serio: PARKBD %s adapter on %s\n", |
| 204 | parkbd_mode ? "AT" : "XT", parkbd_dev->port->name); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void __exit parkbd_exit(void) |
| 210 | { |
| 211 | parport_release(parkbd_dev); |
| 212 | serio_unregister_port(parkbd_port); |
| 213 | parport_unregister_device(parkbd_dev); |
| 214 | } |
| 215 | |
| 216 | module_init(parkbd_init); |
| 217 | module_exit(parkbd_exit); |