Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * Should you need to contact me, the author, you can do so either by |
| 25 | * e-mail - mail your message to <j.cormack@doc.ic.ac.uk>, or by paper mail: |
| 26 | * Justin Cormack, 68 Dartmouth Park Road, London NW5 1SN, UK. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/serio.h> |
| 33 | |
| 34 | #define DRIVER_DESC "Newton keyboard driver" |
| 35 | |
| 36 | MODULE_AUTHOR("Justin Cormack <j.cormack@doc.ic.ac.uk>"); |
| 37 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
| 40 | #define NKBD_KEY 0x7f |
| 41 | #define NKBD_PRESS 0x80 |
| 42 | |
| 43 | static unsigned char nkbd_keycode[128] = { |
| 44 | KEY_A, KEY_S, KEY_D, KEY_F, KEY_H, KEY_G, KEY_Z, KEY_X, |
| 45 | KEY_C, KEY_V, 0, KEY_B, KEY_Q, KEY_W, KEY_E, KEY_R, |
| 46 | KEY_Y, KEY_T, KEY_1, KEY_2, KEY_3, KEY_4, KEY_6, KEY_5, |
| 47 | KEY_EQUAL, KEY_9, KEY_7, KEY_MINUS, KEY_8, KEY_0, KEY_RIGHTBRACE, KEY_O, |
| 48 | KEY_U, KEY_LEFTBRACE, KEY_I, KEY_P, KEY_ENTER, KEY_L, KEY_J, KEY_APOSTROPHE, |
| 49 | KEY_K, KEY_SEMICOLON, KEY_BACKSLASH, KEY_COMMA, KEY_SLASH, KEY_N, KEY_M, KEY_DOT, |
| 50 | KEY_TAB, KEY_SPACE, KEY_GRAVE, KEY_DELETE, 0, 0, 0, KEY_LEFTMETA, |
| 51 | KEY_LEFTSHIFT, KEY_CAPSLOCK, KEY_LEFTALT, KEY_LEFTCTRL, KEY_RIGHTSHIFT, 0, 0, 0, |
| 52 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 53 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 54 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 55 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 56 | KEY_LEFT, KEY_RIGHT, KEY_DOWN, KEY_UP, 0 |
| 57 | }; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct nkbd { |
| 60 | unsigned char keycode[128]; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 61 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct serio *serio; |
| 63 | char phys[32]; |
| 64 | }; |
| 65 | |
| 66 | static irqreturn_t nkbd_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 67 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | struct nkbd *nkbd = serio_get_drvdata(serio); |
| 70 | |
| 71 | /* invalid scan codes are probably the init sequence, so we ignore them */ |
| 72 | if (nkbd->keycode[data & NKBD_KEY]) { |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 73 | input_report_key(nkbd->dev, nkbd->keycode[data & NKBD_KEY], data & NKBD_PRESS); |
| 74 | input_sync(nkbd->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | else if (data == 0xe7) /* end of init sequence */ |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 78 | printk(KERN_INFO "input: %s on %s\n", nkbd->dev->name, serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return IRQ_HANDLED; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | static int nkbd_connect(struct serio *serio, struct serio_driver *drv) |
| 84 | { |
| 85 | struct nkbd *nkbd; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 86 | struct input_dev *input_dev; |
| 87 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 90 | nkbd = kzalloc(sizeof(struct nkbd), GFP_KERNEL); |
| 91 | input_dev = input_allocate_device(); |
| 92 | if (!nkbd || !input_dev) |
Dmitry Torokhov | 2b03b60 | 2006-11-05 22:39:56 -0500 | [diff] [blame] | 93 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | nkbd->serio = serio; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 96 | nkbd->dev = input_dev; |
Dmitry Torokhov | ea08c6f | 2006-06-26 01:46:17 -0400 | [diff] [blame] | 97 | snprintf(nkbd->phys, sizeof(nkbd->phys), "%s/input0", serio->phys); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 98 | memcpy(nkbd->keycode, nkbd_keycode, sizeof(nkbd->keycode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 100 | input_dev->name = "Newton Keyboard"; |
| 101 | input_dev->phys = nkbd->phys; |
| 102 | input_dev->id.bustype = BUS_RS232; |
| 103 | input_dev->id.vendor = SERIO_NEWTON; |
| 104 | input_dev->id.product = 0x0001; |
| 105 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 469ba4d | 2007-04-12 01:34:58 -0400 | [diff] [blame] | 106 | input_dev->dev.parent = &serio->dev; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 107 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 108 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 109 | input_dev->keycode = nkbd->keycode; |
| 110 | input_dev->keycodesize = sizeof(unsigned char); |
| 111 | input_dev->keycodemax = ARRAY_SIZE(nkbd_keycode); |
| 112 | for (i = 0; i < 128; i++) |
| 113 | set_bit(nkbd->keycode[i], input_dev->keybit); |
| 114 | clear_bit(0, input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | serio_set_drvdata(serio, nkbd); |
| 117 | |
| 118 | err = serio_open(serio, drv); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 119 | if (err) |
Dmitry Torokhov | 2b03b60 | 2006-11-05 22:39:56 -0500 | [diff] [blame] | 120 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Dmitry Torokhov | 2b03b60 | 2006-11-05 22:39:56 -0500 | [diff] [blame] | 122 | err = input_register_device(nkbd->dev); |
| 123 | if (err) |
| 124 | goto fail3; |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return 0; |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 127 | |
Dmitry Torokhov | 2b03b60 | 2006-11-05 22:39:56 -0500 | [diff] [blame] | 128 | fail3: serio_close(serio); |
| 129 | fail2: serio_set_drvdata(serio, NULL); |
| 130 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 131 | kfree(nkbd); |
| 132 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void nkbd_disconnect(struct serio *serio) |
| 136 | { |
| 137 | struct nkbd *nkbd = serio_get_drvdata(serio); |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | serio_close(serio); |
| 140 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 3c42f0c | 2005-09-15 02:01:45 -0500 | [diff] [blame] | 141 | input_unregister_device(nkbd->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | kfree(nkbd); |
| 143 | } |
| 144 | |
| 145 | static struct serio_device_id nkbd_serio_ids[] = { |
| 146 | { |
| 147 | .type = SERIO_RS232, |
| 148 | .proto = SERIO_NEWTON, |
| 149 | .id = SERIO_ANY, |
| 150 | .extra = SERIO_ANY, |
| 151 | }, |
| 152 | { 0 } |
| 153 | }; |
| 154 | |
| 155 | MODULE_DEVICE_TABLE(serio, nkbd_serio_ids); |
| 156 | |
| 157 | static struct serio_driver nkbd_drv = { |
| 158 | .driver = { |
| 159 | .name = "newtonkbd", |
| 160 | }, |
| 161 | .description = DRIVER_DESC, |
| 162 | .id_table = nkbd_serio_ids, |
| 163 | .interrupt = nkbd_interrupt, |
| 164 | .connect = nkbd_connect, |
| 165 | .disconnect = nkbd_disconnect, |
| 166 | }; |
| 167 | |
Axel Lin | 65ac9f7 | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 168 | module_serio_driver(nkbd_drv); |