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