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