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