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