1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Stowaway keyboard driver for Linux
4 */
5
6 /*
7 * Copyright (c) 2006 Marek Vasut
8 *
9 * Based on Newton keyboard driver for Linux
10 * by Justin Cormack
11 */
12
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/input.h>
16 #include <linux/serio.h>
17
18 #define DRIVER_DESC "Stowaway keyboard driver"
19
20 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
21 MODULE_DESCRIPTION(DRIVER_DESC);
22 MODULE_LICENSE("GPL");
23
24 #define SKBD_KEY_MASK 0x7f
25 #define SKBD_RELEASE 0x80
26
27 static unsigned char skbd_keycode[128] = {
28 KEY_1, KEY_2, KEY_3, KEY_Z, KEY_4, KEY_5, KEY_6, KEY_7,
29 0, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_GRAVE,
30 KEY_X, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_SPACE,
31 KEY_CAPSLOCK, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0,
32 0, 0, 0, KEY_LEFTALT, 0, 0, 0, 0,
33 0, 0, 0, 0, KEY_C, KEY_V, KEY_B, KEY_N,
34 KEY_MINUS, KEY_EQUAL, KEY_BACKSPACE, KEY_HOME, KEY_8, KEY_9, KEY_0, KEY_ESC,
35 KEY_LEFTBRACE, KEY_RIGHTBRACE, KEY_BACKSLASH, KEY_END, KEY_U, KEY_I, KEY_O, KEY_P,
36 KEY_APOSTROPHE, KEY_ENTER, KEY_PAGEUP,0, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON,
37 KEY_SLASH, KEY_UP, KEY_PAGEDOWN, 0,KEY_M, KEY_COMMA, KEY_DOT, KEY_INSERT,
38 KEY_DELETE, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0,
39 KEY_LEFTSHIFT, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0,
42 0, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7,
43 KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, 0, 0, 0
44 };
45
46 struct skbd {
47 unsigned char keycode[128];
48 struct input_dev *dev;
49 struct serio *serio;
50 char phys[32];
51 };
52
skbd_interrupt(struct serio * serio,unsigned char data,unsigned int flags)53 static irqreturn_t skbd_interrupt(struct serio *serio, unsigned char data,
54 unsigned int flags)
55 {
56 struct skbd *skbd = serio_get_drvdata(serio);
57 struct input_dev *dev = skbd->dev;
58
59 if (skbd->keycode[data & SKBD_KEY_MASK]) {
60 input_report_key(dev, skbd->keycode[data & SKBD_KEY_MASK],
61 !(data & SKBD_RELEASE));
62 input_sync(dev);
63 }
64
65 return IRQ_HANDLED;
66 }
67
skbd_connect(struct serio * serio,struct serio_driver * drv)68 static int skbd_connect(struct serio *serio, struct serio_driver *drv)
69 {
70 struct skbd *skbd;
71 struct input_dev *input_dev;
72 int err = -ENOMEM;
73 int i;
74
75 skbd = kzalloc(sizeof(*skbd), GFP_KERNEL);
76 input_dev = input_allocate_device();
77 if (!skbd || !input_dev)
78 goto fail1;
79
80 skbd->serio = serio;
81 skbd->dev = input_dev;
82 snprintf(skbd->phys, sizeof(skbd->phys), "%s/input0", serio->phys);
83 memcpy(skbd->keycode, skbd_keycode, sizeof(skbd->keycode));
84
85 input_dev->name = "Stowaway Keyboard";
86 input_dev->phys = skbd->phys;
87 input_dev->id.bustype = BUS_RS232;
88 input_dev->id.vendor = SERIO_STOWAWAY;
89 input_dev->id.product = 0x0001;
90 input_dev->id.version = 0x0100;
91 input_dev->dev.parent = &serio->dev;
92
93 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
94 input_dev->keycode = skbd->keycode;
95 input_dev->keycodesize = sizeof(unsigned char);
96 input_dev->keycodemax = ARRAY_SIZE(skbd_keycode);
97 for (i = 0; i < ARRAY_SIZE(skbd_keycode); i++)
98 set_bit(skbd_keycode[i], input_dev->keybit);
99 clear_bit(0, input_dev->keybit);
100
101 serio_set_drvdata(serio, skbd);
102
103 err = serio_open(serio, drv);
104 if (err)
105 goto fail2;
106
107 err = input_register_device(skbd->dev);
108 if (err)
109 goto fail3;
110
111 return 0;
112
113 fail3: serio_close(serio);
114 fail2: serio_set_drvdata(serio, NULL);
115 fail1: input_free_device(input_dev);
116 kfree(skbd);
117 return err;
118 }
119
skbd_disconnect(struct serio * serio)120 static void skbd_disconnect(struct serio *serio)
121 {
122 struct skbd *skbd = serio_get_drvdata(serio);
123
124 serio_close(serio);
125 serio_set_drvdata(serio, NULL);
126 input_unregister_device(skbd->dev);
127 kfree(skbd);
128 }
129
130 static const struct serio_device_id skbd_serio_ids[] = {
131 {
132 .type = SERIO_RS232,
133 .proto = SERIO_STOWAWAY,
134 .id = SERIO_ANY,
135 .extra = SERIO_ANY,
136 },
137 { 0 }
138 };
139
140 MODULE_DEVICE_TABLE(serio, skbd_serio_ids);
141
142 static struct serio_driver skbd_drv = {
143 .driver = {
144 .name = "stowaway",
145 },
146 .description = DRIVER_DESC,
147 .id_table = skbd_serio_ids,
148 .interrupt = skbd_interrupt,
149 .connect = skbd_connect,
150 .disconnect = skbd_disconnect,
151 };
152
153 module_serio_driver(skbd_drv);
154