1 // SPDX-License-Identifier: GPL-2.0-only
3 * Parallel port to Keyboard port adapter driver for Linux
5 * Copyright (c) 1999-2004 Vojtech Pavlik
10 * To connect an AT or XT keyboard to the parallel port, a fairly simple adapter
13 * Parallel port Keyboard port
15 * +5V --------------------- +5V (4)
18 * +5V -------|______|--.
20 * ACK (10) ------------|
22 * STROBE (1) ---|<|----'
25 * +5V -------|______|--.
27 * BUSY (11) -----------|
29 * AUTOFD (14) --|<|----'
31 * GND (18-25) ------------- GND (3)
33 * The diodes can be fairly any type, and the resistors should be somewhere
34 * around 5 kOhm, but the adapter will likely work without the resistors,
37 * The +5V source can be taken either from USB, from mouse or keyboard ports,
38 * or from a joystick port. Unfortunately, the parallel port of a PC doesn't
39 * have a +5V pin, and feeding the keyboard from signal pins is out of question
40 * with 300 mA power reqirement of a typical AT keyboard.
43 #include <linux/module.h>
44 #include <linux/parport.h>
45 #include <linux/slab.h>
46 #include <linux/init.h>
47 #include <linux/serio.h>
49 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
50 MODULE_DESCRIPTION("Parallel port to Keyboard port adapter driver");
51 MODULE_LICENSE("GPL");
53 static unsigned int parkbd_pp_no
;
54 module_param_named(port
, parkbd_pp_no
, int, 0);
55 MODULE_PARM_DESC(port
, "Parallel port the adapter is connected to (default is 0)");
57 static unsigned int parkbd_mode
= SERIO_8042
;
58 module_param_named(mode
, parkbd_mode
, uint
, 0);
59 MODULE_PARM_DESC(mode
, "Mode of operation: XT = 0/AT = 1 (default)");
61 #define PARKBD_CLOCK 0x01 /* Strobe & Ack */
62 #define PARKBD_DATA 0x02 /* AutoFd & Busy */
64 static int parkbd_buffer
;
65 static int parkbd_counter
;
66 static unsigned long parkbd_last
;
67 static int parkbd_writing
;
68 static unsigned long parkbd_start
;
70 static struct pardevice
*parkbd_dev
;
71 static struct serio
*parkbd_port
;
73 static int parkbd_readlines(void)
75 return (parport_read_status(parkbd_dev
->port
) >> 6) ^ 2;
78 static void parkbd_writelines(int data
)
80 parport_write_control(parkbd_dev
->port
, (~data
& 3) | 0x10);
83 static int parkbd_write(struct serio
*port
, unsigned char c
)
87 if (!parkbd_mode
) return -1;
95 parkbd_buffer
= c
| (((int) (~p
& 1)) << 8) | 0x600;
102 static void parkbd_interrupt(void *dev_id
)
105 if (parkbd_writing
) {
107 if (parkbd_counter
&& ((parkbd_counter
== 11) || time_after(jiffies
, parkbd_last
+ HZ
/100))) {
111 parkbd_writelines(3);
115 parkbd_writelines(((parkbd_buffer
>> parkbd_counter
++) & 1) | 2);
117 if (parkbd_counter
== 11) {
121 parkbd_writelines(3);
126 if ((parkbd_counter
== parkbd_mode
+ 10) || time_after(jiffies
, parkbd_last
+ HZ
/100)) {
131 parkbd_buffer
|= (parkbd_readlines() >> 1) << parkbd_counter
++;
133 if (parkbd_counter
== parkbd_mode
+ 10)
134 serio_interrupt(parkbd_port
, (parkbd_buffer
>> (2 - parkbd_mode
)) & 0xff, 0);
137 parkbd_last
= jiffies
;
140 static int parkbd_getport(struct parport
*pp
)
142 struct pardev_cb parkbd_parport_cb
;
144 memset(&parkbd_parport_cb
, 0, sizeof(parkbd_parport_cb
));
145 parkbd_parport_cb
.irq_func
= parkbd_interrupt
;
146 parkbd_parport_cb
.flags
= PARPORT_FLAG_EXCL
;
148 parkbd_dev
= parport_register_dev_model(pp
, "parkbd",
149 &parkbd_parport_cb
, 0);
154 if (parport_claim(parkbd_dev
)) {
155 parport_unregister_device(parkbd_dev
);
159 parkbd_start
= jiffies
;
164 static struct serio
*parkbd_allocate_serio(void)
168 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
170 serio
->id
.type
= parkbd_mode
;
171 serio
->write
= parkbd_write
,
172 strlcpy(serio
->name
, "PARKBD AT/XT keyboard adapter", sizeof(serio
->name
));
173 snprintf(serio
->phys
, sizeof(serio
->phys
), "%s/serio0", parkbd_dev
->port
->name
);
179 static void parkbd_attach(struct parport
*pp
)
181 if (pp
->number
!= parkbd_pp_no
) {
182 pr_debug("Not using parport%d.\n", pp
->number
);
186 if (parkbd_getport(pp
))
189 parkbd_port
= parkbd_allocate_serio();
191 parport_release(parkbd_dev
);
192 parport_unregister_device(parkbd_dev
);
196 parkbd_writelines(3);
198 serio_register_port(parkbd_port
);
200 printk(KERN_INFO
"serio: PARKBD %s adapter on %s\n",
201 parkbd_mode
? "AT" : "XT", parkbd_dev
->port
->name
);
206 static void parkbd_detach(struct parport
*port
)
208 if (!parkbd_port
|| port
->number
!= parkbd_pp_no
)
211 parport_release(parkbd_dev
);
212 serio_unregister_port(parkbd_port
);
213 parport_unregister_device(parkbd_dev
);
217 static struct parport_driver parkbd_parport_driver
= {
219 .match_port
= parkbd_attach
,
220 .detach
= parkbd_detach
,
224 static int __init
parkbd_init(void)
226 return parport_register_driver(&parkbd_parport_driver
);
229 static void __exit
parkbd_exit(void)
231 parport_unregister_driver(&parkbd_parport_driver
);
234 module_init(parkbd_init
);
235 module_exit(parkbd_exit
);