1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/sched.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kbd_ll.h>
#include "usb.h"
#define PCKBD_PRESSED 0x00
#define PCKBD_RELEASED 0x80
#define PCKBD_NEEDS_E0 0x80
#define USBKBD_MODIFIER_BASE 120
#define USBKBD_KEYCODE_OFFSET 2
#define USBKBD_KEYCODE_COUNT 6
#define USBKBD_VALID_KEYCODE(key) ((unsigned char)(key) > 3)
#define USBKBD_FIND_KEYCODE(down, key, count) \
((unsigned char*) memscan((down), (key), (count)) < ((down) + (count)))
#define USBKBD_REPEAT_DELAY (HZ / 4)
#define USBKBD_REPEAT_RATE (HZ / 20)
struct usb_keyboard
{
struct usb_device *dev;
unsigned long down[2];
unsigned char repeat_key;
struct timer_list repeat_timer;
struct list_head list;
};
extern unsigned char usb_kbd_map[];
static int usb_kbd_probe(struct usb_device *dev);
static void usb_kbd_disconnect(struct usb_device *dev);
static void usb_kbd_repeat(unsigned long dummy);
static LIST_HEAD(usb_kbd_list);
static struct usb_driver usb_kbd_driver =
{
"keyboard",
usb_kbd_probe,
usb_kbd_disconnect,
{NULL, NULL}
};
static void
usb_kbd_handle_key(unsigned char key, int down)
{
int scancode = (int) usb_kbd_map[key];
if(scancode)
{
#ifndef CONFIG_MAC_KEYBOARD
if(scancode & PCKBD_NEEDS_E0)
{
handle_scancode(0xe0, 1);
}
#endif /* CONFIG_MAC_KEYBOARD */
handle_scancode((scancode & ~PCKBD_NEEDS_E0), down);
}
}
static void
usb_kbd_repeat(unsigned long dev_id)
{
struct usb_keyboard *kbd = (struct usb_keyboard*) dev_id;
unsigned long flags;
save_flags(flags);
cli();
if(kbd->repeat_key)
{
usb_kbd_handle_key(kbd->repeat_key, 1);
/* reset repeat timer */
kbd->repeat_timer.function = usb_kbd_repeat;
kbd->repeat_timer.expires = jiffies + USBKBD_REPEAT_RATE;
kbd->repeat_timer.data = (unsigned long) kbd;
kbd->repeat_timer.prev = NULL;
kbd->repeat_timer.next = NULL;
add_timer(&kbd->repeat_timer);
}
restore_flags(flags);
}
static int
usb_kbd_irq(int state, void *buffer, int len, void *dev_id)
{
struct usb_keyboard *kbd = (struct usb_keyboard*) dev_id;
unsigned long *down = (unsigned long*) buffer;
if(kbd->down[0] != down[0] || kbd->down[1] != down[1])
{
unsigned char *olddown, *newdown;
unsigned char modsdelta, key;
int i;
/* handle modifier change */
modsdelta = (*(unsigned char*) down ^ *(unsigned char*) kbd->down);
if(modsdelta)
{
for(i = 0; i < 8; i++)
{
if(modsdelta & 0x01)
{
int pressed = (*(unsigned char*) down >> i) & 0x01;
usb_kbd_handle_key(
i + USBKBD_MODIFIER_BASE,
pressed);
}
modsdelta >>= 1;
}
}
olddown = (unsigned char*) kbd->down + USBKBD_KEYCODE_OFFSET;
newdown = (unsigned char*) down + USBKBD_KEYCODE_OFFSET;
/* handle released keys */
for(i = 0; i < USBKBD_KEYCODE_COUNT; i++)
{
key = olddown[i];
if(USBKBD_VALID_KEYCODE(key)
&& !USBKBD_FIND_KEYCODE(newdown, key, USBKBD_KEYCODE_COUNT))
{
usb_kbd_handle_key(key, 0);
}
}
/* handle pressed keys */
kbd->repeat_key = 0;
for(i = 0; i < USBKBD_KEYCODE_COUNT; i++)
{
key = newdown[i];
if(USBKBD_VALID_KEYCODE(key)
&& !USBKBD_FIND_KEYCODE(olddown, key, USBKBD_KEYCODE_COUNT))
{
usb_kbd_handle_key(key, 1);
kbd->repeat_key = key;
}
}
/* set repeat timer if any keys were pressed */
if(kbd->repeat_key)
{
del_timer(&kbd->repeat_timer);
kbd->repeat_timer.function = usb_kbd_repeat;
kbd->repeat_timer.expires = jiffies + USBKBD_REPEAT_DELAY;
kbd->repeat_timer.data = (unsigned long) kbd;
kbd->repeat_timer.prev = NULL;
kbd->repeat_timer.next = NULL;
add_timer(&kbd->repeat_timer);
}
kbd->down[0] = down[0];
kbd->down[1] = down[1];
}
return 1;
}
static int
usb_kbd_probe(struct usb_device *dev)
{
struct usb_interface_descriptor *interface;
struct usb_endpoint_descriptor *endpoint;
struct usb_keyboard *kbd;
if (dev->descriptor.bNumConfigurations < 1)
return -1;
interface = &dev->config[0].altsetting[0].interface[0];
endpoint = &interface->endpoint[0];
if(interface->bInterfaceClass != 3
|| interface->bInterfaceSubClass != 1
|| interface->bInterfaceProtocol != 1)
{
return -1;
}
printk(KERN_INFO "USB HID boot protocol keyboard detected.\n");
kbd = kmalloc(sizeof(struct usb_keyboard), GFP_KERNEL);
if(kbd)
{
memset(kbd, 0, sizeof(*kbd));
kbd->dev = dev;
dev->private = kbd;
usb_set_configuration(dev, dev->config[0].bConfigurationValue);
usb_set_protocol(dev, 0);
usb_set_idle(dev, 0, 0);
usb_request_irq(dev,
usb_rcvctrlpipe(dev, endpoint->bEndpointAddress),
usb_kbd_irq,
endpoint->bInterval,
kbd);
list_add(&kbd->list, &usb_kbd_list);
}
return 0;
}
static void
usb_kbd_disconnect(struct usb_device *dev)
{
struct usb_keyboard *kbd = (struct usb_keyboard*) dev->private;
if(kbd)
{
dev->private = NULL;
list_del(&kbd->list);
del_timer(&kbd->repeat_timer);
kfree(kbd);
}
printk(KERN_INFO "USB HID boot protocol keyboard removed.\n");
}
int usb_kbd_init(void)
{
usb_register(&usb_kbd_driver);
return 0;
}
#ifdef MODULE
int init_module(void)
{
return usb_kbd_init();
}
void cleanup_module(void)
{
usb_deregister(&usb_kbd_driver);
}
#endif
|