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