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