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/serio.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define DRIVER_DESC "Newton keyboard driver" 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds MODULE_AUTHOR("Justin Cormack <j.cormack@doc.ic.ac.uk>"); 371da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 381da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define NKBD_KEY 0x7f 411da177e4SLinus Torvalds #define NKBD_PRESS 0x80 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static unsigned char nkbd_keycode[128] = { 441da177e4SLinus Torvalds KEY_A, KEY_S, KEY_D, KEY_F, KEY_H, KEY_G, KEY_Z, KEY_X, 451da177e4SLinus Torvalds KEY_C, KEY_V, 0, KEY_B, KEY_Q, KEY_W, KEY_E, KEY_R, 461da177e4SLinus Torvalds KEY_Y, KEY_T, KEY_1, KEY_2, KEY_3, KEY_4, KEY_6, KEY_5, 471da177e4SLinus Torvalds KEY_EQUAL, KEY_9, KEY_7, KEY_MINUS, KEY_8, KEY_0, KEY_RIGHTBRACE, KEY_O, 481da177e4SLinus Torvalds KEY_U, KEY_LEFTBRACE, KEY_I, KEY_P, KEY_ENTER, KEY_L, KEY_J, KEY_APOSTROPHE, 491da177e4SLinus Torvalds KEY_K, KEY_SEMICOLON, KEY_BACKSLASH, KEY_COMMA, KEY_SLASH, KEY_N, KEY_M, KEY_DOT, 501da177e4SLinus Torvalds KEY_TAB, KEY_SPACE, KEY_GRAVE, KEY_DELETE, 0, 0, 0, KEY_LEFTMETA, 511da177e4SLinus Torvalds KEY_LEFTSHIFT, KEY_CAPSLOCK, KEY_LEFTALT, KEY_LEFTCTRL, KEY_RIGHTSHIFT, 0, 0, 0, 521da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 561da177e4SLinus Torvalds KEY_LEFT, KEY_RIGHT, KEY_DOWN, KEY_UP, 0 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds struct nkbd { 601da177e4SLinus Torvalds unsigned char keycode[128]; 613c42f0c3SDmitry Torokhov struct input_dev *dev; 621da177e4SLinus Torvalds struct serio *serio; 631da177e4SLinus Torvalds char phys[32]; 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds static irqreturn_t nkbd_interrupt(struct serio *serio, 677d12e780SDavid Howells unsigned char data, unsigned int flags) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds struct nkbd *nkbd = serio_get_drvdata(serio); 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* invalid scan codes are probably the init sequence, so we ignore them */ 721da177e4SLinus Torvalds if (nkbd->keycode[data & NKBD_KEY]) { 733c42f0c3SDmitry Torokhov input_report_key(nkbd->dev, nkbd->keycode[data & NKBD_KEY], data & NKBD_PRESS); 743c42f0c3SDmitry Torokhov input_sync(nkbd->dev); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds else if (data == 0xe7) /* end of init sequence */ 783c42f0c3SDmitry Torokhov printk(KERN_INFO "input: %s on %s\n", nkbd->dev->name, serio->phys); 791da177e4SLinus Torvalds return IRQ_HANDLED; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static int nkbd_connect(struct serio *serio, struct serio_driver *drv) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds struct nkbd *nkbd; 863c42f0c3SDmitry Torokhov struct input_dev *input_dev; 873c42f0c3SDmitry Torokhov int err = -ENOMEM; 881da177e4SLinus Torvalds int i; 891da177e4SLinus Torvalds 903c42f0c3SDmitry Torokhov nkbd = kzalloc(sizeof(struct nkbd), GFP_KERNEL); 913c42f0c3SDmitry Torokhov input_dev = input_allocate_device(); 923c42f0c3SDmitry Torokhov if (!nkbd || !input_dev) 932b03b60eSDmitry Torokhov goto fail1; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds nkbd->serio = serio; 963c42f0c3SDmitry Torokhov nkbd->dev = input_dev; 97ea08c6faSDmitry Torokhov snprintf(nkbd->phys, sizeof(nkbd->phys), "%s/input0", serio->phys); 983c42f0c3SDmitry Torokhov memcpy(nkbd->keycode, nkbd_keycode, sizeof(nkbd->keycode)); 991da177e4SLinus Torvalds 1003c42f0c3SDmitry Torokhov input_dev->name = "Newton Keyboard"; 1013c42f0c3SDmitry Torokhov input_dev->phys = nkbd->phys; 1023c42f0c3SDmitry Torokhov input_dev->id.bustype = BUS_RS232; 1033c42f0c3SDmitry Torokhov input_dev->id.vendor = SERIO_NEWTON; 1043c42f0c3SDmitry Torokhov input_dev->id.product = 0x0001; 1053c42f0c3SDmitry Torokhov input_dev->id.version = 0x0100; 106469ba4dfSDmitry Torokhov input_dev->dev.parent = &serio->dev; 1073c42f0c3SDmitry Torokhov 1087b19ada2SJiri Slaby input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); 1093c42f0c3SDmitry Torokhov input_dev->keycode = nkbd->keycode; 1103c42f0c3SDmitry Torokhov input_dev->keycodesize = sizeof(unsigned char); 1113c42f0c3SDmitry Torokhov input_dev->keycodemax = ARRAY_SIZE(nkbd_keycode); 1123c42f0c3SDmitry Torokhov for (i = 0; i < 128; i++) 1133c42f0c3SDmitry Torokhov set_bit(nkbd->keycode[i], input_dev->keybit); 1143c42f0c3SDmitry Torokhov clear_bit(0, input_dev->keybit); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds serio_set_drvdata(serio, nkbd); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds err = serio_open(serio, drv); 1193c42f0c3SDmitry Torokhov if (err) 1202b03b60eSDmitry Torokhov goto fail2; 1213c42f0c3SDmitry Torokhov 1222b03b60eSDmitry Torokhov err = input_register_device(nkbd->dev); 1232b03b60eSDmitry Torokhov if (err) 1242b03b60eSDmitry Torokhov goto fail3; 1252b03b60eSDmitry Torokhov 1263c42f0c3SDmitry Torokhov return 0; 1273c42f0c3SDmitry Torokhov 1282b03b60eSDmitry Torokhov fail3: serio_close(serio); 1292b03b60eSDmitry Torokhov fail2: serio_set_drvdata(serio, NULL); 1302b03b60eSDmitry Torokhov fail1: input_free_device(input_dev); 1311da177e4SLinus Torvalds kfree(nkbd); 1321da177e4SLinus Torvalds return err; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static void nkbd_disconnect(struct serio *serio) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds struct nkbd *nkbd = serio_get_drvdata(serio); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds serio_close(serio); 1401da177e4SLinus Torvalds serio_set_drvdata(serio, NULL); 1413c42f0c3SDmitry Torokhov input_unregister_device(nkbd->dev); 1421da177e4SLinus Torvalds kfree(nkbd); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 145*9780930aSArvind Yadav static const struct serio_device_id nkbd_serio_ids[] = { 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds .type = SERIO_RS232, 1481da177e4SLinus Torvalds .proto = SERIO_NEWTON, 1491da177e4SLinus Torvalds .id = SERIO_ANY, 1501da177e4SLinus Torvalds .extra = SERIO_ANY, 1511da177e4SLinus Torvalds }, 1521da177e4SLinus Torvalds { 0 } 1531da177e4SLinus Torvalds }; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, nkbd_serio_ids); 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static struct serio_driver nkbd_drv = { 1581da177e4SLinus Torvalds .driver = { 1591da177e4SLinus Torvalds .name = "newtonkbd", 1601da177e4SLinus Torvalds }, 1611da177e4SLinus Torvalds .description = DRIVER_DESC, 1621da177e4SLinus Torvalds .id_table = nkbd_serio_ids, 1631da177e4SLinus Torvalds .interrupt = nkbd_interrupt, 1641da177e4SLinus Torvalds .connect = nkbd_connect, 1651da177e4SLinus Torvalds .disconnect = nkbd_disconnect, 1661da177e4SLinus Torvalds }; 1671da177e4SLinus Torvalds 16865ac9f7aSAxel Lin module_serio_driver(nkbd_drv); 169