Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
stowaway.c
Go to the documentation of this file.
1 /*
2  * Stowaway keyboard driver for Linux
3  */
4 
5 /*
6  * Copyright (c) 2006 Marek Vasut
7  *
8  * Based on Newton keyboard driver for Linux
9  * by Justin Cormack
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Should you need to contact me, the author, you can do so either by
28  * e-mail - mail your message to <[email protected]>, or by paper mail:
29  * Marek Vasut, Liskovecka 559, Frydek-Mistek, 738 01 Czech Republic
30  */
31 
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/input.h>
35 #include <linux/init.h>
36 #include <linux/serio.h>
37 
38 #define DRIVER_DESC "Stowaway keyboard driver"
39 
40 MODULE_AUTHOR("Marek Vasut <[email protected]>");
42 MODULE_LICENSE("GPL");
43 
44 #define SKBD_KEY_MASK 0x7f
45 #define SKBD_RELEASE 0x80
46 
47 static unsigned char skbd_keycode[128] = {
51  KEY_CAPSLOCK, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0,
52  0, 0, 0, KEY_LEFTALT, 0, 0, 0, 0,
53  0, 0, 0, 0, KEY_C, KEY_V, KEY_B, KEY_N,
59  KEY_LEFTSHIFT, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0,
60  0, 0, 0, 0, 0, 0, 0, 0,
61  0, 0, 0, 0, 0, 0, 0, 0,
63  KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, 0, 0, 0
64 };
65 
66 struct skbd {
67  unsigned char keycode[128];
68  struct input_dev *dev;
69  struct serio *serio;
70  char phys[32];
71 };
72 
73 static irqreturn_t skbd_interrupt(struct serio *serio, unsigned char data,
74  unsigned int flags)
75 {
76  struct skbd *skbd = serio_get_drvdata(serio);
77  struct input_dev *dev = skbd->dev;
78 
79  if (skbd->keycode[data & SKBD_KEY_MASK]) {
80  input_report_key(dev, skbd->keycode[data & SKBD_KEY_MASK],
81  !(data & SKBD_RELEASE));
82  input_sync(dev);
83  }
84 
85  return IRQ_HANDLED;
86 }
87 
88 static int skbd_connect(struct serio *serio, struct serio_driver *drv)
89 {
90  struct skbd *skbd;
91  struct input_dev *input_dev;
92  int err = -ENOMEM;
93  int i;
94 
95  skbd = kzalloc(sizeof(struct skbd), GFP_KERNEL);
96  input_dev = input_allocate_device();
97  if (!skbd || !input_dev)
98  goto fail1;
99 
100  skbd->serio = serio;
101  skbd->dev = input_dev;
102  snprintf(skbd->phys, sizeof(skbd->phys), "%s/input0", serio->phys);
103  memcpy(skbd->keycode, skbd_keycode, sizeof(skbd->keycode));
104 
105  input_dev->name = "Stowaway Keyboard";
106  input_dev->phys = skbd->phys;
107  input_dev->id.bustype = BUS_RS232;
108  input_dev->id.vendor = SERIO_STOWAWAY;
109  input_dev->id.product = 0x0001;
110  input_dev->id.version = 0x0100;
111  input_dev->dev.parent = &serio->dev;
112 
113  input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
114  input_dev->keycode = skbd->keycode;
115  input_dev->keycodesize = sizeof(unsigned char);
116  input_dev->keycodemax = ARRAY_SIZE(skbd_keycode);
117  for (i = 0; i < ARRAY_SIZE(skbd_keycode); i++)
118  set_bit(skbd_keycode[i], input_dev->keybit);
119  clear_bit(0, input_dev->keybit);
120 
121  serio_set_drvdata(serio, skbd);
122 
123  err = serio_open(serio, drv);
124  if (err)
125  goto fail2;
126 
127  err = input_register_device(skbd->dev);
128  if (err)
129  goto fail3;
130 
131  return 0;
132 
133  fail3: serio_close(serio);
134  fail2: serio_set_drvdata(serio, NULL);
135  fail1: input_free_device(input_dev);
136  kfree(skbd);
137  return err;
138 }
139 
140 static void skbd_disconnect(struct serio *serio)
141 {
142  struct skbd *skbd = serio_get_drvdata(serio);
143 
144  serio_close(serio);
145  serio_set_drvdata(serio, NULL);
146  input_unregister_device(skbd->dev);
147  kfree(skbd);
148 }
149 
150 static struct serio_device_id skbd_serio_ids[] = {
151  {
152  .type = SERIO_RS232,
153  .proto = SERIO_STOWAWAY,
154  .id = SERIO_ANY,
155  .extra = SERIO_ANY,
156  },
157  { 0 }
158 };
159 
160 MODULE_DEVICE_TABLE(serio, skbd_serio_ids);
161 
162 static struct serio_driver skbd_drv = {
163  .driver = {
164  .name = "stowaway",
165  },
166  .description = DRIVER_DESC,
167  .id_table = skbd_serio_ids,
168  .interrupt = skbd_interrupt,
169  .connect = skbd_connect,
170  .disconnect = skbd_disconnect,
171 };
172 
173 module_serio_driver(skbd_drv);