ukbd.c revision 1.8 1 /* $NetBSD: ukbd.c,v 1.8 1998/08/02 14:22:25 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson <augustss (at) carlstedt.se>
8 * Carlstedt Research & Technology
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/ioctl.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/select.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/poll.h>
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbhid.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdevs.h>
59 #include <dev/usb/usb_quirks.h>
60 #include <dev/usb/hid.h>
61
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/wscons/wskbdvar.h>
64 #include <dev/wscons/wsksymdef.h>
65 #include <dev/wscons/wsksymvar.h>
66 #include <dev/wscons/wskbdmap_mfii.h>
67
68 #include "opt_pckbd_layout.h"
69 #include "opt_wsdisplay_compat.h"
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x) if (ukbddebug) printf x
73 #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x
74 int ukbddebug = 0;
75 #else
76 #define DPRINTF(x)
77 #define DPRINTFN(n,x)
78 #endif
79
80 #define UPROTO_BOOT_KEYBOARD 1
81
82 #define NKEYCODE 6
83
84 #define NUM_LOCK 0x01
85 #define CAPS_LOCK 0x02
86 #define SCROLL_LOCK 0x04
87
88 struct ukbd_data {
89 u_int8_t modifiers;
90 #define MOD_CONTROL_L 0x01
91 #define MOD_CONTROL_R 0x10
92 #define MOD_SHIFT_L 0x02
93 #define MOD_SHIFT_R 0x20
94 #define MOD_ALT_L 0x04
95 #define MOD_ALT_R 0x40
96 #define MOD_WIN_L 0x08
97 #define MOD_WIN_R 0x80
98 u_int8_t reserved;
99 u_int8_t keycode[NKEYCODE];
100 };
101
102 #define PRESS 0
103 #define RELEASE 0x100
104
105 #define NMOD 6
106 static struct {
107 int mask, key;
108 } ukbd_mods[NMOD] = {
109 { MOD_CONTROL_L, 29 },
110 { MOD_CONTROL_R, 58 },
111 { MOD_SHIFT_L, 42 },
112 { MOD_SHIFT_R, 54 },
113 { MOD_ALT_L, 56 },
114 { MOD_ALT_R, 184 },
115 };
116
117 #define NN 0 /* no translation */
118 /* Translate USB keycodes to US keyboard AT scancodes. */
119 static u_int8_t ukbd_trtab[256] = {
120 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
121 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
122 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
123 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
124 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
125 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
126 27, 43, NN, 39, 40, 41, 51, 52, /* 30 - 37 */
127 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
128 65, 66, 67, 68, 87, 88, 170, 70, /* 40 - 47 */
129 127, 210, 199, 201, 211, 207, 209, 205, /* 48 - 4F */
130 203, 208, 200, 69, 181, 55, 74, 78, /* 50 - 57 */
131 156, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
132 72, 73, 82, 83, NN, NN, NN, NN, /* 60 - 67 */
133 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
134 NN, NN, NN, NN, NN, NN, 221, NN, /* 70 - 77 */
135 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7F */
136 NN, NN, NN, NN, NN, NN, NN, NN, /* 80 - 87 */
137 NN, NN, NN, NN, NN, NN, NN, NN, /* 88 - 8F */
138 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
139 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
140 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
141 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
142 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
143 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
144 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
145 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
146 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
147 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
148 NN, NN, NN, NN, NN, NN, NN, NN, /* E0 - E7 */
149 NN, NN, NN, 219, NN, NN, NN, 220, /* E8 - EF */
150 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
151 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
152 };
153
154 #define KEY_ERROR 0x01
155
156 struct ukbd_softc {
157 struct device sc_dev; /* base device */
158 usbd_interface_handle sc_iface; /* interface */
159 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
160 int sc_ep_addr;
161
162 struct ukbd_data sc_ndata;
163 struct ukbd_data sc_odata;
164
165 int sc_disconnected; /* device is gone */
166
167 int sc_leds;
168 struct device *sc_wskbddev;
169 #ifdef WSDISPLAY_COMPAT_RAWKBD
170 int sc_rawkbd;
171 #endif
172
173 int sc_polling;
174 int sc_pollchar;
175 };
176
177 #define UKBDUNIT(dev) (minor(dev))
178 #define UKBD_CHUNK 128 /* chunk size for read */
179 #define UKBD_BSIZE 1020 /* buffer size */
180
181 int ukbd_match __P((struct device *, struct cfdata *, void *));
182 void ukbd_attach __P((struct device *, struct device *, void *));
183
184 void ukbd_cngetc __P((void *, u_int *, int *));
185 void ukbd_cnpollc __P((void *, int));
186
187 const struct wskbd_consops ukbd_consops = {
188 ukbd_cngetc,
189 ukbd_cnpollc,
190 };
191
192 void ukbd_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
193 void ukbd_disco __P((void *));
194
195 int ukbd_enable __P((void *, int));
196 void ukbd_set_leds __P((void *, int));
197 int ukbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
198
199 const struct wskbd_accessops ukbd_accessops = {
200 ukbd_enable,
201 ukbd_set_leds,
202 ukbd_ioctl,
203 };
204
205 const struct wskbd_mapdata ukbd_keymapdata = {
206 pckbd_keydesctab,
207 sizeof(pckbd_keydesctab)/sizeof(pckbd_keydesctab[0]),
208 #ifdef PCKBD_LAYOUT
209 PCKBD_LAYOUT,
210 #else
211 KB_US,
212 #endif
213 };
214
215 extern struct cfdriver ukbd_cd;
216
217 struct cfattach ukbd_ca = {
218 sizeof(struct ukbd_softc), ukbd_match, ukbd_attach
219 };
220
221 int
222 ukbd_match(parent, match, aux)
223 struct device *parent;
224 struct cfdata *match;
225 void *aux;
226 {
227 struct usb_attach_arg *uaa = (struct usb_attach_arg *)aux;
228 usb_interface_descriptor_t *id;
229
230 /* Check that this is a keyboard that speaks the boot protocol. */
231 if (!uaa->iface)
232 return (UMATCH_NONE);
233 id = usbd_get_interface_descriptor(uaa->iface);
234 if (id->bInterfaceClass != UCLASS_HID ||
235 id->bInterfaceSubClass != USUBCLASS_BOOT ||
236 id->bInterfaceProtocol != UPROTO_BOOT_KEYBOARD)
237 return (UMATCH_NONE);
238 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
239 }
240
241 void
242 ukbd_attach(parent, self, aux)
243 struct device *parent;
244 struct device *self;
245 void *aux;
246 {
247 struct ukbd_softc *sc = (struct ukbd_softc *)self;
248 struct usb_attach_arg *uaa = aux;
249 usbd_interface_handle iface = uaa->iface;
250 usb_interface_descriptor_t *id;
251 usb_endpoint_descriptor_t *ed;
252 usbd_status r;
253 char devinfo[1024];
254 struct wskbddev_attach_args a;
255
256 sc->sc_disconnected = 1;
257 sc->sc_iface = iface;
258 id = usbd_get_interface_descriptor(iface);
259 usbd_devinfo(uaa->device, 0, devinfo);
260 printf(": %s (interface class %d/%d)\n", devinfo,
261 id->bInterfaceClass, id->bInterfaceSubClass);
262 ed = usbd_interface2endpoint_descriptor(iface, 0);
263 if (!ed) {
264 printf("%s: could not read endpoint descriptor\n",
265 sc->sc_dev.dv_xname);
266 return;
267 }
268
269 DPRINTFN(10,("ukbd_attach: \
270 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
271 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
272 ed->bEndpointAddress & UE_IN ? "in" : "out",
273 ed->bmAttributes & UE_XFERTYPE,
274 UGETW(ed->wMaxPacketSize), ed->bInterval));
275
276 if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
277 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
278 printf("%s: unexpected endpoint\n",
279 sc->sc_dev.dv_xname);
280 return;
281 }
282
283 if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
284 r = usbd_set_protocol(iface, 0);
285 DPRINTFN(5, ("ukbd_attach: protocol set\n"));
286 if (r != USBD_NORMAL_COMPLETION) {
287 printf("%s: set protocol failed\n",
288 sc->sc_dev.dv_xname);
289 return;
290 }
291 }
292 /* Ignore if SETIDLE fails since it is not crucial. */
293 usbd_set_idle(iface, 0, 0);
294
295 sc->sc_ep_addr = ed->bEndpointAddress;
296 sc->sc_disconnected = 0;
297
298 a.console = 0; /* XXX */
299
300 a.keymap = &ukbd_keymapdata;
301
302 a.accessops = &ukbd_accessops;
303 a.accesscookie = sc;
304
305 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
306
307 /* Set up interrupt pipe. */
308 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
309 USBD_SHORT_XFER_OK,
310 &sc->sc_intrpipe, sc, &sc->sc_ndata,
311 sizeof(sc->sc_ndata), ukbd_intr);
312 if (r != USBD_NORMAL_COMPLETION) {
313 printf("%s: usbd_open_pipe_intr failed, error=%d\n",
314 sc->sc_dev.dv_xname, r);
315 return;
316 }
317 usbd_set_disco(sc->sc_intrpipe, ukbd_disco, sc);
318 }
319
320 void
321 ukbd_disco(p)
322 void *p;
323 {
324 struct ukbd_softc *sc = p;
325
326 DPRINTF(("ukbd_disco: sc=%p\n", sc));
327 usbd_abort_pipe(sc->sc_intrpipe);
328 sc->sc_disconnected = 1;
329 }
330
331 int
332 ukbd_enable(v, on)
333 void *v;
334 int on;
335 {
336 return (0);
337 }
338
339 void
340 ukbd_intr(reqh, addr, status)
341 usbd_request_handle reqh;
342 usbd_private_handle addr;
343 usbd_status status;
344 {
345 struct ukbd_softc *sc = addr;
346 struct ukbd_data *ud = &sc->sc_ndata;
347 int mod, omod;
348 int ibuf[NMOD+2*NKEYCODE]; /* chars events */
349 int nkeys, i, j;
350 int key, c;
351 #define ADDKEY(c) ibuf[nkeys++] = (c)
352
353 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
354 if (status == USBD_CANCELLED)
355 return;
356
357 if (status != USBD_NORMAL_COMPLETION) {
358 DPRINTF(("ukbd_intr: status=%d\n", status));
359 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
360 return;
361 }
362
363 DPRINTFN(5, (" mod=0x%02x key0=0x%02x key1=0x%02x\n",
364 ud->modifiers, ud->keycode[0], ud->keycode[1]));
365
366 if (ud->keycode[0] == KEY_ERROR)
367 return; /* ignore */
368 nkeys = 0;
369 mod = ud->modifiers;
370 omod = sc->sc_odata.modifiers;
371 if (mod != omod)
372 for (i = 0; i < NMOD; i++)
373 if (( mod & ukbd_mods[i].mask) !=
374 (omod & ukbd_mods[i].mask))
375 ADDKEY(ukbd_mods[i].key |
376 (mod & ukbd_mods[i].mask
377 ? PRESS : RELEASE));
378 if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
379 /* Check for released keys. */
380 for (i = 0; i < NKEYCODE; i++) {
381 key = sc->sc_odata.keycode[i];
382 if (key == 0)
383 continue;
384 for (j = 0; j < NKEYCODE; j++)
385 if (key == ud->keycode[j])
386 goto rfound;
387 c = ukbd_trtab[key];
388 if (c)
389 ADDKEY(c | RELEASE);
390 rfound:
391 ;
392 }
393
394 /* Check for pressed keys. */
395 for (i = 0; i < NKEYCODE; i++) {
396 key = ud->keycode[i];
397 if (key == 0)
398 continue;
399 for (j = 0; j < NKEYCODE; j++)
400 if (key == sc->sc_odata.keycode[j])
401 goto pfound;
402 c = ukbd_trtab[key];
403 DPRINTFN(2,("ukbd_intr: press key=0x%02x -> 0x%02x\n",
404 key, c));
405 if (c)
406 ADDKEY(c | PRESS);
407 pfound:
408 ;
409 }
410 }
411 sc->sc_odata = *ud;
412
413 if (sc->sc_polling) {
414 if (nkeys > 0)
415 sc->sc_pollchar = ibuf[0]; /* XXX lost keys? */
416 return;
417 }
418 for (i = 0; i < nkeys; i++) {
419 c = ibuf[i];
420 wskbd_input(sc->sc_wskbddev,
421 c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
422 c & 0xff);
423 }
424 }
425
426 void
427 ukbd_set_leds(v, leds)
428 void *v;
429 int leds;
430 {
431 struct ukbd_softc *sc = v;
432 u_int8_t res;
433
434 DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
435
436 sc->sc_leds = leds;
437 res = 0;
438 if (leds & WSKBD_LED_SCROLL)
439 res |= SCROLL_LOCK;
440 if (leds & WSKBD_LED_NUM)
441 res |= NUM_LOCK;
442 if (leds & WSKBD_LED_CAPS)
443 res |= CAPS_LOCK;
444 usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
445 }
446
447 int
448 ukbd_ioctl(v, cmd, data, flag, p)
449 void *v;
450 u_long cmd;
451 caddr_t data;
452 int flag;
453 struct proc *p;
454 {
455 struct ukbd_softc *sc = v;
456
457 switch (cmd) {
458 case WSKBDIO_GTYPE:
459 *(int *)data = WSKBD_TYPE_PC_XT;
460 return 0;
461 case WSKBDIO_SETLEDS:
462 ukbd_set_leds(v, *(int *)data);
463 return 0;
464 case WSKBDIO_GETLEDS:
465 *(int *)data = sc->sc_leds;
466 return (0);
467 #ifdef WSDISPLAY_COMPAT_RAWKBD
468 case WSKBDIO_SETMODE:
469 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
470 return (0);
471 #endif
472 }
473 return -1;
474 }
475
476 /* Console interface. */
477 /* XXX does not work. */
478 void
479 ukbd_cngetc(v, type, data)
480 void *v;
481 u_int *type;
482 int *data;
483 {
484 struct ukbd_softc *sc = v;
485 usbd_lock_token s;
486 int c;
487
488 DPRINTFN(1,("ukbd_cngetc: enter\n"));
489 s = usbd_lock();
490 sc->sc_polling = 1;
491 sc->sc_pollchar = -1;
492 while(sc->sc_pollchar == -1)
493 usbd_dopoll(sc->sc_iface);
494 sc->sc_polling = 0;
495 c = sc->sc_pollchar;
496 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
497 *data = c & 0xff;
498 usbd_unlock(s);
499 DPRINTFN(1,("ukbd_cngetc: return 0x%02x\n", c));
500 }
501
502 void
503 ukbd_cnpollc(v, on)
504 void *v;
505 int on;
506 {
507 struct ukbd_softc *sc = v;
508
509 DPRINTFN(1,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
510
511 usbd_set_polling(sc->sc_iface, on);
512 }
513