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