xref: /linux/drivers/input/keyboard/newtonkbd.c (revision 7d12e780e003f93433d49ce78cfedf4b4c52adc5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 2000 Justin Cormack
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds  * Newton keyboard driver for Linux
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds  * (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
181da177e4SLinus Torvalds  * GNU General Public License for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
251da177e4SLinus Torvalds  * e-mail - mail your message to <j.cormack@doc.ic.ac.uk>, or by paper mail:
261da177e4SLinus Torvalds  * Justin Cormack, 68 Dartmouth Park Road, London NW5 1SN, UK.
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/module.h>
311da177e4SLinus Torvalds #include <linux/input.h>
321da177e4SLinus Torvalds #include <linux/init.h>
331da177e4SLinus Torvalds #include <linux/serio.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define DRIVER_DESC	"Newton keyboard driver"
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds MODULE_AUTHOR("Justin Cormack <j.cormack@doc.ic.ac.uk>");
381da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
391da177e4SLinus Torvalds MODULE_LICENSE("GPL");
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define NKBD_KEY	0x7f
421da177e4SLinus Torvalds #define NKBD_PRESS	0x80
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static unsigned char nkbd_keycode[128] = {
451da177e4SLinus Torvalds 	KEY_A, KEY_S, KEY_D, KEY_F, KEY_H, KEY_G, KEY_Z, KEY_X,
461da177e4SLinus Torvalds 	KEY_C, KEY_V, 0, KEY_B, KEY_Q, KEY_W, KEY_E, KEY_R,
471da177e4SLinus Torvalds 	KEY_Y, KEY_T, KEY_1, KEY_2, KEY_3, KEY_4, KEY_6, KEY_5,
481da177e4SLinus Torvalds 	KEY_EQUAL, KEY_9, KEY_7, KEY_MINUS, KEY_8, KEY_0, KEY_RIGHTBRACE, KEY_O,
491da177e4SLinus Torvalds 	KEY_U, KEY_LEFTBRACE, KEY_I, KEY_P, KEY_ENTER, KEY_L, KEY_J, KEY_APOSTROPHE,
501da177e4SLinus Torvalds 	KEY_K, KEY_SEMICOLON, KEY_BACKSLASH, KEY_COMMA, KEY_SLASH, KEY_N, KEY_M, KEY_DOT,
511da177e4SLinus Torvalds 	KEY_TAB, KEY_SPACE, KEY_GRAVE, KEY_DELETE, 0, 0, 0, KEY_LEFTMETA,
521da177e4SLinus Torvalds 	KEY_LEFTSHIFT, KEY_CAPSLOCK, KEY_LEFTALT, KEY_LEFTCTRL, KEY_RIGHTSHIFT, 0, 0, 0,
531da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
541da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
551da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
561da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
571da177e4SLinus Torvalds 	KEY_LEFT, KEY_RIGHT, KEY_DOWN, KEY_UP, 0
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds struct nkbd {
611da177e4SLinus Torvalds 	unsigned char keycode[128];
623c42f0c3SDmitry Torokhov 	struct input_dev *dev;
631da177e4SLinus Torvalds 	struct serio *serio;
641da177e4SLinus Torvalds 	char phys[32];
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static irqreturn_t nkbd_interrupt(struct serio *serio,
68*7d12e780SDavid Howells 		unsigned char data, unsigned int flags)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	struct nkbd *nkbd = serio_get_drvdata(serio);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	/* invalid scan codes are probably the init sequence, so we ignore them */
731da177e4SLinus Torvalds 	if (nkbd->keycode[data & NKBD_KEY]) {
743c42f0c3SDmitry Torokhov 		input_report_key(nkbd->dev, nkbd->keycode[data & NKBD_KEY], data & NKBD_PRESS);
753c42f0c3SDmitry Torokhov 		input_sync(nkbd->dev);
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	else if (data == 0xe7) /* end of init sequence */
793c42f0c3SDmitry Torokhov 		printk(KERN_INFO "input: %s on %s\n", nkbd->dev->name, serio->phys);
801da177e4SLinus Torvalds 	return IRQ_HANDLED;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds static int nkbd_connect(struct serio *serio, struct serio_driver *drv)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	struct nkbd *nkbd;
873c42f0c3SDmitry Torokhov 	struct input_dev *input_dev;
883c42f0c3SDmitry Torokhov 	int err = -ENOMEM;
891da177e4SLinus Torvalds 	int i;
901da177e4SLinus Torvalds 
913c42f0c3SDmitry Torokhov 	nkbd = kzalloc(sizeof(struct nkbd), GFP_KERNEL);
923c42f0c3SDmitry Torokhov 	input_dev = input_allocate_device();
933c42f0c3SDmitry Torokhov 	if (!nkbd || !input_dev)
943c42f0c3SDmitry Torokhov 		goto fail;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	nkbd->serio = serio;
973c42f0c3SDmitry Torokhov 	nkbd->dev = input_dev;
98ea08c6faSDmitry Torokhov 	snprintf(nkbd->phys, sizeof(nkbd->phys), "%s/input0", serio->phys);
993c42f0c3SDmitry Torokhov 	memcpy(nkbd->keycode, nkbd_keycode, sizeof(nkbd->keycode));
1001da177e4SLinus Torvalds 
1013c42f0c3SDmitry Torokhov 	input_dev->name = "Newton Keyboard";
1023c42f0c3SDmitry Torokhov 	input_dev->phys = nkbd->phys;
1033c42f0c3SDmitry Torokhov 	input_dev->id.bustype = BUS_RS232;
1043c42f0c3SDmitry Torokhov 	input_dev->id.vendor = SERIO_NEWTON;
1053c42f0c3SDmitry Torokhov 	input_dev->id.product = 0x0001;
1063c42f0c3SDmitry Torokhov 	input_dev->id.version = 0x0100;
1073c42f0c3SDmitry Torokhov 	input_dev->cdev.dev = &serio->dev;
1083c42f0c3SDmitry Torokhov 	input_dev->private = nkbd;
1093c42f0c3SDmitry Torokhov 
1103c42f0c3SDmitry Torokhov 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
1113c42f0c3SDmitry Torokhov 	input_dev->keycode = nkbd->keycode;
1123c42f0c3SDmitry Torokhov 	input_dev->keycodesize = sizeof(unsigned char);
1133c42f0c3SDmitry Torokhov 	input_dev->keycodemax = ARRAY_SIZE(nkbd_keycode);
1143c42f0c3SDmitry Torokhov 	for (i = 0; i < 128; i++)
1153c42f0c3SDmitry Torokhov 		set_bit(nkbd->keycode[i], input_dev->keybit);
1163c42f0c3SDmitry Torokhov 	clear_bit(0, input_dev->keybit);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	serio_set_drvdata(serio, nkbd);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	err = serio_open(serio, drv);
1213c42f0c3SDmitry Torokhov 	if (err)
1223c42f0c3SDmitry Torokhov 		goto fail;
1233c42f0c3SDmitry Torokhov 
1243c42f0c3SDmitry Torokhov 	input_register_device(nkbd->dev);
1253c42f0c3SDmitry Torokhov 	return 0;
1263c42f0c3SDmitry Torokhov 
1273c42f0c3SDmitry Torokhov  fail:	serio_set_drvdata(serio, NULL);
1283c42f0c3SDmitry Torokhov 	input_free_device(input_dev);
1291da177e4SLinus Torvalds 	kfree(nkbd);
1301da177e4SLinus Torvalds 	return err;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static void nkbd_disconnect(struct serio *serio)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct nkbd *nkbd = serio_get_drvdata(serio);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	serio_close(serio);
1381da177e4SLinus Torvalds 	serio_set_drvdata(serio, NULL);
1393c42f0c3SDmitry Torokhov 	input_unregister_device(nkbd->dev);
1401da177e4SLinus Torvalds 	kfree(nkbd);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static struct serio_device_id nkbd_serio_ids[] = {
1441da177e4SLinus Torvalds 	{
1451da177e4SLinus Torvalds 		.type	= SERIO_RS232,
1461da177e4SLinus Torvalds 		.proto	= SERIO_NEWTON,
1471da177e4SLinus Torvalds 		.id	= SERIO_ANY,
1481da177e4SLinus Torvalds 		.extra	= SERIO_ANY,
1491da177e4SLinus Torvalds 	},
1501da177e4SLinus Torvalds 	{ 0 }
1511da177e4SLinus Torvalds };
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, nkbd_serio_ids);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static struct serio_driver nkbd_drv = {
1561da177e4SLinus Torvalds 	.driver		= {
1571da177e4SLinus Torvalds 		.name	= "newtonkbd",
1581da177e4SLinus Torvalds 	},
1591da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
1601da177e4SLinus Torvalds 	.id_table	= nkbd_serio_ids,
1611da177e4SLinus Torvalds 	.interrupt	= nkbd_interrupt,
1621da177e4SLinus Torvalds 	.connect	= nkbd_connect,
1631da177e4SLinus Torvalds 	.disconnect	= nkbd_disconnect,
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds static int __init nkbd_init(void)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	serio_register_driver(&nkbd_drv);
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void __exit nkbd_exit(void)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	serio_unregister_driver(&nkbd_drv);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds module_init(nkbd_init);
1781da177e4SLinus Torvalds module_exit(nkbd_exit);
179